-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseFlagsmithProject.ts
More file actions
86 lines (73 loc) · 2.72 KB
/
useFlagsmithProject.ts
File metadata and controls
86 lines (73 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { useState, useEffect, useMemo } from 'react';
import { useApi, discoveryApiRef, fetchApiRef } from '@backstage/core-plugin-api';
import {
FlagsmithClient,
FlagsmithProject,
FlagsmithEnvironment,
FlagsmithFeature,
FlagsmithTag,
} from '../api/FlagsmithClient';
export interface UseFlagsmithProjectResult {
project: FlagsmithProject | null;
environments: FlagsmithEnvironment[];
features: FlagsmithFeature[];
tagMap: Map<number, FlagsmithTag>;
loading: boolean;
error: string | null;
client: FlagsmithClient;
}
export function useFlagsmithProject(
projectId: string | undefined,
): UseFlagsmithProjectResult {
const discoveryApi = useApi(discoveryApiRef);
const fetchApi = useApi(fetchApiRef);
const client = useMemo(
() => new FlagsmithClient(discoveryApi, fetchApi),
[discoveryApi, fetchApi],
);
const [project, setProject] = useState<FlagsmithProject | null>(null);
const [environments, setEnvironments] = useState<FlagsmithEnvironment[]>([]);
const [features, setFeatures] = useState<FlagsmithFeature[]>([]);
const [tags, setTags] = useState<FlagsmithTag[]>([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
if (!projectId) {
setError('No Flagsmith project ID found in entity annotations');
setLoading(false);
return;
}
const fetchData = async () => {
try {
const projectData = await client.getProject(parseInt(projectId, 10));
setProject(projectData);
const envs = await client.getProjectEnvironments(parseInt(projectId, 10));
setEnvironments(envs || []);
const projectFeatures = await client.getProjectFeatures(projectId);
setFeatures(projectFeatures || []);
const projectTags = await client.getProjectTags(parseInt(projectId, 10));
setTags(projectTags || []);
} catch (err) {
setError(err instanceof Error ? err.message : 'Unknown error');
} finally {
setLoading(false);
}
};
fetchData();
}, [projectId, client]);
// Memoize environments to prevent unnecessary re-renders in child components
// Only create new reference when environment IDs actually change
const envIds = environments.map(e => e.id).join(',');
const memoizedEnvironments = useMemo(
() => environments,
// eslint-disable-next-line react-hooks/exhaustive-deps
[envIds],
);
// Create a map of tag ID to tag object for efficient lookup
const tagMap = useMemo(() => {
const map = new Map<number, FlagsmithTag>();
tags.forEach(tag => map.set(tag.id, tag));
return map;
}, [tags]);
return { project, environments: memoizedEnvironments, features, tagMap, loading, error, client };
}