Skip to content

fix: editor: fallback to default entrypoint #16757

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 28, 2025
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
fix: editor: fallback to default entrypoint
  • Loading branch information
mtojek committed Feb 28, 2025
commit 20c49f2dc6cbb7d499df465ded6e9b34b591d4bd
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