|
| 1 | +/** |
| 2 | + * Renders an atproto publisher's identity, with three branches: |
| 3 | + * |
| 4 | + * - **Verified handle**: shows `@handle`. Either the aggregator |
| 5 | + * already resolved the handle at ingest (we trust that), or our |
| 6 | + * local `LocalActorResolver` round-tripped the DID document's |
| 7 | + * `alsoKnownAs` back to the same DID. |
| 8 | + * - **Unverified publisher**: DID document claims a handle but the |
| 9 | + * handle's domain doesn't point back to the same DID. Treat as |
| 10 | + * untrusted -- the publisher might be impersonating someone else. |
| 11 | + * Surface as `Unverified publisher` in error styling. Callers |
| 12 | + * should also disable destructive actions (install, etc.). |
| 13 | + * - **Missing handle**: no claimed handle at all (or DID document |
| 14 | + * resolution failed entirely). Fall back to the raw DID. |
| 15 | + * |
| 16 | + * `aggregatorHandle` is what the registry's `searchPackages` / |
| 17 | + * `resolvePackage` endpoint returned for this DID -- best-effort, may |
| 18 | + * be `null`. When absent, this component falls back to a per-DID |
| 19 | + * `LocalActorResolver` lookup via `resolveDidToHandle`, cached in |
| 20 | + * localStorage for 24h so repeat renders don't refetch. |
| 21 | + */ |
| 22 | + |
| 23 | +import { useLingui } from "@lingui/react/macro"; |
| 24 | +import { useQuery } from "@tanstack/react-query"; |
| 25 | +import * as React from "react"; |
| 26 | + |
| 27 | +import { resolveDidToHandle } from "../lib/api/registry.js"; |
| 28 | + |
| 29 | +export type PublisherHandleStatus = "ok" | "invalid" | "missing"; |
| 30 | + |
| 31 | +export interface PublisherHandleResult { |
| 32 | + status: PublisherHandleStatus; |
| 33 | + /** Verified handle (only present when `status === "ok"`). */ |
| 34 | + handle?: string; |
| 35 | +} |
| 36 | + |
| 37 | +export interface PublisherHandleProps { |
| 38 | + did: string; |
| 39 | + aggregatorHandle?: string | null; |
| 40 | + /** |
| 41 | + * Called every time the resolution status changes, so callers can |
| 42 | + * gate install buttons or other side effects on |
| 43 | + * `status === "invalid"`. Optional. |
| 44 | + */ |
| 45 | + onResolved?: (result: PublisherHandleResult) => void; |
| 46 | + /** Visual variant. `card` is the smaller list-item form. */ |
| 47 | + variant?: "card" | "detail"; |
| 48 | + className?: string; |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Hook form: returns the same tri-state result without rendering. Use |
| 53 | + * when a parent needs to coordinate UI (e.g. disable install) based on |
| 54 | + * the resolution. |
| 55 | + */ |
| 56 | +export function usePublisherHandle( |
| 57 | + did: string, |
| 58 | + aggregatorHandle?: string | null, |
| 59 | +): PublisherHandleResult { |
| 60 | + const { data: didHandleResolution } = useQuery({ |
| 61 | + queryKey: ["registry", "did-handle", did], |
| 62 | + queryFn: () => resolveDidToHandle(did), |
| 63 | + enabled: Boolean(did) && !aggregatorHandle, |
| 64 | + staleTime: 5 * 60 * 1000, |
| 65 | + }); |
| 66 | + |
| 67 | + if (aggregatorHandle) return { status: "ok", handle: aggregatorHandle }; |
| 68 | + if (!didHandleResolution) return { status: "missing" }; |
| 69 | + if (didHandleResolution.status === "ok") { |
| 70 | + return { status: "ok", handle: didHandleResolution.handle }; |
| 71 | + } |
| 72 | + return { status: didHandleResolution.status }; |
| 73 | +} |
| 74 | + |
| 75 | +export function PublisherHandle({ |
| 76 | + did, |
| 77 | + aggregatorHandle, |
| 78 | + onResolved, |
| 79 | + variant = "card", |
| 80 | + className, |
| 81 | +}: PublisherHandleProps) { |
| 82 | + const { t } = useLingui(); |
| 83 | + const result = usePublisherHandle(did, aggregatorHandle); |
| 84 | + |
| 85 | + // Notify the caller every time the result changes. Effect (not |
| 86 | + // inline) so we don't re-fire on every parent re-render. |
| 87 | + const onResolvedRef = React.useRef(onResolved); |
| 88 | + onResolvedRef.current = onResolved; |
| 89 | + React.useEffect(() => { |
| 90 | + onResolvedRef.current?.(result); |
| 91 | + }, [result.status, result.handle]); |
| 92 | + |
| 93 | + const textClass = variant === "card" ? "text-xs" : "text-sm"; |
| 94 | + |
| 95 | + if (result.status === "ok" && result.handle) { |
| 96 | + return ( |
| 97 | + <span className={`truncate ${textClass} text-kumo-subtle ${className ?? ""}`}> |
| 98 | + @{result.handle} |
| 99 | + </span> |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + if (result.status === "invalid") { |
| 104 | + return ( |
| 105 | + <span className={`truncate ${textClass} font-medium text-kumo-error ${className ?? ""}`}> |
| 106 | + {t`Unverified publisher`} |
| 107 | + </span> |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + return <span className={`truncate ${textClass} text-kumo-subtle ${className ?? ""}`}>{did}</span>; |
| 112 | +} |
0 commit comments