-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathuseIntegrations.ts
More file actions
188 lines (169 loc) · 6.95 KB
/
Copy pathuseIntegrations.ts
File metadata and controls
188 lines (169 loc) · 6.95 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import {
buildTeamRepositoryOptions,
normalizeRepositoryNames,
repositoryLoadWarning,
repositoryOptionsEqual,
} from "@posthog/core/integrations/repositories";
import { useQuery } from "@tanstack/react-query";
import { useEffect, useMemo } from "react";
import { useAuthStore } from "@/features/auth";
import { getPostHogApiClient } from "@/lib/posthogApiClient";
import { useRepositoryCacheStore } from "../stores/repositoryCacheStore";
export const integrationKeys = {
all: ["integrations"] as const,
lists: () => [...integrationKeys.all, "list"] as const,
github: () => [...integrationKeys.all, "github"] as const,
repos: (integrationId: number) =>
[...integrationKeys.all, "repos", integrationId] as const,
};
interface RepositoryLoadResult {
repositoriesByIntegration: Record<number, string[]>;
partialError: string | null;
}
interface UseIntegrationsOptions {
enabled?: boolean;
}
export function useIntegrations(options: UseIntegrationsOptions = {}) {
const { enabled = true } = options;
const { projectId, oauthAccessToken } = useAuthStore();
// Persisted snapshot from the last successful fetch. Survives app launches
// so the picker can render instantly while we refetch in the background.
const cachedOptions = useRepositoryCacheStore((s) => s.options);
const setCachedOptions = useRepositoryCacheStore((s) => s.setOptions);
const integrationsQuery = useQuery({
queryKey: integrationKeys.github(),
queryFn: async () => {
const data = await getPostHogApiClient().getIntegrations();
return data.filter((i) => i.kind === "github");
},
enabled: enabled && !!projectId && !!oauthAccessToken,
});
const githubIntegrations = enabled ? (integrationsQuery.data ?? []) : [];
const repositoriesQuery = useQuery({
queryKey: [
...integrationKeys.all,
"repos",
githubIntegrations.map((i) => i.id),
],
queryFn: async (): Promise<RepositoryLoadResult> => {
const repositoriesByIntegration: Record<number, string[]> = {};
const results = await Promise.allSettled(
githubIntegrations.map(async (integration) => ({
integrationId: integration.id,
repositories: normalizeRepositoryNames(
await getPostHogApiClient().getGithubRepositories(integration.id),
),
})),
);
let failedCount = 0;
for (const result of results) {
if (result.status === "fulfilled") {
repositoriesByIntegration[result.value.integrationId] =
result.value.repositories;
continue;
}
failedCount += 1;
}
return {
repositoriesByIntegration,
partialError: repositoryLoadWarning(
failedCount,
githubIntegrations.length,
),
};
},
enabled: enabled && githubIntegrations.length > 0,
});
const repositoriesByIntegration =
repositoriesQuery.data?.repositoriesByIntegration ?? {};
const repositories = Object.values(repositoriesByIntegration).flat().sort();
// Memoize the derived options list keyed on the underlying query data so
// its reference is stable across renders when the data hasn't actually
// changed. Without this, every render produces a fresh array — which both
// churns the cache-write effect below AND defeats downstream React.memo /
// useMemo callers that depend on `repositoryOptions`.
const liveRepositoryOptions = useMemo(
() =>
buildTeamRepositoryOptions(githubIntegrations, repositoriesByIntegration),
[githubIntegrations, repositoriesByIntegration],
);
// Mirror the latest successful fetch into the persisted cache so the next
// cold start can render the picker instantly. We sync whatever the live
// result is — including an empty array — but only once both queries have
// succeeded, so a transient fetch failure or in-flight refresh can't wipe
// out a working snapshot. Compare contents before writing so we don't
// bump `updatedAt` (and re-render every cache subscriber) on no-op syncs.
const integrationsSettled =
integrationsQuery.isFetched && !integrationsQuery.isError;
// biome-ignore lint/correctness/useExhaustiveDependencies: setCachedOptions is a stable Zustand action
useEffect(() => {
if (!enabled) return;
if (!integrationsSettled) return;
if (githubIntegrations.length === 0) {
// No integrations — clear the cache so the picker doesn't surface
// stale repos for a connection the user has since removed.
if (cachedOptions.length > 0) setCachedOptions([]);
return;
}
if (!repositoriesQuery.isSuccess) return;
// Skip the write when the cache already matches — otherwise every
// render that produces a structurally-equal options list (e.g. after
// an unrelated re-render of the consumer) would push a new `updatedAt`,
// re-trigger every cache subscriber, and the consumer's `useEffect`
// would fire again on the new reference. Infinite loop.
if (repositoryOptionsEqual(liveRepositoryOptions, cachedOptions)) return;
setCachedOptions(liveRepositoryOptions);
}, [
enabled,
integrationsSettled,
githubIntegrations.length,
repositoriesQuery.isSuccess,
liveRepositoryOptions,
cachedOptions,
]);
// Prefer live data once it's in; fall back to the persisted snapshot so
// consumers always have *something* to render on cold start.
const repositoryOptions =
liveRepositoryOptions.length > 0 ? liveRepositoryOptions : cachedOptions;
const repositoryWarning = repositoriesQuery.data?.partialError ?? null;
const hasCachedRepositories = cachedOptions.length > 0;
const refetch = async () => {
if (!enabled) {
return;
}
await integrationsQuery.refetch();
await repositoriesQuery.refetch();
};
return {
hasGithubIntegration: !enabled
? null
: integrationsQuery.isFetched
? githubIntegrations.length > 0
: // If we have cached repos we know there's at least one integration,
// so don't gate the screen on the integrations query.
hasCachedRepositories
? true
: null,
githubIntegrations,
repositories,
repositoriesByIntegration,
repositoryOptions,
/** True iff we have cached options but the live fetch is still running.
* Lets the UI render the cached list while showing a subtle background
* refresh indicator instead of a blocking spinner. */
isRefreshingInBackground:
enabled &&
hasCachedRepositories &&
(integrationsQuery.isLoading || repositoriesQuery.isLoading),
/** Only true when we have nothing to show yet — no cache, no live data.
* Consumers should treat this as "block the screen on a spinner";
* background refreshes don't count. */
isLoading: enabled
? !hasCachedRepositories &&
(integrationsQuery.isLoading || repositoriesQuery.isLoading)
: false,
error: enabled ? (integrationsQuery.error?.message ?? null) : null,
repositoryWarning: enabled ? repositoryWarning : null,
refetch,
};
}