Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import type { MonacoEditorProps } from "./MonacoEditor";
import { Language } from "./PublishTemplateVersionDialog";
import TemplateVersionEditorPage, {
findEntrypointFile,
getActivePath,
} from "./TemplateVersionEditorPage";

const { API } = apiModule;
Expand Down Expand Up @@ -413,6 +414,34 @@ function renderEditorPage(queryClient: QueryClient) {
);
}

describe("Get active path", () => {
it("empty path", () => {
const ft: FileTree = {
"main.tf": "foobar",
};
const searchParams = new URLSearchParams({ path: "" });
const activePath = getActivePath(searchParams, ft);
expect(activePath).toBe("main.tf");
});
it("invalid path", () => {
const ft: FileTree = {
"main.tf": "foobar",
};
const searchParams = new URLSearchParams({ path: "foobaz" });
const activePath = getActivePath(searchParams, ft);
expect(activePath).toBe("main.tf");
});
it("valid path", () => {
const ft: FileTree = {
"main.tf": "foobar",
"foobar.tf": "foobaz",
};
const searchParams = new URLSearchParams({ path: "foobar.tf" });
const activePath = getActivePath(searchParams, ft);
expect(activePath).toBe("foobar.tf");
});
});

describe("Find entrypoint", () => {
it("empty tree", () => {
const ft: FileTree = {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { type FC, useEffect, useState } from "react";
import { Helmet } from "react-helmet-async";
import { useMutation, useQuery, useQueryClient } from "react-query";
import { useNavigate, useParams, useSearchParams } from "react-router-dom";
import { type FileTree, traverse } from "utils/filetree";
import { type FileTree, existsFile, traverse } from "utils/filetree";
import { pageTitle } from "utils/page";
import { TarReader, TarWriter } from "utils/tar";
import { createTemplateVersionFileTree } from "utils/templateVersion";
Expand Down Expand Up @@ -88,9 +88,8 @@ export const TemplateVersionEditorPage: FC = () => {
useState<TemplateVersion>();

// File navigation
// It can be undefined when a selected file is deleted
const activePath: string | undefined =
searchParams.get("path") ?? findEntrypointFile(fileTree ?? {});
const activePath = getActivePath(searchParams, fileTree || {});

const onActivePathChange = (path: string | undefined) => {
if (path) {
searchParams.set("path", path);
Expand Down Expand Up @@ -392,4 +391,15 @@ export const findEntrypointFile = (fileTree: FileTree): string | undefined => {
return initialFile;
};

export const getActivePath = (
searchParams: URLSearchParams,
fileTree: FileTree,
): string | undefined => {
const selectedPath = searchParams.get("path");
if (selectedPath && existsFile(selectedPath, fileTree)) {
return selectedPath;
}
return findEntrypointFile(fileTree);
};

export default TemplateVersionEditorPage;
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy