|
| 1 | +import { Project } from "@microbit/makecode-embed/react"; |
| 2 | +import { useEffect } from "react"; |
| 3 | +import { IntlShape, useIntl } from "react-intl"; |
| 4 | +import { useNavigate } from "react-router"; |
| 5 | +import { useSearchParams } from "react-router-dom"; |
| 6 | +import { useDeployment } from "../deployment"; |
| 7 | +import { MicrobitOrgResource } from "../model"; |
| 8 | +import { useStore } from "../store"; |
| 9 | +import { createDataSamplesPageUrl } from "../urls"; |
| 10 | + |
| 11 | +const ImportPage = () => { |
| 12 | + const navigate = useNavigate(); |
| 13 | + const intl = useIntl(); |
| 14 | + const { activitiesBaseUrl } = useDeployment(); |
| 15 | + const resource = useMicrobitResourceSearchParams(); |
| 16 | + const loadProject = useStore((s) => s.loadProject); |
| 17 | + |
| 18 | + useEffect(() => { |
| 19 | + const updateAsync = async () => { |
| 20 | + if (!resource || !activitiesBaseUrl) { |
| 21 | + return; |
| 22 | + } |
| 23 | + const code = await fetchMicrobitOrgResourceTargetCode( |
| 24 | + activitiesBaseUrl, |
| 25 | + resource, |
| 26 | + intl |
| 27 | + ); |
| 28 | + loadProject(code); |
| 29 | + navigate(createDataSamplesPageUrl()); |
| 30 | + }; |
| 31 | + void updateAsync(); |
| 32 | + }, [activitiesBaseUrl, intl, loadProject, navigate, resource]); |
| 33 | + |
| 34 | + return <></>; |
| 35 | +}; |
| 36 | + |
| 37 | +const useMicrobitResourceSearchParams = (): MicrobitOrgResource | undefined => { |
| 38 | + const [params] = useSearchParams(); |
| 39 | + const id = params.get("id"); |
| 40 | + const project = params.get("project"); |
| 41 | + const name = params.get("name"); |
| 42 | + return id && name && project ? { id, project, name } : undefined; |
| 43 | +}; |
| 44 | + |
| 45 | +const isValidProject = (content: Project): content is Project => { |
| 46 | + return ( |
| 47 | + content && |
| 48 | + typeof content === "object" && |
| 49 | + "text" in content && |
| 50 | + !!content.text |
| 51 | + ); |
| 52 | +}; |
| 53 | + |
| 54 | +const fetchMicrobitOrgResourceTargetCode = async ( |
| 55 | + activitiesBaseUrl: string, |
| 56 | + resource: MicrobitOrgResource, |
| 57 | + intl: IntlShape |
| 58 | +): Promise<Project> => { |
| 59 | + const url = `${activitiesBaseUrl}${encodeURIComponent( |
| 60 | + resource.id |
| 61 | + )}-makecode.json`; |
| 62 | + let json; |
| 63 | + try { |
| 64 | + const response = await fetch(url); |
| 65 | + if (!response.ok) { |
| 66 | + throw new Error(`Unexpected response ${response.status}`); |
| 67 | + } |
| 68 | + json = (await response.json()) as object; |
| 69 | + } catch (e) { |
| 70 | + const rethrow = new Error( |
| 71 | + intl.formatMessage({ id: "code-download-error" }) |
| 72 | + ); |
| 73 | + rethrow.stack = e instanceof Error ? e.stack : undefined; |
| 74 | + throw rethrow; |
| 75 | + } |
| 76 | + if ( |
| 77 | + !("editorContent" in json) || |
| 78 | + typeof json.editorContent !== "object" || |
| 79 | + !json.editorContent || |
| 80 | + !isValidProject(json.editorContent) |
| 81 | + ) { |
| 82 | + throw new Error(intl.formatMessage({ id: "code-format-error" })); |
| 83 | + } |
| 84 | + return json.editorContent; |
| 85 | +}; |
| 86 | + |
| 87 | +export default ImportPage; |
0 commit comments