don't ask me how, but it works

This commit is contained in:
Chris Sanden
2026-05-05 20:58:03 +02:00
parent a536be1047
commit ac6bfa1022
3 changed files with 327 additions and 394 deletions

View File

@@ -8,8 +8,6 @@ import { Redirect, router, Stack, useFocusEffect } from 'expo-router';
import { useCallback, useEffect, useState } from 'react'; import { useCallback, useEffect, useState } from 'react';
import { Alert, Modal, Pressable, ScrollView, Text, View, ActivityIndicator } from 'react-native'; import { Alert, Modal, Pressable, ScrollView, Text, View, ActivityIndicator } from 'react-native';
import type { SubjectColor } from '@/lib/subjectColors';
const FLOW_STEPS = [ const FLOW_STEPS = [
{ {
label: '1', label: '1',
@@ -77,8 +75,6 @@ export default function Subjects() {
}, [session?.user.id]); }, [session?.user.id]);
const GetSubjects = useCallback(async () => { const GetSubjects = useCallback(async () => {
if (!session?.user.id) return;
const GetSubjects = async () => {
if (!session?.user.id) { if (!session?.user.id) {
SetIsLoading(false); SetIsLoading(false);
return; return;
@@ -86,8 +82,6 @@ export default function Subjects() {
SetIsLoading(true); SetIsLoading(true);
SetIsLoading(true);
const { data, error } = await supabase const { data, error } = await supabase
.from('subjects') .from('subjects')
.select('*') .select('*')
@@ -98,14 +92,11 @@ export default function Subjects() {
if (error) { if (error) {
Alert.alert('Subjects could not be fetched, please try again'); Alert.alert('Subjects could not be fetched, please try again');
SetIsLoading(false);
return; return;
} }
SetSubjects((data as Subject[]) ?? []); SetSubjects((data as Subject[]) ?? []);
}, [session?.user.id]); }, [session?.user.id]);
SetIsLoading(false);
};
useFocusEffect( useFocusEffect(
useCallback(() => { useCallback(() => {
@@ -121,6 +112,8 @@ export default function Subjects() {
if (needsSetup) { if (needsSetup) {
return <Redirect href="/setup" />; return <Redirect href="/setup" />;
}
const RenderSubjectCard = (subject: Subject) => { const RenderSubjectCard = (subject: Subject) => {
const colorKey: SubjectColor = subject.color ?? 'slate'; const colorKey: SubjectColor = subject.color ?? 'slate';
const colorSet = SUBJECT_COLORS[colorKey]; const colorSet = SUBJECT_COLORS[colorKey];

View File

@@ -345,12 +345,6 @@ export default function ViewDetailsSubject() {
</View> </View>
</View> </View>
<View className="mt-5">
<View className="mb-2 flex-row items-center justify-between">
<Text className="text-sm font-semibold text-text-secondary">
Assignments completed
</Text>
{totalAssignments > 0 ? (
<View className="mt-5"> <View className="mt-5">
<View className="mb-2 flex-row items-center justify-between"> <View className="mb-2 flex-row items-center justify-between">
<Text className="text-sm font-semibold text-text-secondary"> <Text className="text-sm font-semibold text-text-secondary">
@@ -379,8 +373,11 @@ export default function ViewDetailsSubject() {
remainingAssignments === 1 ? '' : 's' remainingAssignments === 1 ? '' : 's'
} remaining`} } remaining`}
</Text> </Text>
<Text className="mt-1 text-xs text-text-muted">
Based only on completed assignments in this subject.
</Text>
</View> </View>
) : null}
<Text className="mt-4 text-sm text-text-muted"> <Text className="mt-4 text-sm text-text-muted">
Last changed: {formatDateTime(subject.lastChanged)} Last changed: {formatDateTime(subject.lastChanged)}

View File

@@ -31,18 +31,19 @@ function formatTrackedTime(totalSeconds: number) {
} }
export default function ViewDetailsTask() { export default function ViewDetailsTask() {
const { tId } = useLocalSearchParams<{ tId: string }>(); const { tId } = useLocalSearchParams<{ tId: string }>();
const [task, SetTask] = useState<Task | null>(null); const [task, SetTask] = useState<Task | null>(null);
const [session, SetSession] = useState<Session | null>(null); const [session, SetSession] = useState<Session | null>(null);
const [completedFocusSessions, setCompletedFocusSessions] = useState(0); const [isLoading, SetIsLoading] = useState(false);
const [contextMeta, setContextMeta] = useState({ const [completedFocusSessions, setCompletedFocusSessions] = useState(0);
const [contextMeta, setContextMeta] = useState({
subjectTitle: 'No Subject', subjectTitle: 'No Subject',
assignmentTitle: 'No Assignment', assignmentTitle: 'No Assignment',
subjectColor: 'slate' as SubjectColor, subjectColor: 'slate' as SubjectColor,
}); });
useEffect(() => { useEffect(() => {
supabase.auth.getSession().then(({ data }) => SetSession(data.session ?? null)); supabase.auth.getSession().then(({ data }) => SetSession(data.session ?? null));
const { data: sub } = supabase.auth.onAuthStateChange((_event, newSession) => { const { data: sub } = supabase.auth.onAuthStateChange((_event, newSession) => {
@@ -50,9 +51,9 @@ useEffect(() => {
}); });
return () => sub.subscription.unsubscribe(); return () => sub.subscription.unsubscribe();
}, []); }, []);
const loadTaskStudyActivity = useCallback(async (taskId: string, userId: string) => { const loadTaskStudyActivity = useCallback(async (taskId: string, userId: string) => {
const { count, error } = await supabase const { count, error } = await supabase
.from('sprint_sessions') .from('sprint_sessions')
.select('sessionId', { count: 'exact', head: true }) .select('sessionId', { count: 'exact', head: true })
@@ -67,9 +68,11 @@ const loadTaskStudyActivity = useCallback(async (taskId: string, userId: string)
} }
setCompletedFocusSessions(count ?? 0); setCompletedFocusSessions(count ?? 0);
}, []); }, []);
const GetTask = useCallback(async (taskId: string) => {
SetIsLoading(true);
const GetTask = useCallback(async (taskId: string) => {
const { data, error } = await supabase const { data, error } = await supabase
.from('tasks') .from('tasks')
.select('*') .select('*')
@@ -77,6 +80,14 @@ const GetTask = useCallback(async (taskId: string) => {
.single(); .single();
if (error || !data) { if (error || !data) {
SetTask(null);
setContextMeta({
subjectTitle: 'Unknown Subject',
assignmentTitle: 'Unknown Assignment',
subjectColor: 'slate',
});
setCompletedFocusSessions(0);
SetIsLoading(false);
Alert.alert('Task could not be fetched, please try again'); Alert.alert('Task could not be fetched, please try again');
return; return;
} }
@@ -84,30 +95,21 @@ const GetTask = useCallback(async (taskId: string) => {
SetTask(data); SetTask(data);
await loadTaskStudyActivity(taskId, data.uId); await loadTaskStudyActivity(taskId, data.uId);
let nextContextMeta = {
subjectTitle: 'Unknown Subject',
assignmentTitle: 'Unknown Assignment',
subjectColor: 'slate' as SubjectColor,
};
if (data.aId) { if (data.aId) {
const { data: assignmentData, error: assignmentError } = await supabase const { data: assignmentData, error: assignmentError } = await supabase
.from('assignments') .from('assignments')
.select('title, sId') .select('title, sId')
.eq('aId', data.aId) .eq('aId', data.aId)
const GetTask = async (taskId: string) => {
SetIsLoading(true);
const { data, error } = await supabase
.from('tasks')
.select('*')
.eq('tId', taskId)
.single(); .single();
SetIsLoading(false); if (!assignmentError && assignmentData) {
nextContextMeta.assignmentTitle = assignmentData.title ?? 'Unknown Assignment';
if (assignmentError || !assignmentData) {
setContextMeta({
subjectTitle: 'Unknown Subject',
assignmentTitle: 'Unknown Assignment',
subjectColor: 'slate',
});
return;
}
if (assignmentData.sId) { if (assignmentData.sId) {
const { data: subjectData, error: subjectError } = await supabase const { data: subjectData, error: subjectError } = await supabase
@@ -116,66 +118,30 @@ const GetTask = useCallback(async (taskId: string) => {
.eq('sId', assignmentData.sId) .eq('sId', assignmentData.sId)
.single(); .single();
if (subjectError || !subjectData) { if (!subjectError && subjectData) {
SetTask(data); nextContextMeta = {
if (data.aId) {
SetIsLoading(true);
const { data: assignmentData, error: assignmentError } = await supabase
.from('assignments')
.select('title, sId')
.eq('aId', data.aId)
.single();
SetIsLoading(false);
if (assignmentError || !assignmentData) {
setContextMeta({
subjectTitle: 'Unknown Subject',
assignmentTitle: assignmentData.title ?? 'Unknown Assignment',
subjectColor: 'slate',
});
return;
}
setContextMeta({
subjectTitle: subjectData.title ?? 'Unknown Subject', subjectTitle: subjectData.title ?? 'Unknown Subject',
assignmentTitle: assignmentData.title ?? 'Unknown Assignment', assignmentTitle: assignmentData.title ?? 'Unknown Assignment',
subjectColor: (subjectData.color as SubjectColor | undefined) ?? 'slate', subjectColor: (subjectData.color as SubjectColor | undefined) ?? 'slate',
}); };
}
}
} }
} }
}, [loadTaskStudyActivity]);
if (assignmentData.sId) {
SetIsLoading(true);
const { data: subjectData, error: subjectError } = await supabase
.from('subjects')
.select('title, color')
.eq('sId', assignmentData.sId)
.single();
setContextMeta(nextContextMeta);
SetIsLoading(false); SetIsLoading(false);
}, [loadTaskStudyActivity]);
if (subjectError || !subjectData) { useFocusEffect(
setContextMeta({
subjectTitle: 'Unknown Subject',
assignmentTitle: assignmentData.title ?? 'Unknown Assignment',
subjectColor: 'slate',
});
return;
}
useFocusEffect(
useCallback(() => { useCallback(() => {
if (session && tId) { if (session && tId) {
GetTask(tId); void GetTask(tId);
} }
}, [GetTask, session, tId]) }, [GetTask, session, tId])
); );
const handleSprintStart = async () => { const handleSprintStart = async () => {
const activeSession = await GetActiveSession(); const activeSession = await GetActiveSession();
if (!activeSession) { if (!activeSession) {
@@ -189,9 +155,7 @@ const handleSprintStart = async () => {
return; return;
} }
const secondsLeft = Math.ceil((activeSession.endTime - Date.now()) / 1000);
const secondsLeft = Math.ceil((activeSession.endTime - Date.now()) / 1000)
if (secondsLeft <= 0) { if (secondsLeft <= 0) {
await finalizeStoredSession('expired', activeSession); await finalizeStoredSession('expired', activeSession);
@@ -200,19 +164,19 @@ const handleSprintStart = async () => {
params: { params: {
tId: task?.tId, tId: task?.tId,
durationMinutes: String(DEFAULT_FOCUS_DURATION_MINUTES), durationMinutes: String(DEFAULT_FOCUS_DURATION_MINUTES),
} },
}); });
return; return;
} }
if (activeSession.taskId === task?.tId) { if (activeSession.taskId === task?.tId) {
router.push({ router.push({
pathname: '/task/timer', pathname: '/task/timer',
params: { params: {
tId: activeSession.taskId ?? undefined, tId: activeSession.taskId ?? undefined,
durationMinutes: String(DEFAULT_FOCUS_DURATION_MINUTES), durationMinutes: String(DEFAULT_FOCUS_DURATION_MINUTES),
}}); },
});
return; return;
} }
@@ -220,7 +184,7 @@ const handleSprintStart = async () => {
'Active session in progress', 'Active session in progress',
`End the current session and start a new ${DEFAULT_FOCUS_DURATION_MINUTES} minute sprint on this task?`, `End the current session and start a new ${DEFAULT_FOCUS_DURATION_MINUTES} minute sprint on this task?`,
[ [
{ text: 'Cancel', style: 'cancel', }, { text: 'Cancel', style: 'cancel' },
{ {
text: 'Start new sprint', text: 'Start new sprint',
style: 'destructive', style: 'destructive',
@@ -239,37 +203,7 @@ const handleSprintStart = async () => {
); );
}; };
const colorSet = getSubjectColorSet(contextMeta.subjectColor); const DeleteTask = async (taskId: string) => {
if (isLoading) {
return (
<View className="flex-1 items-center justify-center bg-app-bg">
<ActivityIndicator size="large" />
</View>
);
}
if (!task) {
return (
<View className="flex-1 bg-app-bg px-5 pt-6">
<Stack.Screen
options={{
title: 'Task Details',
headerRight: () => (
<Pressable
className="rounded-full bg-app-subtle px-4 py-2"
onPress={async () => await supabase.auth.signOut()}
>
<Text className="text-sm font-semibold text-text-secondary">
Logout
</Text>
</Pressable>
),
}}
/>
const DeleteTask = async (taskId: string) => {
Alert.alert( Alert.alert(
'Delete Task', 'Delete Task',
'Are you sure you want to delete this task?', 'Are you sure you want to delete this task?',
@@ -308,11 +242,19 @@ const DeleteTask = async (taskId: string) => {
}, },
] ]
); );
}; };
const colorSet = getSubjectColorSet(contextMeta.subjectColor); const colorSet = getSubjectColorSet(contextMeta.subjectColor);
if (!task) { if (isLoading) {
return (
<View className="flex-1 items-center justify-center bg-app-bg">
<ActivityIndicator size="large" />
</View>
);
}
if (!task) {
return ( return (
<View className="flex-1 bg-app-bg px-5 pt-6"> <View className="flex-1 bg-app-bg px-5 pt-6">
<Stack.Screen <Stack.Screen
@@ -356,11 +298,11 @@ if (!task) {
</View> </View>
</View> </View>
); );
} }
const isOwner = session?.user.id === task.uId; const isOwner = session?.user.id === task.uId;
return ( return (
<View className="flex-1 bg-app-bg px-5 pt-6"> <View className="flex-1 bg-app-bg px-5 pt-6">
<Stack.Screen <Stack.Screen
options={{ options={{
@@ -394,9 +336,9 @@ return (
backgroundColor: task.isCompleted ? colorSet.strong : '#EFEBE3', backgroundColor: task.isCompleted ? colorSet.strong : '#EFEBE3',
}} }}
> >
{task.isCompleted && ( {task.isCompleted ? (
<Text className="text-sm font-bold text-text-inverse"></Text> <Text className="text-sm font-bold text-text-inverse"></Text>
)} ) : null}
</View> </View>
<View className="flex-1"> <View className="flex-1">
@@ -479,11 +421,11 @@ return (
</View> </View>
</View> </View>
{isOwner && ( {isOwner ? (
<View className="mt-5 border-t border-app-border pt-5"> <View className="mt-5 border-t border-app-border pt-5">
<Pressable <Pressable
className="h-14 items-center justify-center rounded-2xl bg-accent" className="h-14 items-center justify-center rounded-2xl bg-accent"
onPress={() => handleSprintStart()} onPress={handleSprintStart}
> >
<Text className="text-base font-bold text-text-inverse"> <Text className="text-base font-bold text-text-inverse">
Start Sprint Start Sprint
@@ -508,6 +450,7 @@ return (
Edit Edit
</Text> </Text>
</Pressable> </Pressable>
<Pressable <Pressable
className="flex-1 items-center justify-center rounded-2xl border border-app-border bg-app-surface py-3" className="flex-1 items-center justify-center rounded-2xl border border-app-border bg-app-surface py-3"
onPress={() => DeleteTask(task.tId)} onPress={() => DeleteTask(task.tId)}
@@ -518,7 +461,7 @@ return (
</Pressable> </Pressable>
</View> </View>
</View> </View>
)} ) : null}
</View> </View>
</View> </View>
); );