Skip to content
Merged
Changes from 1 commit
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 Oct 14, 2024
c6a0097
Add import page
microbit-grace Oct 14, 2024
089aa45
Remove not needed commented out block
microbit-grace Oct 14, 2024
a7c3c9f
Remove activitiesBaseUrl
microbit-grace Oct 15, 2024
8d72fb1
Merge branch 'main' of https://github.com/microbit-foundation/ml-trai…
microbit-grace Oct 15, 2024
f80fb61
Revert Homepage.tsx
microbit-grace Oct 15, 2024
8bbdc60
Fix merge
microbit-grace Oct 15, 2024
34160b0
Temp activitiesBaseUrl
microbit-grace Oct 15, 2024
0ab41d4
Hardcode activitiesBaseUrl
microbit-grace Oct 15, 2024
f3b0fd6
Remove debug logging
microbit-grace Oct 15, 2024
f0370fe
Temp hardcoding for local testing
microbit-grace Oct 15, 2024
d94db35
Change hardcoded url to preview url
microbit-grace Oct 15, 2024
2ba8c6b
Use deployment activitiesBaseUrl
microbit-grace Oct 15, 2024
6c3960f
Update ml-trainer-microbit version
microbit-grace Oct 15, 2024
33152af
Updated testing-library/react lib
microbit-grace Oct 15, 2024
fa5dca4
Add loadProject store test
microbit-grace Oct 15, 2024
92dc85a
Revert "Updated testing-library/react lib"
microbit-grace Oct 16, 2024
f1e1577
Revert "Add loadProject store test"
microbit-grace Oct 16, 2024
c44bb77
Tweak copy
microbit-matt-hillsdon Oct 16, 2024
a2833ab
Tweaks
microbit-matt-hillsdon Oct 16, 2024
7fa21b2
Add timestamp on loading a project
microbit-matt-hillsdon Oct 16, 2024
628faf2
Comment
microbit-matt-hillsdon Oct 16, 2024
0c2d83e
Merge branch 'main' into open-in-createai
microbit-matt-hillsdon Oct 16, 2024
b7f5b13
Share code to get gestures; loadProject as a new session
microbit-matt-hillsdon Oct 16, 2024
04c8ef9
Lint
microbit-matt-hillsdon Oct 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Revert Homepage.tsx
  • Loading branch information
microbit-grace committed Oct 15, 2024
commit f80fb61d2eb9681b24628de288ac16719048cb79
88 changes: 4 additions & 84 deletions src/pages/HomePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {
useInterval,
VStack,
} from "@chakra-ui/react";
import { ReactNode, useCallback, useEffect, useState } from "react";
import { FormattedMessage, IntlShape, useIntl } from "react-intl";
import { ReactNode, useCallback, useState } from "react";
import { FormattedMessage, useIntl } from "react-intl";
import { useNavigate } from "react-router";
import DefaultPageLayout from "../components/DefaultPageLayout";
import PercentageDisplay from "../components/PercentageDisplay";
Expand All @@ -24,13 +24,8 @@ import { useDeployment } from "../deployment";
import blockImage from "../images/block.png";
import xyzGraph from "../images/xyz-graph.png";
import clap from "../images/clap-square.png";
import { createNewPageUrl, createSessionPageUrl } from "../urls";
import { createNewPageUrl } from "../urls";
import { flags } from "../flags";
import { useSearchParams } from "react-router-dom";
import { MicrobitOrgResource } from "../model";
import { Project } from "@microbit/makecode-embed/react";
import { useStore } from "../store";
import { SessionPageId } from "../pages-config";

const graphData = {
x: [
Expand Down Expand Up @@ -68,88 +63,13 @@ const graphData = {
],
};

const useMicrobitResourceSearchParams = (): MicrobitOrgResource | undefined => {
const [params] = useSearchParams();
const id = params.get("id");
const project = params.get("project");
const name = params.get("name");

const resource: MicrobitOrgResource | undefined =
id && name && project
? {
id,
project,
name,
}
: undefined;
return resource;
};

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;
};

const HomePage = () => {
const navigate = useNavigate();
const handleGetStarted = useCallback(() => {
navigate(createNewPageUrl());
}, [navigate]);
const intl = useIntl();
const { appNameFull, activitiesBaseUrl } = useDeployment();
const resource = useMicrobitResourceSearchParams();
const loadProject = useStore((s) => s.loadProject);
console.log("resource", resource);

useEffect(() => {
const updateAsync = async () => {
if (!resource || !activitiesBaseUrl) {
return;
}
const code = await fetchMicrobitOrgResourceTargetCode(
activitiesBaseUrl,
resource,
intl
);
console.log("code", code);
loadProject(code);
navigate(createSessionPageUrl(SessionPageId.DataSamples));
};
void updateAsync();
}, [activitiesBaseUrl, intl, loadProject, navigate, resource]);

const { appNameFull } = useDeployment();
return (
<DefaultPageLayout
toolbarItemsRight={
Expand Down