/** * Account-scoped query key factory. * * Every server-state key starts with `['account', accountId, ...]`, which gives us: * - Automatic isolation between accounts (no shared cache entries). * - One-call cache purge on account removal: * `queryClient.removeQueries({ queryKey: queryKeys.account(id) })`. * * Never construct raw arrays in feature code — always go through this factory. */ import type { QueryClient } from '@tanstack/react-query'; export const queryKeys = { /** Root of everything this app writes into the query cache. */ root: ['gitea-native'] as const, /** Top-level scope for one account. */ account: (accountId: string) => ['account', accountId] as const, // --- instance/account level ----------------------------------------------- version: (accountId: string) => [...queryKeys.account(accountId), 'version'] as const, me: (accountId: string) => [...queryKeys.account(accountId), 'me'] as const, unreadCount: (accountId: string) => [...queryKeys.account(accountId), 'unread-count'] as const, activity: (accountId: string, username: string) => [...queryKeys.account(accountId), 'activity', username] as const, // --- repositories ---------------------------------------------------------- repos: { mine: (accountId: string, params: Record) => [...queryKeys.account(accountId), 'repos', 'mine', params] as const, starred: (accountId: string, params: Record) => [...queryKeys.account(accountId), 'repos', 'starred', params] as const, search: (accountId: string, params: Record) => [...queryKeys.account(accountId), 'repos', 'search', params] as const, }, repo: (accountId: string, owner: string, name: string) => [...queryKeys.account(accountId), 'repo', owner, name] as const, repoDetail: (accountId: string, owner: string, name: string) => [...queryKeys.repo(accountId, owner, name), 'detail'] as const, repoLanguages: (accountId: string, owner: string, name: string) => [...queryKeys.repo(accountId, owner, name), 'languages'] as const, contents: (accountId: string, owner: string, name: string, path: string, ref?: string) => [...queryKeys.repo(accountId, owner, name), 'contents', { path, ref }] as const, file: (accountId: string, owner: string, name: string, path: string, ref?: string) => [...queryKeys.repo(accountId, owner, name), 'file', { path, ref }] as const, readme: (accountId: string, owner: string, name: string, ref?: string) => [...queryKeys.repo(accountId, owner, name), 'readme', { ref }] as const, branches: (accountId: string, owner: string, name: string, params: Record) => [...queryKeys.repo(accountId, owner, name), 'branches', params] as const, branch: (accountId: string, owner: string, name: string, branch: string) => [...queryKeys.repo(accountId, owner, name), 'branch', branch] as const, tags: (accountId: string, owner: string, name: string, params: Record) => [...queryKeys.repo(accountId, owner, name), 'tags', params] as const, commits: (accountId: string, owner: string, name: string, params: Record) => [...queryKeys.repo(accountId, owner, name), 'commits', params] as const, commit: (accountId: string, owner: string, name: string, sha: string) => [...queryKeys.repo(accountId, owner, name), 'commit', sha] as const, // --- issues & pull requests ------------------------------------------------ issueSearchRoot: (accountId: string) => [...queryKeys.account(accountId), 'issues', 'search'] as const, issueSearch: (accountId: string, params: Record) => [...queryKeys.issueSearchRoot(accountId), params] as const, repoIssuesRoot: (accountId: string, owner: string, name: string) => [...queryKeys.repo(accountId, owner, name), 'issues'] as const, repoIssues: ( accountId: string, owner: string, name: string, params: Record, ) => [...queryKeys.repoIssuesRoot(accountId, owner, name), params] as const, issue: (accountId: string, owner: string, name: string, index: number) => [...queryKeys.repo(accountId, owner, name), 'issue', index] as const, issueCommentsRoot: (accountId: string, owner: string, name: string, index: number) => [...queryKeys.issue(accountId, owner, name, index), 'comments'] as const, issueComments: ( accountId: string, owner: string, name: string, index: number, params: Record, ) => [...queryKeys.issueCommentsRoot(accountId, owner, name, index), params] as const, repoMilestones: (accountId: string, owner: string, name: string) => [...queryKeys.repo(accountId, owner, name), 'milestones'] as const, repoCollaborators: (accountId: string, owner: string, name: string) => [...queryKeys.repo(accountId, owner, name), 'collaborators'] as const, repoLabels: (accountId: string, owner: string, name: string) => [...queryKeys.repo(accountId, owner, name), 'labels'] as const, pullsRoot: (accountId: string, owner: string, name: string) => [...queryKeys.repo(accountId, owner, name), 'pulls'] as const, pulls: (accountId: string, owner: string, name: string, params: Record) => [...queryKeys.pullsRoot(accountId, owner, name), params] as const, pull: (accountId: string, owner: string, name: string, index: number) => [...queryKeys.repo(accountId, owner, name), 'pull', index] as const, pullFiles: (accountId: string, owner: string, name: string, index: number) => [...queryKeys.pull(accountId, owner, name, index), 'files'] as const, pullDiff: (accountId: string, owner: string, name: string, index: number) => [...queryKeys.pull(accountId, owner, name, index), 'diff'] as const, pullReviews: (accountId: string, owner: string, name: string, index: number) => [...queryKeys.pull(accountId, owner, name, index), 'reviews'] as const, combinedStatus: (accountId: string, owner: string, name: string, ref: string) => [...queryKeys.repo(accountId, owner, name), 'status', ref] as const, commitsRoot: (accountId: string, owner: string, name: string) => [...queryKeys.repo(accountId, owner, name), 'commits'] as const, // --- notifications --------------------------------------------------------- notificationsRoot: (accountId: string) => [...queryKeys.account(accountId), 'notifications'] as const, notifications: (accountId: string, params: Record) => [...queryKeys.notificationsRoot(accountId), params] as const, } as const; /** * Removes every cached query belonging to an account. * Called on account removal and when a token turns out to be invalid. */ export function purgeAccountCache(queryClient: QueryClient, accountId: string): void { queryClient.removeQueries({ queryKey: queryKeys.account(accountId) }); }