77 lines
2.3 KiB
TypeScript
77 lines
2.3 KiB
TypeScript
/**
|
|
* A user's settings follow their account from browser to browser, and the
|
|
* server reads some of them itself (a digest is drawn in its reader's style).
|
|
* This module types them for `GET` and `PATCH /api/me/settings`; the
|
|
* client keeps them with its session.
|
|
*
|
|
* @module
|
|
*/
|
|
|
|
import type { ColorScheme, StyleName } from "./theme.ts";
|
|
|
|
/**
|
|
* Returned by `GET /api/me/settings`, and by a `PATCH` once applied.
|
|
* The client's `useSettings` reads it from the session.
|
|
*/
|
|
export interface UserSettings {
|
|
/**
|
|
* Null until the user picks one, so the instance's default style
|
|
* (`GERBEUR_DEFAULT_STYLE`) applies, and so a browser's earlier choice can be
|
|
* carried over on first sign-in.
|
|
*/
|
|
style: StyleName | null;
|
|
/** Null until the user picks one, which reads as "auto". */
|
|
colorScheme: ColorScheme | null;
|
|
/** Whether the weekly digest is sent. */
|
|
digestWeekly: boolean;
|
|
/** Whether the monthly recap is sent. */
|
|
digestMonthly: boolean;
|
|
}
|
|
|
|
/**
|
|
* Sent to `PATCH /api/me/settings` with the settings to change; the
|
|
* others keep their value.
|
|
*/
|
|
export type UpdateSettingsRequest = Partial<UserSettings>;
|
|
|
|
/** The settings of a user who never changed any. Digests are on by default. */
|
|
export const DEFAULT_SETTINGS: UserSettings = {
|
|
style: null,
|
|
colorScheme: null,
|
|
digestWeekly: true,
|
|
digestMonthly: true,
|
|
};
|
|
|
|
/** Lists how often a digest can come: weekly, or monthly as a recap. */
|
|
export const DIGEST_CADENCES = ["weekly", "monthly"] as const;
|
|
/**
|
|
* One of the {@link DIGEST_CADENCES}. An unsubscribe link names one, which
|
|
* turns off its setting alone.
|
|
*/
|
|
export type DigestCadence = typeof DIGEST_CADENCES[number];
|
|
|
|
const DAY_MS = 86_400_000;
|
|
/**
|
|
* How long each cadence's period lasts, rolling: a digest covers the one just
|
|
* ended, and the `/community` page the one ending now.
|
|
*/
|
|
export const PERIOD_MS: Record<DigestCadence, number> = {
|
|
weekly: 7 * DAY_MS,
|
|
monthly: 30 * DAY_MS,
|
|
};
|
|
|
|
/** Checks whether `value` is a {@link DigestCadence}. */
|
|
export function isDigestCadence(value: unknown): value is DigestCadence {
|
|
return typeof value === "string" &&
|
|
(DIGEST_CADENCES as readonly string[]).includes(value);
|
|
}
|
|
|
|
/** Names the setting that switches each cadence of digest. */
|
|
export const DIGEST_SETTING: Record<
|
|
DigestCadence,
|
|
"digestWeekly" | "digestMonthly"
|
|
> = {
|
|
weekly: "digestWeekly",
|
|
monthly: "digestMonthly",
|
|
};
|