Twitch-VOD-Manager/src/renderer-shared.ts
xRangerDE 752a5b2556 cleanup: centralise localStorage try/catch in renderer-shared — safeLocalStorageGet/Set/Remove
Every renderer module that persists state was wrapping its localStorage.getItem/setItem/removeItem call in the same try/catch idiom — handling private-browsing quirks and other sandbox contexts where storage isn't writable. Three identical patterns repeated nine times across renderer-streamers (filter / sort / hide-downloaded state), renderer-updates (skipped-update version), and renderer.ts (active-tab persistence).

Introduced three helpers in renderer-shared.ts:
- safeLocalStorageGet(key, fallback = '') — wraps getItem with the try/catch + fallback
- safeLocalStorageSet(key, value) — wraps setItem
- safeLocalStorageRemove(key) — wraps removeItem (needed for clearSkippedUpdateVersion which actually deletes the entry rather than blanking it)

Refactored 9 callsites. Reduces the noise:before:
    try { return localStorage.getItem(KEY) ?? ''; } catch { return ''; }
    try { localStorage.setItem(KEY, value); } catch { /* localStorage may be unavailable */ }
after:
    return safeLocalStorageGet(KEY);
    safeLocalStorageSet(KEY, value);

Left the VOD scroll-positions persistence in renderer-streamers untouched — its surrounding try/catch wraps JSON.parse/stringify logic that doesn't fit the simple helper signature.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-11 07:33:23 +02:00

101 lines
2.9 KiB
TypeScript

function byId<T = any>(id: string): T {
return document.getElementById(id) as T;
}
function query<T = any>(selector: string): T {
return document.querySelector(selector) as T;
}
function queryAll<T = any>(selector: string): T[] {
return Array.from(document.querySelectorAll(selector)) as T[];
}
function escapeHtml(value: string): string {
return value
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#39;');
}
/* localStorage helpers — every renderer module that persists state was
wrapping its get/set calls in the same try/catch idiom to handle
environments where localStorage isn't writable (private-browsing
quirks, certain sandboxed contexts). Centralising the pattern. */
function safeLocalStorageGet(key: string, fallback = ''): string {
try { return localStorage.getItem(key) ?? fallback; } catch { return fallback; }
}
function safeLocalStorageSet(key: string, value: string): void {
try { localStorage.setItem(key, value); } catch { /* localStorage may be unavailable */ }
}
function safeLocalStorageRemove(key: string): void {
try { localStorage.removeItem(key); } catch { /* localStorage may be unavailable */ }
}
let config: AppConfig = {};
let currentStreamer: string | null = null;
let isConnected = false;
let downloading = false;
let queue: QueueItem[] = [];
let selectedQueueIds: string[] = [];
let expandedQueueIds: Set<string> = new Set();
let queueDragDropInitialized = false;
let cutterFile: string | null = null;
let cutterVideoInfo: VideoInfo | null = null;
let cutterStartTime = 0;
let cutterEndTime = 0;
let isCutting = false;
let mergeFiles: string[] = [];
let isMerging = false;
let clipDialogData: ClipDialogData | null = null;
let clipTotalSeconds = 0;
let updateReady = false;
let debugLogAutoRefreshTimer: number | null = null;
let runtimeMetricsAutoRefreshTimer: number | null = null;
let draggedQueueItemId: string | null = null;
const TEMPLATE_EXACT_TOKENS = new Set([
'{title}',
'{id}',
'{channel}',
'{channel_id}',
'{date}',
'{part}',
'{part_padded}',
'{trim_start}',
'{trim_end}',
'{trim_length}',
'{length}',
'{ext}',
'{random_string}'
]);
const TEMPLATE_CUSTOM_TOKEN_PATTERNS = [
/^\{date_custom=".*"\}$/,
/^\{trim_start_custom=".*"\}$/,
/^\{trim_end_custom=".*"\}$/,
/^\{trim_length_custom=".*"\}$/,
/^\{length_custom=".*"\}$/
];
function isKnownTemplateToken(token: string): boolean {
if (TEMPLATE_EXACT_TOKENS.has(token)) {
return true;
}
return TEMPLATE_CUSTOM_TOKEN_PATTERNS.some((pattern) => pattern.test(token));
}
function collectUnknownTemplatePlaceholders(template: string): string[] {
const tokens = (template.match(/\{[^{}]+\}/g) || []).map((token) => token.trim());
const unknown = tokens.filter((token) => !isKnownTemplateToken(token));
return Array.from(new Set(unknown));
}