refactor: consolidate and debloat calendar sync implementation

Merge duplicated Google/Outlook engines, background handlers, and API
layers into shared calendarSync modules to cut complexity without changing
user-facing sync behavior.
This commit is contained in:
SethBurkart123
2026-06-28 10:45:45 +10:00
parent f4230e02b9
commit 7779edb063
52 changed files with 1965 additions and 3258 deletions
+58
View File
@@ -0,0 +1,58 @@
import browser from "webextension-polyfill";
export interface EventMapEntry {
id: string;
date: string;
}
export type EventMapRecord = Record<string, string | EventMapEntry>;
export function eventMapKey(origin: string, seqtaKey: string): string {
return `${origin}::${seqtaKey}`;
}
export function normalizeEventMapEntry(
value: string | EventMapEntry | undefined,
): EventMapEntry | undefined {
if (value == null) return undefined;
if (typeof value === "string") return { id: value, date: "" };
if (typeof value.id === "string" && value.id.length > 0) {
return { id: value.id, date: value.date ?? "" };
}
return undefined;
}
export function getStoredEventId(
value: string | EventMapEntry | undefined,
): string | undefined {
return normalizeEventMapEntry(value)?.id;
}
export function lessonDateFromSeqtaKey(seqtaKey: string): string | undefined {
for (const part of seqtaKey.split(":")) {
if (/^\d{4}-\d{2}-\d{2}$/.test(part)) return part;
}
return undefined;
}
export function createCalendarStateStorage<T extends object>(storageKey: string) {
async function read(): Promise<T> {
const got = await browser.storage.local.get(storageKey);
const raw = got[storageKey];
if (!raw || typeof raw !== "object" || Array.isArray(raw)) return {} as T;
return raw as T;
}
async function write(patch: Partial<T>): Promise<T> {
const current = await read();
const next = { ...current, ...patch };
await browser.storage.local.set({ [storageKey]: next });
return next;
}
async function clear(): Promise<void> {
await browser.storage.local.remove(storageKey);
}
return { read, write, clear };
}