Files
gitea-native/src/components/git/notification-row.tsx
T
JustZoe101 08229fdf86 feat(ui): component kit, layout primitives and git components
- UI kit: button, card, field, avatar, badge, list-item, segmented, skeleton,
  empty/error/loading state views, icon + icon-button, search bar
- layout: Screen, SectionHeader, header buttons, QueryList with pull-to-refresh
- git components: repo list item, file rows/breadcrumbs, commit row, issue row,
  native Markdown renderer, notification row with subject-URL parsing
2026-10-09 16:41:17 +02:00

145 lines
4.6 KiB
TypeScript

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 (
<Pressable
onPress={onPress}
accessibilityRole="button"
accessibilityLabel={`${thread.subject?.title ?? ''} ${thread.repository?.full_name ?? ''}`}
style={({ pressed }) => [
styles.row,
{ borderBottomColor: colors.border },
pressed && { backgroundColor: colors.surfacePressed },
]}
>
<View
style={[
styles.unreadDot,
{ backgroundColor: unread ? colors.primary : 'transparent' },
]}
accessibilityElementsHidden
importantForAccessibility="no"
/>
<View style={[styles.iconBox, { backgroundColor: colors.surfaceSecondary }]}>
<Icon name={iconForType(type)} size={18} color={colors.textSecondary} />
</View>
<View style={styles.body}>
<AppText variant="bodyMedium" numberOfLines={2} style={!unread && { opacity: 0.85 }}>
{thread.subject?.title ?? thread.repository?.full_name ?? ''}
</AppText>
<AppText variant="caption" tone="tertiary" numberOfLines={1}>
{[
thread.repository?.full_name,
typeLabel,
thread.updated_at ? formatRelativeTime(thread.updated_at, t) : null,
]
.filter(Boolean)
.join(' · ')}
</AppText>
</View>
{unread ? <AppText variant="caption" tone="primary">{t('inbox.unread')}</AppText> : null}
</Pressable>
);
}
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 },
});