()
| 12 | import { docToString } from "../lib/useDoc"; |
| 13 | |
| 14 | export function LoadFileButton() { |
| 15 | const [open, setOpen] = useState(false); |
| 16 | const [doc, setDoc] = useState(""); |
| 17 | const [chart, setChart] = useState<null | Awaited< |
| 18 | ReturnType<typeof prepareChart> |
| 19 | >>(null); |
| 20 | const hasProAccess = useHasProAccess(); |
| 21 | |
| 22 | return ( |
| 23 | <> |
| 24 | <input |
| 25 | type="file" |
| 26 | accept=".txt" |
| 27 | data-testid="load-file-input" |
| 28 | className="sr-only" |
| 29 | onChange={(event) => { |
| 30 | const file = (event.target as HTMLInputElement).files?.[0]; |
| 31 | if (!file) return; |
| 32 | const reader = new FileReader(); |
| 33 | reader.onload = async (event) => { |
| 34 | const result = event.target?.result as string; |
| 35 | try { |
| 36 | prepareChart({ |
| 37 | doc: result, |
| 38 | set: false, |
| 39 | details: { |
| 40 | id: "", |
| 41 | title: "", |
| 42 | isHosted: false, |
| 43 | }, |
| 44 | }) |
| 45 | .then((chart) => { |
| 46 | if (chart) { |
| 47 | // build a chart with a normal expiry date |
| 48 | const { text, details, ...rest } = chart; |
| 49 | const meta = { ...rest.meta, expires: getExpirationDate() }; |
| 50 | const doc = docToString({ text, meta, details }); |
| 51 | setChart(chart); |
| 52 | setDoc(doc); |
| 53 | setOpen(true); |
| 54 | } else { |
| 55 | setOpen(false); |
| 56 | } |
| 57 | }) |
| 58 | .catch((error) => { |
| 59 | console.error(error); |
| 60 | }); |
| 61 | } catch (error) { |
| 62 | console.error(error); |
| 63 | } |
| 64 | }; |
| 65 | reader.readAsText(file); |
| 66 | }} |
| 67 | /> |
| 68 | <EditorActionTextButton |
| 69 | icon={PiFolderOpenDuotone} |
| 70 | data-testid="load-file-button" |
| 71 | data-session-activity="Load File" |
nothing calls this directly
no test coverage detected