2025-12-29 13:55:20 +01:00
|
|
|
import { THEME_BUTTON_IMAGES, THEME_BACKGROUND_IMAGES } from '../../generated/asset-manifest';
|
|
|
|
|
|
2026-03-17 17:04:47 +08:00
|
|
|
export const resolveAssetUrl = (path: string, baseUrl = import.meta.env.BASE_URL): string => `${baseUrl}${path}`;
|
|
|
|
|
|
2025-12-29 13:55:20 +01:00
|
|
|
/**
|
|
|
|
|
* Preloads an array of image URLs by creating Image objects.
|
|
|
|
|
* Images are loaded in the background and cached by the browser.
|
|
|
|
|
*/
|
|
|
|
|
const preloadImages = (imagePaths: readonly string[]): void => {
|
|
|
|
|
imagePaths.forEach((path) => {
|
|
|
|
|
const img = new Image();
|
2026-03-17 17:04:47 +08:00
|
|
|
img.src = resolveAssetUrl(path);
|
2025-12-29 13:55:20 +01:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Preloads all theme-related assets (buttons, backgrounds) on app startup.
|
|
|
|
|
* This prevents visible loading delays when switching themes.
|
|
|
|
|
*/
|
|
|
|
|
export const preloadThemeAssets = (): void => {
|
|
|
|
|
preloadImages(THEME_BUTTON_IMAGES);
|
|
|
|
|
preloadImages(THEME_BACKGROUND_IMAGES);
|
|
|
|
|
};
|
2026-03-10 18:08:20 +08:00
|
|
|
|
|
|
|
|
const scheduleIdlePreload = (callback: () => void): void => {
|
|
|
|
|
if (typeof globalThis === 'undefined') {
|
|
|
|
|
callback();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const requestIdle = (
|
|
|
|
|
globalThis as typeof globalThis & {
|
|
|
|
|
requestIdleCallback?: (callback: IdleRequestCallback, options?: IdleRequestOptions) => number;
|
|
|
|
|
}
|
|
|
|
|
).requestIdleCallback;
|
|
|
|
|
|
|
|
|
|
if (typeof requestIdle === 'function') {
|
|
|
|
|
requestIdle(() => callback(), { timeout: 1500 });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
globalThis.setTimeout(callback, 500);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Preloads the reply modal chunk during idle time so the first `No.` click
|
|
|
|
|
* doesn't have to wait for the lazy import to resolve.
|
|
|
|
|
*/
|
|
|
|
|
export const preloadReplyModal = (): void => {
|
|
|
|
|
scheduleIdlePreload(() => {
|
|
|
|
|
void import('../../components/reply-modal');
|
|
|
|
|
});
|
|
|
|
|
};
|