From 05ac9ac41646e1b001658abfdc79caa985a0f452 Mon Sep 17 00:00:00 2001 From: 0xPierre <43297207+PierreCrb@users.noreply.github.com> Date: Tue, 6 Sep 2022 10:05:31 +0200 Subject: [PATCH] add typescript export code ref to #129 (#138) * add typescript export code ref to #129 * add .tsx --- package.json | 2 +- src/components/Header.tsx | 97 ++++++++++++++++++++++++++++----------- src/utils/codesandbox.ts | 11 +++-- yarn.lock | 10 ++-- 4 files changed, 83 insertions(+), 37 deletions(-) diff --git a/package.json b/package.json index 23865e7c76..a30a20c142 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,7 @@ "react-dnd-html5-backend": "^10.0.2", "react-dom": "^16.12.0", "react-hotkeys-hook": "^2.4.0", - "react-icons": "^3.9.0", + "react-icons": "^4.4.0", "react-redux": "^7.1.3", "react-scripts": "3.3.0", "react-split-pane": "^0.1.89", diff --git a/src/components/Header.tsx b/src/components/Header.tsx index df8fdc4783..d0038570ce 100644 --- a/src/components/Header.tsx +++ b/src/components/Header.tsx @@ -24,6 +24,7 @@ import { import { ExternalLinkIcon, SmallCloseIcon, CheckIcon } from '@chakra-ui/icons' import { DiGithubBadge } from 'react-icons/di' import { AiFillThunderbolt } from 'react-icons/ai' +import { SiTypescript } from 'react-icons/si' import { buildParameters } from '~utils/codesandbox' import { generateCode } from '~utils/code' import useDispatch from '~hooks/useDispatch' @@ -31,39 +32,83 @@ import { useSelector } from 'react-redux' import { getComponents } from '~core/selectors/components' import { getShowLayout, getShowCode } from '~core/selectors/app' import HeaderMenu from '~components/headerMenu/HeaderMenu' +import { FaReact } from 'react-icons/fa' const CodeSandboxButton = () => { const components = useSelector(getComponents) const [isLoading, setIsLoading] = useState(false) + const exportToCodeSandbox = async (isTypeScript: boolean) => { + setIsLoading(true) + const code = await generateCode(components) + setIsLoading(false) + const parameters = buildParameters(code, isTypeScript) + + window.open( + `https://codesandbox.io/api/v1/sandboxes/define?parameters=${parameters}`, + '_blank', + ) + } + return ( - - + - window.open( - `https://codesandbox.io/api/v1/sandboxes/define?parameters=${parameters}`, - '_blank', - ) - }} - isLoading={isLoading} - rightIcon={} - variant="ghost" - size="xs" - > - Export code - - + + + + + Export format + + Export the code in CodeSandbox in which format ? + + + + + + + + + )} + ) } diff --git a/src/utils/codesandbox.ts b/src/utils/codesandbox.ts index 9e2e07b505..055bfadd57 100644 --- a/src/utils/codesandbox.ts +++ b/src/utils/codesandbox.ts @@ -1,6 +1,9 @@ import { getParameters } from 'codesandbox/lib/api/define' -export const buildParameters = (code: string): string => { +export const buildParameters = ( + code: string, + isTypeScript: boolean, +): string => { return getParameters({ files: { 'public/index.html': { @@ -26,7 +29,7 @@ export const buildParameters = (code: string): string => { `, isBinary: false, }, - 'index.js': { + [isTypeScript ? 'index.tsx' : 'index.js']: { content: `import React from "react"; import ReactDOM from "react-dom"; @@ -37,7 +40,7 @@ ReactDOM.render(, rootElement); `, isBinary: false, }, - 'App.js': { + [isTypeScript ? 'App.tsx' : 'App.jsx']: { content: code, isBinary: false, }, @@ -47,7 +50,7 @@ ReactDOM.render(, rootElement); "version": "1.0.0", "description": "", "keywords": [], - "main": "src/index.js", + "main": "src/${isTypeScript ? 'index.tsx' : 'index.js'}", "dependencies": { "@chakra-ui/react": "^1.5.0", "@chakra-ui/icons": "^1.0.9", diff --git a/yarn.lock b/yarn.lock index ee84fab78b..b5fde57ef9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -12952,12 +12952,10 @@ react-hotkeys-hook@^2.4.0: dependencies: hotkeys-js "3.8.1" -react-icons@^3.9.0: - version "3.9.0" - resolved "https://registry.yarnpkg.com/react-icons/-/react-icons-3.9.0.tgz#89a00f20a0e02e6bfd899977eaf46eb4624239d5" - integrity sha512-gKbYKR+4QsD3PmIHLAM9TDDpnaTsr3XZeK1NTAb6WQQ+gxEdJ0xuCgLq0pxXdS7Utg2AIpcVhM1ut/jlDhcyNg== - dependencies: - camelcase "^5.0.0" +react-icons@^4.4.0: + version "4.4.0" + resolved "https://registry.yarnpkg.com/react-icons/-/react-icons-4.4.0.tgz#a13a8a20c254854e1ec9aecef28a95cdf24ef703" + integrity sha512-fSbvHeVYo/B5/L4VhB7sBA1i2tS8MkT0Hb9t2H1AVPkwGfVHLJCqyr2Py9dKMxsyM63Eng1GkdZfbWj+Fmv8Rg== react-is@16.13.1, react-is@^16.8.1: version "16.13.1"