|
| 1 | +import { Modal } from "@carbon/react" |
| 2 | +import { InlineLoadingStatus } from "carbon-components-react" |
| 3 | +import hljs from "highlight.js/lib/core" |
| 4 | +import json from "highlight.js/lib/languages/json" |
| 5 | +import { useState } from "react" |
| 6 | +import { createPortal } from "react-dom" |
| 7 | +import Editor from "react-simple-code-editor" |
| 8 | +import { importFlowFromJson } from "../../../singletons/store/appActions" |
| 9 | + |
| 10 | +hljs.registerLanguage("json", json) |
| 11 | + |
| 12 | +interface ImportFlowModalProps { |
| 13 | + open: boolean |
| 14 | + setOpen: (open: boolean) => void |
| 15 | +} |
| 16 | + |
| 17 | +interface JsonEditorProps { |
| 18 | + content: string |
| 19 | + setContent: (content: string) => void |
| 20 | +} |
| 21 | + |
| 22 | +const getLoadingDescription = (status: InlineLoadingStatus) => { |
| 23 | + switch (status) { |
| 24 | + case "active": |
| 25 | + return "importing JSON" |
| 26 | + case "error": |
| 27 | + return "Invalid Flow JSON" |
| 28 | + case "inactive": |
| 29 | + case "finished": |
| 30 | + return "" |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +const FlowJsonEditor = ({ content, setContent }: JsonEditorProps) => { |
| 35 | + return ( |
| 36 | + <div className="options-modal__editor"> |
| 37 | + <Editor |
| 38 | + value={content} |
| 39 | + onValueChange={(code) => setContent(code)} |
| 40 | + highlight={(code) => hljs.highlight(code, { language: "json" }).value} |
| 41 | + padding={16} |
| 42 | + textareaClassName="options-modal__editor-textarea" |
| 43 | + /> |
| 44 | + </div> |
| 45 | + ) |
| 46 | +} |
| 47 | + |
| 48 | +export const ImportFlowModal = ({ open, setOpen }: ImportFlowModalProps) => { |
| 49 | + const [loadingStatus, setLoadingStatus] = |
| 50 | + useState<InlineLoadingStatus>("inactive") |
| 51 | + const [content, setContent] = useState("") |
| 52 | + |
| 53 | + const resetAndCloseModal = () => { |
| 54 | + setOpen(false) |
| 55 | + setLoadingStatus("inactive") |
| 56 | + setContent("") |
| 57 | + } |
| 58 | + |
| 59 | + const doImport = () => { |
| 60 | + setLoadingStatus("active") |
| 61 | + try { |
| 62 | + importFlowFromJson(content) |
| 63 | + resetAndCloseModal() |
| 64 | + } catch { |
| 65 | + setLoadingStatus("error") |
| 66 | + return |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + const updateContent = (content: string) => { |
| 71 | + loadingStatus !== "inactive" && setLoadingStatus("inactive") |
| 72 | + setContent(content) |
| 73 | + } |
| 74 | + |
| 75 | + return createPortal( |
| 76 | + <Modal |
| 77 | + open={open} |
| 78 | + onRequestClose={resetAndCloseModal} |
| 79 | + size="md" |
| 80 | + modalHeading="Import a Flow JSON" |
| 81 | + primaryButtonText="Import" |
| 82 | + secondaryButtonText="Cancel" |
| 83 | + loadingStatus={loadingStatus} |
| 84 | + loadingDescription={getLoadingDescription(loadingStatus)} |
| 85 | + onRequestSubmit={doImport} |
| 86 | + > |
| 87 | + <FlowJsonEditor content={content} setContent={updateContent} /> |
| 88 | + </Modal>, |
| 89 | + document.body |
| 90 | + ) |
| 91 | +} |
0 commit comments