generated from storybookjs/addon-kit
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: gist sharing, format code on blur
- Loading branch information
Showing
9 changed files
with
360 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import React, { FC, ReactNode } from 'react'; | ||
import { Spinner } from '@alfalab/core-components/spinner'; | ||
|
||
import { ActionButton } from './ActionButton'; | ||
import { ShareMIcon } from '@alfalab/icons-glyph/ShareMIcon'; | ||
import { copyToClipboard } from './utils'; | ||
import { share } from './utils/share'; | ||
import { CUSTOM_EVENTS, dispatchCustomEvent } from './events'; | ||
|
||
export type ActionButtonProps = { | ||
code?: string; | ||
disabled?: boolean; | ||
title?: string; | ||
doneTitle?: string; | ||
}; | ||
|
||
export const ShareButton: FC<ActionButtonProps> = ({ code, disabled, title, doneTitle }) => { | ||
const [toastOpen, setToastOpen] = React.useState(false); | ||
const [toastText, setToastText] = React.useState<ReactNode>(''); | ||
|
||
const handleShare = async () => { | ||
dispatchCustomEvent(CUSTOM_EVENTS.SHARE); | ||
|
||
setToastOpen(true); | ||
setToastText(<Spinner visible={true} colors='inverted' />); | ||
|
||
try { | ||
const url = await share(code); | ||
copyToClipboard(url); | ||
setToastText(doneTitle); | ||
} catch (error) { | ||
setToastText(error.message); | ||
} finally { | ||
setTimeout(() => { | ||
setToastOpen(false); | ||
}, 500); | ||
} | ||
}; | ||
|
||
return ( | ||
<ActionButton | ||
icon={ShareMIcon} | ||
onClick={() => handleShare()} | ||
title={title} | ||
doneTitle={toastText as string} | ||
disabled={disabled || toastOpen} | ||
toastProps={{ | ||
autoCloseDelay: 0, | ||
open: toastOpen, | ||
}} | ||
/> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { getConfig } from '../../config'; | ||
|
||
export async function share(code: string) { | ||
const config = getConfig(); | ||
|
||
const { sandboxPath, shareMode, githubToken } = config; | ||
|
||
if (shareMode === 'url') { | ||
return `${window.parent.location.origin}?path=${sandboxPath}/code=${encodeURIComponent( | ||
code, | ||
)}`; | ||
} | ||
|
||
if (shareMode === 'gist') { | ||
const id = await createGist(code, githubToken); | ||
return `${window.parent.location.origin}?path=${sandboxPath}/code=gist:${id}`; | ||
} | ||
} | ||
|
||
const FILENAME = 'example.js'; | ||
|
||
export async function createGist(code: string, token: string) { | ||
const apiUrl = 'https://api.github.com/gists'; | ||
|
||
const data = { | ||
description: 'Example from Storybook', | ||
public: false, | ||
files: { | ||
[FILENAME]: { | ||
content: code, | ||
}, | ||
}, | ||
}; | ||
|
||
const response = await fetch(apiUrl, { | ||
method: 'POST', | ||
headers: { | ||
Authorization: `token ${token}`, | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify(data), | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error(`Failed to create gist: ${response.statusText}`); | ||
} | ||
|
||
const result = await response.json(); | ||
|
||
return result.id; | ||
} | ||
|
||
export async function loadGist(id: string) { | ||
const apiUrl = `https://api.github.com/gists/${id}`; | ||
|
||
const response = await fetch(apiUrl); | ||
|
||
if (!response.ok) { | ||
throw new Error(`Failed to load gist: ${response.statusText}`); | ||
} | ||
|
||
const result = await response.json(); | ||
if ('message' in result) { | ||
throw new Error(`${result.status}: ${result.message}`); | ||
} | ||
|
||
return result.files[FILENAME].content; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.