Files
Charis 597d1ce56e fix(studio): pass caller options to grant-gated query factories (#53088)
## Context

Grant-gated query factories returned `queryFn: skipToken` when the
caller had no grant. TanStack Query keeps one options object per cache
entry, overwritten by whichever observer rendered last. When that was a
grant-less observer, invalidation refetched with `skipToken` and failed
with `Missing queryFn`, breaking the query for every other observer on
the key. #53075 patched two call sites; this fixes the factories.

## Changes

- New `grantedQueryOptions` helper (`data/granted-query-options.ts`).
Without a grant, the factory sets `queryFn: undefined` instead of
`skipToken`, so TanStack borrows the query function from a subscribed
observer that has one. The caller's `enabled` applies only when the
factory has a query function.
- `apiKeysQueryOptions`, `revealedApiKeysQueryOptions`,
`branchesQueryOptions`, `branchQueryOptions` and
`branchDiffQueryOptions` take caller options as a second argument. Call
sites change from `useQuery({ ...f(vars), enabled })` to
`useQuery(f(vars, { enabled }))`, so callers can't override the grant
gate.
- New type-aware `fga/no-spread-granted-query-options` lint rule
(error). `grantedQueryOptions` returns a type-only brand, and the rule
flags any spread or `Object.assign` of a branded value, however it was
built or named.
- Restores typed linting as a separate pass
(`eslint.type-checks.config.cjs`, run by `pnpm lint` via
`lint:type-checks`), as #44678 did. Enabling type info in the main
config changes what other plugins report, so it stays separate.
- `studio-queries` skill documents the pattern.

Callers still pass their own grant, so the UI gates on the same grant
the request carries.

## Testing

- `data/api-keys/api-keys-query.test.tsx`: a granted and a grant-less
component share the key, with the grant-less one rendering last;
invalidation via the delete mutation refetches successfully. Fails on
master with `Missing queryFn`.
- `data/granted-query-options.test.ts`: `enabled` gating, factory fields
win over caller fields, `select` type inference.
- Lint rule cases in `scripts/__tests__/fga-eslint-rules.test.ts`.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
2026-11-06 11:04:00 -05:00

115 lines
3.3 KiB
TypeScript

import { useQuery } from '@tanstack/react-query'
import { ForwardIcon, GitBranchIcon, PlusIcon } from 'lucide-react'
import { PageType, useRegisterCommands, useRegisterPage, useSetPage } from 'ui-patterns/CommandMenu'
import { COMMAND_MENU_SECTIONS } from '../App/CommandMenu/CommandMenu.utils'
import { orderCommandSectionsByPriority } from '../App/CommandMenu/ordering'
import { branchesQueryOptions } from '@/data/branches/branches-query'
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
import { useGrant } from '@/lib/grants'
import { useAppStateSnapshot } from '@/state/app-state'
const SWITCH_BRANCH_PAGE_NAME = 'Switch branch'
const EMPTY_ARRAY = [] as Array<any>
export function useBranchCommands() {
const setPage = useSetPage()
const { setShowCreateBranchModal } = useAppStateSnapshot()
const { data: selectedProject } = useSelectedProjectQuery()
const ref = selectedProject?.ref || '_'
const isBranchingEnabled = selectedProject?.is_branch_enabled === true
const parentProjectRef = selectedProject?.parent_project_ref || selectedProject?.ref
const createBranchGrant = useGrant('POST /v1/projects/{ref}/branches', {
projectRef: parentProjectRef,
})
const canCreateBranches = createBranchGrant.status === 'granted'
const branchesGrant = useGrant('GET /v1/projects/{ref}/branches', {
projectRef: parentProjectRef,
})
let { data: branches } = useQuery(
branchesQueryOptions(
{ projectRef: parentProjectRef, grant: branchesGrant.grant },
{ enabled: isBranchingEnabled }
)
)
branches ??= EMPTY_ARRAY
useRegisterPage(
SWITCH_BRANCH_PAGE_NAME,
{
type: PageType.Commands,
sections: [
{
id: 'switch-branch',
name: 'Switch branch',
commands: branches.map((branch) => ({
id: `branch-${branch.id}`,
name: branch.name,
route: `/project/${branch.project_ref}`,
icon: () => <ForwardIcon />,
})),
},
],
},
{ enabled: isBranchingEnabled && branches.length > 0, deps: [branches] }
)
useRegisterCommands(
COMMAND_MENU_SECTIONS.ACTIONS,
[
{
id: 'switch-branch',
name: 'Switch branch',
value: 'Switch branch, Change branch, Select branch',
action: () => setPage(SWITCH_BRANCH_PAGE_NAME),
icon: () => <GitBranchIcon />,
},
],
{
enabled: isBranchingEnabled && branches.length > 0,
orderSection: orderCommandSectionsByPriority,
sectionMeta: { priority: 3 },
}
)
useRegisterCommands(
COMMAND_MENU_SECTIONS.ACTIONS,
[
{
id: 'create-branch',
name: 'Create branch',
value: 'Create branch, New branch',
action: () => setShowCreateBranchModal(true),
icon: () => <PlusIcon />,
},
],
{
enabled: canCreateBranches,
orderSection: orderCommandSectionsByPriority,
sectionMeta: { priority: 3 },
}
)
useRegisterCommands(
COMMAND_MENU_SECTIONS.NAVIGATE,
[
{
id: 'nav-branch-management',
name: 'Branch management',
route: `/project/${ref}/branches`,
},
{
id: 'nav-branch-merge-requests',
name: 'Merge requests',
value: 'Branch',
route: `/project/${ref}/branches/merge-requests`,
},
],
{ enabled: !!selectedProject }
)
}