import { ExtendedKind } from '@/constants' import { getGitRepublicRepoContext, gitRepublicRepoWebUrl, type GitRepublicRepoContext } from '@/lib/git-republic-event' import { cn } from '@/lib/utils' import { Event, nip19 } from 'nostr-tools' import { useMemo } from 'react' import { useTranslation } from 'react-i18next' import { ExternalLink, GitBranch, CircleDot, Tag } from 'lucide-react' import MarkdownArticle from './MarkdownArticle/MarkdownArticle' function repoHeadline(ctx: GitRepublicRepoContext): string { const name = ctx.displayName || ctx.repoId try { const npub = nip19.npubEncode(ctx.ownerHex) const short = `${npub.slice(0, 14)}…` return `${short} / ${name}` } catch { return name } } export default function GitRepublicEventCard({ event, className, variant = 'full' }: { event: Event className?: string variant?: 'full' | 'compact' }) { const { t } = useTranslation() const ctx = useMemo(() => getGitRepublicRepoContext(event), [event]) const webUrl = useMemo(() => (ctx ? gitRepublicRepoWebUrl(ctx) : null), [ctx]) const subject = event.tags.find((t) => t[0] === 'subject')?.[1] const titleTag = event.tags.find((t) => t[0] === 'title')?.[1] const tagName = event.tags.find((t) => t[0] === 'tag')?.[1] const description = event.kind === ExtendedKind.GIT_REPO_ANNOUNCEMENT ? event.tags.find((t) => t[0] === 'description')?.[1] : undefined const isDraft = event.tags.some((t) => t[0] === 'draft' && t[1] === 'true') const isPrerelease = event.tags.some((t) => t[0] === 'prerelease' && t[1] === 'true') const { Icon, badge, headline } = useMemo(() => { if (event.kind === ExtendedKind.GIT_REPO_ANNOUNCEMENT) { const name = event.tags.find((t) => t[0] === 'name')?.[1] || ctx?.repoId || t('Git Republic repository') return { Icon: GitBranch, badge: t('Git Republic repository'), headline: name } } if (event.kind === ExtendedKind.GIT_ISSUE) { return { Icon: CircleDot, badge: t('Git Republic issue'), headline: subject || t('Git Republic issue') } } if (event.kind === ExtendedKind.GIT_RELEASE) { const h = titleTag || tagName || t('Git Republic release') return { Icon: Tag, badge: t('Git Republic release'), headline: h } } return { Icon: GitBranch, badge: t('Git Republic'), headline: t('Git Republic event') } }, [event, ctx?.repoId, subject, tagName, titleTag, t]) const body = event.kind === ExtendedKind.GIT_REPO_ANNOUNCEMENT ? description || event.content : event.content const compact = variant === 'compact' return (
{badge} {isDraft ? ( {t('Draft')} ) : null} {isPrerelease ? ( {t('Pre-release')} ) : null}
{compact && (isDraft || isPrerelease) ? (
{isDraft ? ( {t('Draft')} ) : null} {isPrerelease ? ( {t('Pre-release')} ) : null}
) : null}

{headline}

{event.kind === ExtendedKind.GIT_RELEASE && tagName ? (

{tagName}

) : null} {ctx ? (

{repoHeadline(ctx)}

) : null} {webUrl ? ( e.stopPropagation()} > {t('Open in Git Republic')} ) : null}
{body.trim() ? (
) : null}
) }