Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | 4x 15x 15x 15x 15x 15x 3x 15x 15x 3x 15x 15x 12x 12x 12x 12x 12x | import type { WebRefreshCadence } from "@/objects/Knowledge";
import { WEB_LIMITS, type WebSourceForm } from "../webSource";
/**
* What a recommendation's action put in a source form's address, read back
* (`recommendations.ts` `actionHref`): only values the form would accept,
* so a hand-edited address fills in nothing it could not save. A form shows
* them as a suggestion; nothing is saved until the person saves.
*/
export interface SourcePrefill {
name?: string;
/** Website settings to set over the form's own. */
web: Partial<Pick<WebSourceForm, "urls" | "refresh" | "max_age_days">>;
/** Whether anything was filled in. */
any: boolean;
}
const CADENCES: readonly WebRefreshCadence[] = ["manual", "hourly", "daily", "weekly"];
export function sourcePrefill(params: URLSearchParams): SourcePrefill {
const web: SourcePrefill["web"] = {};
const url = params.get("url")?.trim();
if (url && /^https?:\/\/[^\s]+$/i.test(url)) web.urls = url;
const refresh = params.get("refresh");
if (refresh && (CADENCES as readonly string[]).includes(refresh)) {
web.refresh = refresh as WebRefreshCadence;
}
const days = Number(params.get("max_age_days"));
if (Number.isInteger(days) && days >= 1 && days <= WEB_LIMITS.maxAgeDays) {
web.max_age_days = String(days);
}
const name = params.get("name")?.trim().slice(0, 255) || undefined;
return { name, web, any: !!name || Object.keys(web).length > 0 };
}
/** What a prefill changes, in words, for the note above the form. */
export function describePrefill(prefill: SourcePrefill): string[] {
const out: string[] = [];
if (prefill.web.urls) out.push(`starting at ${prefill.web.urls}`);
if (prefill.web.refresh) out.push(`read ${prefill.web.refresh}`);
if (prefill.web.max_age_days) out.push(`keeping ${prefill.web.max_age_days} days of pages`);
return out;
}
|