Skip to content

feat(site): allow any file extension on template editor #12000

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 20 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add file tree on template files
  • Loading branch information
BrunoQuaresma committed Feb 5, 2024
commit 2353ea92edd85183b2721126ec02ed61947ca6a7
18 changes: 13 additions & 5 deletions site/src/modules/templates/TemplateFiles/TemplateFileTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ type ContextMenu = {

interface TemplateFilesTreeProps {
onSelect: (path: string) => void;
onDelete: (path: string) => void;
onRename: (path: string) => void;
onDelete?: (path: string) => void;
onRename?: (path: string) => void;
fileTree: FileTree;
activePath?: string;
Label?: FC<{ path: string; filename: string; isFolder: boolean }>;
}

export const TemplateFileTree: FC<TemplateFilesTreeProps> = ({
Expand All @@ -42,6 +43,7 @@ export const TemplateFileTree: FC<TemplateFilesTreeProps> = ({
onDelete,
onRename,
onSelect,
Label,
}) => {
const [contextMenu, setContextMenu] = useState<ContextMenu | undefined>();
const buildTreeItems = (
Expand Down Expand Up @@ -69,7 +71,13 @@ export const TemplateFileTree: FC<TemplateFilesTreeProps> = ({
<TreeItem
nodeId={currentPath}
key={currentPath}
label={filename}
label={
Label ? (
<Label path={currentPath} filename={filename} isFolder={isFolder} />
) : (
filename
)
}
css={(theme) => css`
overflow: hidden;
user-select: none;
Expand Down Expand Up @@ -187,7 +195,7 @@ export const TemplateFileTree: FC<TemplateFilesTreeProps> = ({
if (!contextMenu) {
return;
}
onRename(contextMenu.path);
onRename && onRename(contextMenu.path);
setContextMenu(undefined);
}}
>
Expand All @@ -198,7 +206,7 @@ export const TemplateFileTree: FC<TemplateFilesTreeProps> = ({
if (!contextMenu) {
return;
}
onDelete(contextMenu.path);
onDelete && onDelete(contextMenu.path);
setContextMenu(undefined);
}}
>
Expand Down
157 changes: 107 additions & 50 deletions site/src/modules/templates/TemplateFiles/TemplateFiles.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { useTheme, type Interpolation, type Theme } from "@emotion/react";
import { type FC } from "react";
import { useMemo, type FC, useCallback } from "react";
import { SyntaxHighlighter } from "components/SyntaxHighlighter/SyntaxHighlighter";
import { TemplateVersionFiles } from "utils/templateVersion";
import RadioButtonCheckedOutlined from "@mui/icons-material/RadioButtonCheckedOutlined";
import { FileTree } from "utils/filetree";
import set from "lodash/fp/set";
import { TemplateFileTree } from "./TemplateFileTree";

const languageByExtension: Record<string, string> = {
tf: "hcl",
Expand All @@ -29,21 +32,34 @@ export const TemplateFiles: FC<TemplateFilesProps> = ({
}) => {
const filenames = Object.keys(currentFiles);
const theme = useTheme();
const filesWithDiff = filenames.filter(
(filename) => fileInfo(filename).hasDiff,

const fileInfo = useCallback(
(filename: string) => {
const value = currentFiles[filename].trim();
const previousValue = baseFiles ? baseFiles[filename].trim() : undefined;
const hasDiff = previousValue && value !== previousValue;

return {
value,
previousValue,
hasDiff,
};
},
[baseFiles, currentFiles],
);

function fileInfo(filename: string) {
const value = currentFiles[filename].trim();
const previousValue = baseFiles ? baseFiles[filename].trim() : undefined;
const hasDiff = previousValue && value !== previousValue;
const filesWithDiff = filenames.filter(
(filename) => fileInfo(filename).hasDiff && false,
);

return {
value,
previousValue,
hasDiff,
};
}
const fileTree: FileTree = useMemo(() => {
let tree: FileTree = {};
for (const filename of filenames) {
const info = fileInfo(filename);
tree = set(filename.split("/"), info.value, tree);
}
return tree;
}, [fileInfo, filenames]);

return (
<div>
Expand Down Expand Up @@ -99,45 +115,74 @@ export const TemplateFiles: FC<TemplateFilesProps> = ({
</ul>
</div>
)}
<div css={styles.files}>
{[...filenames]
.sort((a, b) => a.localeCompare(b))
.map((filename) => {
const info = fileInfo(filename);
<div css={{ display: "flex", alignItems: "flex-start", gap: 32 }}>
<div css={styles.sidebar}>
<TemplateFileTree
fileTree={fileTree}
onSelect={function (path: string): void {
window.location.hash = path;
}}
Label={({ path, filename, isFolder }) => {
if (isFolder) {
return <>{filename}</>;
}

return (
<div key={filename} css={styles.filePanel} id={filename}>
<header css={styles.fileHeader}>
{filename}
{info.hasDiff && (
<RadioButtonCheckedOutlined
css={{
width: 14,
height: 14,
color: theme.roles.warning.fill.outline,
}}
/>
)}
</header>
<SyntaxHighlighter
language={
languageByExtension[filename.split(".").pop() ?? ""]
}
value={info.value}
compareWith={info.previousValue}
editorProps={{
// 18 is the editor line height
height: Math.min(numberOfLines(info.value) * 18, 560),
onMount: (editor) => {
editor.updateOptions({
scrollBeyondLastLine: false,
});
},
const hasDiff = fileInfo(path).hasDiff;
return (
<span
css={{
color: hasDiff
? theme.roles.warning.fill.outline
: undefined,
}}
/>
</div>
);
})}
>
{filename}
</span>
);
}}
/>
</div>

<div css={styles.files}>
{[...filenames]
.sort((a, b) => a.localeCompare(b))
.map((filename) => {
const info = fileInfo(filename);

return (
<div key={filename} css={styles.filePanel} id={filename}>
<header css={styles.fileHeader}>
{filename}
{info.hasDiff && (
<RadioButtonCheckedOutlined
css={{
width: 14,
height: 14,
color: theme.roles.warning.fill.outline,
}}
/>
)}
</header>
<SyntaxHighlighter
language={
languageByExtension[filename.split(".").pop() ?? ""]
}
value={info.value}
compareWith={info.previousValue}
editorProps={{
// 18 is the editor line height
height: Math.min(numberOfLines(info.value) * 18, 560),
onMount: (editor) => {
editor.updateOptions({
scrollBeyondLastLine: false,
});
},
}}
/>
</div>
);
})}
</div>
</div>
</div>
);
Expand All @@ -148,10 +193,22 @@ const numberOfLines = (content: string) => {
};

const styles = {
sidebar: (theme) => ({
width: 240,
flexShrink: 0,
borderRadius: 8,
overflow: "auto",
border: `1px solid ${theme.palette.divider}`,
padding: "4px 0",
position: "sticky",
top: 32,
}),

files: {
display: "flex",
flexDirection: "column",
gap: 16,
flex: 1,
},

filePanel: (theme) => ({
Expand Down