|
1 |
| -import { useQuery } from "react-query"; |
2 |
| -import { getPreviousTemplateVersionByName } from "api/api"; |
3 |
| -import { TemplateVersion } from "api/typesGenerated"; |
4 | 1 | import { Loader } from "components/Loader/Loader";
|
5 | 2 | import { TemplateFiles } from "components/TemplateFiles/TemplateFiles";
|
6 | 3 | import { useTemplateLayoutContext } from "pages/TemplatePage/TemplateLayout";
|
7 |
| -import { useOrganizationId } from "hooks/useOrganizationId"; |
8 |
| -import { useTab } from "hooks/useTab"; |
9 |
| -import { FC, useEffect } from "react"; |
| 4 | +import { FC } from "react"; |
10 | 5 | import { Helmet } from "react-helmet-async";
|
11 |
| -import { |
12 |
| - getTemplateVersionFiles, |
13 |
| - TemplateVersionFiles, |
14 |
| -} from "utils/templateVersion"; |
15 | 6 | import { getTemplatePageTitle } from "../utils";
|
16 |
| - |
17 |
| -const fetchTemplateFiles = async ( |
18 |
| - organizationId: string, |
19 |
| - templateName: string, |
20 |
| - activeVersion: TemplateVersion, |
21 |
| -) => { |
22 |
| - const previousVersion = await getPreviousTemplateVersionByName( |
23 |
| - organizationId, |
24 |
| - templateName, |
25 |
| - activeVersion.name, |
26 |
| - ); |
27 |
| - const loadFilesPromises: ReturnType<typeof getTemplateVersionFiles>[] = []; |
28 |
| - loadFilesPromises.push(getTemplateVersionFiles(activeVersion)); |
29 |
| - if (previousVersion) { |
30 |
| - loadFilesPromises.push(getTemplateVersionFiles(previousVersion)); |
31 |
| - } |
32 |
| - const [currentFiles, previousFiles] = await Promise.all(loadFilesPromises); |
33 |
| - return { |
34 |
| - currentFiles, |
35 |
| - previousFiles, |
36 |
| - }; |
37 |
| -}; |
38 |
| - |
39 |
| -const useTemplateFiles = ( |
40 |
| - organizationId: string, |
41 |
| - templateName: string, |
42 |
| - activeVersion: TemplateVersion, |
43 |
| -) => |
44 |
| - useQuery({ |
45 |
| - queryKey: ["templateFiles", templateName], |
46 |
| - queryFn: () => |
47 |
| - fetchTemplateFiles(organizationId, templateName, activeVersion), |
48 |
| - }); |
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 |
| -}; |
| 7 | +import { useFileTab, useTemplateFiles } from "components/TemplateFiles/hooks"; |
69 | 8 |
|
70 | 9 | const TemplateFilesPage: FC = () => {
|
71 | 10 | const { template, activeVersion } = useTemplateLayoutContext();
|
72 |
| - const orgId = useOrganizationId(); |
73 | 11 | const { data: templateFiles } = useTemplateFiles(
|
74 |
| - orgId, |
75 | 12 | template.name,
|
76 | 13 | activeVersion,
|
77 | 14 | );
|
|
0 commit comments