Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

5.1 KB
Raw Blame
import type { ThemeColors } from '@/modules/ui/theme.constants';
import { router } from 'expo-router';
import { useState } from 'react';
import {
  ActivityIndicator,
  RefreshControl,
  StyleSheet,
  Text,
  TouchableOpacity,
  View,
} from 'react-native';
import { SafeAreaView } from 'react-native-safe-area-context';
import { useAppTranslations } from '@/modules/i18n/hooks/use-app-translations';
import { useApiClient } from '@/modules/api/providers/api.provider';
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 { useDocuments } from '../hooks/use-documents.hook';
import { syncUnsyncedDocuments } from '../documents.sync.services';

export function DocumentsListScreen() {
  const t = useAppTranslations();
  const themeColors = useThemeColor();
  const apiClient = useApiClient();
  const { currentOrganizationId, isLoading: isLoadingOrganizations } = useOrganizations();
  const [isDrawerVisible, setIsDrawerVisible] = useState(false);
  const documentsQuery = useDocuments();

  const styles = createStyles({ themeColors });

  const onRefresh = async () => {
    await documentsQuery.refetch();
    if (currentOrganizationId != null) {
      void syncUnsyncedDocuments({ organizationId: currentOrganizationId, apiClient });
    }
  };

  if (isLoadingOrganizations) {
    return (
      <View style={[styles.container, styles.centerContent]}>
        <ActivityIndicator size="large" color={themeColors.primary} />
      </View>
    );
  }

  return (
    <SafeAreaView style={styles.container}>
      <View style={styles.header}>
        <View style={styles.topRow}>
          <OrganizationPickerButton variant="plain" onPress={() => setIsDrawerVisible(true)} />
          <TouchableOpacity
            style={styles.userButton}
            onPress={() => router.navigate('/(app)/(with-organizations)/(tabs)/settings')}
            accessibilityRole="button"
            accessibilityLabel={t.documents.userSettings}
          >
            <Icon name="user" size={20} color={themeColors.foreground} />
          </TouchableOpacity>
        </View>

        <Text style={styles.title}>{t.documents.title}</Text>

        <TouchableOpacity
          style={styles.searchButton}
          onPress={() => router.navigate('/(app)/(with-organizations)/(tabs)/search')}
          accessibilityRole="button"
          accessibilityLabel={t.documents.search.placeholder}
        >
          <Icon name="search" size={18} color={themeColors.mutedForeground} />
          <Text style={styles.searchButtonText}>{t.documents.search.placeholder}</Text>
        </TouchableOpacity>
      </View>

      {documentsQuery.isLoading ? (
        <View style={styles.centerContent}>
          <ActivityIndicator size="large" color={themeColors.primary} />
        </View>
      ) : (
        <DocumentsList
          key={currentOrganizationId}
          documents={documentsQuery.documents}
          onLoadMore={documentsQuery.loadMore}
          isFetchingNextPage={documentsQuery.isFetchingNextPage}
          isFetchNextPageError={documentsQuery.isFetchNextPageError}
          emptyState={{
            title: t.documents.emptyTitle,
            subtitle: t.documents.emptySubtitle,
          }}
          refreshControl={
            <RefreshControl refreshing={documentsQuery.isRefetching} onRefresh={onRefresh} />
          }
        />
      )}

      <OrganizationPickerDrawer
        visible={isDrawerVisible}
        onClose={() => setIsDrawerVisible(false)}
      />
    </SafeAreaView>
  );
}

function createStyles({ themeColors }: { themeColors: ThemeColors }) {
  return StyleSheet.create({
    container: {
      flex: 1,
      backgroundColor: themeColors.background,
    },
    centerContent: {
      flex: 1,
      justifyContent: 'center',
      alignItems: 'center',
    },
    header: {
      padding: 16,
      paddingTop: 12,
      gap: 12,
    },
    topRow: {
      flexDirection: 'row',
      alignItems: 'center',
      justifyContent: 'space-between',
      gap: 12,
    },
    userButton: {
      width: 36,
      height: 36,
      borderRadius: 18,
      alignItems: 'center',
      justifyContent: 'center',
      backgroundColor: themeColors.secondaryBackground,
      borderWidth: 1,
      borderColor: themeColors.border,
    },
    title: {
      fontSize: 28,
      fontWeight: 'bold',
      color: themeColors.foreground,
    },
    searchButton: {
      flexDirection: 'row',
      alignItems: 'center',
      gap: 8,
      height: 44,
      paddingHorizontal: 12,
      borderWidth: 1,
      borderColor: themeColors.border,
      borderRadius: 8,
      backgroundColor: themeColors.secondaryBackground,
    },
    searchButtonText: {
      fontSize: 16,
      color: themeColors.mutedForeground,
    },
  });
}