Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | 63x 63x 63x 6x 6x 1x 63x 60x 60x 63x 63x 63x 2599x 2085x 1402x 1503x 1154x 115x 54x 106x 130x 3094x 395x 739x 969x 156x 163x 814x 814x 5650x 6981x 1075x 2278x 34x | import { QueryClient } from '@tanstack/react-query';
import type { ActivitySortOrder } from './api/activity-log';
const STALE_TIME_DEFAULT_MS = 30_000;
const GC_TIME_DEFAULT_MS = 5 * 60_000;
const CLIENT_KEY = '__jetpackBackupQueryClient' as const;
declare global {
interface Window {
[ CLIENT_KEY ]?: QueryClient;
}
}
/**
* Refetch what Backup shows once the admin shell shows it again after keeping it hidden a while.
*
* @param event - The shell's `jetpack-admin-shell:page-shown` event.
*/
export function refetchWhenStale( event: Event ): void {
const { page, stale } = ( event as CustomEvent ).detail ?? {};
if ( page === 'jetpack-backup' && stale ) {
window[ CLIENT_KEY ]?.invalidateQueries( { queryKey: [ 'backup' ] } );
}
}
/**
* The one QueryClient every route of the modernized Backup dashboard renders against.
*
* Parked on `window` because each wp-build route bundles its own copy of this module: a
* module-scope client left every route with its own cache, cold on first arrival.
*
* @return The shared client, created on first use.
*/
function sharedClient(): QueryClient {
if ( ! window[ CLIENT_KEY ] ) {
window[ CLIENT_KEY ] = new QueryClient( {
defaultOptions: {
queries: {
staleTime: STALE_TIME_DEFAULT_MS,
gcTime: GC_TIME_DEFAULT_MS,
retry: 1,
refetchOnWindowFocus: false,
},
},
} );
// Here, not at module scope, so the routes' copies of this module listen once between them.
window.addEventListener( 'jetpack-admin-shell:page-shown', refetchWhenStale );
}
return window[ CLIENT_KEY ];
}
// Each route also bundles its own react-query, so sentinels compared by identity —
// `skipToken`, `isCancelledError` — never match against a client another route created.
export const queryClient = sharedClient();
/**
* Stable cache keys for each query the dashboard issues.
*
* Centralizing keys here means any consumer can invalidate or read
* cached data without re-deriving the tuple shape ad hoc.
*/
export const keys = {
capabilities: () => [ 'backup', 'capabilities' ] as const,
// The site's recent backup attempts, from the unconditionally
// registered `/jetpack/v4/backups`. Polled while a backup runs, so it
// deliberately does not share the activity-log family's key.
backups: () => [ 'backup', 'backups' ] as const,
// Client-only: the pending "Back up now" request. Never fetched.
enqueueRequested: () => [ 'backup', 'enqueue-requested' ] as const,
siteSize: () => [ 'backup', 'site-size' ] as const,
// The site's retention and storage policies. Separate from
// `siteSize` because it is a separate route with a much flatter
// change rate — but the storage meter needs both, so the two are
// always read together.
sitePolicies: () => [ 'backup', 'site-policies' ] as const,
// The hour WordPress.com runs the site's daily backup. Its own key rather than a
// slice of `siteSize`: a different route, and a far longer stale time.
backupSchedule: () => [ 'backup', 'schedule' ] as const,
// The Backup product being promoted, for the no-plan screen's price.
// Not keyed on anything: WordPress.com picks the currency from the
// site, so one site only ever sees one answer.
promotedProduct: () => [ 'backup', 'promoted-product' ] as const,
// The storage add-on being offered. Keyed on both byte figures because the route
// derives its answer from both — the same pair that gates the query. Nulls appear
// in the key only while it is disabled, so no request is made under one.
storageAddonOffer: ( storageUsed: number | null, storageLimit: number | null ) =>
[ 'backup', 'storage-addon-offer', storageUsed, storageLimit ] as const,
// Family prefix for any rewindable-activity-log page. Use as a
// query-filter root to scan all cached pages (e.g. when looking up
// a row by id across pages).
activityLogRoot: () => [ 'backup', 'activity-log' ] as const,
// All three distinguish one fetch from another: page 1 ascending and page 1
// descending are different rows.
activityLogPage: ( page: number, pageSize: number, sortOrder: ActivitySortOrder ) =>
[ 'backup', 'activity-log', { page, pageSize, sortOrder } ] as const,
// The few seconds of the log around one backup, for a selection no loaded page holds.
activityLogAround: ( rewindId: string ) =>
[ 'backup', 'activity-log', 'around', rewindId ] as const,
// Every loaded page of backup sizes, as one infinite query.
backupSizes: () => [ 'backup', 'sizes' ] as const,
fileTree: ( rewindId: string, folderPath: string | null ) =>
[ 'backup', 'file-tree', rewindId, folderPath ] as const,
fileContents: ( rewindId: string, path: string ) =>
[ 'backup', 'file-contents', rewindId, path ] as const,
// Keyed on the file's own period, not the parent backup's rewindId:
// upstream records one row per file version and matches the period
// exactly, so two backups sharing a file share this entry.
pathInfo: ( filePeriod: string, manifestPath: string ) =>
[ 'backup', 'path-info', filePeriod, manifestPath ] as const,
downloadStatus: ( rewindId: string, downloadId: number ) =>
[ 'backup', 'download-status', rewindId, downloadId ] as const,
downloads: () => [ 'backup', 'downloads' ] as const,
restoreStatus: ( restoreId: number ) => [ 'backup', 'restore-status', restoreId ] as const,
// The site's recent restores, not one restore's status; see `useRecentRestores`.
recentRestores: () => [ 'backup', 'recent-restores' ] as const,
knownThreats: ( rewindId: string ) => [ 'backup', 'known-threats', rewindId ] as const,
backupsKnownThreats: ( withCleanRewindId = false ) =>
[ 'backup', 'known-threats', 'backups', withCleanRewindId ] as const,
backupsKnownThreatsRoot: () => [ 'backup', 'known-threats', 'backups' ] as const,
};
|