issue(28): fix partial search and prevent blank screen in mobile faceted search

This commit is contained in:
ShivaniKhaire 2026-10-30 08:29:03 +05:30
parent b520220e1c
commit 0b8d2c7c48
3 changed files with 335 additions and 9 deletions

View File

@ -1,12 +1,300 @@
import React from "react";
import { ActivityIndicator, FlatList, Pressable, StyleSheet, Text, View } from "react-native";
import {
ActivityIndicator,
FlatList,
Pressable,
ScrollView,
StyleSheet,
Text,
View,
} from "react-native";
import { Ionicons } from "@expo/vector-icons";
import { Colors } from "@/theme";
import type { FacetedSearchItem } from "../types/search.types";
import type { FacetedSearchItem, SearchModuleOption } from "../types/search.types";
interface Props { items: FacetedSearchItem[]; total: number; loading: boolean; fetchingMore: boolean; onLoadMore:()=>void; onPress:(item:FacetedSearchItem)=>void; }
export function SearchResultList({items,total,loading,fetchingMore,onLoadMore,onPress}:Props){
if(loading&&!items.length) return <View style={styles.center}><ActivityIndicator color={Colors.primary}/><Text style={styles.muted}>Searching content…</Text></View>;
return <FlatList data={items} keyExtractor={(item)=>item.content_id} contentContainerStyle={items.length?styles.list:styles.empty} ListHeaderComponent={items.length?<Text style={styles.count}>{total} result{total===1?"":"s"}</Text>:null} renderItem={({item})=><Pressable onPress={()=>onPress(item)} style={({pressed})=>[styles.card,pressed&&styles.pressed]}><View style={styles.cardText}><Text style={styles.title}>{item.title}</Text><Text style={styles.meta}>{item.module_name} · {item.template_name}</Text>{item.summary?<Text numberOfLines={2} style={styles.summary}>{item.summary}</Text>:null}</View><Ionicons name="chevron-forward" size={20} color={Colors.textSecondary}/></Pressable>} ListEmptyComponent={<View style={styles.center}><Ionicons name="search-outline" size={30} color={Colors.textSecondary}/><Text style={styles.emptyTitle}>No matching content</Text><Text style={styles.muted}>Try a broader scope or remove one of the filters.</Text></View>} ListFooterComponent={fetchingMore?<ActivityIndicator style={{margin:18}} color={Colors.primary}/>:null} onEndReached={onLoadMore} onEndReachedThreshold={.35}/>;
interface Props {
items: FacetedSearchItem[];
total: number;
loading: boolean;
fetchingMore: boolean;
onLoadMore: () => void;
onPress: (item: FacetedSearchItem) => void;
matchingModules?: SearchModuleOption[];
onSelectModule?: (module: SearchModuleOption) => void;
activeModuleCode?: string | null;
}
const styles=StyleSheet.create({list:{paddingHorizontal:20,paddingBottom:28},empty:{flexGrow:1},count:{paddingVertical:9,fontSize:12,fontWeight:"700",color:Colors.textSecondary},card:{flexDirection:"row",alignItems:"center",gap:12,padding:15,marginBottom:10,borderRadius:16,backgroundColor:Colors.white,borderWidth:1,borderColor:"#EEF0F2",shadowColor:"#000",shadowOpacity:.05,shadowRadius:6,shadowOffset:{width:0,height:2},elevation:1},pressed:{opacity:.75},cardText:{flex:1},title:{fontSize:16,fontWeight:"800",color:Colors.textPrimary},meta:{marginTop:3,fontSize:11.5,fontWeight:"600",color:Colors.primaryDark},summary:{marginTop:6,fontSize:12.5,lineHeight:18,color:Colors.textSecondary},center:{flex:1,alignItems:"center",justifyContent:"center",padding:32},emptyTitle:{marginTop:8,fontSize:16,fontWeight:"800",color:Colors.textPrimary},muted:{marginTop:5,textAlign:"center",fontSize:12.5,color:Colors.textSecondary}});
export function SearchResultList({
items,
total,
loading,
fetchingMore,
onLoadMore,
onPress,
matchingModules = [],
onSelectModule,
activeModuleCode,
}: Props) {
if (loading && !items.length && !matchingModules.length) {
return (
<View style={styles.center}>
<ActivityIndicator color={Colors.primary} />
<Text style={styles.muted}>Searching content…</Text>
</View>
);
}
const headerComponent = (
<View style={styles.headerWrapper}>
{matchingModules.length > 0 ? (
<View style={styles.modulesSection}>
<Text style={styles.modulesHeading}>
Matching Modules ({matchingModules.length})
</Text>
<ScrollView
horizontal
showsHorizontalScrollIndicator={false}
contentContainerStyle={styles.modulesScroll}
>
{matchingModules.map((mod) => {
const isActive = activeModuleCode === mod.code;
return (
<Pressable
key={mod.code}
style={[
styles.moduleCard,
isActive && styles.moduleCardActive,
]}
onPress={() => onSelectModule?.(mod)}
>
<View style={styles.moduleIconBox}>
<Ionicons
name="layers-outline"
size={16}
color={isActive ? Colors.white : Colors.primaryDark}
/>
</View>
<View style={styles.moduleTextContainer}>
<Text
numberOfLines={1}
style={[
styles.moduleName,
isActive && styles.moduleNameActive,
]}
>
{mod.name}
</Text>
<Text
style={[
styles.moduleBadge,
isActive && styles.moduleBadgeActive,
]}
>
{isActive ? "Filtered" : "Filter by module"}
</Text>
</View>
<Ionicons
name={isActive ? "close-circle" : "chevron-forward"}
size={14}
color={isActive ? Colors.white : Colors.textSecondary}
/>
</Pressable>
);
})}
</ScrollView>
</View>
) : null}
{items.length > 0 ? (
<Text style={styles.count}>
{total} result{total === 1 ? "" : "s"}
</Text>
) : null}
</View>
);
return (
<FlatList
data={items}
keyExtractor={(item) => item.content_id}
contentContainerStyle={items.length ? styles.list : styles.empty}
ListHeaderComponent={headerComponent}
renderItem={({ item }) => (
<Pressable
onPress={() => onPress(item)}
style={({ pressed }) => [styles.card, pressed && styles.pressed]}
>
<View style={styles.cardText}>
<Text style={styles.title}>{item.title}</Text>
<Text style={styles.meta}>
{item.module_name} · {item.template_name}
</Text>
{item.summary ? (
<Text numberOfLines={2} style={styles.summary}>
{item.summary}
</Text>
) : null}
</View>
<Ionicons
name="chevron-forward"
size={20}
color={Colors.textSecondary}
/>
</Pressable>
)}
ListEmptyComponent={
!loading ? (
<View style={styles.center}>
<Ionicons
name="search-outline"
size={30}
color={Colors.textSecondary}
/>
<Text style={styles.emptyTitle}>No matching content</Text>
<Text style={styles.muted}>
Try a broader search term or remove one of the filters.
</Text>
</View>
) : null
}
ListFooterComponent={
fetchingMore ? (
<ActivityIndicator style={{ margin: 18 }} color={Colors.primary} />
) : null
}
onEndReached={onLoadMore}
onEndReachedThreshold={0.35}
/>
);
}
const styles = StyleSheet.create({
list: {
paddingHorizontal: 20,
paddingBottom: 28,
},
empty: {
flexGrow: 1,
paddingHorizontal: 20,
},
headerWrapper: {
paddingBottom: 4,
},
modulesSection: {
marginBottom: 12,
marginTop: 6,
},
modulesHeading: {
fontSize: 12,
fontWeight: "800",
color: Colors.textSecondary,
textTransform: "uppercase",
marginBottom: 8,
letterSpacing: 0.5,
},
modulesScroll: {
gap: 8,
paddingRight: 10,
},
moduleCard: {
flexDirection: "row",
alignItems: "center",
gap: 8,
paddingVertical: 8,
paddingHorizontal: 12,
borderRadius: 12,
backgroundColor: "#F3F7F5",
borderWidth: 1,
borderColor: "#E2ECE6",
},
moduleCardActive: {
backgroundColor: Colors.primary,
borderColor: Colors.primary,
},
moduleIconBox: {
justifyContent: "center",
alignItems: "center",
},
moduleTextContainer: {
maxWidth: 180,
},
moduleName: {
fontSize: 13,
fontWeight: "700",
color: Colors.textPrimary,
},
moduleNameActive: {
color: Colors.white,
},
moduleBadge: {
fontSize: 10,
color: Colors.textSecondary,
marginTop: 1,
},
moduleBadgeActive: {
color: "rgba(255,255,255,0.85)",
},
count: {
paddingVertical: 9,
fontSize: 12,
fontWeight: "700",
color: Colors.textSecondary,
},
card: {
flexDirection: "row",
alignItems: "center",
gap: 12,
padding: 15,
marginBottom: 10,
borderRadius: 16,
backgroundColor: Colors.white,
borderWidth: 1,
borderColor: "#EEF0F2",
shadowColor: "#000",
shadowOpacity: 0.05,
shadowRadius: 6,
shadowOffset: { width: 0, height: 2 },
elevation: 1,
},
pressed: {
opacity: 0.75,
},
cardText: {
flex: 1,
},
title: {
fontSize: 16,
fontWeight: "800",
color: Colors.textPrimary,
},
meta: {
marginTop: 3,
fontSize: 11.5,
fontWeight: "600",
color: Colors.primaryDark,
},
summary: {
marginTop: 6,
fontSize: 12.5,
lineHeight: 18,
color: Colors.textSecondary,
},
center: {
flex: 1,
alignItems: "center",
justifyContent: "center",
padding: 32,
},
emptyTitle: {
marginTop: 8,
fontSize: 16,
fontWeight: "800",
color: Colors.textPrimary,
},
muted: {
marginTop: 5,
textAlign: "center",
fontSize: 12.5,
color: Colors.textSecondary,
},
});

View File

@ -1,4 +1,4 @@
import { useInfiniteQuery } from "@tanstack/react-query";
import { keepPreviousData, useInfiniteQuery } from "@tanstack/react-query";
import { useSettingsStore } from "@/store/settings.store";
import { searchContents } from "../api/search.api";
import type { FacetedSearchRequest } from "../types/search.types";
@ -24,8 +24,10 @@ export function useFacetedSearch(
queryFn: ({ pageParam }) =>
searchContents({ ...request, page: pageParam }, preview),
initialPageParam: 1,
placeholderData: keepPreviousData,
enabled,
getNextPageParam: (last) =>
last.page * last.page_size < last.total ? last.page + 1 : undefined,
});
}

View File

@ -1,4 +1,4 @@
import React, { useEffect, useMemo, useState } from "react";
import React, { useCallback, useEffect, useMemo, useState } from "react";
import { Platform, Pressable, StyleSheet, Text, TextInput, View } from "react-native";
import { SafeAreaView } from "react-native-safe-area-context";
import { Ionicons } from "@expo/vector-icons";
@ -18,6 +18,7 @@ import type {
FacetFilter,
FilterLogic,
SearchFacetDefinition,
SearchModuleOption,
SearchScope,
} from "../types/search.types";
@ -147,6 +148,38 @@ export default function FacetedSearchScreen({
);
};
const matchingModules = useMemo(() => {
const q = (debouncedQuery || query).trim().toLowerCase();
if (!q || isModuleMode || !capabilities.data?.modules) return [];
const tokens = q.split(/\s+/).filter(Boolean);
return capabilities.data.modules.filter((m) => {
const name = m.name.toLowerCase();
const code = m.code.toLowerCase();
if (name.includes(q) || code.includes(q)) return true;
return tokens.some(
(tok) => tok.length >= 2 && (name.includes(tok) || code.includes(tok)),
);
});
}, [debouncedQuery, query, isModuleMode, capabilities.data?.modules]);
const activeModuleCode = useMemo(() => {
return effectiveScope.module_codes?.length === 1
? effectiveScope.module_codes[0]
: null;
}, [effectiveScope.module_codes]);
const handleToggleModuleScope = useCallback(
(module: SearchModuleOption) => {
const currentCodes = effectiveScope.module_codes ?? [];
const isAlreadySelected = currentCodes.includes(module.code);
handleScopeChange({
...effectiveScope,
module_codes: isAlreadySelected ? null : [module.code],
});
},
[effectiveScope, handleScopeChange],
);
return (
<SafeAreaView style={styles.safe} edges={["top", "left", "right"]}>
<View style={styles.header}>
@ -285,6 +318,9 @@ export default function FacetedSearchScreen({
total={merged.total}
loading={capabilities.isLoading || search.isLoading}
fetchingMore={search.isFetchingNextPage}
matchingModules={matchingModules}
onSelectModule={handleToggleModuleScope}
activeModuleCode={activeModuleCode}
onLoadMore={() => {
if (search.hasNextPage && !search.isFetchingNextPage) {
void search.fetchNextPage();