Something went wrong. Try again.
Monorepo for Tangled forked from tangled.org/core
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143import { ClientResponseError, ok } from "@atcute/client";import type { Datetime } from "@atcute/lexicons/syntax";import type { BobbinContext } from "#lib/api/client.ts";import { mainSchema as getPreferencesSchema } from "#lib/api/lexicons/types/com/bad-example/pocket/getPreferences.ts";import { mainSchema as putPreferencesSchema } from "#lib/api/lexicons/types/com/bad-example/pocket/putPreferences.ts";
const KNOT_PREF = "org.tangled.temp.actor.defs#savedKnotPref";const SPINDLE_PREF = "org.tangled.temp.actor.defs#savedSpindlePref";
export type ServiceKind = "knots" | "spindles";
export interface Preferences { knots: string[]; spindles: string[];}
export const hostOf = (input: string): string => input .trim() .replace(/^[a-z]+:\/\//i, "") .replace(/\/.*$/, "") .toLowerCase();
type StoredPreferences = Awaited<ReturnType<typeof readStored>>;
// pocket answers NotFound for an account that never saved anything, which is just an empty listconst readStored = async (ctx: BobbinContext) => { try { const { preferences } = await ok(ctx.xrpc.call(getPreferencesSchema, { params: {} })); return preferences; } catch (cause) { if (cause instanceof ClientResponseError && cause.error === "NotFound") return []; throw cause; }};
const itemsOf = (hosts: string[]) => hosts.map((host) => ({ id: host, value: host }));
const valuesOf = (stored: StoredPreferences, type: string): string[] => stored .filter((pref) => pref.$type === type) .flatMap((pref) => pref.items.map((item) => item.value));
const servicesOf = (stored: StoredPreferences): Preferences => ({ knots: valuesOf(stored, KNOT_PREF), spindles: valuesOf(stored, SPINDLE_PREF)});
// throws on failure so callers never mistake a failed read for an empty listexport const getPreferences = async (ctx: BobbinContext): Promise<Preferences> => servicesOf(await readStored(ctx));
// put replaces the whole list, so entries of other types are sent back as readconst putStored = async ( ctx: BobbinContext, stored: StoredPreferences, preferences: Preferences): Promise<void> => { const others = stored.filter((pref) => pref.$type !== KNOT_PREF && pref.$type !== SPINDLE_PREF); await ok( ctx.xrpc.call(putPreferencesSchema, { input: { preferences: [ ...others, { $type: KNOT_PREF, items: itemsOf(preferences.knots) }, { $type: SPINDLE_PREF, items: itemsOf(preferences.spindles) } ] } }) );};
/** rewrites one of the two lists and answers with what it saved */export const editSavedServices = async ( ctx: BobbinContext, kind: ServiceKind, edit: (hosts: string[]) => string[]): Promise<string[]> => { const stored = await readStored(ctx); const targets = servicesOf(stored); const next = { ...targets, [kind]: edit(targets[kind]) }; await putStored(ctx, stored, next); return next[kind];};
const ONBOARDING_PREF = "org.tangled.temp.actor.defs#onboardingPref";
export interface OnboardingState { startedAt: Datetime; completedAt?: Datetime; nudgeDismissedAt?: Datetime;}
const onboardingOf = (stored: StoredPreferences): OnboardingState | null => { const pref = stored.find((entry) => entry.$type === ONBOARDING_PREF); if (!pref || !("startedAt" in pref)) return null; return { startedAt: pref.startedAt, ...(pref.completedAt ? { completedAt: pref.completedAt } : {}), ...(pref.nudgeDismissedAt ? { nudgeDismissedAt: pref.nudgeDismissedAt } : {}) };};
export const getOnboarding = async (ctx: BobbinContext): Promise<OnboardingState | null> => onboardingOf(await readStored(ctx));
export const showsOnboardingNudge = (state: OnboardingState | null): boolean => Boolean(state && !state.completedAt && !state.nudgeDismissedAt);
const editOnboarding = async ( ctx: BobbinContext, edit: (current: OnboardingState | null, now: Datetime) => OnboardingState | null): Promise<OnboardingState | null> => { const stored = await readStored(ctx); const current = onboardingOf(stored); const next = edit(current, new Date().toISOString() as Datetime); if (next === current) return current; await ok( ctx.xrpc.call(putPreferencesSchema, { input: { preferences: [ ...stored.filter((pref) => pref.$type !== ONBOARDING_PREF), ...(next ? [{ $type: ONBOARDING_PREF, ...next } as const] : []) ] } }) ); return next;};
export const startOnboarding = (ctx: BobbinContext) => editOnboarding(ctx, (current, now) => current ?? { startedAt: now });
export const completeOnboarding = (ctx: BobbinContext) => editOnboarding(ctx, (current, now) => current && !current.completedAt ? { ...current, completedAt: now } : current );
export const dismissOnboardingNudge = (ctx: BobbinContext) => editOnboarding(ctx, (current, now) => current && !current.nudgeDismissedAt ? { ...current, nudgeDismissedAt: now } : current );