Skip to content

Commit 7ecd286

Browse files
Handle MakeCode shareId created by logged in users (#628)
* Handle MakeCode shareId created by logged in users (these use a different pattern, starting with S) * Rename shortId to shareId
1 parent 3e75eca commit 7ecd286

File tree

5 files changed

+23
-20
lines changed

5 files changed

+23
-20
lines changed

src/App.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,11 @@ const createRouter = () => {
184184
{
185185
path: createOpenSharedProjectPageUrl(),
186186
loader: ({ params }) => {
187-
if (!params.shortId || !/^_[a-zA-Z0-9]+$/.test(params.shortId)) {
188-
throw "Not a shared key";
187+
if (
188+
!params.shareId ||
189+
!/^_[a-zA-Z\d]{12}$|^S(?:\d{5}-){3}\d{5}$/.test(params.shareId)
190+
) {
191+
throw new Error("Not a valid shareId");
189192
}
190193
return null;
191194
},

src/e2e/app/open-shared-project-page.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ export class OpenSharedProjectPage {
1515
}
1616

1717
/**
18-
* Unlike other page fixtures, you must specify a shortId in the goto
18+
* Unlike other page fixtures, you must specify a shareId in the goto
1919
*/
20-
async goto(shortId: string, flags: string[] = ["open"]) {
21-
const response = await this.page.goto(`${this.url}${shortId}`);
20+
async goto(shareId: string, flags: string[] = ["open"]) {
21+
const response = await this.page.goto(`${this.url}${shareId}`);
2222
await this.page.evaluate(
2323
(flags) => localStorage.setItem("flags", flags.join(",")),
2424
flags

src/e2e/open-shared-project-page.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ test.describe("import shared page", () => {
3939
});
4040

4141
test("error", async ({ openSharedProjectPage }) => {
42-
await openSharedProjectPage.goto("_aBadSh0rt1d");
42+
await openSharedProjectPage.goto("_aaBadSh0rt1d");
4343
await openSharedProjectPage
4444
.expectMain()
4545
.toHaveAttribute("aria-busy", "true");

src/pages/OpenSharedProjectPage.tsx

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,17 @@ const OpenSharedProjectPage = () => {
7272
const [name, setName] = useState("");
7373
const { sharedState, header, dataset, projectText } =
7474
useProjectPreload(setName);
75-
const { shortId } = useParams();
75+
const { shareId } = useParams();
7676

7777
const handleOpenProject = useCallback(() => {
7878
if (!header || !projectText) return;
7979
loadProject({ header: { ...header, name }, text: projectText }, name);
8080
logging.event({
8181
type: "import-shared-project-complete",
82-
detail: { shortId },
82+
detail: { shareId },
8383
});
8484
navigate(createDataSamplesPageUrl());
85-
}, [loadProject, header, projectText, name, navigate, logging, shortId]);
85+
}, [loadProject, header, projectText, name, navigate, logging, shareId]);
8686

8787
if (sharedState === SharedState.Failed) {
8888
return <ErrorPreloading />;
@@ -141,7 +141,7 @@ const OpenSharedProjectPage = () => {
141141
color="brand.600"
142142
textDecoration="underline"
143143
href={`https://makecode.microbit.org/${encodeURIComponent(
144-
shortId!
144+
shareId!
145145
)}`}
146146
>
147147
{children}
@@ -373,7 +373,7 @@ const ErrorPreloading = () => {
373373
* it update the name on preload via a callback.
374374
*/
375375
const useProjectPreload = (setName: (name: string) => void) => {
376-
const { shortId } = useParams();
376+
const { shareId } = useParams();
377377
const logging = useLogging();
378378

379379
const [sharedState, setSharedState] = useState<SharedState>(
@@ -385,15 +385,15 @@ const useProjectPreload = (setName: (name: string) => void) => {
385385
const [dataset, setDataset] = useState<ActionData[]>();
386386

387387
useEffect(() => {
388-
if (!shortId) return;
388+
if (!shareId) return;
389389
let cleanedUp = false;
390390
const loadAsync = async () => {
391391
try {
392392
logging.event({
393393
type: "import-shared-project-start",
394-
detail: { shortId },
394+
detail: { shareId },
395395
});
396-
const header = await fetchSharedHeader(shortId);
396+
const header = await fetchSharedHeader(shareId);
397397
if (cleanedUp) {
398398
throw new Error("Cancelled");
399399
}
@@ -414,12 +414,12 @@ const useProjectPreload = (setName: (name: string) => void) => {
414414
setProjectText(text);
415415
logging.event({
416416
type: "import-shared-project-preloaded",
417-
detail: { shortId },
417+
detail: { shareId },
418418
});
419419
} catch (e: unknown) {
420420
logging.event({
421421
type: "import-shared-project-failed",
422-
detail: { shortId, error: e },
422+
detail: { shareId, error: e },
423423
});
424424
if (!cleanedUp) {
425425
setSharedState(SharedState.Failed);
@@ -430,7 +430,7 @@ const useProjectPreload = (setName: (name: string) => void) => {
430430
return () => {
431431
cleanedUp = true;
432432
};
433-
}, [logging, shortId, setName]);
433+
}, [logging, shareId, setName]);
434434
return {
435435
sharedState,
436436
header,
@@ -440,9 +440,9 @@ const useProjectPreload = (setName: (name: string) => void) => {
440440
};
441441
};
442442

443-
const fetchSharedHeader = async (shortId: string): Promise<Header> => {
443+
const fetchSharedHeader = async (shareId: string): Promise<Header> => {
444444
const headerResponse = await fetch(
445-
`https://www.makecode.com/api/${encodeURIComponent(shortId)}`
445+
`https://www.makecode.com/api/${encodeURIComponent(shareId)}`
446446
);
447447
if (!headerResponse.ok) {
448448
throw new Error("Network error");

src/urls.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const createNewPageUrl = () => `${basepath}new`;
1515

1616
export const createImportPageUrl = () => `${basepath}import`;
1717

18-
export const createOpenSharedProjectPageUrl = () => `${basepath}:shortId`;
18+
export const createOpenSharedProjectPageUrl = () => `${basepath}:shareId`;
1919

2020
export const createDataSamplesPageUrl = () => `${basepath}data-samples`;
2121

0 commit comments

Comments
 (0)