-
Notifications
You must be signed in to change notification settings - Fork 366
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(open in codesandbox button): optimize for long sandoxes (#120)
* fix(open in csb): support long strings * Update website/docs/docs/advanced-usage/components.md Co-authored-by: Sanne <[email protected]> * Update website/docs/docs/advanced-usage/components.md Co-authored-by: Sanne <[email protected]> Co-authored-by: Sanne <[email protected]>
- Loading branch information
Showing
9 changed files
with
174 additions
and
83 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
sandpack-react/src/common/OpenInCodeSandboxButton/OpenInCodeSandboxButton.stories.tsx
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,32 @@ | ||
import { | ||
SandpackProvider, | ||
SandpackThemeProvider, | ||
SandpackCodeEditor, | ||
} from "../../"; | ||
|
||
import { OpenInCodeSandboxButton, UnstyledOpenInCodeSandboxButton } from "."; | ||
|
||
export default { | ||
title: "components/OpenInCodeSandboxButton", | ||
component: OpenInCodeSandboxButton, | ||
}; | ||
|
||
export const Main = (): JSX.Element => ( | ||
<SandpackProvider> | ||
<SandpackThemeProvider> | ||
<SandpackCodeEditor /> | ||
<OpenInCodeSandboxButton /> | ||
</SandpackThemeProvider> | ||
</SandpackProvider> | ||
); | ||
|
||
export const Unstyled = (): JSX.Element => ( | ||
<SandpackProvider> | ||
<SandpackThemeProvider> | ||
<SandpackCodeEditor /> | ||
<UnstyledOpenInCodeSandboxButton> | ||
Open in CodeSandbox | ||
</UnstyledOpenInCodeSandboxButton> | ||
</SandpackThemeProvider> | ||
</SandpackProvider> | ||
); |
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
95 changes: 95 additions & 0 deletions
95
sandpack-react/src/common/OpenInCodeSandboxButton/UnstyledOpenInCodeSandboxButton.tsx
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,95 @@ | ||
import type { SandpackBundlerFiles } from "@codesandbox/sandpack-client"; | ||
import { getParameters } from "codesandbox-import-utils/lib/api/define"; | ||
import * as React from "react"; | ||
|
||
import { useSandpack } from "../../hooks/useSandpack"; | ||
import type { SandboxEnvironment } from "../../types"; | ||
|
||
const CSB_URL = "https://codesandbox.io/api/v1/sandboxes/define"; | ||
|
||
const getFileParameters = ( | ||
files: SandpackBundlerFiles, | ||
environment?: SandboxEnvironment | ||
) => { | ||
type NormalizedFiles = Record< | ||
string, | ||
{ | ||
content: string; | ||
isBinary: boolean; | ||
} | ||
>; | ||
|
||
const normalizedFiles = Object.keys(files).reduce((prev, next) => { | ||
const fileName = next.replace("/", ""); | ||
const value = { | ||
content: files[next].code, | ||
isBinary: false, | ||
}; | ||
|
||
return { ...prev, [fileName]: value }; | ||
}, {} as NormalizedFiles); | ||
|
||
return getParameters({ | ||
files: normalizedFiles, | ||
...(environment ? { template: environment } : null), | ||
}); | ||
}; | ||
|
||
export const UnstyledOpenInCodeSandboxButton: React.FC<{ | ||
className?: string; | ||
}> = ({ children, ...props }) => { | ||
const [paramsValues, setParamsValues] = React.useState(""); | ||
const { sandpack } = useSandpack(); | ||
const formRef = React.useRef<HTMLFormElement>(null); | ||
|
||
React.useEffect( | ||
function debounce() { | ||
const timer = setTimeout(() => { | ||
const params = getFileParameters(sandpack.files, sandpack.environment); | ||
|
||
setParamsValues(params); | ||
}, 600); | ||
|
||
return () => { | ||
clearTimeout(timer); | ||
}; | ||
}, | ||
[sandpack.environment, sandpack.files] | ||
); | ||
|
||
// Register the usage of the codesandbox link | ||
React.useEffect(function registerUsage() { | ||
sandpack.openInCSBRegisteredRef.current = true; | ||
}, []); | ||
|
||
/** | ||
* This is a safe limit to avoid too long requests (401), | ||
* as all parameters are attached in the URL | ||
*/ | ||
if (paramsValues.length > 1500) { | ||
return ( | ||
<button | ||
onClick={() => formRef.current?.submit()} | ||
title="Open in CodeSandbox" | ||
{...props} | ||
> | ||
<form ref={formRef} action={CSB_URL} method="POST" target="_blank"> | ||
<input name="parameters" type="hidden" value={paramsValues} /> | ||
</form> | ||
{children} | ||
</button> | ||
); | ||
} | ||
|
||
return ( | ||
<a | ||
href={`${CSB_URL}?parameters=${paramsValues}&query=file=${sandpack.activePath}%26from-sandpack=true`} | ||
rel="noreferrer noopener" | ||
target="_blank" | ||
title="Open in CodeSandbox" | ||
{...props} | ||
> | ||
{children} | ||
</a> | ||
); | ||
}; |
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,2 @@ | ||
export { OpenInCodeSandboxButton } from "./OpenInCodeSandboxButton"; | ||
export { UnstyledOpenInCodeSandboxButton } from "./UnstyledOpenInCodeSandboxButton"; |
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 was deleted.
Oops, something went wrong.
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