75 lines
4.9 KiB
TypeScript
75 lines
4.9 KiB
TypeScript
// What the host sends, made complete for the screens. The Rust host already fills defaults (store.rs migrates old
|
|
// state files), but the window must not depend on that: a field an older or newer host leaves out, or a value of the
|
|
// wrong type, becomes its default here instead of a "cannot read properties of undefined" at render time.
|
|
import { type Account, DEFAULT_SETTINGS, type Project, type Settings, STUDIO_COLORS, type Studio, type Workspace } from '../api';
|
|
import { NO_OVERRIDES, type Overrides } from './identity';
|
|
|
|
type Rec = Record<string, unknown>;
|
|
const isRec = (v: unknown): v is Rec => !!v && typeof v === 'object' && !Array.isArray(v);
|
|
const str = (v: unknown, d = ''): string => (typeof v === 'string' ? v : d);
|
|
const strOrNull = (v: unknown): string | null => (typeof v === 'string' && v ? v : null);
|
|
const arr = (v: unknown): unknown[] => (Array.isArray(v) ? v : []);
|
|
const folderName = (p: string): string => p.split(/[\\/]/).filter(Boolean).pop() ?? p;
|
|
|
|
export function normalizeProjects(v: unknown): Project[] {
|
|
const seen = new Set<string>();
|
|
return arr(v).filter(isRec).filter(p => typeof p.id === 'string' && typeof p.path === 'string' && !seen.has(p.id) && !!seen.add(p.id)).map(p => {
|
|
const path = p.path as string;
|
|
return { id: p.id as string, path, name: str(p.name).trim() || folderName(path), playAccount: strOrNull(p.playAccount), appleAccount: strOrNull(p.appleAccount), added: str(p.added), missing: p.missing === true, storeboundPin: strOrNull(p.storeboundPin) };
|
|
});
|
|
}
|
|
|
|
export function normalizeAccounts(v: unknown): Account[] {
|
|
return arr(v).filter(isRec).filter(a => typeof a.id === 'string' && (a.kind === 'play' || a.kind === 'apple'))
|
|
.map(a => ({ id: a.id as string, label: str(a.label) || (a.id as string), kind: a.kind as Account['kind'], created: str(a.created), studio: strOrNull(a.studio), profile: strOrNull(a.profile) }));
|
|
}
|
|
|
|
function studios(v: unknown): Studio[] {
|
|
return arr(v).filter(isRec).filter(s => typeof s.id === 'string').map(s => ({
|
|
id: s.id as string,
|
|
name: str(s.name) || (s.id as string),
|
|
color: (STUDIO_COLORS as readonly string[]).includes(str(s.color)) ? (s.color as Studio['color']) : 'slate',
|
|
logo: typeof s.logo === 'string' && s.logo.startsWith('data:image/') ? s.logo : null,
|
|
created: str(s.created),
|
|
}));
|
|
}
|
|
|
|
function overrides(v: unknown): Overrides {
|
|
if (!isRec(v)) return NO_OVERRIDES;
|
|
const pairs = (x: unknown) => arr(x).filter(isRec).filter(p => typeof p.ios === 'string' && typeof p.android === 'string').map(p => ({ ios: p.ios as string, android: p.android as string }));
|
|
return {
|
|
pairs: pairs(v.pairs),
|
|
unpairs: pairs(v.unpairs),
|
|
links: arr(v.links).filter(isRec).filter(l => typeof l.key === 'string' && typeof l.project === 'string').map(l => ({ key: l.key as string, project: l.project as string })),
|
|
appStudio: arr(v.appStudio).filter(isRec).filter(l => typeof l.key === 'string' && typeof l.studio === 'string').map(l => ({ key: l.key as string, studio: l.studio as string })),
|
|
ignored: arr(v.ignored).filter((x): x is string => typeof x === 'string'),
|
|
};
|
|
}
|
|
|
|
function pick<T>(v: unknown, d: T, ok: (x: unknown) => boolean): T { return ok(v) ? (v as T) : d; }
|
|
const isNum = (x: unknown) => typeof x === 'number' && Number.isFinite(x);
|
|
const isBool = (x: unknown) => typeof x === 'boolean';
|
|
const isHm = (x: unknown) => typeof x === 'string' && /^(?:[01][0-9]|2[0-3]):[0-5][0-9]$/.test(x);
|
|
|
|
export function normalizeSettings(v: unknown): Settings {
|
|
const d = DEFAULT_SETTINGS;
|
|
if (!isRec(v)) return d;
|
|
const r = isRec(v.refresh) ? v.refresh : {}, c = isRec(v.cache) ? v.cache : {}, n = isRec(v.notify) ? v.notify : {};
|
|
const q = isRec(n.quietHours) ? n.quietHours : {};
|
|
return {
|
|
refresh: { autoMinutes: pick(r.autoMinutes, d.refresh.autoMinutes, isNum), onLaunch: pick(r.onLaunch, d.refresh.onLaunch, isBool), parallel: pick(r.parallel, d.refresh.parallel, isNum), icons: pick(r.icons, d.refresh.icons, isBool),
|
|
autoRead: pick(r.autoRead, d.refresh.autoRead, isBool), everyMinutes: pick(r.everyMinutes, d.refresh.everyMinutes, (x: unknown) => [0, 5, 15, 30, 60].includes(x as number)) },
|
|
cache: { keepDays: pick(c.keepDays, d.cache.keepDays, isNum) },
|
|
notify: {
|
|
reviewState: pick(n.reviewState, d.notify.reviewState, isBool), rejected: pick(n.rejected, d.notify.rejected, isBool), approved: pick(n.approved, d.notify.approved, isBool), live: pick(n.live, d.notify.live, isBool),
|
|
lowReviews: pick(n.lowReviews, d.notify.lowReviews, isBool), crash: pick(n.crash, d.notify.crash, isBool),
|
|
quietHours: { enabled: pick(q.enabled, d.notify.quietHours.enabled, isBool), from: pick(q.from, d.notify.quietHours.from, isHm), to: pick(q.to, d.notify.quietHours.to, isHm) },
|
|
},
|
|
channel: v.channel === 'beta' ? 'beta' : 'stable',
|
|
};
|
|
}
|
|
|
|
export function normalizeWorkspace(v: unknown): Workspace {
|
|
const w = isRec(v) ? v : {};
|
|
return { studios: studios(w.studios), overrides: overrides(w.overrides), settings: normalizeSettings(w.settings) };
|
|
}
|