Something went wrong. Try again.
Monorepo for the nibling.run platform
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166import { comments, forkCount, likeCount, listProjects, loadProject, myLike, resolveActor, resolveRepo, type Actor, type Comment, type FileTree, type Project, type ProjectRef, type Session, type TangledRepo,} from "@nibling/sdk";import { loadBase } from "../platform/base.ts";import { cardFromProject, handled, linkName, newestFirst, type ProjectCard } from "./cards.ts";import { projectName } from "./route.ts";
/** What the preview area shows: a static HTML entry rendered in place, or a command only the editor runs. */export type PreviewPlan = | { readonly kind: "static"; readonly entry: string } | { readonly kind: "command"; readonly command: string } | { readonly kind: "none" };
export interface ProjectView { readonly project: Project; readonly author: Actor; /** The author's handle when it resolved, else the DID. */ readonly authorName: string; /** The project's slug, or its record key when it has none: what its addresses use. */ readonly name: string; readonly files: FileTree; readonly preview: PreviewPlan;}
export interface ThreadComment { readonly comment: Comment; readonly authorName: string; /** 0 for a comment on the project, 1 for a reply. Deeper replies show at 1, under their thread. */ readonly depth: 0 | 1;}
export interface ProjectPageData extends ProjectView { /** Fills in after the project shows. */ readonly social: Promise<ProjectSocial>;}
/** Social counts and the thread come from Constellation; each is undefined when it is unavailable. */export interface ProjectSocial { readonly likes?: number; readonly forks?: number; readonly comments?: readonly ThreadComment[]; /** The viewer's own like record, when signed in and they have liked it. */ readonly myLike?: string;}
/** The project, its files, and how to preview it. Enough for the embed and the bare preview. */export async function loadProjectView(ref: ProjectRef): Promise<ProjectView> { const { project, files } = await loadProject(ref, { loadBase }); const author = await resolveActor(project.did); return { project, author, authorName: author.handle ?? author.did, name: projectName(project), files, preview: previewPlan(project, files) };}
/** `name` is the project's slug or its record key. Resolves once the project and its files are read. */export async function loadProjectPage(actor: string, name: string, viewer?: Session): Promise<ProjectPageData> { const view = await loadProjectView({ actor, name }); return { ...view, social: handled(loadSocial(view.project.uri, viewer)) };}
async function loadSocial(uri: string, viewer: Session | undefined): Promise<ProjectSocial> { const [likes, forks, thread, mine] = await Promise.all([ likeCount(uri), forkCount(uri), comments(uri).then((list) => list && threadOf(list)), viewer ? myLike(viewer, uri).catch(() => undefined) : undefined, ]); return { ...(likes !== undefined ? { likes } : {}), ...(forks !== undefined ? { forks } : {}), ...(thread ? { comments: thread } : {}), ...(mine ? { myLike: mine } : {}), };}
/** What a project surface shows for `actor/name` when there is no such project, scoped by the cheapest * check that answers it: whether the identity resolves at all, then whether it has done anything on * nibling. The project page's fallback and the embed (which cannot navigate to the result on its own) * render the same decision. */export type ProjectMiss = | { readonly tier: "no-project"; readonly actorName: string; /** Up to four of the author's projects, closest name first, then newest. */ readonly projects: readonly ProjectCard[]; readonly repo?: TangledRepo; } | { readonly tier: "no-activity"; readonly actorName: string; readonly repo?: TangledRepo } | { readonly tier: "unknown" };
/** * `listProjects` is the "has nibling activity" signal: the same one-request read the actor page already * uses to list someone's projects, so a miss costs nothing extra to decide when that is empty or not. */export async function loadProjectMiss(actor: string, name: string): Promise<ProjectMiss> { let resolved: Actor; try { resolved = await resolveActor(actor); } catch { return { tier: "unknown" }; } const actorName = resolved.handle ?? resolved.did; const [projects, repo] = await Promise.all([listProjects(resolved.did).catch(() => []), resolveRepo(`${actorName}/${name}`).catch(() => undefined)]); if (projects.length === 0) return { tier: "no-activity", actorName, ...(repo && { repo }) }; const cards = rankByName(projects.map((p) => cardFromProject(p, actorName)), name).slice(0, 4); return { tier: "no-project", actorName, projects: cards, ...(repo && { repo }) };}
/** A Bluesky compose intent, pre-filled with a public mention: the intent has no way to target a reply * without a post URI, so inviting someone this way is a mention, not a reply. */export function bskyInviteHref(actorName: string, address: string): string { return `https://bsky.app/intent/compose?text=${encodeURIComponent(`@${actorName} come build with me on nibling ${address}`)}`;}
/** Closest slug or record name first (edit distance), then most recently updated. */function rankByName(cards: readonly ProjectCard[], name: string): ProjectCard[] { const target = name.toLowerCase(); const distance = (card: ProjectCard) => editDistance(target, card.path.toLowerCase()); return [...cards].sort((a, b) => distance(a) - distance(b) || newestFirst(a, b));}
/** Levenshtein distance between `a` and `b`. */function editDistance(a: string, b: string): number { const row = Array.from({ length: a.length + 1 }, (_, i) => i); for (let j = 1; j <= b.length; j++) { let diagonal = row[0]!; row[0] = j; for (let i = 1; i <= a.length; i++) { const above = row[i]!; row[i] = a[i - 1] === b[j - 1] ? diagonal : 1 + Math.min(diagonal, above, row[i - 1]!); diagonal = above; } } return row[a.length]!;}
export function previewPlan(project: Project, files: FileTree): PreviewPlan { const preview = project.record.preview; if (preview?.kind === "command" && preview.command) return { kind: "command", command: preview.command }; const entry = preview?.entry ?? "index.html"; return files.has(entry) ? { kind: "static", entry } : { kind: "none" };}
async function threadOf(list: readonly Comment[]): Promise<ThreadComment[]> { const authors = [...new Set(list.map((c) => c.author))]; const names = new Map(await Promise.all(authors.map(async (did) => [did, await linkName(did)] as const))); const present = new Set(list.map((c) => c.uri)); return list.map((comment) => ({ comment, authorName: names.get(comment.author)!, depth: comment.record.parent && present.has(comment.record.parent.uri) ? 1 : 0, }));}