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 = UseInfiniteQueryResult>, Error>; export interface QueryListProps { query: ListQuery; 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({ query, items, keyExtractor, renderItem, empty, loadingRows = 6, header, footer, bottomInset = spacing.xl, }: QueryListProps) { const { t } = useTranslation(); const { colors } = useTheme(); const hasData = query.data !== undefined; if (!hasData && query.isPending) { return ( {header} ); } if (!hasData && query.isError) { return ( {header} void query.refetch()} /> ); } return ( 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 ? ( void query.refetch()} /> ) : ( {empty} )} ListFooterComponent={ <> {query.isFetchingNextPage ? ( ) : null} {footer} } contentContainerStyle={items.length === 0 ? styles.grow : undefined} showsVerticalScrollIndicator /> ); } function EmptyWrapper({ children }: { children: ReactNode }) { return {children}; } /** First-load placeholder rows (avatar/icon + two text lines). */ export function ListSkeleton({ rows = 6 }: { rows?: number }) { return ( {Array.from({ length: rows }, (_, index) => ( ))} ); } 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 }, });