Something went wrong. Try again.
Streams is a chronological ATProto PDS explorer. streams.synthetic.software
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163<script lang="ts"> import { fetchRecords, getCollections, resolveToEndpoint } from '$lib/atproto/agent'; import { getComponent } from '$lib/lexicons'; import { SvelteMap } from 'svelte/reactivity'; import { setContext } from 'svelte'; import RecordRow from './RecordRow.svelte';
interface Props { handleOrDid: string; mode: 'timeline' | 'raw'; allExpanded: boolean; }
interface RecordEntry { uri: string; cid: string; value: { $type: string; createdAt?: string; [key: string]: unknown }; }
const DISPLAY_PAGE = 50; const FETCH_PER_COLLECTION = 20;
let { handleOrDid, mode, allExpanded }: Props = $props();
let records: RecordEntry[] = $state([]); let loading = $state(false); let loadingMore = $state(false); let error: string | null = $state(null); let hasMore = $state(false);
// Internal buffer — fetched but not yet displayed let buffer: RecordEntry[] = []; let cursors = new Map<string, string>(); let pdsUrl = $state(''); let did = ''; let collections: string[] = [];
// Expose pdsUrl to descendant components that need to construct blob URLs setContext('pdsUrl', { get url() { return pdsUrl; } });
function byDate(a: RecordEntry, b: RecordEntry) { const ba = b.value.createdAt ? new Date(b.value.createdAt).getTime() : 0; const aa = a.value.createdAt ? new Date(a.value.createdAt).getTime() : 0; return ba - aa; }
async function fetchIntoBuffer() { if (!pdsUrl) return; const targets = cursors.size > 0 ? [...cursors.keys()] : collections; const nextCursors = new SvelteMap<string, string>();
await Promise.all( targets.map(async (collection) => { const cursor = cursors.get(collection); const data = await fetchRecords(pdsUrl, did, collection, FETCH_PER_COLLECTION, cursor); for (const r of data.records) { buffer.push({ uri: r.uri, cid: r.cid, value: r.value as RecordEntry['value'] }); } if (data.cursor && data.records.length === FETCH_PER_COLLECTION) { nextCursors.set(collection, data.cursor); } }) );
buffer.sort(byDate); cursors = nextCursors; }
function drainBuffer() { const next = buffer.splice(0, DISPLAY_PAGE); records = [...records, ...next].sort(byDate); hasMore = buffer.length > 0 || cursors.size > 0; }
async function loadStream(target: string) { if (!target) return; loading = true; error = null; records = []; buffer = []; cursors = new Map(); pdsUrl = ''; did = ''; collections = []; hasMore = false;
try { const resolved = await resolveToEndpoint(target); did = resolved.did; pdsUrl = resolved.pdsUrl;
collections = await getCollections(pdsUrl, did);
await fetchIntoBuffer(); drainBuffer(); } catch (e) { error = e instanceof Error ? e.message : String(e); } finally { loading = false; } }
async function loadMore() { if (loadingMore) return; loadingMore = true; try { if (buffer.length < DISPLAY_PAGE && cursors.size > 0) { await fetchIntoBuffer(); } drainBuffer(); } catch (e) { error = e instanceof Error ? e.message : String(e); } finally { loadingMore = false; } }
const timelineVisible = $derived( records.filter((r) => getComponent(r.value.$type) !== null).length );
$effect(() => { if (handleOrDid) loadStream(handleOrDid); });</script>
{#if loading} <p class="opacity-60">Loading…</p>{:else if error} <p class="text-red">{error}</p>{:else if records.length === 0 && handleOrDid} <p class="stream-status">No records found.</p>{:else if mode === 'timeline' && !hasMore && !loadingMore && timelineVisible === 0 && records.length > 0} <p class="stream-status"> No supported record types in this timeline. {records.length} {records.length === 1 ? 'record' : 'records'} found — switch to raw view to inspect them. </p>{/if}
{#each records as entry (entry.uri)} <RecordRow record={entry.value} uri={entry.uri} {mode} {allExpanded} />{/each}
{#if hasMore} <span class="my-4 flex w-full justify-center"> <button class="uppercase {loadingMore ? 'disabled' : 'cursor-pointer underline'}" onclick={loadMore} disabled={loadingMore} > {loadingMore ? 'Loading…' : 'Load more'} </button> </span>{/if}