Files
gitea-native/src/core/api/query-keys.ts
T
JustZoe101 ddd834b7ac feat(phase3): comments, issue compose, PR diff viewer, reviews, merge, checks
- API: comments CRUD, create/edit issue, milestones, collaborators, pull files,
  raw unified diff (getText), reviews, merge, combined commit status
- hooks: comment thread + composer mutations, issue compose mutations,
  pull files/diff/reviews, create review, merge, combined status
- UI: TextArea, ChipSelect, CommentItem, CommentComposer, native DiffView
  (parseDiff), StatusChecks
- screens: IssueCompose (create/edit), PullFiles, PullDiff, PullReview; issue
  detail gains thread + close/reopen; PR detail gains reviews, merge, files,
  checks; commit detail shows checks
- navigation/types + i18n (en/nl) updated; centralized safeHost helper
- tests: diff parser, endpoint body shapes (merge capitalisation), client getText
2026-10-09 17:02:08 +02:00

150 lines
6.8 KiB
TypeScript

/**
* 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<string, unknown>) =>
[...queryKeys.account(accountId), 'repos', 'mine', params] as const,
starred: (accountId: string, params: Record<string, unknown>) =>
[...queryKeys.account(accountId), 'repos', 'starred', params] as const,
search: (accountId: string, params: Record<string, unknown>) =>
[...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<string, unknown>) =>
[...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<string, unknown>) =>
[...queryKeys.repo(accountId, owner, name), 'tags', params] as const,
commits: (accountId: string, owner: string, name: string, params: Record<string, unknown>) =>
[...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<string, unknown>) =>
[...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<string, unknown>,
) => [...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<string, unknown>,
) => [...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<string, unknown>) =>
[...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<string, unknown>) =>
[...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) });
}