import { isDid } from "@atcute/lexicons/syntax";
export interface ProfileLinkTarget {
href: string;
label: string;
external: boolean;
}
const ATPROTO_BROWSER = "https://pdsls.dev";
export const profileLinkTarget = (raw: string): ProfileLinkTarget | null => {
const link = raw.trim();
if (isDid(link)) return { href: `/${link}`, label: link, external: false };
if (link.startsWith("at://")) {
const [authority, ...rest] = link.slice("at://".length).split("/").filter(Boolean);
if (!authority) return null;
if (rest.length === 0) return { href: `/${authority}`, label: authority, external: false };
return {
href: `${ATPROTO_BROWSER}/${link}`,
label: link.slice("at://".length),
external: true
};
}
try {
const url = new URL(link);
if (url.protocol !== "http:" && url.protocol !== "https:") return null;
return { href: url.toString(), label: url.hostname, external: true };
} catch {
return null;
}
};