53 lines
2.1 KiB
TypeScript
53 lines
2.1 KiB
TypeScript
// The tray card window's entry (card.html): theme and language from the same prefs as the main window, the host
|
|
// through Tauri (or a fixture in a plain dev browser).
|
|
import { createRoot } from 'react-dom/client';
|
|
import { applyPrefs, loadPrefs, resolveLang } from '../prefs';
|
|
import { Card, type CardHost } from './Card';
|
|
import { cardApi, inTauri } from './card-api';
|
|
import type { CardSnapshot } from './model';
|
|
import '../styles.css';
|
|
import '../design.css';
|
|
import './card.css';
|
|
|
|
const sys = () => (typeof navigator !== 'undefined' ? navigator.language : 'en');
|
|
|
|
function prefs() {
|
|
const p = loadPrefs();
|
|
if (import.meta.env.DEV) {
|
|
const q = new URLSearchParams(location.search);
|
|
const th = q.get('theme'), lg = q.get('lang');
|
|
if (th === 'light' || th === 'dark') p.theme = th;
|
|
if (lg === 'en' || lg === 'tr') p.lang = lg;
|
|
}
|
|
applyPrefs(p, document.documentElement, sys());
|
|
return resolveLang(p.lang, sys());
|
|
}
|
|
|
|
async function devHost(): Promise<CardHost> {
|
|
const { CARD_FIXTURE } = await import('./fixture');
|
|
const q = new URLSearchParams(location.search);
|
|
const snap: CardSnapshot = q.get('empty')
|
|
? { ...CARD_FIXTURE, rejections: [], releases: [], health: [], reviews: { recent: 0, low: [] } }
|
|
: { ...CARD_FIXTURE, busy: !!q.get('busy') };
|
|
return { get: async () => snap, onSnapshot: () => () => {}, onShown: () => () => {}, action: (a, arg) => console.info('card action', a, arg ?? '') };
|
|
}
|
|
|
|
const tauriHost: CardHost = cardApi;
|
|
|
|
void (async () => {
|
|
let lang = prefs();
|
|
const host = inTauri() ? tauriHost : await devHost();
|
|
const root = createRoot(document.getElementById('card')!);
|
|
const render = () => root.render(<Card key={lang} host={host} lang={lang} />);
|
|
render();
|
|
const relang = () => { const l = prefs(); if (l !== lang) { lang = l; render(); } };
|
|
// theme or language changed in the main window (same origin: shared localStorage)
|
|
window.addEventListener('storage', relang);
|
|
host.onShown(() => {
|
|
relang();
|
|
document.body.classList.remove('tc-in');
|
|
void document.body.offsetWidth;
|
|
document.body.classList.add('tc-in');
|
|
});
|
|
document.body.classList.add('tc-in');
|
|
})();
|