-
Notifications
You must be signed in to change notification settings - Fork 228
Expand file tree
/
Copy pathpageSizePref.ts
More file actions
62 lines (48 loc) · 1.72 KB
/
Copy pathpageSizePref.ts
File metadata and controls
62 lines (48 loc) · 1.72 KB
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
export const VALIDATION_HISTORY_PAGE_SIZE_OPTIONS = [10, 25, 50] as const;
export type ValidationHistoryPageSize = (typeof VALIDATION_HISTORY_PAGE_SIZE_OPTIONS)[number];
export const DEFAULT_VALIDATION_HISTORY_PAGE_SIZE: ValidationHistoryPageSize =
VALIDATION_HISTORY_PAGE_SIZE_OPTIONS[0];
export const VALIDATION_HISTORY_PAGE_SIZE_STORAGE_KEY = 'validation-history-page-size';
export function isValidationHistoryPageSize(value: number): value is ValidationHistoryPageSize {
return VALIDATION_HISTORY_PAGE_SIZE_OPTIONS.includes(value as ValidationHistoryPageSize);
}
function getLocalStorage(): Storage | null {
if (typeof window === 'undefined') {
return null;
}
try {
return window.localStorage;
} catch {
return null;
}
}
export function readValidationHistoryPageSize(): ValidationHistoryPageSize {
const storage = getLocalStorage();
if (!storage) {
return DEFAULT_VALIDATION_HISTORY_PAGE_SIZE;
}
try {
const stored = storage.getItem(VALIDATION_HISTORY_PAGE_SIZE_STORAGE_KEY);
const parsed = stored ? Number(stored) : DEFAULT_VALIDATION_HISTORY_PAGE_SIZE;
return isValidationHistoryPageSize(parsed)
? parsed
: DEFAULT_VALIDATION_HISTORY_PAGE_SIZE;
} catch {
return DEFAULT_VALIDATION_HISTORY_PAGE_SIZE;
}
}
export function persistValidationHistoryPageSize(size: number): ValidationHistoryPageSize {
const nextSize = isValidationHistoryPageSize(size)
? size
: DEFAULT_VALIDATION_HISTORY_PAGE_SIZE;
const storage = getLocalStorage();
if (!storage) {
return nextSize;
}
try {
storage.setItem(VALIDATION_HISTORY_PAGE_SIZE_STORAGE_KEY, String(nextSize));
} catch {
// A blocked storage write should not break pagination.
}
return nextSize;
}