Skip to content

Commit d8762c6

Browse files
feat(site): Show main.tf file first on template files page (#6854)
1 parent b120247 commit d8762c6

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

site/src/hooks/useTab.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export const useTab = (tabKey: string, defaultValue: string): UseTabResult => {
1313
value,
1414
set: (value: string) => {
1515
searchParams.set(tabKey, value)
16-
setSearchParams(searchParams)
16+
setSearchParams(searchParams, { replace: true })
1717
},
1818
}
1919
}

site/src/pages/TemplatePage/TemplateFilesPage/TemplateFilesPage.tsx

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ import { TemplateFiles } from "components/TemplateFiles/TemplateFiles"
66
import { useTemplateLayoutContext } from "components/TemplateLayout/TemplateLayout"
77
import { useOrganizationId } from "hooks/useOrganizationId"
88
import { useTab } from "hooks/useTab"
9-
import { FC } from "react"
9+
import { FC, useEffect } from "react"
1010
import { Helmet } from "react-helmet-async"
1111
import { pageTitle } from "util/page"
12-
import { getTemplateVersionFiles } from "util/templateVersion"
12+
import {
13+
getTemplateVersionFiles,
14+
TemplateVersionFiles,
15+
} from "util/templateVersion"
1316

1417
const fetchTemplateFiles = async (
1518
organizationId: string,
@@ -44,23 +47,43 @@ const useTemplateFiles = (
4447
fetchTemplateFiles(organizationId, templateName, activeVersion),
4548
})
4649

50+
const useFileTab = (templateFiles: TemplateVersionFiles | undefined) => {
51+
// Tabs The default tab is the tab that has main.tf but until we loads the
52+
// files and check if main.tf exists we don't know which tab is the default
53+
// one so we just use empty string
54+
const tab = useTab("file", "")
55+
const isLoaded = tab.value !== ""
56+
useEffect(() => {
57+
if (templateFiles && !isLoaded) {
58+
const terraformFileIndex = Object.keys(templateFiles).indexOf("main.tf")
59+
// If main.tf exists use the index if not just use the first tab
60+
tab.set(terraformFileIndex !== -1 ? terraformFileIndex.toString() : "0")
61+
}
62+
}, [isLoaded, tab, templateFiles])
63+
64+
return {
65+
...tab,
66+
isLoaded,
67+
}
68+
}
69+
4770
const TemplateFilesPage: FC = () => {
4871
const { template, activeVersion } = useTemplateLayoutContext()
4972
const orgId = useOrganizationId()
50-
const tab = useTab("file", "0")
5173
const { data: templateFiles } = useTemplateFiles(
5274
orgId,
5375
template.name,
5476
activeVersion,
5577
)
78+
const tab = useFileTab(templateFiles?.currentFiles)
5679

5780
return (
5881
<>
5982
<Helmet>
6083
<title>{pageTitle(`${template?.name} · Source Code`)}</title>
6184
</Helmet>
6285

63-
{templateFiles ? (
86+
{templateFiles && tab.isLoaded ? (
6487
<TemplateFiles
6588
currentFiles={templateFiles.currentFiles}
6689
previousFiles={templateFiles.previousFiles}

0 commit comments

Comments
 (0)
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