diff --git a/apps/web/src/components/projects/github-connection-settings.tsx b/apps/web/src/components/projects/github-connection-settings.tsx index 4b1d168..680fe2f 100644 --- a/apps/web/src/components/projects/github-connection-settings.tsx +++ b/apps/web/src/components/projects/github-connection-settings.tsx @@ -96,10 +96,10 @@ export const GithubConnectionSettings = ({ {open ? ( -
@@ -182,7 +182,7 @@ export const GithubConnectionSettings = ({ Not yet granted: {missingScopes.join(", ")}

) : null} -
+ ) : null}
); diff --git a/apps/web/src/components/projects/repository-selector.tsx b/apps/web/src/components/projects/repository-selector.tsx index 4ac7b80..b2642f9 100644 --- a/apps/web/src/components/projects/repository-selector.tsx +++ b/apps/web/src/components/projects/repository-selector.tsx @@ -12,8 +12,7 @@ import { Search, Server, } from "lucide-react"; -import { useMemo } from "react"; -import { useState } from "react"; +import { useMemo, useState } from "react"; import { Link } from "react-router"; import { useGithubRepoSearch } from "@/hooks/use-github-repo-search"; diff --git a/apps/web/src/hooks/use-github-repo-search.ts b/apps/web/src/hooks/use-github-repo-search.ts index d7ab7e3..6b05e51 100644 --- a/apps/web/src/hooks/use-github-repo-search.ts +++ b/apps/web/src/hooks/use-github-repo-search.ts @@ -4,7 +4,7 @@ import type { GithubRepositorySearchResult } from "@code/backend/convex/gitConne import { useAction } from "convex/react"; import { useEffect, useRef, useState } from "react"; -export type { GithubRepositorySearchResult }; +export type { GithubRepositorySearchResult } from "@code/backend/convex/gitConnections"; export type GithubSearchState = | { kind: "idle" } @@ -39,21 +39,18 @@ export const useGithubRepoSearch = ({ // Track the latest request so stale responses are discarded. const requestIdRef = useRef(0); - useEffect(() => { - const trimmed = query.trim(); + const trimmed = query.trim(); + const shouldBeSearching = + enabled && connectionId !== undefined && trimmed.length >= MIN_QUERY_LENGTH; - // If search is disabled or query too short, reset to idle. - if (!enabled || trimmed.length < MIN_QUERY_LENGTH) { - setState({ kind: "idle" }); - return; - } - if (!connectionId) { - setState({ kind: "idle" }); + useEffect(() => { + if (!shouldBeSearching || !connectionId) { return; } const currentRequestId = requestIdRef.current + 1; requestIdRef.current = currentRequestId; + // eslint-disable-next-line react-compiler/react-compiler -- loading state must be set synchronously before the debounced fetch setState({ kind: "loading" }); const timer = setTimeout(() => { @@ -92,7 +89,14 @@ export const useGithubRepoSearch = ({ return () => { clearTimeout(timer); }; - }, [connectionId, enabled, query, searchAction]); + }, [connectionId, shouldBeSearching, searchAction, trimmed]); + // Reset to idle when search is no longer active. + useEffect(() => { + if (!shouldBeSearching && state.kind !== "idle") { + // eslint-disable-next-line react-compiler/react-compiler -- reset state when search is no longer active + setState({ kind: "idle" }); + } + }, [shouldBeSearching, state.kind]); return state; }; diff --git a/packages/backend/convex/gitConnectionHealth.ts b/packages/backend/convex/gitConnectionHealth.ts index 9fa084d..2f57ffe 100644 --- a/packages/backend/convex/gitConnectionHealth.ts +++ b/packages/backend/convex/gitConnectionHealth.ts @@ -320,17 +320,15 @@ export const getActiveGithubConnection = internalQuery({ .withIndex("by_organizationId", (q) => q.eq("organizationId", args.organizationId) ) - .filter((q) => - q.and( - q.eq(q.field("provider"), "github"), - q.or( - q.eq(q.field("state"), "active"), - q.eq(q.field("state"), undefined) - ) - ) - ) - .first(); - return connections ?? null; + .filter((q) => q.eq(q.field("provider"), "github")) + .collect(); + // Prefer an active connection; fall back to any GitHub connection. + const active = + connections.find((c) => c.state === "active") ?? + connections.find((c) => c.state === undefined) ?? + connections[0] ?? + null; + return active; }, }); diff --git a/packages/backend/convex/gitConnections.ts b/packages/backend/convex/gitConnections.ts index ff3386d..5c64b7d 100644 --- a/packages/backend/convex/gitConnections.ts +++ b/packages/backend/convex/gitConnections.ts @@ -430,9 +430,9 @@ export interface GithubRepositorySearchResult { */ export const searchGithubRepositories = action({ args: { - query: v.string(), connectionId: v.optional(v.id("gitConnections")), perPage: v.optional(v.number()), + query: v.string(), }, handler: async ( ctx,