This repository was archived by the owner on Aug 6, 2026. It is now read-only.
Repository navigation
feat(code): unify user-scoped GitHub connect into a shared hook #1996
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
apps/code/src/renderer/features/integrations/hooks/useGithubUserConnect.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| import { useOptionalAuthenticatedClient } from "@features/auth/hooks/authClient"; | ||
| import { useAuthStateValue } from "@features/auth/hooks/authQueries"; | ||
| import { useGitHubIntegrationCallback } from "@features/integrations/hooks/useGitHubIntegrationCallback"; | ||
| import { trpcClient } from "@renderer/trpc/client"; | ||
| import { IS_DEV } from "@shared/constants/environment"; | ||
| import { useQueryClient } from "@tanstack/react-query"; | ||
| import { useCallback, useEffect, useRef, useState } from "react"; | ||
| import { toast } from "sonner"; | ||
|
|
||
| const POLL_INTERVAL_MS = 3_000; | ||
| const POLL_TIMEOUT_MS = 300_000; | ||
|
|
||
| export type GithubUserConnectState = "idle" | "connecting" | "timed-out"; | ||
|
|
||
| interface Options { | ||
| projectId: number | null; | ||
| } | ||
|
|
||
| interface Result { | ||
| state: GithubUserConnectState; | ||
| connect: () => Promise<void>; | ||
| reset: () => void; | ||
| } | ||
|
|
||
| async function openUrlInBrowser(url: string): Promise<void> { | ||
| try { | ||
| await trpcClient.os.openExternal.mutate({ url }); | ||
| } catch { | ||
| window.open(url, "_blank", "noopener,noreferrer"); | ||
| } | ||
| } | ||
|
|
||
| export function useGithubUserConnect({ projectId }: Options): Result { | ||
| const client = useOptionalAuthenticatedClient(); | ||
| const cloudRegion = useAuthStateValue((s) => s.cloudRegion); | ||
| const queryClient = useQueryClient(); | ||
| const [state, setState] = useState<GithubUserConnectState>("idle"); | ||
| const stateRef = useRef(state); | ||
| stateRef.current = state; | ||
| const pollTimerRef = useRef<ReturnType<typeof setInterval> | null>(null); | ||
| const pollTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null); | ||
|
|
||
| const stopPolling = useCallback(() => { | ||
| if (pollTimerRef.current) { | ||
| clearInterval(pollTimerRef.current); | ||
| pollTimerRef.current = null; | ||
| } | ||
| if (pollTimeoutRef.current) { | ||
| clearTimeout(pollTimeoutRef.current); | ||
| pollTimeoutRef.current = null; | ||
| } | ||
| }, []); | ||
|
|
||
| const invalidate = useCallback( | ||
| (pid: number | null) => { | ||
| if (pid !== null) { | ||
| void queryClient.invalidateQueries({ | ||
| queryKey: ["integrations", pid], | ||
| }); | ||
| } | ||
| void queryClient.invalidateQueries({ | ||
| queryKey: ["integrations", "list"], | ||
| }); | ||
| void queryClient.invalidateQueries({ | ||
| queryKey: ["user-github-integrations"], | ||
| }); | ||
| void queryClient.invalidateQueries({ queryKey: ["github_login"] }); | ||
| }, | ||
| [queryClient], | ||
| ); | ||
|
|
||
| useEffect(() => stopPolling, [stopPolling]); | ||
|
|
||
| // Window-focus fallback: deep link from PostHog Cloud may not fire reliably, | ||
| // so refetch when the user returns to the app while a connect is in flight. | ||
| useEffect(() => { | ||
| if (state !== "connecting") return; | ||
| const onFocus = () => invalidate(projectId); | ||
| window.addEventListener("focus", onFocus); | ||
| return () => window.removeEventListener("focus", onFocus); | ||
| }, [state, projectId, invalidate]); | ||
|
|
||
| useGitHubIntegrationCallback({ | ||
| onSuccess: (callbackProjectId) => { | ||
| stopPolling(); | ||
| setState("idle"); | ||
| invalidate(callbackProjectId ?? projectId); | ||
| }, | ||
| onError: (message) => { | ||
| stopPolling(); | ||
| setState("idle"); | ||
| toast.error(message); | ||
| }, | ||
| onTimedOut: () => { | ||
| stopPolling(); | ||
| setState("timed-out"); | ||
| invalidate(projectId); | ||
| }, | ||
| }); | ||
|
|
||
| const connect = useCallback(async () => { | ||
| if (stateRef.current !== "idle") return; | ||
| if (!cloudRegion || projectId === null || !client) return; | ||
| stopPolling(); | ||
| setState("connecting"); | ||
| try { | ||
| const res = await client.startGithubUserIntegrationConnect(projectId); | ||
| const installUrl = res.install_url?.trim() ?? ""; | ||
| if (!installUrl) { | ||
| throw new Error("GitHub connection did not return a URL"); | ||
| } | ||
| await openUrlInBrowser(installUrl); | ||
|
|
||
| if (IS_DEV) { | ||
| pollTimerRef.current = setInterval( | ||
| () => invalidate(projectId), | ||
| POLL_INTERVAL_MS, | ||
| ); | ||
| } | ||
|
|
||
| pollTimeoutRef.current = setTimeout(() => { | ||
| stopPolling(); | ||
| setState("timed-out"); | ||
| }, POLL_TIMEOUT_MS); | ||
| } catch (error) { | ||
| setState("idle"); | ||
| toast.error( | ||
| error instanceof Error | ||
| ? error.message | ||
| : "Failed to start GitHub connection", | ||
| ); | ||
| } | ||
| }, [client, cloudRegion, projectId, invalidate, stopPolling]); | ||
|
|
||
| const reset = useCallback(() => { | ||
| stopPolling(); | ||
| setState("idle"); | ||
| }, [stopPolling]); | ||
|
|
||
| return { state, connect, reset }; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
toast.errorfires unconditionally regardless of connection stateonErrorcallstoast.error(message)even whenstate === "idle". ForGitHubConnectionBanner— which is mounted in the persistent inbox UI — this means that if a stale pending error callback is consumed on mount viaconsumePendingCallback(e.g. left over from a previous connect attempt in another session), the user will see an unexpected error toast with no obvious cause. The original banner didn't useuseGitHubIntegrationCallbackat all, so this is a new behaviour for that consumer.Guard the toast (and optionally the full
onErrorbody) behind a state check:Prompt To Fix With AI