a tool for shared writing and social publishing
1import { AtUri } from "@atproto/api";
2import {
3 isDocumentCollection,
4 isPublicationCollection,
5} from "src/utils/collectionHelpers";
6
7/**
8 * Converts a DID to a Bluesky profile URL
9 */
10export function didToBlueskyUrl(did: string): string {
11 return `https://bsky.app/profile/${did}`;
12}
13
14/**
15 * Converts an AT URI (publication or document) to the appropriate URL
16 */
17export function atUriToUrl(atUri: string): string {
18 try {
19 const uri = new AtUri(atUri);
20
21 if (isPublicationCollection(uri.collection)) {
22 return `/lish/uri/${encodeURIComponent(atUri)}`;
23 } else if (isDocumentCollection(uri.collection)) {
24 return `/lish/uri/${encodeURIComponent(atUri)}`;
25 }
26
27 return "#";
28 } catch (e) {
29 console.error("Failed to parse AT URI:", atUri, e);
30 return "#";
31 }
32}
33
34/**
35 * Opens a mention link in the appropriate way
36 * - DID mentions open in a new tab (external Bluesky)
37 * - Publication/document mentions navigate in the same tab
38 */
39export function handleMentionClick(
40 e: MouseEvent | React.MouseEvent,
41 type: "did" | "at-uri",
42 value: string,
43) {
44 e.preventDefault();
45 e.stopPropagation();
46
47 if (type === "did") {
48 // Open Bluesky profile in new tab
49 window.open(didToBlueskyUrl(value), "_blank", "noopener,noreferrer");
50 } else {
51 // Navigate to publication/document in same tab
52 const url = atUriToUrl(value);
53 if (url.startsWith("/lish/uri/")) {
54 // Redirect route - navigate to it
55 window.location.href = url;
56 } else {
57 window.location.href = url;
58 }
59 }
60}