import React, { useState, useCallback, useMemo } from 'react';
import {
View,
Text,
StyleSheet,
SectionList,
RefreshControl,
useColorScheme,
TextInput,
TouchableOpacity,
} from 'react-native';
import { useRouter } from 'expo-router';
import { Ionicons } from '@expo/vector-icons';
import { Colors } from '../../constants/Colors';
import { useTasks, groupTasksByStatus } from '../../hooks/useTasks';
import { TaskListItem } from '../../components/TaskListItem';
import { TaskListSkeleton } from '../../components/TaskListSkeleton';
import { EmptyState } from '../../components/EmptyState';
import type { TaskSummary } from '../../lib/api';
interface Section {
title: string;
data: TaskSummary[];
}
export default function TasksScreen() {
const colorScheme = useColorScheme() ?? 'light';
const colors = Colors[colorScheme];
const router = useRouter();
const [searchQuery, setSearchQuery] = useState('');
const { data: tasks, isLoading, isError, refetch, isRefetching } = useTasks();
// Group and filter tasks
const sections = useMemo(() => {
if (!tasks) return [];
// Filter by search query
const filteredTasks = searchQuery
? tasks.filter(
(task) =>
task.name.toLowerCase().includes(searchQuery.toLowerCase()) ||
task.contractName?.toLowerCase().includes(searchQuery.toLowerCase())
)
: tasks;
// Group by status
const groups = groupTasksByStatus(filteredTasks);
const result: Section[] = [];
if (groups.running.length > 0) {
result.push({ title: 'Running', data: groups.running });
}
if (groups.blocked.length > 0) {
result.push({ title: 'Needs Attention', data: groups.blocked });
}
if (groups.pending.length > 0) {
result.push({ title: 'Pending', data: groups.pending });
}
if (groups.completed.length > 0) {
result.push({ title: 'Completed', data: groups.completed });
}
return result;
}, [tasks, searchQuery]);
const handleTaskPress = useCallback(
(task: TaskSummary) => {
router.push(`/task/${task.id}`);
},
[router]
);
const handleRefresh = useCallback(() => {
refetch();
}, [refetch]);
const clearSearch = useCallback(() => {
setSearchQuery('');
}, []);
// Render loading state
if (isLoading) {
return (
);
}
// Render error state
if (isError) {
return (
);
}
// Render empty state
if (!tasks || tasks.length === 0) {
return (
);
}
return (
{/* Search bar */}
{searchQuery.length > 0 && (
)}
{/* Task list */}
item.id}
renderItem={({ item }) => (
)}
renderSectionHeader={({ section }) => (
{section.title}
{section.data.length}
)}
refreshControl={
}
ListEmptyComponent={
searchQuery ? (
) : null
}
stickySectionHeadersEnabled
contentContainerStyle={sections.length === 0 ? styles.emptyContent : undefined}
/>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
searchContainer: {
flexDirection: 'row',
alignItems: 'center',
margin: 16,
paddingHorizontal: 12,
paddingVertical: 10,
borderRadius: 10,
borderWidth: StyleSheet.hairlineWidth,
gap: 8,
},
searchInput: {
flex: 1,
fontSize: 16,
padding: 0,
},
sectionHeader: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
paddingHorizontal: 16,
paddingVertical: 8,
},
sectionTitle: {
fontSize: 14,
fontWeight: '600',
textTransform: 'uppercase',
letterSpacing: 0.5,
},
sectionCount: {
fontSize: 14,
fontWeight: '500',
},
emptyContent: {
flex: 1,
},
});