a tool for shared writing and social publishing
1import { AtUri } from "@atproto/syntax";
2import { PubLeafletPublication } from "lexicons/api";
3import { isProductionDomain } from "src/utils/isProductionDeployment";
4import { Json } from "supabase/database.types";
5import {
6 normalizePublicationRecord,
7 isLeafletPublication,
8 type NormalizedPublication,
9} from "src/utils/normalizeRecords";
10
11type PublicationInput =
12 | { uri: string; record: Json | NormalizedPublication | null }
13 | { uri: string; record: unknown };
14
15/**
16 * Gets the public URL for a publication.
17 * Works with both pub.leaflet.publication and site.standard.publication records.
18 */
19export function getPublicationURL(pub: PublicationInput): string {
20 const normalized = normalizePublicationRecord(pub.record);
21
22 // If we have a normalized record with a URL (site.standard format), use it
23 if (normalized?.url && isProductionDomain()) {
24 return normalized.url;
25 }
26
27 // Fall back to checking raw record for legacy base_path
28 if (
29 isLeafletPublication(pub.record) &&
30 pub.record.base_path &&
31 isProductionDomain()
32 ) {
33 return `https://${pub.record.base_path}`;
34 }
35
36 return getBasePublicationURL(pub);
37}
38
39export function getBasePublicationURL(pub: PublicationInput): string {
40 const normalized = normalizePublicationRecord(pub.record);
41 const aturi = new AtUri(pub.uri);
42
43 //use rkey, fallback to name
44 const name = aturi.rkey || normalized?.name;
45 return `/lish/${aturi.host}/${encodeURIComponent(name || "")}`;
46}