import * as TID from "@atcute/tid";
import { createResource, createSignal, For, onMount, Show } from "solid-js";
import {
getAllBacklinks,
getDidBacklinks,
getRecordBacklinks,
LinksWithDids,
LinksWithRecords,
} from "../utils/api.js";
import { localDateFromTimestamp } from "../utils/date.js";
import { Button } from "./button.jsx";
type Backlink = {
path: string;
counts: { distinct_dids: number; records: number };
};
const linksBySource = (links: Record) => {
let out: Record = {};
Object.keys(links)
.toSorted()
.forEach((collection) => {
const paths = links[collection];
Object.keys(paths)
.toSorted()
.forEach((path) => {
if (paths[path].records === 0) return;
if (out[collection]) out[collection].push({ path, counts: paths[path] });
else out[collection] = [{ path, counts: paths[path] }];
});
});
return out;
};
const Backlinks = (props: { target: string }) => {
const fetchBacklinks = async () => {
const res = await getAllBacklinks(props.target);
return linksBySource(res.links);
};
const [response] = createResource(fetchBacklinks);
const [show, setShow] = createSignal<{
collection: string;
path: string;
showDids: boolean;
} | null>();
return (
No backlinks found.}
>
{(collection) => (
{collection}
{({ path, counts }) => (
{path.slice(1)}
{" from "}
Distinct identities
Records
)}
)}
);
};
// switching on !!did everywhere is pretty annoying, this could probably be two components
// but i don't want to duplicate or think about how to extract the paging logic
const BacklinkItems = ({
target,
collection,
path,
dids,
cursor,
}: {
target: string;
collection: string;
path: string;
dids: boolean;
cursor?: string;
}) => {
const [links, setLinks] = createSignal();
const [more, setMore] = createSignal(false);
onMount(async () => {
const links = await (dids ? getDidBacklinks : getRecordBacklinks)(
target,
collection,
path,
cursor,
);
setLinks(links);
});
// TODO: could pass the `total` into this component, which can be checked against each call to this endpoint to find if it's stale.
// also hmm 'total' is misleading/wrong on that api
return (
Loading…