Skip to content

Commit 6e53dd9

Browse files
committed
Allow loading pipeline from JSON string
Closes #10
1 parent 6e2c25b commit 6e53dd9

File tree

1 file changed

+63
-2
lines changed

1 file changed

+63
-2
lines changed

app/components/ui/Overlay.tsx

+63-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {
3939
} from '~/store/store';
4040
import { Field, FieldGroup, Label } from '../catalyst/fieldset';
4141
import { Input } from '../catalyst/input';
42+
import { Textarea } from '../catalyst/textarea';
4243
import { GithubIcon } from './GithubIcon';
4344
import { Help } from './Help';
4445
import { Settings } from './Settings';
@@ -66,13 +67,15 @@ function MenuHolder({
6667

6768
export function OverlayUI() {
6869
const fileInputRef = useRef<HTMLInputElement>(null);
70+
const loadInputRef = useRef<HTMLTextAreaElement>(null);
6971
const [pipeline, setPipeline] = useAtom(pipelineAtom);
7072
const setStatus = useSetAtom(pipelineStatusAtom);
7173
const litlyticsConfig = useAtomValue(litlyticsConfigStore);
7274
const webllm = useAtomValue(webllmAtom);
7375
const { undo, redo, canUndo, canRedo } = useAtomValue(pipelineUndoAtom);
7476
const [isOpen, setIsOpen] = useState(false);
7577
const [isSaveOpen, setIsSaveOpen] = useState(false);
78+
const [isLoadOpen, setIsLoadOpen] = useState(false);
7679
const [isHelpOpen, setIsHelpOpen] = useState(false);
7780
const [isSettingsOpen, setIsSettingsOpen] = useState(false);
7881
const [isHelpFirstTime, setHelpFirstTime] = useAtom(helpAtom);
@@ -145,6 +148,26 @@ export function OverlayUI() {
145148
}
146149
};
147150

151+
const loadFromFile = () => fileInputRef.current?.click();
152+
153+
const loadFromString = () => {
154+
setError(undefined);
155+
const inputStr = loadInputRef.current?.value;
156+
if (!inputStr?.length) {
157+
setError(new Error('Input string is required!'));
158+
return;
159+
}
160+
try {
161+
const pipelineJson = JSON.parse(inputStr);
162+
setPipeline(pipelineJson);
163+
setStatus({ status: 'init' });
164+
loadInputRef.current!.value = '';
165+
setIsLoadOpen(false);
166+
} catch (err) {
167+
setError(err as Error);
168+
}
169+
};
170+
148171
const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
149172
const file = event.target.files?.[0];
150173
if (file) {
@@ -154,6 +177,7 @@ export function OverlayUI() {
154177
const json = JSON.parse(e.target?.result as string) as Pipeline;
155178
setPipeline(json);
156179
setStatus({ status: 'init' });
180+
setIsLoadOpen(false);
157181
} catch (error) {
158182
console.error('Error parsing JSON:', error);
159183
}
@@ -193,7 +217,7 @@ export function OverlayUI() {
193217

194218
<DropdownDivider />
195219

196-
<DropdownItem onClick={() => fileInputRef.current?.click()}>
220+
<DropdownItem onClick={() => setIsLoadOpen(true)}>
197221
<FolderIcon aria-hidden="true" className="h-5 w-5" />
198222
<DropdownLabel>Open pipeline</DropdownLabel>
199223
</DropdownItem>
@@ -268,6 +292,43 @@ export function OverlayUI() {
268292
</DialogActions>
269293
</Dialog>
270294

295+
{/* Pipeline open dialog */}
296+
<Dialog
297+
size="xl"
298+
open={isLoadOpen}
299+
onClose={setIsLoadOpen}
300+
topClassName="z-20"
301+
>
302+
<DialogTitle>Load pipeline</DialogTitle>
303+
<DialogBody className="w-full">
304+
<FieldGroup>
305+
<Field>
306+
<Label>Pipeline JSON</Label>
307+
<Textarea
308+
name="pipeline-json"
309+
placeholder="Pipeline JSON"
310+
autoFocus
311+
ref={loadInputRef}
312+
/>
313+
</Field>
314+
</FieldGroup>
315+
{error && (
316+
<div className="flex items-center justify-between bg-red-400 dark:bg-red-700 rounded-xl py-1 px-2 my-2">
317+
Error loading pipeline: {error.message}
318+
</div>
319+
)}
320+
</DialogBody>
321+
<DialogActions className="flex justify-between">
322+
<Button plain onClick={() => setIsLoadOpen(false)}>
323+
Close
324+
</Button>
325+
<Button onClick={loadFromString}>Load from string</Button>
326+
<Button color="green" onClick={loadFromFile}>
327+
Load from file
328+
</Button>
329+
</DialogActions>
330+
</Dialog>
331+
271332
{/* Pipeline save dialog */}
272333
<Dialog
273334
size="xl"
@@ -293,7 +354,7 @@ export function OverlayUI() {
293354
</FieldGroup>
294355
{error && (
295356
<div className="flex items-center justify-between bg-red-400 dark:bg-red-700 rounded-xl py-1 px-2 my-2">
296-
Error saving pipleine: {error.message}
357+
Error saving pipeline: {error.message}
297358
</div>
298359
)}
299360
</DialogBody>

0 commit comments

Comments
 (0)