()
| 17 | * It warns them that it will replace their current sandbox. |
| 18 | */ |
| 19 | export function LoadFromHashDialog() { |
| 20 | const [isOpen, setIsOpen] = useState(false); |
| 21 | const { hash } = useLocation(); |
| 22 | useEffect(() => { |
| 23 | // Check if matches #load:... |
| 24 | if (hash.startsWith("#load:")) { |
| 25 | setIsOpen(true); |
| 26 | } |
| 27 | }, [hash]); |
| 28 | return ( |
| 29 | <Dialog.Root open={isOpen} onOpenChange={setIsOpen}> |
| 30 | <Dialog.Portal> |
| 31 | <Overlay /> |
| 32 | <Content> |
| 33 | <div className="grid gap-4"> |
| 34 | <Dialog.Title> |
| 35 | <SectionTitle> |
| 36 | <Trans>Load from link?</Trans> |
| 37 | </SectionTitle> |
| 38 | </Dialog.Title> |
| 39 | <Dialog.Description> |
| 40 | <Warning className="mr-px inline-block -translate-y-px" />{" "} |
| 41 | <Trans>This will replace your current sandbox.</Trans> |
| 42 | </Dialog.Description> |
| 43 | <div className="flex gap-1 items-center mt-4 justify-between"> |
| 44 | <Button2 |
| 45 | onClick={() => { |
| 46 | setIsOpen(false); |
| 47 | // wipe the hash |
| 48 | window.location.hash = ""; |
| 49 | }} |
| 50 | > |
| 51 | Cancel |
| 52 | </Button2> |
| 53 | <Button2 |
| 54 | color="inverted" |
| 55 | onClick={() => { |
| 56 | const graphText = hash.slice("#load:".length); |
| 57 | const graph = decompressFromEncodedURIComponent(graphText); |
| 58 | if (!graph) return; |
| 59 | try { |
| 60 | const [text, metaStr] = graph.split(newDelimiters); |
| 61 | const meta = JSON.parse(metaStr.trim()); |
| 62 | meta.expires = addDays(new Date(), 1).toISOString(); |
| 63 | const final = `${text}\n=====\n${JSON.stringify( |
| 64 | meta, |
| 65 | null, |
| 66 | 2 |
| 67 | )}\n=====`; |
| 68 | localStorage.setItem(SANDBOX_STORAGE_KEY, final); |
| 69 | prepareChart({ |
| 70 | doc: final, |
| 71 | details: { |
| 72 | id: "", |
| 73 | title: "", |
| 74 | isHosted: false, |
| 75 | }, |
| 76 | }); |
nothing calls this directly
no test coverage detected