Files
gitea-native/src/core/api/query-keys.ts
T
JustZoe101 5e8c9492b3 feat(phase5): star/watch/fork, releases, wiki fallback, capability probe
- endpoints: is-starred (404→false), star/unstar, subscription watch/unwatch,
  fork, releases list/detail
- hooks: useIsStarred/useToggleStar, useSubscription/useToggleWatch,
  useForkRepository, useReleases/useRelease
- repo overview: star/watch/fork actions, Releases + Wiki entries
  (wiki via explicit browser fallback gated by capability probe)
- screens: Releases list, Release detail with downloadable assets
- core/capabilities: version parsing + per-capability minimum versions + tests
- i18n (en/nl); README/ROADMAP/FEATURE_PARITY updated for phases 3-5
2026-10-09 17:34:15 +02:00

162 lines
7.3 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,
repoStar: (accountId: string, owner: string, name: string) =>
[...queryKeys.repo(accountId, owner, name), 'star'] as const,
repoSubscription: (accountId: string, owner: string, name: string) =>
[...queryKeys.repo(accountId, owner, name), 'subscription'] as const,
releases: (accountId: string, owner: string, name: string, params: Record<string, unknown>) =>
[...queryKeys.repo(accountId, owner, name), 'releases', params] as const,
release: (accountId: string, owner: string, name: string, id: number) =>
[...queryKeys.repo(accountId, owner, name), 'release', id] 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) });
}