import { Pressable, StyleSheet, View } from 'react-native'; import { useTranslation } from 'react-i18next'; import { AppText } from '@/components/ui/app-text'; import { Icon, type IconName } from '@/components/ui/icon'; import type { NotificationSubjectType, NotificationThread } from '@/core/api/types'; import { formatRelativeTime } from '@/core/utils/format'; import { useTheme } from '@/theme/theme-provider'; import { HIT_TARGET, radius, spacing } from '@/theme/tokens'; export type NotificationTarget = | { kind: 'issue'; owner: string; repo: string; index: number } | { kind: 'pull'; owner: string; repo: string; index: number } | { kind: 'commit'; owner: string; repo: string; sha: string } | { kind: 'repo'; owner: string; repo: string }; /** * Extracts a navigation target from a notification subject URL. * Accepts both API paths (`/api/v1/repos/…`) and absolute instance URLs. */ export function parseSubjectUrl(url: string | undefined | null): NotificationTarget | null { if (!url) return null; let path = url; try { const parsed = new URL(url); path = parsed.pathname; } catch { // Relative path — use as-is. } const match = path.match( /^\/(?:api\/v1\/)?(?:repos\/)?([^/]+)\/([^/]+?)(?:\/(issues|pulls|commits?)\/([^/?#]+))?$/, ); if (!match) return null; const [, owner, repo, section, tail] = match; if (section === 'issues') { const index = Number(tail); return Number.isInteger(index) && index > 0 ? { kind: 'issue', owner, repo, index } : null; } if (section === 'pulls') { const index = Number(tail); return Number.isInteger(index) && index > 0 ? { kind: 'pull', owner, repo, index } : null; } if (section === 'commits' && tail) return { kind: 'commit', owner, repo, sha: tail }; if (section === 'commit' && tail) return { kind: 'commit', owner, repo, sha: tail }; return { kind: 'repo', owner, repo }; } function iconForType(type: NotificationSubjectType | undefined): IconName { switch (type) { case 'Issue': return 'issue'; case 'Pull': return 'pullRequest'; case 'Commit': return 'commit'; default: return 'repo'; } } export interface NotificationRowProps { thread: NotificationThread; onPress: () => void; } /** Inbox row: unread indicator, subject, repository and relative time. */ export function NotificationRow({ thread, onPress }: NotificationRowProps) { const { t } = useTranslation(); const { colors } = useTheme(); const unread = thread.unread === true; const type = thread.subject?.type; const typeLabel = { Issue: t('inbox.typeIssue'), Pull: t('inbox.typePull'), Commit: t('inbox.typeCommit'), Repository: t('inbox.typeRepository'), }[type ?? 'Repository']; return ( [ styles.row, { borderBottomColor: colors.border }, pressed && { backgroundColor: colors.surfacePressed }, ]} > {thread.subject?.title ?? thread.repository?.full_name ?? ''} {[ thread.repository?.full_name, typeLabel, thread.updated_at ? formatRelativeTime(thread.updated_at, t) : null, ] .filter(Boolean) .join(' · ')} {unread ? {t('inbox.unread')} : null} ); } const styles = StyleSheet.create({ row: { flexDirection: 'row', alignItems: 'center', gap: spacing.sm, minHeight: HIT_TARGET, paddingVertical: spacing.md, paddingHorizontal: spacing.lg, borderBottomWidth: StyleSheet.hairlineWidth, }, unreadDot: { width: 8, height: 8, borderRadius: radius.pill, }, iconBox: { width: 32, height: 32, borderRadius: radius.md, alignItems: 'center', justifyContent: 'center', }, body: { flex: 1, gap: 2 }, });