@@ -6,10 +6,13 @@ import { TemplateFiles } from "components/TemplateFiles/TemplateFiles"
6
6
import { useTemplateLayoutContext } from "components/TemplateLayout/TemplateLayout"
7
7
import { useOrganizationId } from "hooks/useOrganizationId"
8
8
import { useTab } from "hooks/useTab"
9
- import { FC } from "react"
9
+ import { FC , useEffect } from "react"
10
10
import { Helmet } from "react-helmet-async"
11
11
import { pageTitle } from "util/page"
12
- import { getTemplateVersionFiles } from "util/templateVersion"
12
+ import {
13
+ getTemplateVersionFiles ,
14
+ TemplateVersionFiles ,
15
+ } from "util/templateVersion"
13
16
14
17
const fetchTemplateFiles = async (
15
18
organizationId : string ,
@@ -44,23 +47,43 @@ const useTemplateFiles = (
44
47
fetchTemplateFiles ( organizationId , templateName , activeVersion ) ,
45
48
} )
46
49
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
+
47
70
const TemplateFilesPage : FC = ( ) => {
48
71
const { template, activeVersion } = useTemplateLayoutContext ( )
49
72
const orgId = useOrganizationId ( )
50
- const tab = useTab ( "file" , "0" )
51
73
const { data : templateFiles } = useTemplateFiles (
52
74
orgId ,
53
75
template . name ,
54
76
activeVersion ,
55
77
)
78
+ const tab = useFileTab ( templateFiles ?. currentFiles )
56
79
57
80
return (
58
81
< >
59
82
< Helmet >
60
83
< title > { pageTitle ( `${ template ?. name } · Source Code` ) } </ title >
61
84
</ Helmet >
62
85
63
- { templateFiles ? (
86
+ { templateFiles && tab . isLoaded ? (
64
87
< TemplateFiles
65
88
currentFiles = { templateFiles . currentFiles }
66
89
previousFiles = { templateFiles . previousFiles }
0 commit comments