1- import { View , Text , FlatList , Pressable , ActivityIndicator } from "react-native" ;
21import { SafeAreaView } from "react-native-safe-area-context" ;
32import { useLocalSearchParams , useRouter , Stack } from "expo-router" ;
4- import { useQuery , useView } from "@objectstack/client-react" ;
3+ import { useQuery , useView , useFields } from "@objectstack/client-react" ;
4+ import { ListViewRenderer } from "~/components/renderers" ;
5+ import type { FieldDefinition , ListViewMeta } from "~/components/renderers" ;
56
67export default function ObjectListScreen ( ) {
78 const { appName, objectName } = useLocalSearchParams < {
@@ -11,6 +12,7 @@ export default function ObjectListScreen() {
1112 const router = useRouter ( ) ;
1213
1314 const { data : viewData , isLoading : viewLoading } = useView ( objectName ! , "list" ) ;
15+ const { data : fieldsData } = useFields ( objectName ! ) ;
1416 const { data, isLoading, error, refetch } = useQuery ( objectName ! , {
1517 top : 50 ,
1618 enabled : ! ! objectName ,
@@ -20,56 +22,25 @@ export default function ObjectListScreen() {
2022 objectName ?. replace ( / _ / g, " " ) . replace ( / \b \w / g, ( c ) => c . toUpperCase ( ) ) ?? "Objects" ;
2123
2224 const records = data ?. records ?? [ ] ;
25+ const fields : FieldDefinition [ ] = fieldsData ?? [ ] ;
26+ const listView : ListViewMeta | undefined = viewData ?? undefined ;
2327
2428 return (
2529 < SafeAreaView className = "flex-1 bg-background" edges = { [ "left" , "right" ] } >
2630 < Stack . Screen options = { { title : displayName } } />
27- { isLoading || viewLoading ? (
28- < View className = "flex-1 items-center justify-center" >
29- < ActivityIndicator size = "large" color = "#1e40af" />
30- </ View >
31- ) : error ? (
32- < View className = "flex-1 items-center justify-center px-6" >
33- < Text className = "text-base text-destructive" > { error . message } </ Text >
34- < Pressable
35- className = "mt-4 rounded-xl bg-primary px-5 py-3"
36- onPress = { refetch }
37- >
38- < Text className = "font-semibold text-primary-foreground" > Retry</ Text >
39- </ Pressable >
40- </ View >
41- ) : (
42- < FlatList
43- data = { records }
44- keyExtractor = { ( item : any , index : number ) => item . id ?? item . _id ?? String ( index ) }
45- contentContainerStyle = { { padding : 20 , paddingBottom : 40 } }
46- renderItem = { ( { item } : { item : any } ) => (
47- < Pressable
48- className = "mb-2 rounded-xl border border-border bg-card p-4"
49- onPress = { ( ) =>
50- router . push ( `/(app)/${ appName } /${ objectName } /${ item . id ?? item . _id } ` )
51- }
52- >
53- < Text className = "text-base font-medium text-card-foreground" >
54- { item . name ?? item . label ?? item . title ?? item . subject ?? item . id ?? "Record" }
55- </ Text >
56- { item . description ? (
57- < Text className = "mt-1 text-sm text-muted-foreground" numberOfLines = { 2 } >
58- { item . description }
59- </ Text >
60- ) : null }
61- </ Pressable >
62- ) }
63- ListEmptyComponent = {
64- < View className = "items-center justify-center pt-20" >
65- < Text className = "text-lg font-semibold text-foreground" > No Records</ Text >
66- < Text className = "mt-2 text-sm text-muted-foreground" >
67- No records found for this object.
68- </ Text >
69- </ View >
70- }
71- />
72- ) }
31+ < ListViewRenderer
32+ view = { listView }
33+ fields = { fields }
34+ records = { records }
35+ isLoading = { isLoading || viewLoading }
36+ error = { error }
37+ onRefresh = { refetch }
38+ onRowPress = { ( record ) =>
39+ router . push (
40+ `/(app)/${ appName } /${ objectName } /${ ( record . id ?? record . _id ) as string } ` ,
41+ )
42+ }
43+ />
7344 </ SafeAreaView >
7445 ) ;
7546}
0 commit comments