Files
gitea-native/src/components/layout/query-list.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

151 lines
4.4 KiB
TypeScript

import { FlashList } from '@shopify/flash-list';
import type { ReactElement, ReactNode } from 'react';
import { ActivityIndicator, StyleSheet, View } from 'react-native';
import { useTranslation } from 'react-i18next';
import type { InfiniteData, UseInfiniteQueryResult } from '@tanstack/react-query';
import type { RawResponse } from '@/core/api/client';
import { apiErrorMessage } from '@/core/api/error-text';
import { ErrorState, LoadingView } from '@/components/ui/state-views';
import { Skeleton } from '@/components/ui/skeleton';
import { useTheme } from '@/theme/theme-provider';
import { spacing } from '@/theme/tokens';
type ListQuery<T> = UseInfiniteQueryResult<InfiniteData<RawResponse<T[]>>, Error>;
export interface QueryListProps<T> {
query: ListQuery<T>;
items: T[];
keyExtractor: (item: T, index: number) => string;
renderItem: (item: T, index: number) => ReactElement | null;
/** Rendered when the query succeeded with zero items. */
empty: ReactNode;
/** Rows of skeleton placeholders during the first load. */
loadingRows?: number;
header?: ReactElement;
footer?: ReactElement;
/** Extra bottom padding for the last item (avoids a clipped feel at scroll end). */
bottomInset?: number;
}
/**
* One list behaviour for the whole app:
* first-load skeleton → error+retry → empty state → FlashList with pull-to-refresh
* and infinite scroll (`onEndReached` guarded by `hasNextPage`).
*/
export function QueryList<T>({
query,
items,
keyExtractor,
renderItem,
empty,
loadingRows = 6,
header,
footer,
bottomInset = spacing.xl,
}: QueryListProps<T>) {
const { t } = useTranslation();
const { colors } = useTheme();
const hasData = query.data !== undefined;
if (!hasData && query.isPending) {
return (
<View style={styles.flex}>
{header}
<ListSkeleton rows={loadingRows} />
</View>
);
}
if (!hasData && query.isError) {
return (
<View style={styles.flex}>
{header}
<ErrorState
title={t('errors.title')}
body={apiErrorMessage(query.error, t)}
retryLabel={t('common.retry')}
onRetry={() => void query.refetch()}
/>
</View>
);
}
return (
<FlashList
data={items}
keyExtractor={keyExtractor}
renderItem={({ item, index }) => renderItem(item, index)}
onEndReachedThreshold={0.4}
onEndReached={() => {
if (query.hasNextPage && !query.isFetchingNextPage) void query.fetchNextPage();
}}
refreshing={query.isRefetching && !query.isFetchingNextPage}
onRefresh={() => void query.refetch()}
ListHeaderComponent={header}
ListEmptyComponent={query.isError ? (
<ErrorState
title={t('errors.title')}
body={apiErrorMessage(query.error, t)}
retryLabel={t('common.retry')}
onRetry={() => void query.refetch()}
/>
) : (
<EmptyWrapper>{empty}</EmptyWrapper>
)}
ListFooterComponent={
<>
{query.isFetchingNextPage ? (
<View style={styles.footerSpinner}>
<ActivityIndicator color={colors.primary} />
</View>
) : null}
{footer}
<View style={{ height: bottomInset }} />
</>
}
contentContainerStyle={items.length === 0 ? styles.grow : undefined}
showsVerticalScrollIndicator
/>
);
}
function EmptyWrapper({ children }: { children: ReactNode }) {
return <View style={styles.empty}>{children}</View>;
}
/** First-load placeholder rows (avatar/icon + two text lines). */
export function ListSkeleton({ rows = 6 }: { rows?: number }) {
return (
<View>
{Array.from({ length: rows }, (_, index) => (
<View key={index} style={styles.skeletonRow}>
<Skeleton width={36} height={36} circular />
<View style={styles.skeletonBody}>
<Skeleton width="72%" height={14} />
<Skeleton width="48%" height={12} />
</View>
</View>
))}
</View>
);
}
export const queryListLoadingView = LoadingView;
const styles = StyleSheet.create({
flex: { flex: 1 },
grow: { flexGrow: 1 },
empty: { flexGrow: 1, paddingTop: spacing.xxl },
footerSpinner: { paddingVertical: spacing.lg, alignItems: 'center' },
skeletonRow: {
flexDirection: 'row',
alignItems: 'center',
gap: spacing.md,
paddingHorizontal: spacing.lg,
paddingVertical: spacing.md,
},
skeletonBody: { flex: 1, gap: spacing.sm },
});