// THE POSTS LIBRARY PAGE'S VIEW, AS ITS URL SAYS IT (lane posts-library): // /tools/library?lib=&topic=&state=&gone=1&media=1&author=&q=&new=1&offset= // A phone and a desktop open the same view from the same link. Plain ESM. import { LIBRARY_STATES } from "./client.mjs"; export const LIBRARY_PATH = "/tools/library"; export const PAGE_SIZE = 50; const SLUG = /^[A-Za-z0-9][A-Za-z0-9._-]*$/; const TOPIC = /^[a-z0-9][a-z0-9-]{0,119}$/; const HANDLE = /^@?[A-Za-z0-9_]{1,30}$/; const one = (v) => (Array.isArray(v) ? v[0] : v); const flag = (v) => one(v) === "1"; /** * The view a page's search params ask for; anything ill-shaped is dropped, never thrown. * @param {Record} sp * @returns {{ lib: string|null, topic: string|null, state: string|null, gone: boolean, media: boolean, author: string|null, q: string, fresh: boolean, offset: number }} */ export function parseLibraryView(sp) { const str = (k) => (typeof one(sp[k]) === "string" ? one(sp[k]).trim() : ""); const offset = Number(str("offset")); return { lib: SLUG.test(str("lib")) ? str("lib") : null, topic: TOPIC.test(str("topic")) ? str("topic") : null, state: LIBRARY_STATES.includes(str("state")) ? str("state") : null, gone: flag(sp.gone), media: flag(sp.media), author: HANDLE.test(str("author")) ? str("author").replace(/^@/, "") : null, q: str("q").slice(0, 200), fresh: flag(sp.new), offset: Number.isInteger(offset) && offset > 0 ? offset : 0, }; } /** The page's address for a view (a partial one: what is absent is off). */ export function libraryHref(view = {}) { const q = new URLSearchParams(); if (view.lib) q.set("lib", view.lib); if (view.topic) q.set("topic", view.topic); if (view.state) q.set("state", view.state); if (view.gone) q.set("gone", "1"); if (view.media) q.set("media", "1"); if (view.author) q.set("author", view.author); if (view.q) q.set("q", view.q); if (view.fresh) q.set("new", "1"); if (view.offset) q.set("offset", String(view.offset)); const s = q.toString(); return s ? `${LIBRARY_PATH}?${s}` : LIBRARY_PATH; } /** * The editor's listing parameters for a view. `seenAt` is the topic's Mark * seen time: "new" means first shown by a read after it (never marked: every * post is new, so no `since`). * @param {ReturnType} view * @param {{ seenAt?: string | null }} [o] */ export function listingParamsOf(view, { seenAt = null } = {}) { return { slug: view.lib, topic: view.topic, state: view.state, gone: view.gone, media: view.media, author: view.author, q: view.q, since: view.fresh && seenAt ? seenAt : null, }; } /** The filters in force, in words, for a confirm ("unjudged, with media, by @x"); "" for none. */ export function filterWords(view) { const out = []; if (view.state) out.push(view.state === "unjudged" ? "unjudged" : view.state === "keep" ? "kept" : view.state === "drop" ? "dropped" : "marked later"); if (view.fresh) out.push("new since Mark seen"); if (view.gone) out.push("gone from the page"); if (view.media) out.push("with media"); if (view.author) out.push(`by @${view.author}`); if (view.q) out.push(`matching “${view.q}”`); return out.join(", "); } /** "gone from the page at