Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4216e28

Browse files
authored
fix: editor: fallback to default entrypoint (#16757)
Related: #16753 (comment)
1 parent 930816f commit 4216e28

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

site/src/pages/TemplateVersionEditorPage/TemplateVersionEditorPage.test.tsx

+29
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import type { MonacoEditorProps } from "./MonacoEditor";
2727
import { Language } from "./PublishTemplateVersionDialog";
2828
import TemplateVersionEditorPage, {
2929
findEntrypointFile,
30+
getActivePath,
3031
} from "./TemplateVersionEditorPage";
3132

3233
const { API } = apiModule;
@@ -413,6 +414,34 @@ function renderEditorPage(queryClient: QueryClient) {
413414
);
414415
}
415416

417+
describe("Get active path", () => {
418+
it("empty path", () => {
419+
const ft: FileTree = {
420+
"main.tf": "foobar",
421+
};
422+
const searchParams = new URLSearchParams({ path: "" });
423+
const activePath = getActivePath(searchParams, ft);
424+
expect(activePath).toBe("main.tf");
425+
});
426+
it("invalid path", () => {
427+
const ft: FileTree = {
428+
"main.tf": "foobar",
429+
};
430+
const searchParams = new URLSearchParams({ path: "foobaz" });
431+
const activePath = getActivePath(searchParams, ft);
432+
expect(activePath).toBe("main.tf");
433+
});
434+
it("valid path", () => {
435+
const ft: FileTree = {
436+
"main.tf": "foobar",
437+
"foobar.tf": "foobaz",
438+
};
439+
const searchParams = new URLSearchParams({ path: "foobar.tf" });
440+
const activePath = getActivePath(searchParams, ft);
441+
expect(activePath).toBe("foobar.tf");
442+
});
443+
});
444+
416445
describe("Find entrypoint", () => {
417446
it("empty tree", () => {
418447
const ft: FileTree = {};

site/src/pages/TemplateVersionEditorPage/TemplateVersionEditorPage.tsx

+14-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { type FC, useEffect, useState } from "react";
2020
import { Helmet } from "react-helmet-async";
2121
import { useMutation, useQuery, useQueryClient } from "react-query";
2222
import { useNavigate, useParams, useSearchParams } from "react-router-dom";
23-
import { type FileTree, traverse } from "utils/filetree";
23+
import { type FileTree, existsFile, traverse } from "utils/filetree";
2424
import { pageTitle } from "utils/page";
2525
import { TarReader, TarWriter } from "utils/tar";
2626
import { createTemplateVersionFileTree } from "utils/templateVersion";
@@ -88,9 +88,8 @@ export const TemplateVersionEditorPage: FC = () => {
8888
useState<TemplateVersion>();
8989

9090
// File navigation
91-
// It can be undefined when a selected file is deleted
92-
const activePath: string | undefined =
93-
searchParams.get("path") ?? findEntrypointFile(fileTree ?? {});
91+
const activePath = getActivePath(searchParams, fileTree || {});
92+
9493
const onActivePathChange = (path: string | undefined) => {
9594
if (path) {
9695
searchParams.set("path", path);
@@ -392,4 +391,15 @@ export const findEntrypointFile = (fileTree: FileTree): string | undefined => {
392391
return initialFile;
393392
};
394393

394+
export const getActivePath = (
395+
searchParams: URLSearchParams,
396+
fileTree: FileTree,
397+
): string | undefined => {
398+
const selectedPath = searchParams.get("path");
399+
if (selectedPath && existsFile(selectedPath, fileTree)) {
400+
return selectedPath;
401+
}
402+
return findEntrypointFile(fileTree);
403+
};
404+
395405
export default TemplateVersionEditorPage;

0 commit comments

Comments
 (0)