/* Copyright (c) 2025 SharpMars Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ import { Client, simpleFetchHandler } from "@atcute/client"; import type { RecordKey } from "@atcute/lexicons"; import { createResource, For, Show, Suspense } from "solid-js"; import type { PubLeafletComment, PubLeafletRichtextFacet } from "@atcute/leaflet"; import { formatDistance } from "date-fns"; export function LeafletComments(props: { rkey: RecordKey }) { const leafletUrl = "https://sharpmars.leaflet.pub/"; const atUriPrefix = "at://did:plc:irx36xprktslecsbopbwnh5w/pub.leaflet.document/"; const splitTextByFacet = (text: string, facets?: PubLeafletRichtextFacet.Main[]) => { if (!facets || facets.length == 0) return text; const sortedFacets = facets.toSorted((a, b) => a.index.byteStart - b.index.byteStart); const encoder = new TextEncoder(); const decoder = new TextDecoder(); const encodedText = encoder.encode(text); const indexSet = new Set(); indexSet.add(0); indexSet.add(encodedText.length); for (const facet of sortedFacets) { indexSet.add(facet.index.byteStart); indexSet.add(facet.index.byteEnd); } const indexes = [...indexSet].sort((a, b) => a - b); const ranges: { start: number; end: number }[] = []; for (let i = 0; i < indexSet.size - 1; i++) { ranges.push({ start: indexes[i], end: indexes[i + 1] }); } const segments = ranges.map((range) => { const segmentFacets = sortedFacets .filter((val) => val.index.byteStart >= range.start && val.index.byteEnd <= range.end) .flatMap((val) => val.features) .map((val) => val.$type); return ( {decoder.decode(encodedText.subarray(range.start, range.end))} ); }); return segments; }; const [comments] = createResource(async () => { const atUri = atUriPrefix + props.rkey; let cursor = null; const records = []; do { const res = await ( await fetch( `https://constellation.microcosm.blue/xrpc/blue.microcosm.links.getBacklinks?subject=${atUri}&source=pub.leaflet.comment:subject&limit=100` ) ).json(); records.push(...res.records); cursor = res.cursor; } while (cursor); const slingshot = new Client({ handler: simpleFetchHandler({ service: "https://slingshot.microcosm.blue" }) }); const comments: { handle: string; createdAt: string; text: ReturnType; attachment: boolean; }[] = []; for (const record of records) { const commentRes = await slingshot.get("com.atproto.repo.getRecord", { params: { rkey: record.rkey, collection: record.collection, repo: record.did }, headers: [["Accept", "application/json"]], }); const handleRes = await slingshot.get("com.bad-example.identity.resolveMiniDoc", { params: { identifier: record.did }, }); const handle = handleRes.ok ? handleRes.data.handle : record.did; if (commentRes.ok) { const comment = commentRes.data.value as PubLeafletComment.Main; comments.push({ handle: handle ? handle : record.did, createdAt: comment.createdAt, text: splitTextByFacet(comment.plaintext, comment.facets), attachment: !!comment.attachment, }); } } return comments; }); return (

Comments

To comment or see them in their original form check out{" "} leaflet.pub

{(comment, i) => (

{comment.handle}

{formatDistance(new Date(comment.createdAt), new Date(), { addSuffix: true })}

Check quote on{" "} leaflet.pub

{comment.text}

)}
); }