Skip to content

Commit 50a7c3f

Browse files
committed
add filetree for embed
1 parent 4c58a64 commit 50a7c3f

File tree

1 file changed

+52
-9
lines changed
  • packages/app/src/app/pages/common/Modals/ShareModal2

1 file changed

+52
-9
lines changed

packages/app/src/app/pages/common/Modals/ShareModal2/index.tsx

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,27 @@
22
Done:
33
- convert to typescript
44
- include darkMode in settings
5-
TODO:
6-
- add default module to show
75
- check what initial path does
6+
- add default module to show
7+
TODO:
88
- visual polish
99
- overflow-x
1010
- multiline
1111
- style more info in brackets
1212
- total height of the container is beyond preview
13-
- bonus: add postMessage to embed for smoother embed modal
13+
BONUS:
14+
- add postMessage to embed for smoother embed modal
1415
*/
1516

1617
import React from 'react';
1718
import queryString from 'query-string';
19+
import { useOvermind } from 'app/overmind';
1820
import { ThemeProvider } from 'styled-components';
21+
1922
import { theme } from '@codesandbox/common/lib/design-language';
23+
import { Sandbox } from '@codesandbox/common/lib/types';
2024

25+
import FileTree from 'embed/components/Sidebar/FileTree';
2126
import { Section, SectionBody, Switch } from './components';
2227

2328
import {
@@ -42,6 +47,7 @@ const globalOptions = {
4247
enableESLint: false,
4348
defaultFile: null,
4449
showNavigation: false,
50+
currentModuleId: null,
4551
};
4652

4753
const presets = {
@@ -78,7 +84,18 @@ function getView({ showEditor, showPreview }) {
7884
return 'preview';
7985
}
8086

81-
function getUrl(settings) {
87+
const getModulePath = ({
88+
sandbox,
89+
moduleId,
90+
}: {
91+
sandbox: Sandbox;
92+
moduleId: string;
93+
}): string => {
94+
const selectedModule = sandbox.modules.find(module => module.id === moduleId);
95+
return selectedModule.path;
96+
};
97+
98+
function getUrl({ settings, sandbox }) {
8299
const flags = {
83100
hidenavigation: settings.showNavigation ? 0 : 1,
84101
theme: settings.darkMode ? 'dark' : 'light',
@@ -91,20 +108,28 @@ function getUrl(settings) {
91108
highlights: settings.highlightLines || null,
92109
eslint: settings.enableESLint ? 1 : null,
93110
initialpath: settings.initialPath || null,
111+
module: settings.currentModuleId
112+
? getModulePath({ sandbox, moduleId: settings.currentModuleId })
113+
: null,
94114
};
95115

96116
const stringified = queryString.stringify(flags, {
97117
encode: false,
98118
skipNull: true,
99119
});
100120

101-
const url =
102-
`https://codesandbox.io/embed/dark-magic-variant-5pj49?` + stringified;
121+
const url = `https://codesandbox.io/embed/${sandbox.alias}?` + stringified;
103122

104123
return url;
105124
}
106125

107126
function ShareModal() {
127+
const {
128+
state: {
129+
editor: { currentSandbox: sandbox, mainModule },
130+
},
131+
} = useOvermind();
132+
108133
const [settings, setSettings] = React.useState({
109134
preset: 'split-view',
110135
...globalOptions,
@@ -270,13 +295,31 @@ function ShareModal() {
270295
}
271296
/>
272297
</Option>
298+
<Option multiline>
299+
Default file to show
300+
<ThemeProvider
301+
theme={{
302+
// we borrow this component from embed, so we have to pass
303+
// it the required theme token
304+
colors: { sideBar: { border: theme.colors.grays[700] } },
305+
}}
306+
>
307+
<FileTree
308+
sandbox={sandbox}
309+
currentModuleId={settings.currentModuleId || mainModule.id}
310+
setCurrentModuleId={value =>
311+
change({ currentModuleId: value })
312+
}
313+
/>
314+
</ThemeProvider>
315+
</Option>
273316
</Section>
274317
<SectionBody>
275318
<TextArea
276319
code
277320
rows={5}
278321
readOnly
279-
value={getUrl(settings)}
322+
value={getUrl({ settings, sandbox })}
280323
ref={urlContainer}
281324
/>
282325
<Button onClick={copyEmbedCode}>
@@ -287,14 +330,14 @@ function ShareModal() {
287330
<Input
288331
code
289332
readOnly
290-
value={getUrl(settings).replace('embed', 's')}
333+
value={getUrl({ settings, sandbox }).replace('embed', 's')}
291334
/>
292335
</Option>
293336
</SectionBody>
294337
</Sidebar>
295338
<Preview>
296339
<iframe
297-
src={getUrl(settings)}
340+
src={getUrl({ settings, sandbox })}
298341
title="Dark Magic Variant"
299342
allow="geolocation; microphone; camera; midi; vr; accelerometer; gyroscope; payment; ambient-light-sensor; encrypted-media; usb"
300343
sandbox="allow-modals allow-forms allow-popups allow-scripts allow-same-origin"

0 commit comments

Comments
 (0)