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 ?
+
+
+ }
+ onClick={async () => {
+ await exportToCodeSandbox(false)
+ if (onClose) {
+ onClose()
+ }
+ }}
+ >
+ JSX
+
+ }
+ onClick={async () => {
+ await exportToCodeSandbox(true)
+ if (onClose) {
+ onClose()
+ }
+ }}
+ >
+ TSX
+
+
+
+
+ >
+ )}
+
)
}
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 => {