Skip to content

Commit 58cd675

Browse files
Rationalise URLs (#970)
The previous scheme assumed that we'd maintain independent state for the tabs, but we decided against that. So now we can go for a more traditional URL routing scheme.
1 parent e3b00ec commit 58cd675

File tree

15 files changed

+73
-183
lines changed

15 files changed

+73
-183
lines changed

src/base.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export const baseUrl = (() => {
2+
let base = process.env.PUBLIC_URL || "/";
3+
if (!base.endsWith("/")) {
4+
base += "/";
5+
}
6+
return base;
7+
})();

src/documentation/api/ApiDocumentation.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,7 @@ import { docStyles } from "../../common/documentation-styles";
1313
import HeadedScrollablePanel from "../../common/HeadedScrollablePanel";
1414
import { splitDocString } from "../../editor/codemirror/language-server/docstrings";
1515
import { ApiDocsEntry, ApiDocsResponse } from "../../language-server/apidocs";
16-
import {
17-
Anchor,
18-
RouterParam,
19-
useRouterParam,
20-
useRouterState,
21-
} from "../../router-hooks";
16+
import { Anchor, useRouterTabSlug, useRouterState } from "../../router-hooks";
2217
import DocString from "../common/DocString";
2318
import { useAnimationDirection } from "../common/documentation-animation-hooks";
2419
import DocumentationBreadcrumbHeading from "../common/DocumentationBreadcrumbHeading";
@@ -32,7 +27,7 @@ interface ApiDocumentationProps {
3227
}
3328

3429
export const ApiDocumentation = ({ docs }: ApiDocumentationProps) => {
35-
const [anchor, setAnchor] = useRouterParam(RouterParam.api);
30+
const [anchor, setAnchor] = useRouterTabSlug("api");
3631
const handleNavigate = useCallback(
3732
(id: string | undefined) => {
3833
setAnchor(id ? { id } : undefined, "documentation-user");

src/documentation/api/apidocs-util.test.ts

Lines changed: 1 addition & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@
44
* SPDX-License-Identifier: MIT
55
*/
66
import { ApiDocsResponse } from "../../language-server/apidocs";
7-
import {
8-
moduleAndApiFromId,
9-
pullModulesToTop,
10-
resolveDottedName,
11-
} from "./apidocs-util";
7+
import { moduleAndApiFromId, pullModulesToTop } from "./apidocs-util";
128

139
describe("pullModulesToTop", () => {
1410
it("pulls modules up", () => {
@@ -72,68 +68,6 @@ describe("pullModulesToTop", () => {
7268
});
7369
});
7470

75-
describe("resolveDottedName", () => {
76-
const docs: ApiDocsResponse = {
77-
microbit: {
78-
fullName: "microbit",
79-
id: "microbit",
80-
name: "microbit",
81-
kind: "module",
82-
children: [
83-
{
84-
fullName: "microbit.Button",
85-
name: "Button",
86-
kind: "class",
87-
id: "microbit.Button",
88-
children: [
89-
{
90-
fullName: "microbit.Button.is_pressed",
91-
kind: "function",
92-
id: "microbit.Button.is_pressed",
93-
name: "is_pressed",
94-
},
95-
],
96-
},
97-
],
98-
},
99-
"microbit.compass": {
100-
fullName: "microbit.compass",
101-
id: "microbit.compass",
102-
name: "compass",
103-
kind: "module",
104-
children: [
105-
{
106-
fullName: "microbit.compass.get_x",
107-
kind: "function",
108-
id: "microbit.compass.get_x",
109-
name: "get_x",
110-
},
111-
],
112-
},
113-
};
114-
it("finds modules", () => {
115-
expect(resolveDottedName(docs, "microbit.compass")).toEqual(
116-
docs["microbit.compass"]
117-
);
118-
expect(resolveDottedName(docs, "microbit")).toEqual(docs["microbit"]);
119-
});
120-
it("finds classes", () => {
121-
expect(resolveDottedName(docs, "microbit.Button")?.fullName).toEqual(
122-
"microbit.Button"
123-
);
124-
});
125-
it("finds functions", () => {
126-
expect(resolveDottedName(docs, "microbit.compass.get_x")?.fullName).toEqual(
127-
"microbit.compass.get_x"
128-
);
129-
});
130-
it("finds class members", () => {
131-
expect(
132-
resolveDottedName(docs, "microbit.Button.is_pressed")?.fullName
133-
).toEqual("microbit.Button.is_pressed");
134-
});
135-
});
136-
13771
describe("moduleAndApiFromId", () => {
13872
it("correctly splits module and apiId from id with three segments", () => {
13973
const id = "microbit.display.scroll";

src/documentation/api/apidocs-util.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,6 @@ export const resolveModule = (
3939
);
4040
};
4141

42-
export const resolveDottedName = (docs: ApiDocsResponse, name: string) => {
43-
let entry = resolveModule(docs, name);
44-
if (!entry) {
45-
return undefined;
46-
}
47-
const remainder = name.substring(entry.fullName.length + 1);
48-
if (remainder.length > 0) {
49-
for (const part of remainder.split(".")) {
50-
if (entry && entry.children) {
51-
entry = entry.children.find((e) => e.name === part);
52-
} else {
53-
return undefined;
54-
}
55-
}
56-
}
57-
return entry;
58-
};
59-
6042
export const moduleAndApiFromId = (id: string) => {
6143
const idSegments = id.split(".");
6244
const pythonModuleName = idSegments[0];

src/documentation/common/DocumentationContent.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ const DocumentationApiLinkMark = (
7474
e.preventDefault();
7575
setState({
7676
tab: "api",
77-
api: { id: props.mark.name },
77+
slug: { id: props.mark.name },
7878
});
7979
}}
8080
>
@@ -96,7 +96,7 @@ const DocumentationInternalLinkMark = (
9696
{
9797
...state,
9898
tab: "reference",
99-
reference: {
99+
slug: {
100100
id: props.mark.slug.current,
101101
},
102102
},

src/documentation/ideas/IdeasDocumentation.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { docStyles } from "../../common/documentation-styles";
1212
import HeadedScrollablePanel from "../../common/HeadedScrollablePanel";
1313
import { getAspectRatio, imageUrlBuilder } from "../../common/imageUrlBuilder";
1414
import { useResizeObserverContentRect } from "../../common/use-resize-observer";
15-
import { Anchor, RouterParam, useRouterParam } from "../../router-hooks";
15+
import { Anchor, useRouterTabSlug } from "../../router-hooks";
1616
import { useAnimationDirection } from "../common/documentation-animation-hooks";
1717
import DocumentationBreadcrumbHeading from "../common/DocumentationBreadcrumbHeading";
1818
import DocumentationContent, {
@@ -27,7 +27,7 @@ interface IdeasDocumentationProps {
2727
}
2828

2929
const IdeasDocumentation = ({ ideas }: IdeasDocumentationProps) => {
30-
const [anchor, setAnchor] = useRouterParam(RouterParam.idea);
30+
const [anchor, setAnchor] = useRouterTabSlug("ideas");
3131
const direction = useAnimationDirection(anchor);
3232
const ideaId = anchor?.id;
3333
const handleNavigate = useCallback(

src/documentation/reference/ReferenceDocumentation.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { useCallback } from "react";
88
import { useIntl } from "react-intl";
99
import { docStyles } from "../../common/documentation-styles";
1010
import HeadedScrollablePanel from "../../common/HeadedScrollablePanel";
11-
import { Anchor, RouterParam, useRouterParam } from "../../router-hooks";
11+
import { Anchor, useRouterTabSlug } from "../../router-hooks";
1212
import { useAnimationDirection } from "../common/documentation-animation-hooks";
1313
import DocumentationBreadcrumbHeading from "../common/DocumentationBreadcrumbHeading";
1414
import DocumentationContent from "../common/DocumentationContent";
@@ -29,7 +29,7 @@ interface ReferenceDocumentationProps {
2929
* generate the API documentation.
3030
*/
3131
const ReferenceToolkit = ({ toolkit }: ReferenceDocumentationProps) => {
32-
const [anchor, setAnchor] = useRouterParam(RouterParam.reference);
32+
const [anchor, setAnchor] = useRouterTabSlug("reference");
3333
const direction = useAnimationDirection(anchor);
3434
const topicOrEntryId = anchor?.id.split("/")[0];
3535
const handleNavigate = useCallback(

src/documentation/search/search.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ describe("Search", () => {
6565
id: "indentations",
6666
navigation: {
6767
tab: "reference",
68-
reference: { id: "indentations" },
68+
slug: { id: "indentations" },
6969
},
7070
extract: {
7171
title: [

src/documentation/search/search.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export class SearchIndex {
9393
containerTitle: content.containerTitle,
9494
navigation: {
9595
tab: this.tab,
96-
[this.tab]: { id: content.id },
96+
slug: { id: content.id },
9797
},
9898
extract: extracts,
9999
};

src/editor/codemirror/CodeMirror.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ const CodeMirror = ({
240240
setRouterState(
241241
{
242242
tab,
243-
[tab]: { id },
243+
slug: { id },
244244
},
245245
"documentation-from-code"
246246
);

0 commit comments

Comments
 (0)