Skip to content

Commit f88e0ba

Browse files
authored
refactor: ♻️ use backend-provided resource specs instead of environment variables (#804)
- Remove QUERY_ALL_RESOURCE_SPECS workaround query that was finding specs by name - Simplify useResourceSpecs hook to only query QUERY_PROJECT_TYPES - Update ProductsActiveFiltersBar and ProductsFilters to use specMachine from hook - Remove RESOURCE_SPEC_MACHINE and RESOURCE_SPEC_MATERIAL env variable fallbacks - Add skip condition to queries when spec IDs are not yet available This completes the migration from env-based spec IDs to backend-provided instanceVariables.specs which now exposes specMachine and specMaterial. Closes interfacer-gui-9lv.17 (already closed) Related to interfacer-gui-3hv (workaround removal)
1 parent ea4cacb commit f88e0ba

File tree

4 files changed

+25
-59
lines changed

4 files changed

+25
-59
lines changed

components/ProductsActiveFiltersBar.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
import { useQuery } from "@apollo/client";
1818
import { Tag } from "@bbtgnn/polaris-interfacer";
19+
import { useResourceSpecs } from "hooks/useResourceSpecs";
1920
import { QUERY_MACHINES } from "lib/QueryAndMutation";
20-
import { RESOURCE_SPEC_MACHINE } from "lib/resourceSpecs";
2121
import { isPrefixedTag, prefixedTag, TAG_PREFIX } from "lib/tagging";
2222
import { useTranslation } from "next-i18next";
2323
import { useRouter } from "next/router";
@@ -73,8 +73,12 @@ export default function ProductsActiveFiltersBar() {
7373
const { t } = useTranslation("productsProps");
7474
const router = useRouter();
7575

76+
// Get spec IDs from backend via hook
77+
const { specMachine } = useResourceSpecs();
78+
7679
const { data: machinesData } = useQuery<MachinesQueryData>(QUERY_MACHINES, {
77-
variables: { resourceSpecId: RESOURCE_SPEC_MACHINE },
80+
variables: { resourceSpecId: specMachine?.id || "" },
81+
skip: !specMachine?.id,
7882
});
7983

8084
const availableMachines = machinesData?.economicResources?.edges?.map(e => e.node) || [];

components/ProductsFilters.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616

1717
import { useQuery } from "@apollo/client";
18+
import { useResourceSpecs } from "hooks/useResourceSpecs";
1819
import { QUERY_MACHINES } from "lib/QueryAndMutation";
19-
import { MACHINE_TYPES, RESOURCE_SPEC_MACHINE } from "lib/resourceSpecs";
20+
import { MACHINE_TYPES } from "lib/resourceSpecs";
2021
import {
2122
CO2_THRESHOLDS_KG,
2223
ENERGY_THRESHOLDS_KWH,
@@ -97,11 +98,15 @@ export default function ProductsFilters() {
9798
const { t } = useTranslation("productsProps");
9899
const router = useRouter();
99100

101+
// Get spec IDs from backend via hook
102+
const { specMachine } = useResourceSpecs();
103+
100104
// Query available machines from backend
101105
const { data: machinesData, loading: machinesLoading } = useQuery<MachinesQueryData>(QUERY_MACHINES, {
102106
variables: {
103-
resourceSpecId: RESOURCE_SPEC_MACHINE,
107+
resourceSpecId: specMachine?.id || "",
104108
},
109+
skip: !specMachine?.id,
105110
});
106111

107112
// Get machines list from query or use fallback

hooks/useResourceSpecs.ts

Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -15,57 +15,27 @@
1515
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616

1717
import { useQuery } from "@apollo/client";
18-
import { QUERY_ALL_RESOURCE_SPECS, QUERY_PROJECT_TYPES } from "lib/QueryAndMutation";
18+
import { QUERY_PROJECT_TYPES } from "lib/QueryAndMutation";
1919
import { GetProjectTypesQuery } from "lib/types";
2020

21-
interface ResourceSpec {
22-
id: string;
23-
name: string;
24-
}
25-
26-
interface AllResourceSpecsQuery {
27-
resourceSpecifications: {
28-
edges: Array<{
29-
node: ResourceSpec;
30-
}>;
31-
};
32-
}
33-
3421
/**
3522
* Hook to get all ResourceSpecification IDs including DPP, Machine, Material
3623
*
37-
* WORKAROUND: The backend seeds specDpp, specMachine, specMaterial but doesn't
38-
* expose them via instanceVariables yet. This hook queries resourceSpecifications
39-
* directly and finds specs by name as a fallback.
40-
*
41-
* Once backend exposes these via instanceVariables, this hook can be simplified.
24+
* Gets specs from backend instanceVariables which now exposes all required specs:
25+
* specProjectDesign, specProjectProduct, specProjectService, specDpp, specMachine, specMaterial
4226
*/
4327
export const useResourceSpecs = () => {
44-
// Try to get specs from instanceVariables first
45-
const { data: instanceData, loading: instanceLoading } = useQuery<GetProjectTypesQuery>(QUERY_PROJECT_TYPES);
46-
47-
// Fallback: query all resourceSpecifications and find by name
48-
const { data: allSpecsData, loading: allSpecsLoading } = useQuery<AllResourceSpecsQuery>(QUERY_ALL_RESOURCE_SPECS);
49-
50-
const instanceSpecs = instanceData?.instanceVariables?.specs;
51-
const allSpecs = allSpecsData?.resourceSpecifications?.edges?.map(e => e.node) || [];
52-
53-
// Helper to find spec by name from allSpecs
54-
const findSpecByName = (name: string): ResourceSpec | undefined => {
55-
return allSpecs.find(s => s.name.toLowerCase() === name.toLowerCase());
56-
};
57-
58-
// Project types - try instanceVariables first, fallback to query by name
59-
const specProjectDesign = instanceSpecs?.specProjectDesign || findSpecByName("Design");
60-
const specProjectProduct = instanceSpecs?.specProjectProduct || findSpecByName("Product");
61-
const specProjectService = instanceSpecs?.specProjectService || findSpecByName("Service");
28+
const { data: instanceData, loading } = useQuery<GetProjectTypesQuery>(QUERY_PROJECT_TYPES);
6229

63-
// DPP, Machine, Material - try instanceVariables first, fallback to query by name
64-
const specDpp = instanceSpecs?.specDpp || findSpecByName("DPP");
65-
const specMachine = instanceSpecs?.specMachine || findSpecByName("Machine");
66-
const specMaterial = instanceSpecs?.specMaterial || findSpecByName("Material");
30+
const specs = instanceData?.instanceVariables?.specs;
6731

68-
const loading = instanceLoading || allSpecsLoading;
32+
// Extract individual specs
33+
const specProjectDesign = specs?.specProjectDesign;
34+
const specProjectProduct = specs?.specProjectProduct;
35+
const specProjectService = specs?.specProjectService;
36+
const specDpp = specs?.specDpp;
37+
const specMachine = specs?.specMachine;
38+
const specMaterial = specs?.specMaterial;
6939

7040
// Check if we have all required specs
7141
const hasProjectSpecs = !!(specProjectDesign && specProjectProduct && specProjectService);

lib/QueryAndMutation.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,6 @@ import { gql } from "@apollo/client";
2020
* Query to fetch all resource specifications
2121
* Used as fallback when instanceVariables doesn't expose specDpp, specMachine, specMaterial
2222
*/
23-
export const QUERY_ALL_RESOURCE_SPECS = gql`
24-
query GetAllResourceSpecs {
25-
resourceSpecifications(first: 50) {
26-
edges {
27-
node {
28-
id
29-
name
30-
}
31-
}
32-
}
33-
}
34-
`;
35-
3623
export const QUERY_VARIABLES = gql`
3724
query GetVariables {
3825
instanceVariables {

0 commit comments

Comments
 (0)