-
Notifications
You must be signed in to change notification settings - Fork 99
refactor(web): extract useDraftOverride + useStoredCredentialPresence shared hooks (LUM-2222, LUM-2223) #33246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
vex-assistant-bot
merged 3 commits into
main
from
devin/1780497732-lum-2222-2223-shared-hooks
Jun 3, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
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
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,30 @@ | ||
| import { useCallback, useEffect, useState } from "react"; | ||
|
|
||
| /** | ||
| * Manages a local draft that overrides a server-derived value. | ||
| * | ||
| * Returns `[effectiveValue, setDraft]` where `effectiveValue` is the | ||
| * draft when set, otherwise the server value. The draft auto-clears | ||
| * when the server value converges (e.g. after a save + cache refetch), | ||
| * preventing the UI from briefly reverting to stale server state during | ||
| * the refetch window. | ||
| * | ||
| * Pass `undefined` to clear the draft (revert to server value). | ||
| * Any `T` value — including `null` — is stored as a valid draft. | ||
| */ | ||
| export function useDraftOverride<T>(serverValue: T): [T, (draft: T | undefined) => void] { | ||
| const [draft, setDraft] = useState<{ value: T } | undefined>(undefined); | ||
|
|
||
| useEffect(() => { | ||
| if (draft !== undefined && serverValue === draft.value) { | ||
| setDraft(undefined); | ||
| } | ||
| }, [serverValue, draft]); | ||
|
|
||
| const effective = draft !== undefined ? draft.value : serverValue; | ||
| const updateDraft = useCallback( | ||
| (d: T | undefined) => setDraft(d === undefined ? undefined : { value: d }), | ||
| [], | ||
| ); | ||
| return [effective, updateDraft]; | ||
| } |
90 changes: 90 additions & 0 deletions
90
apps/web/src/domains/settings/ai/use-stored-credential-presence.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,90 @@ | ||
| import { useEffect, useMemo } from "react"; | ||
|
|
||
| import { useQuery } from "@tanstack/react-query"; | ||
|
|
||
| import { secretsReadPost } from "@/generated/daemon/sdk.gen"; | ||
| import { ApiError, assertHasResponse, extractErrorMessage } from "@/utils/api-errors"; | ||
| import { shouldRetryDaemonError } from "@/utils/daemon-errors"; | ||
| import { captureError } from "@/lib/sentry/capture-error"; | ||
| import { useIsOrgReady } from "@/hooks/use-is-org-ready"; | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Query key | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| const STORED_CREDENTIAL_PRESENCE_QK = "stored-credential-presence" as const; | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Hook | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| interface UseStoredCredentialPresenceOptions { | ||
| assistantId: string | undefined; | ||
| /** Credential kind sent to the daemon (e.g. "api_key", "credential"). */ | ||
| credentialKind: string; | ||
| /** Credential identifier sent to the daemon (e.g. "tavily", "anthropic:api_key"). */ | ||
| credentialName: string; | ||
| /** Extra guard — the query only fires when all conditions are true. */ | ||
| enabled?: boolean; | ||
| /** Sentry context tag for error reporting. */ | ||
| errorContext: string; | ||
| } | ||
|
|
||
| /** | ||
| * Checks whether a stored credential exists on the daemon. | ||
| * | ||
| * Wraps `secretsReadPost` in a TanStack Query hook with org-readiness | ||
| * gating, retry logic for transient daemon errors, and Sentry reporting | ||
| * for persistent failures. | ||
| */ | ||
| export function useStoredCredentialPresence({ | ||
| assistantId, | ||
| credentialKind, | ||
| credentialName, | ||
| enabled = true, | ||
| errorContext, | ||
| }: UseStoredCredentialPresenceOptions) { | ||
| const isOrgReady = useIsOrgReady(); | ||
|
|
||
| const queryKey = useMemo( | ||
| () => [STORED_CREDENTIAL_PRESENCE_QK, assistantId ?? "", credentialKind, credentialName] as const, | ||
| [assistantId, credentialKind, credentialName], | ||
| ); | ||
|
|
||
| const query = useQuery({ | ||
| queryKey, | ||
| queryFn: async () => { | ||
| const { data, error, response } = await secretsReadPost({ | ||
| path: { assistant_id: assistantId! }, | ||
| body: { type: credentialKind, name: credentialName }, | ||
| throwOnError: false, | ||
| }); | ||
| assertHasResponse(response, error, "Failed to check stored credential"); | ||
| if (!response.ok) { | ||
| throw new ApiError( | ||
| response.status, | ||
| extractErrorMessage( | ||
| error, | ||
| response, | ||
| `Failed to check stored credential (HTTP ${response.status})`, | ||
| ), | ||
| ); | ||
| } | ||
| return data!.found; | ||
| }, | ||
| enabled: !!assistantId && enabled && isOrgReady, | ||
| retry: shouldRetryDaemonError, | ||
| staleTime: 30_000, | ||
| }); | ||
|
|
||
| useEffect(() => { | ||
| if (!query.error) return; | ||
| captureError(query.error, { context: errorContext, bestEffort: true }); | ||
| }, [query.error, errorContext]); | ||
|
|
||
| return { | ||
| hasStoredCredential: query.data ?? false, | ||
| isLoading: query.isLoading, | ||
| queryKey, | ||
| }; | ||
| } |
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.
When the saved
activeProfileis non-null, this hook treatsnullas “no draft”, but the dropdown also usesnullto mean “clear the default profile” (val === "" ? null : val). As a result, selecting the blank/default option immediately falls back to the saved profile,isProfileDirtystays false, and the user cannot saveactiveProfile: null; the previousdraftInitializedflag kept those two states distinct.Useful? React with 👍 / 👎.
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.
False positive — the old code had the same behavior for
null.Old code path (when
val === ""triggerssetDraftActiveProfile(null)+setDraftInitialized(true)):New code path (same trigger):
Both fall back to
activeProfileidentically. ThedraftInitializedflag only controlled the initial render path (!draftInitialized && activeProfile !== null), not the null-draft case — once initialized,draftActiveProfile ?? activeProfilealready collapsesnullintoactiveProfile, exactly likeuseDraftOverridedoes.Also, the dropdown options are all non-empty profile name strings —
val === ""can't be triggered from the options list. Theval === "" ? null : valguard is defensive.Resolved — no behavioral regression.