Skip to content

Commit e72eefa

Browse files
Merge pull request #32 from DiamondLightSource/use-display-names-in-tree
Use display names in tree
2 parents 6e9d5e5 + 83fca9b commit e72eefa

File tree

8 files changed

+163
-11
lines changed

8 files changed

+163
-11
lines changed

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<meta charset="UTF-8" />
55
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
66
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7-
<title>Vite + React + TS</title>
7+
<title>Daedalus</title>
88
</head>
99
<body>
1010
<div id="root"></div>

src/routes/DataBrowserPage.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import ArchiverMenuBar from "../components/ArchiverMenuBar";
33
import TracesPanel from "../components/TracesPanel";
44
import DataBrowserPlot from "../components/DataBrowserPlot";
55
import DLSAppBar from "../components/AppBar";
6-
import { createContext, useState } from "react";
6+
import { createContext, useEffect, useState } from "react";
77

88
export const MenuContext = createContext<{
99
menusOpen: {
@@ -20,6 +20,11 @@ export const MenuContext = createContext<{
2020
*/
2121
export function DataBrowserPage() {
2222
const [menusOpen, setMenusOpen] = useState({ trace: false, archiver: false });
23+
24+
useEffect(() => {
25+
document.title = "Data Browser | Daedalus";
26+
}, []);
27+
2328
return (
2429
<MenuContext.Provider value={{ menusOpen, setMenusOpen }}>
2530
<Box sx={{ display: "flex", width: "100%" }}>

src/routes/DemoPage.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { demoReducer, demoInitialState, FileState } from "../store";
44
import { useWindowWidth, useWindowHeight } from "../utils/helper";
55
import FileDisplay from "../components/FileDisplay";
66
import FileNavigationBar from "../components/FileNavigationBar";
7-
import { createContext } from "react";
7+
import { createContext, useEffect } from "react";
88
import DLSAppBar from "../components/AppBar";
99

1010
const FileStateContext = createContext<{
@@ -17,6 +17,10 @@ export function DemoPage() {
1717
const width = useWindowWidth();
1818
const height = useWindowHeight();
1919

20+
useEffect(() => {
21+
document.title = "Demo | Daedalus";
22+
}, []);
23+
2024
return (
2125
<>
2226
<Box sx={{ flexGrow: 1 }}>

src/routes/EditorPage.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export function EditorPage() {
4141

4242
// Only run once on mount
4343
useEffect(() => {
44+
document.title = "Editor | Daedalus";
4445
loadScreens();
4546
}, []);
4647

src/routes/LandingPage.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@ import DLSAppBar from "../components/AppBar";
33
import { useWindowHeight, APP_BAR_HEIGHT } from "../utils/helper";
44
import LinkCard from "../components/LinkCard";
55
import { PageRouteInfo } from "./PageRouteInfo";
6+
import { useEffect } from "react";
67

78
export function LandingPage() {
9+
useEffect(() => {
10+
document.title = "Home | Daedalus";
11+
}, []);
12+
813
// get width
914
return (
1015
<>

src/routes/SynopticPage.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export function SynopticPage() {
5050

5151
useEffect(() => {
5252
// Only trigger once
53+
document.title = `${params.beamline} Synoptic | Daedalus`;
5354
if (state.filesLoaded) {
5455
if (params.beamline && params.beamline !== state.currentBeamline)
5556
dispatch({
@@ -66,6 +67,7 @@ export function SynopticPage() {
6667

6768
// Only run once on mount
6869
useEffect(() => {
70+
document.title = "Synoptic | Daedalus";
6971
loadScreens();
7072
}, []);
7173

src/tests/parser.test.ts

Lines changed: 116 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
22
import {
33
parseScreenTree,
44
RecursiveTreeViewBuilder,
5-
RecursiveAppendDuplicateFileMacros
5+
RecursiveAppendDuplicateFileMacros,
6+
buildUrlId
67
} from "../utils/parser";
78
import { FileIDs } from "../store";
89

@@ -60,6 +61,41 @@ describe("parseScreenTree()", (): void => {
6061
expect(tree.length).toEqual(1);
6162
expect(firstFile).toEqual("top.bob");
6263
});
64+
65+
it("successfully fetches the file with a display name", async (): Promise<void> => {
66+
const mockSuccessResponse = JSON.parse(`
67+
{
68+
"file": "top.bob",
69+
"displayName": "Top Synoptic",
70+
"children": [
71+
{
72+
"file": "middle1.bob",
73+
"displayName": "Middle File 1",
74+
"children": [
75+
{
76+
"file": "bottom.bob",
77+
"displayName": "Bottom File"
78+
}
79+
]
80+
},
81+
{
82+
"file": "middle2.bob",
83+
"displayName": "Middle File 2"
84+
}
85+
]
86+
}`);
87+
const mockJsonPromise = Promise.resolve(mockSuccessResponse);
88+
const mockFetchPromise = Promise.resolve({
89+
json: (): Promise<unknown> => mockJsonPromise
90+
});
91+
const mockFetch = (): Promise<unknown> => mockFetchPromise;
92+
vi.spyOn(globalWithFetch, "fetch").mockImplementation(mockFetch);
93+
94+
const [tree, , firstFile] = await parseScreenTree("test-map.json");
95+
expect(tree.length).toEqual(1);
96+
expect(firstFile).toEqual("top.bob");
97+
expect(tree[0].label).toEqual("Top Synoptic");
98+
});
6399
});
64100

65101
describe("RecursiveTreeViewBuilder()", (): void => {
@@ -219,6 +255,58 @@ describe("RecursiveTreeViewBuilder()", (): void => {
219255

220256
expect(actualUrlIds.sort()).toEqual(expectedUrlIds.sort());
221257
});
258+
259+
it("parses child screens to generate the expected urlId strings with display names", async (): Promise<void> => {
260+
const testJson = {
261+
file: "top.bob",
262+
displayName: "Top Screen",
263+
children: [
264+
{
265+
file: "middle1.bob",
266+
displayName: "Middle Screen",
267+
children: [
268+
{
269+
file: "path1/index.bob",
270+
displayName: "Index 1"
271+
},
272+
{
273+
file: "path2/index.bob",
274+
displayName: "Index 2"
275+
},
276+
{
277+
file: "index.bob",
278+
displayName: "Index 3"
279+
},
280+
{
281+
file: "path1/not_index.bob",
282+
displayName: "Not Index"
283+
}
284+
]
285+
},
286+
{
287+
file: "path0/index.bob",
288+
displayName: "Index"
289+
}
290+
]
291+
};
292+
293+
const { fileMap } = await RecursiveTreeViewBuilder(
294+
testJson.children,
295+
"Top Screen"
296+
);
297+
298+
const expectedUrlIds = [
299+
"Top Screen+Index",
300+
"Top Screen+Middle Screen",
301+
"Top Screen+Middle Screen+Index 1",
302+
"Top Screen+Middle Screen+Index 2",
303+
"Top Screen+Middle Screen+Index 3",
304+
"Top Screen+Middle Screen+Not Index"
305+
];
306+
const actualUrlIds = Object.values(fileMap).map(x => x.urlId);
307+
308+
expect(actualUrlIds.sort()).toEqual(expectedUrlIds.sort());
309+
});
222310
});
223311

224312
describe("RecursiveAppendDuplicateFileMacros()", (): void => {
@@ -351,3 +439,30 @@ describe("RecursiveAppendDuplicateFileMacros()", (): void => {
351439
expect(aMacros).toEqual({ Another: "AnotherFilesMacros" });
352440
});
353441
});
442+
443+
describe("buildUrlId()", (): void => {
444+
it("uses the file name if no display name", (): void => {
445+
const { urlId, fileLabel } = buildUrlId("testing.bob", "", undefined);
446+
447+
expect(urlId).toBe("testing");
448+
expect(fileLabel).toBe("testing");
449+
});
450+
451+
it("uses display name as url id", (): void => {
452+
const { urlId, fileLabel } = buildUrlId("testing.bob", "", "My File");
453+
454+
expect(urlId).toBe("My File");
455+
expect(fileLabel).toBe("My File");
456+
});
457+
458+
it("correctly attaches a prefix", (): void => {
459+
const { urlId, fileLabel } = buildUrlId(
460+
"testing.bob",
461+
"First File",
462+
"My File"
463+
);
464+
465+
expect(urlId).toBe("First File+My File");
466+
expect(fileLabel).toBe("My File");
467+
});
468+
});

src/utils/parser.ts

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ export async function parseScreenTree(
1717
const response = await httpRequest(filepath);
1818
const json = await response.json();
1919

20-
const { urlId, fileLabel: topLevelScreen } = buildUrlId(json.file, "");
20+
const { urlId, fileLabel: topLevelScreen } = buildUrlId(
21+
json.file,
22+
"",
23+
json.displayName
24+
);
2125

2226
// Process the child items
2327
const { fileMap, treeViewItems } = RecursiveTreeViewBuilder(
@@ -76,7 +80,11 @@ export const RecursiveTreeViewBuilder = (
7680
}
7781

7882
for (const sibling of jsonSiblings) {
79-
const { urlId, fileLabel } = buildUrlId(sibling.file, idPrefix);
83+
const { urlId, fileLabel } = buildUrlId(
84+
sibling.file,
85+
idPrefix,
86+
sibling.displayName
87+
);
8088
const guid = crypto.randomUUID();
8189

8290
const treeViewItem: TreeViewBaseItem = {
@@ -88,9 +96,9 @@ export const RecursiveTreeViewBuilder = (
8896
if (!sibling.duplicate) {
8997
fileMap[guid] = {
9098
file: sibling.file,
91-
urlId,
92-
macros: sibling.macros ? [sibling.macros] : [],
93-
exists: sibling?.exists
99+
exists: sibling?.exists,
100+
urlId: urlId,
101+
macros: sibling.macros ? [sibling.macros] : []
94102
};
95103

96104
// If not a duplicate, check for children
@@ -139,9 +147,21 @@ export const RecursiveAppendDuplicateFileMacros = (
139147
}
140148
};
141149

142-
const buildUrlId = (filepath: string, idPrefix: string) => {
150+
/**
151+
* Builds the ID to display in the URL, based on the file name
152+
* and/or a given display name
153+
* @param filepath string path of bob file
154+
* @param idPrefix string prefix of all parent file IDs
155+
* @param displayName string alternate name of file to display
156+
* @returns string ID of url, label to display for current file
157+
*/
158+
export const buildUrlId = (
159+
filepath: string,
160+
idPrefix: string,
161+
displayName?: string
162+
) => {
143163
const splitFilePath = filepath.split(".bob")[0].split("/");
144-
let fileLabel: string = splitFilePath.pop()!;
164+
let fileLabel: string = displayName || splitFilePath.pop()!;
145165
if (fileLabel === "index") {
146166
const parentDir = splitFilePath.pop();
147167
if (parentDir) {

0 commit comments

Comments
 (0)