Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

feat(mobile): add search tab (#1334)

Corentin Thomasset0ab82351e5d3
diff --git a/apps/mobile/app/(app)/(with-organizations)/(tabs)/_layout.tsx b/apps/mobile/app/(app)/(with-organizations)/(tabs)/_layout.tsx
index 2d99faa5..56a7401b 100644
--- a/apps/mobile/app/(app)/(with-organizations)/(tabs)/_layout.tsx
+++ b/apps/mobile/app/(app)/(with-organizations)/(tabs)/_layout.tsx
@@ -36,6 +36,16 @@ export default function TabLayout() {
tabBarLabel: () => null,
}}
/>
+ <Tabs.Screen
+ name="search"
+ options={{
+ title: 'Search',
+ tabBarIcon: ({ color }) => (
+ <Icon name="search" size={30} color={color} style={{ height: 30 }} />
+ ),
+ tabBarLabel: () => null,
+ }}
+ />
<Tabs.Screen
name="import"
options={{
diff --git a/apps/mobile/app/(app)/(with-organizations)/(tabs)/search.tsx b/apps/mobile/app/(app)/(with-organizations)/(tabs)/search.tsx
new file mode 100644
index 00000000..3a1bf29d
--- /dev/null
+++ b/apps/mobile/app/(app)/(with-organizations)/(tabs)/search.tsx
@@ -0,0 +1,3 @@
+import { DocumentsSearchScreen } from '@/modules/documents/screens/documents-search.screen';
+
+export default DocumentsSearchScreen;
diff --git a/apps/mobile/src/modules/documents/components/documents-list.tsx b/apps/mobile/src/modules/documents/components/documents-list.tsx
new file mode 100644
index 00000000..bd10b14f
--- /dev/null
+++ b/apps/mobile/src/modules/documents/components/documents-list.tsx
@@ -0,0 +1,218 @@
+import type { ComponentProps } from 'react';
+import type { Document } from '../documents.types';
+import type { CoerceDates } from '@/modules/api/api.models';
+import type { ThemeColors } from '@/modules/ui/theme.constants';
+import { formatBytes } from '@corentinth/chisels';
+import { router } from 'expo-router';
+import { useState } from 'react';
+import { FlatList, StyleSheet, Text, TouchableOpacity, View } from 'react-native';
+import { DocumentActionSheet } from '@/modules/documents/components/document-action-sheet';
+import { Icon } from '@/modules/ui/components/icon';
+import { useThemeColor } from '@/modules/ui/providers/use-theme-color';
+
+type DocumentsListProps = {
+ documents: CoerceDates<Document>[];
+ emptyState: {
+ title: string;
+ subtitle?: string;
+ };
+ refreshControl?: ComponentProps<typeof FlatList>['refreshControl'];
+ keyboardShouldPersistTaps?: ComponentProps<typeof FlatList>['keyboardShouldPersistTaps'];
+};
+
+export function DocumentsList({
+ documents,
+ emptyState,
+ refreshControl,
+ keyboardShouldPersistTaps,
+}: DocumentsListProps) {
+ const themeColors = useThemeColor();
+ const [onDocumentActionSheet, setOnDocumentActionSheet] = useState<
+ CoerceDates<Document> | undefined
+ >(undefined);
+
+ const styles = createStyles({ themeColors });
+
+ const formatDate = (date: Date) => {
+ return new Intl.DateTimeFormat('en-US', {
+ month: 'short',
+ day: 'numeric',
+ year: 'numeric',
+ }).format(date);
+ };
+
+ return (
+ <>
+ {onDocumentActionSheet && (
+ <DocumentActionSheet
+ visible={true}
+ document={onDocumentActionSheet}
+ onClose={() => setOnDocumentActionSheet(undefined)}
+ />
+ )}
+ <FlatList
+ data={documents}
+ keyExtractor={(item) => item.id}
+ renderItem={({ item }) => (
+ <TouchableOpacity
+ onPress={() => {
+ router.push({
+ pathname: '/(app)/document/details',
+ params: {
+ documentId: item.id,
+ organizationId: item.organizationId,
+ },
+ });
+ }}
+ >
+ <View style={styles.documentCard}>
+ <View
+ style={{
+ backgroundColor: themeColors.muted,
+ padding: 10,
+ borderRadius: 6,
+ marginRight: 12,
+ }}
+ >
+ <Icon name="file-text" size={24} color={themeColors.primary} />
+ </View>
+ <View style={styles.documentContent}>
+ <Text style={styles.documentTitle} numberOfLines={1} ellipsizeMode="tail">
+ {item.name}
+ </Text>
+ <View style={styles.documentMeta}>
+ <Text style={styles.metaText}>{formatBytes({ bytes: item.originalSize })}</Text>
+ <Text style={styles.metaSplitter}>-</Text>
+ <Text style={styles.metaText}>{formatDate(item.createdAt)}</Text>
+ {item.localUri !== undefined && (
+ <View
+ style={[
+ styles.unsyncedBadge,
+ { backgroundColor: `${themeColors.primary}20` },
+ ]}
+ >
+ <Icon name="upload" size={12} color={themeColors.primary} />
+ </View>
+ )}
+ {item.tags.length > 0 && (
+ <View style={styles.tagsContainer}>
+ {item.tags.map((tag) => (
+ <View
+ key={tag.id}
+ style={[styles.tag, { backgroundColor: `${tag.color}10` }]}
+ >
+ <Text style={[styles.tagText, { color: tag.color }]}>{tag.name}</Text>
+ </View>
+ ))}
+ </View>
+ )}
+ </View>
+ </View>
+ <TouchableOpacity
+ style={styles.moreButton}
+ onPress={(e) => {
+ e.stopPropagation();
+ setOnDocumentActionSheet(item);
+ }}
+ hitSlop={{ top: 10, bottom: 10, left: 10, right: 10 }}
+ >
+ <Icon name="more-vertical" size={20} color={themeColors.mutedForeground} />
+ </TouchableOpacity>
+ </View>
+ </TouchableOpacity>
+ )}
+ ListEmptyComponent={
+ <View style={styles.emptyContainer}>
+ <Text style={styles.emptyText}>{emptyState.title}</Text>
+ {emptyState.subtitle !== undefined && (
+ <Text style={styles.emptySubtext}>{emptyState.subtitle}</Text>
+ )}
+ </View>
+ }
+ contentContainerStyle={documents.length === 0 ? styles.emptyList : undefined}
+ refreshControl={refreshControl}
+ keyboardShouldPersistTaps={keyboardShouldPersistTaps}
+ />
+ </>
+ );
+}
+
+function createStyles({ themeColors }: { themeColors: ThemeColors }) {
+ return StyleSheet.create({
+ emptyList: {
+ flex: 1,
+ },
+ emptyContainer: {
+ flex: 1,
+ justifyContent: 'center',
+ alignItems: 'center',
+ paddingVertical: 60,
+ },
+ emptyText: {
+ fontSize: 18,
+ fontWeight: '600',
+ color: themeColors.foreground,
+ marginBottom: 8,
+ },
+ emptySubtext: {
+ fontSize: 14,
+ color: themeColors.mutedForeground,
+ },
+ documentCard: {
+ padding: 16,
+ borderBottomWidth: 1,
+ borderColor: themeColors.border,
+ flexDirection: 'row',
+ alignItems: 'center',
+ },
+ documentContent: {
+ flex: 1,
+ marginRight: 8,
+ },
+ documentTitle: {
+ fontSize: 16,
+ fontWeight: '600',
+ color: themeColors.foreground,
+ },
+ moreButton: {
+ padding: 8,
+ },
+ documentMeta: {
+ flexDirection: 'row',
+ alignItems: 'center',
+ gap: 8,
+ marginTop: 4,
+ },
+ metaText: {
+ fontSize: 13,
+ color: themeColors.mutedForeground,
+ },
+ metaSplitter: {
+ fontSize: 13,
+ color: themeColors.mutedForeground,
+ },
+ tagsContainer: {
+ flexDirection: 'row',
+ flexWrap: 'wrap',
+ gap: 8,
+ },
+ tag: {
+ paddingHorizontal: 10,
+ paddingVertical: 6,
+ borderRadius: 6,
+ },
+ tagText: {
+ fontSize: 12,
+ fontWeight: '500',
+ lineHeight: 12,
+ },
+ unsyncedBadge: {
+ flexDirection: 'row',
+ alignItems: 'center',
+ gap: 4,
+ paddingHorizontal: 8,
+ paddingVertical: 4,
+ borderRadius: 12,
+ },
+ });
+}
diff --git a/apps/mobile/src/modules/documents/documents.services.ts b/apps/mobile/src/modules/documents/documents.services.ts
index 81a1acdc..d7610180 100644
--- a/apps/mobile/src/modules/documents/documents.services.ts
+++ b/apps/mobile/src/modules/documents/documents.services.ts
@@ -4,7 +4,6 @@ import type { AuthClient } from '../auth/auth.client';
import type { Document } from './documents.types';
import * as FileSystem from 'expo-file-system/legacy';
import { coerceDates } from '../api/api.models';
-import { documentsLocalStorage } from './documents.local-storage';
export function getFormData(pojo: Record<string, string | FormDataValue | Blob>): FormData {
const formData = new FormData();
@@ -44,37 +43,38 @@ export async function fetchOrganizationDocuments({
organizationId,
pageIndex,
pageSize,
- filters,
+ searchQuery,
+ sortField,
+ sortOrder,
apiClient,
}: {
organizationId: string;
pageIndex: number;
pageSize: number;
- filters?: {
- tags?: string[];
- };
+ searchQuery?: string;
+ sortField?: 'createdAt' | 'updatedAt' | 'name' | 'documentDate';
+ sortOrder?: 'asc' | 'desc';
apiClient: ApiClient;
}) {
- const { documents: apiDocuments, documentsCount } = await apiClient<{
+ const { documents, documentsCount } = await apiClient<{
documents: Document[];
documentsCount: number;
}>({
method: 'GET',
path: `/api/organizations/${organizationId}/documents`,
query: {
+ searchQuery,
pageIndex,
pageSize,
- ...filters,
+ sortField,
+ sortOrder,
},
});
- const remote = apiDocuments.map(coerceDates);
- const local = await documentsLocalStorage.getUnsyncedDocumentsByOrganization(organizationId);
- const documents = [...local, ...remote];
return {
- documentsCount: documentsCount + local.length,
+ documentsCount,
documents: documents.map(coerceDates),
};
}
diff --git a/apps/mobile/src/modules/documents/screens/documents-list.screen.tsx b/apps/mobile/src/modules/documents/screens/documents-list.screen.tsx
index a4d1f961..9e7395cd 100644
--- a/apps/mobile/src/modules/documents/screens/documents-list.screen.tsx
+++ b/apps/mobile/src/modules/documents/screens/documents-list.screen.tsx
@@ -1,26 +1,13 @@
-import type { Document } from '../documents.types';
-import type { CoerceDates } from '@/modules/api/api.models';
import type { ThemeColors } from '@/modules/ui/theme.constants';
-import { formatBytes } from '@corentinth/chisels';
import { useQuery } from '@tanstack/react-query';
-import { router } from 'expo-router';
import { useState } from 'react';
-import {
- ActivityIndicator,
- FlatList,
- RefreshControl,
- StyleSheet,
- Text,
- TouchableOpacity,
- View,
-} from 'react-native';
+import { ActivityIndicator, RefreshControl, StyleSheet, Text, View } from 'react-native';
import { SafeAreaView } from 'react-native-safe-area-context';
import { useApiClient } from '@/modules/api/providers/api.provider';
-import { DocumentActionSheet } from '@/modules/documents/components/document-action-sheet';
+import { DocumentsList } from '@/modules/documents/components/documents-list';
import { OrganizationPickerButton } from '@/modules/organizations/components/organization-picker-button';
import { OrganizationPickerDrawer } from '@/modules/organizations/components/organization-picker-drawer';
import { useOrganizations } from '@/modules/organizations/organizations.provider';
-import { Icon } from '@/modules/ui/components/icon';
import { useThemeColor } from '@/modules/ui/providers/use-theme-color';
import { fetchOrganizationDocuments } from '../documents.services';
import { syncUnsyncedDocuments } from '../documents.sync.services';
@@ -29,9 +16,6 @@ export function DocumentsListScreen() {
const themeColors = useThemeColor();
const apiClient = useApiClient();
const { currentOrganizationId, isLoading: isLoadingOrganizations } = useOrganizations();
- const [onDocumentActionSheet, setOnDocumentActionSheet] = useState<
- CoerceDates<Document> | undefined
- >(undefined);
const [isDrawerVisible, setIsDrawerVisible] = useState(false);
const pagination = { pageIndex: 0, pageSize: 20 };
@@ -53,14 +37,6 @@ export function DocumentsListScreen() {
const styles = createStyles({ themeColors });
- const formatDate = (date: Date) => {
- return new Intl.DateTimeFormat('en-US', {
- month: 'short',
- day: 'numeric',
- year: 'numeric',
- }).format(date);
- };
-
const onRefresh = async () => {
await documentsQuery.refetch();
if (currentOrganizationId != null) {
@@ -78,13 +54,6 @@ export function DocumentsListScreen() {
return (
<SafeAreaView style={styles.container}>
- {onDocumentActionSheet && (
- <DocumentActionSheet
- visible={true}
- document={onDocumentActionSheet}
- onClose={() => setOnDocumentActionSheet(undefined)}
- />
- )}
<View style={styles.header}>
<Text style={styles.title}>Documents</Text>
<OrganizationPickerButton onPress={() => setIsDrawerVisible(true)} />
@@ -95,86 +64,12 @@ export function DocumentsListScreen() {
<ActivityIndicator size="large" color={themeColors.primary} />
</View>
) : (
- <FlatList
- data={documentsQuery.data?.documents ?? []}
- keyExtractor={(item) => item.id}
- renderItem={({ item }) => (
- <TouchableOpacity
- onPress={() => {
- router.push({
- pathname: '/(app)/document/details',
- params: {
- documentId: item.id,
- organizationId: item.organizationId,
- },
- });
- }}
- >
- <View style={styles.documentCard}>
- <View
- style={{
- backgroundColor: themeColors.muted,
- padding: 10,
- borderRadius: 6,
- marginRight: 12,
- }}
- >
- <Icon name="file-text" size={24} color={themeColors.primary} />
- </View>
- <View style={styles.documentContent}>
- <Text style={styles.documentTitle} numberOfLines={1} ellipsizeMode="tail">
- {item.name}
- </Text>
- <View style={styles.documentMeta}>
- <Text style={styles.metaText}>{formatBytes({ bytes: item.originalSize })}</Text>
- <Text style={styles.metaSplitter}>-</Text>
- <Text style={styles.metaText}>{formatDate(item.createdAt)}</Text>
- {item.localUri !== undefined && (
- <View
- style={[
- styles.unsyncedBadge,
- { backgroundColor: `${themeColors.primary}20` },
- ]}
- >
- <Icon name="upload" size={12} color={themeColors.primary} />
- </View>
- )}
- {item.tags.length > 0 && (
- <View style={styles.tagsContainer}>
- {item.tags.map((tag) => (
- <View
- key={tag.id}
- style={[styles.tag, { backgroundColor: `${tag.color}10` }]}
- >
- <Text style={[styles.tagText, { color: tag.color }]}>{tag.name}</Text>
- </View>
- ))}
- </View>
- )}
- </View>
- </View>
- <TouchableOpacity
- style={styles.moreButton}
- onPress={(e) => {
- e.stopPropagation();
- setOnDocumentActionSheet(item);
- }}
- hitSlop={{ top: 10, bottom: 10, left: 10, right: 10 }}
- >
- <Icon name="more-vertical" size={20} color={themeColors.mutedForeground} />
- </TouchableOpacity>
- </View>
- </TouchableOpacity>
- )}
- ListEmptyComponent={
- <View style={styles.emptyContainer}>
- <Text style={styles.emptyText}>No documents yet</Text>
- <Text style={styles.emptySubtext}>Upload your first document to get started</Text>
- </View>
- }
- contentContainerStyle={
- documentsQuery.data?.documents.length === 0 ? styles.emptyList : undefined
- }
+ <DocumentsList
+ documents={documentsQuery.data?.documents ?? []}
+ emptyState={{
+ title: 'No documents yet',
+ subtitle: 'Upload your first document to get started',
+ }}
refreshControl={
<RefreshControl refreshing={documentsQuery.isRefetching} onRefresh={onRefresh} />
}
@@ -212,80 +107,5 @@ function createStyles({ themeColors }: { themeColors: ThemeColors }) {
fontWeight: 'bold',
color: themeColors.foreground,
},
- emptyList: {
- flex: 1,
- },
- emptyContainer: {
- flex: 1,
- justifyContent: 'center',
- alignItems: 'center',
- paddingVertical: 60,
- },
- emptyText: {
- fontSize: 18,
- fontWeight: '600',
- color: themeColors.foreground,
- marginBottom: 8,
- },
- emptySubtext: {
- fontSize: 14,
- color: themeColors.mutedForeground,
- },
- documentCard: {
- padding: 16,
- borderBottomWidth: 1,
- borderColor: themeColors.border,
- flexDirection: 'row',
- alignItems: 'center',
- },
- documentContent: {
- flex: 1,
- marginRight: 8,
- },
- documentTitle: {
- fontSize: 16,
- fontWeight: '600',
- color: themeColors.foreground,
- },
- moreButton: {
- padding: 8,
- },
- documentMeta: {
- flexDirection: 'row',
- alignItems: 'center',
- gap: 8,
- marginTop: 4,
- },
- metaText: {
- fontSize: 13,
- color: themeColors.mutedForeground,
- },
- metaSplitter: {
- fontSize: 13,
- color: themeColors.mutedForeground,
- },
- tagsContainer: {
- flexDirection: 'row',
- flexWrap: 'wrap',
- gap: 8,
- },
- tag: {
- paddingHorizontal: 10,
- paddingVertical: 6,
- borderRadius: 6,
- },
- tagText: {
- fontSize: 12,
- fontWeight: '500',
- lineHeight: 12,
- },
- unsyncedBadge: {
- flexDirection: 'row',
- alignItems: 'center',
- gap: 4,
- paddingHorizontal: 8,
- paddingVertical: 4,
- borderRadius: 12,
- },
});
}
diff --git a/apps/mobile/src/modules/documents/screens/documents-search.screen.tsx b/apps/mobile/src/modules/documents/screens/documents-search.screen.tsx
new file mode 100644
index 00000000..267b4ccb
--- /dev/null
+++ b/apps/mobile/src/modules/documents/screens/documents-search.screen.tsx
@@ -0,0 +1,189 @@
+import type { ThemeColors } from '@/modules/ui/theme.constants';
+import { useQuery } from '@tanstack/react-query';
+import { router } from 'expo-router';
+import { useEffect, useState } from 'react';
+import {
+ ActivityIndicator,
+ Keyboard,
+ StyleSheet,
+ Text,
+ TextInput,
+ TouchableOpacity,
+ View,
+} from 'react-native';
+import { SafeAreaView } from 'react-native-safe-area-context';
+import { useApiClient } from '@/modules/api/providers/api.provider';
+import { DocumentsList } from '@/modules/documents/components/documents-list';
+import { useOrganizations } from '@/modules/organizations/organizations.provider';
+import { Icon } from '@/modules/ui/components/icon';
+import { useThemeColor } from '@/modules/ui/providers/use-theme-color';
+import { fetchOrganizationDocuments } from '../documents.services';
+
+const SEARCH_DEBOUNCE_MS = 300;
+const pagination = { pageIndex: 0, pageSize: 20 };
+
+export function DocumentsSearchScreen() {
+ const themeColors = useThemeColor();
+ const apiClient = useApiClient();
+ const { currentOrganizationId } = useOrganizations();
+
+ const [searchQuery, setSearchQuery] = useState('');
+ const [debouncedSearchQuery, setDebouncedSearchQuery] = useState('');
+
+ useEffect(() => {
+ const timeout = setTimeout(
+ () => setDebouncedSearchQuery(searchQuery.trim()),
+ SEARCH_DEBOUNCE_MS,
+ );
+ return () => clearTimeout(timeout);
+ }, [searchQuery]);
+
+ const searchResultsQuery = useQuery({
+ queryKey: [
+ 'organizations',
+ currentOrganizationId,
+ 'documents',
+ 'search',
+ debouncedSearchQuery,
+ pagination,
+ ],
+ queryFn: async () => {
+ if (currentOrganizationId == null) {
+ return { documents: [], documentsCount: 0 };
+ }
+
+ return fetchOrganizationDocuments({
+ organizationId: currentOrganizationId,
+ searchQuery: debouncedSearchQuery,
+ ...pagination,
+ apiClient,
+ });
+ },
+ enabled:
+ currentOrganizationId !== null && currentOrganizationId !== '' && debouncedSearchQuery !== '',
+ });
+
+ const handleCancel = () => {
+ Keyboard.dismiss();
+ setSearchQuery('');
+
+ if (router.canGoBack()) {
+ router.back();
+ } else {
+ router.navigate('/(app)/(with-organizations)/(tabs)/list');
+ }
+ };
+
+ const styles = createStyles({ themeColors });
+
+ return (
+ <SafeAreaView style={styles.container}>
+ <View style={styles.header}>
+ <View style={styles.searchInputContainer}>
+ <Icon name="search" size={18} color={themeColors.mutedForeground} />
+ <TextInput
+ style={styles.searchInput}
+ placeholder="Search documents"
+ placeholderTextColor={themeColors.mutedForeground}
+ value={searchQuery}
+ onChangeText={setSearchQuery}
+ autoFocus
+ autoCapitalize="none"
+ autoCorrect={false}
+ returnKeyType="search"
+ />
+ {searchQuery !== '' && (
+ <TouchableOpacity
+ onPress={() => setSearchQuery('')}
+ hitSlop={{ top: 10, bottom: 10, left: 10, right: 10 }}
+ accessibilityRole="button"
+ accessibilityLabel="Clear search text"
+ >
+ <Icon name="x" size={18} color={themeColors.mutedForeground} />
+ </TouchableOpacity>
+ )}
+ </View>
+ <TouchableOpacity onPress={handleCancel} hitSlop={{ top: 10, bottom: 10 }}>
+ <Text style={styles.cancelButtonText}>Cancel</Text>
+ </TouchableOpacity>
+ </View>
+
+ {debouncedSearchQuery === '' ? (
+ <View style={styles.centerContent}>
+ <Icon name="search" size={40} color={themeColors.mutedForeground} />
+ <Text style={styles.hintText}>Search your documents</Text>
+ <Text style={styles.hintSubtext}>Find documents by name or content</Text>
+ </View>
+ ) : searchResultsQuery.isLoading ? (
+ <View style={styles.centerContent}>
+ <ActivityIndicator size="large" color={themeColors.primary} />
+ </View>
+ ) : (
+ <DocumentsList
+ documents={searchResultsQuery.data?.documents ?? []}
+ emptyState={{
+ title: 'No documents found',
+ subtitle: `No results for "${debouncedSearchQuery}"`,
+ }}
+ keyboardShouldPersistTaps="handled"
+ />
+ )}
+ </SafeAreaView>
+ );
+}
+
+function createStyles({ themeColors }: { themeColors: ThemeColors }) {
+ return StyleSheet.create({
+ container: {
+ flex: 1,
+ backgroundColor: themeColors.background,
+ },
+ centerContent: {
+ flex: 1,
+ justifyContent: 'center',
+ alignItems: 'center',
+ gap: 8,
+ paddingHorizontal: 24,
+ },
+ header: {
+ flexDirection: 'row',
+ alignItems: 'center',
+ gap: 12,
+ padding: 16,
+ paddingTop: 20,
+ borderBottomWidth: 1,
+ borderBottomColor: themeColors.border,
+ },
+ searchInputContainer: {
+ flex: 1,
+ flexDirection: 'row',
+ alignItems: 'center',
+ gap: 8,
+ height: 44,
+ paddingHorizontal: 12,
+ borderWidth: 1,
+ borderColor: themeColors.border,
+ borderRadius: 8,
+ backgroundColor: themeColors.secondaryBackground,
+ },
+ searchInput: {
+ flex: 1,
+ fontSize: 16,
+ color: themeColors.foreground,
+ },
+ cancelButtonText: {
+ fontSize: 16,
+ color: themeColors.primary,
+ },
+ hintText: {
+ fontSize: 18,
+ fontWeight: '600',
+ color: themeColors.foreground,
+ marginTop: 8,
+ },
+ hintSubtext: {
+ fontSize: 14,
+ color: themeColors.mutedForeground,
+ },
+ });
+}