-
Notifications
You must be signed in to change notification settings - Fork 2
[WIP - DO NOT MERGE] Handle opening project from microbit.org #386
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
9f4cc04
c6a0097
089aa45
a7c3c9f
8d72fb1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { Project } from "@microbit/makecode-embed/react"; | ||
import { useEffect } from "react"; | ||
import { IntlShape, useIntl } from "react-intl"; | ||
import { useNavigate } from "react-router"; | ||
import { useSearchParams } from "react-router-dom"; | ||
import { useDeployment } from "../deployment"; | ||
import { MicrobitOrgResource } from "../model"; | ||
import { SessionPageId } from "../pages-config"; | ||
import { useStore } from "../store"; | ||
import { createSessionPageUrl } from "../urls"; | ||
|
||
const ImportPage = () => { | ||
const navigate = useNavigate(); | ||
const intl = useIntl(); | ||
const { activitiesBaseUrl } = useDeployment(); | ||
const resource = useMicrobitResourceSearchParams(); | ||
const loadProject = useStore((s) => s.loadProject); | ||
console.log("resource", resource); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To remove |
||
|
||
useEffect(() => { | ||
const updateAsync = async () => { | ||
if (!resource || !activitiesBaseUrl) { | ||
return; | ||
} | ||
const code = await fetchMicrobitOrgResourceTargetCode( | ||
activitiesBaseUrl, | ||
resource, | ||
intl | ||
); | ||
console.log("code", code); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To remove |
||
loadProject(code); | ||
navigate(createSessionPageUrl(SessionPageId.DataSamples)); | ||
Check failure on line 32 in src/pages/ImportPage.tsx
|
||
}; | ||
void updateAsync(); | ||
}, [activitiesBaseUrl, intl, loadProject, navigate, resource]); | ||
|
||
return <></>; | ||
}; | ||
|
||
const useMicrobitResourceSearchParams = (): MicrobitOrgResource | undefined => { | ||
const [params] = useSearchParams(); | ||
const id = params.get("id"); | ||
const project = params.get("project"); | ||
const name = params.get("name"); | ||
|
||
return id && name && project ? { id, project, name } : undefined; | ||
}; | ||
|
||
const isValidEditorContent = (content: Project): content is Project => { | ||
return ( | ||
content && | ||
typeof content === "object" && | ||
"text" in content && | ||
!!content.text | ||
); | ||
}; | ||
|
||
const fetchMicrobitOrgResourceTargetCode = async ( | ||
activitiesBaseUrl: string, | ||
resource: MicrobitOrgResource, | ||
intl: IntlShape | ||
): Promise<Project> => { | ||
const url = `${activitiesBaseUrl}${resource.id}-makecode.json`; | ||
let json; | ||
try { | ||
const response = await fetch(url); | ||
json = (await response.json()) as object; | ||
} catch (e) { | ||
const rethrow = new Error( | ||
intl.formatMessage({ id: "code-download-error" }) | ||
); | ||
rethrow.stack = e instanceof Error ? e.stack : undefined; | ||
throw rethrow; | ||
} | ||
if ( | ||
!("editorContent" in json) || | ||
typeof json.editorContent !== "object" || | ||
!json.editorContent || | ||
!isValidEditorContent(json.editorContent) | ||
) { | ||
throw new Error(intl.formatMessage({ id: "code-format-error" })); | ||
} | ||
return json.editorContent; | ||
}; | ||
|
||
export default ImportPage; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One string added. Do not merge till strings clean up is merged
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've now merged that