Skip to content

feat(site): Show main.tf file first on template files page #6854

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 2 commits into from
Mar 30, 2023
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
2 changes: 1 addition & 1 deletion site/src/hooks/useTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const useTab = (tabKey: string, defaultValue: string): UseTabResult => {
value,
set: (value: string) => {
searchParams.set(tabKey, value)
setSearchParams(searchParams)
setSearchParams(searchParams, { replace: true })
},
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import { TemplateFiles } from "components/TemplateFiles/TemplateFiles"
import { useTemplateLayoutContext } from "components/TemplateLayout/TemplateLayout"
import { useOrganizationId } from "hooks/useOrganizationId"
import { useTab } from "hooks/useTab"
import { FC } from "react"
import { FC, useEffect } from "react"
import { Helmet } from "react-helmet-async"
import { pageTitle } from "util/page"
import { getTemplateVersionFiles } from "util/templateVersion"
import {
getTemplateVersionFiles,
TemplateVersionFiles,
} from "util/templateVersion"

const fetchTemplateFiles = async (
organizationId: string,
Expand Down Expand Up @@ -44,23 +47,43 @@ const useTemplateFiles = (
fetchTemplateFiles(organizationId, templateName, activeVersion),
})

const useFileTab = (templateFiles: TemplateVersionFiles | undefined) => {
// Tabs The default tab is the tab that has main.tf but until we loads the
// files and check if main.tf exists we don't know which tab is the default
// one so we just use empty string
const tab = useTab("file", "")
const isLoaded = tab.value !== ""
Comment on lines +54 to +55
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we can consolidate useTab and useFileTab into one hook. Does it make sense to have useTab return its own isLoading variable? Something like:

export const useTab = (tabKey = "file", defaultValue = ""): UseTabResult => {
    return {
        isLoading: value !== "",
        ....
    }
}

// in TemplateFilesPage component

const tab = useTab()

useEffect(() => {
    if (templateFiles && !tab.isLoading) {
      const terraformFileIndex = Object.keys(templateFiles).indexOf("main.tf")
      tab.set(terraformFileIndex !== -1 ? terraformFileIndex.toString() : "0")
    }
}, [tab, templateFiles])

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since useTab is getting used from other components I would not touch that. The isLoading, for now, is a specific thing from this page so that is the reason I created the useFileTab to handle this specific logic. If at some point we see this logic is something inherited for most of the tabs we have, I would consolidate them but not now tho.

useEffect(() => {
if (templateFiles && !isLoaded) {
const terraformFileIndex = Object.keys(templateFiles).indexOf("main.tf")
// If main.tf exists use the index if not just use the first tab
tab.set(terraformFileIndex !== -1 ? terraformFileIndex.toString() : "0")
}
}, [isLoaded, tab, templateFiles])

return {
...tab,
isLoaded,
}
}

const TemplateFilesPage: FC = () => {
const { template, activeVersion } = useTemplateLayoutContext()
const orgId = useOrganizationId()
const tab = useTab("file", "0")
const { data: templateFiles } = useTemplateFiles(
orgId,
template.name,
activeVersion,
)
const tab = useFileTab(templateFiles?.currentFiles)

return (
<>
<Helmet>
<title>{pageTitle(`${template?.name} · Source Code`)}</title>
</Helmet>

{templateFiles ? (
{templateFiles && tab.isLoaded ? (
<TemplateFiles
currentFiles={templateFiles.currentFiles}
previousFiles={templateFiles.previousFiles}
Expand Down