forked from microbit-foundation/cctd-ml-machine
-
Notifications
You must be signed in to change notification settings - Fork 2
Handle opening project from microbit.org (first cut for testing) #388
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
Merged
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
9f4cc04
WIP locally working open in createAI flow
microbit-grace c6a0097
Add import page
microbit-grace 089aa45
Remove not needed commented out block
microbit-grace a7c3c9f
Remove activitiesBaseUrl
microbit-grace 8d72fb1
Merge branch 'main' of https://github.com/microbit-foundation/ml-trai…
microbit-grace f80fb61
Revert Homepage.tsx
microbit-grace 8bbdc60
Fix merge
microbit-grace 34160b0
Temp activitiesBaseUrl
microbit-grace 0ab41d4
Hardcode activitiesBaseUrl
microbit-grace f3b0fd6
Remove debug logging
microbit-grace f0370fe
Temp hardcoding for local testing
microbit-grace d94db35
Change hardcoded url to preview url
microbit-grace 2ba8c6b
Use deployment activitiesBaseUrl
microbit-grace 6c3960f
Update ml-trainer-microbit version
microbit-grace 33152af
Updated testing-library/react lib
microbit-grace fa5dca4
Add loadProject store test
microbit-grace 92dc85a
Revert "Updated testing-library/react lib"
microbit-grace f1e1577
Revert "Add loadProject store test"
microbit-grace c44bb77
Tweak copy
microbit-matt-hillsdon a2833ab
Tweaks
microbit-matt-hillsdon 7fa21b2
Add timestamp on loading a project
microbit-matt-hillsdon 628faf2
Comment
microbit-matt-hillsdon 0c2d83e
Merge branch 'main' into open-in-createai
microbit-matt-hillsdon b7f5b13
Share code to get gestures; loadProject as a new session
microbit-matt-hillsdon 04c8ef9
Lint
microbit-matt-hillsdon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,7 +36,7 @@ jobs: | |
- run: npm ci | ||
env: | ||
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
- run: npm install --no-save @microbit-foundation/[email protected].10 @microbit-foundation/[email protected] @microbit-foundation/[email protected] | ||
- run: npm install --no-save @microbit-foundation/[email protected].13 @microbit-foundation/[email protected] @microbit-foundation/[email protected] | ||
if: github.repository_owner == 'microbit-foundation' | ||
env: | ||
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
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 { useStore } from "../store"; | ||
import { createDataSamplesPageUrl } from "../urls"; | ||
|
||
const ImportPage = () => { | ||
const navigate = useNavigate(); | ||
const intl = useIntl(); | ||
const { activitiesBaseUrl } = useDeployment(); | ||
const resource = useMicrobitResourceSearchParams(); | ||
const loadProject = useStore((s) => s.loadProject); | ||
|
||
useEffect(() => { | ||
const updateAsync = async () => { | ||
if (!resource || !activitiesBaseUrl) { | ||
return; | ||
} | ||
const code = await fetchMicrobitOrgResourceTargetCode( | ||
activitiesBaseUrl, | ||
resource, | ||
intl | ||
); | ||
loadProject(code); | ||
navigate(createDataSamplesPageUrl()); | ||
}; | ||
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 isValidProject = (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}${encodeURIComponent( | ||
resource.id | ||
)}-makecode.json`; | ||
let json; | ||
try { | ||
const response = await fetch(url); | ||
if (!response.ok) { | ||
throw new Error(`Unexpected response ${response.status}`); | ||
} | ||
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 || | ||
!isValidProject(json.editorContent) | ||
) { | ||
throw new Error(intl.formatMessage({ id: "code-format-error" })); | ||
} | ||
return json.editorContent; | ||
}; | ||
|
||
export default ImportPage; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.