import { memo, useMemo } from "react"; import { Terminal } from "lucide-react"; import { Avatar, AvatarFallback } from "@/components/ui/avatar"; import { Badge } from "@/components/ui/badge"; import type { CompanyUserProfile } from "../lib/company-members"; import { cn } from "../lib/utils"; function initialsForProfile(profile: string): string { const parts = profile.trim().split(/[\s_-]+/).filter(Boolean); if (parts.length >= 2) { return `${parts[0]![0] ?? ""}${parts[parts.length - 1]![0] ?? ""}`.toUpperCase(); } return profile.trim().slice(0, 2).toUpperCase() || "PR"; } export interface ResolvedActorProfileAttribution { profile: string; viaUserLabel: string | null; isEphemeralProfile: boolean; } /** * Resolves `--as ` comment attribution so profile-attributed comments * render ` via ` (delegated human session) or * `` + `profile` pill (`local-board` / unauthenticated local mode) * instead of `local-board`, `You`, or `User`. */ export function resolveActorProfileAttribution(input: { actorProfile?: string | null; authorUserId?: string | null; userLabelMap?: ReadonlyMap | null; userProfileMap?: ReadonlyMap | null; }): ResolvedActorProfileAttribution | null { const profile = input.actorProfile?.trim(); if (!profile) return null; const rawUserId = input.authorUserId?.trim() || null; if (!rawUserId || rawUserId === "local-board") { return { profile, viaUserLabel: null, isEphemeralProfile: true, }; } const mappedProfileLabel = input.userProfileMap?.get(rawUserId)?.label?.trim(); const mappedUserLabel = input.userLabelMap?.get(rawUserId)?.trim(); const fallbackUserLabel = rawUserId.startsWith("user-") ? rawUserId.slice("user-".length) : rawUserId; return { profile, viaUserLabel: mappedProfileLabel || mappedUserLabel || fallbackUserLabel, isEphemeralProfile: false, }; } export const ActorProfileIdentity = memo(function ActorProfileIdentity({ attribution, showAvatar = true, className, }: { attribution: ResolvedActorProfileAttribution; showAvatar?: boolean; className?: string; }) { return ( {showAvatar ? ( {initialsForProfile(attribution.profile)} ) : null} {attribution.profile} {attribution.viaUserLabel ? ( via {attribution.viaUserLabel} ) : ( )} ); }); export const CommentKvLabelsBar = memo(function CommentKvLabelsBar({ kvLabels, className, }: { kvLabels?: Record | null; className?: string; }) { const entries = useMemo(() => { if (!kvLabels) return []; return Object.entries(kvLabels).filter( ([key, value]) => typeof key === "string" && key.length > 0 && typeof value === "string", ); }, [kvLabels]); if (entries.length === 0) return null; return (
{entries.map(([key, value]) => ( {key} = {value} ))}
); });