refactor: add AuthGuard component and fix API client empty response handling

- Add reusable AuthGuard component for protected routes
- Move auth logic from index.tsx to (tabs)/_layout.tsx via AuthGuard
- Fix ApiClient to handle empty responses (204 No Content)
- Use useFocusEffect in chat.tsx to load messages after auth is ready
- Extract getDateKey() helper function in calendar.tsx
This commit is contained in:
2026-01-25 13:03:17 +01:00
parent 43d40b46d7
commit a42e2a7c1c
7 changed files with 112 additions and 65 deletions

View File

@@ -33,6 +33,14 @@ type MonthItem = {
label: string; // e.g. "January 2024"
};
/**
* Formats a Date object to a string key in YYYY-MM-DD format.
* Used for grouping and looking up events by date.
*/
const getDateKey = (date: Date): string => {
return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, "0")}-${String(date.getDate()).padStart(2, "0")}`;
};
const generateMonths = (
centerYear: number,
centerMonth: number,
@@ -108,7 +116,7 @@ const Calendar = () => {
const map = new Map<string, ExpandedEvent[]>();
events.forEach((e) => {
const date = new Date(e.occurrenceStart);
const key = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, "0")}-${String(date.getDate()).padStart(2, "0")}`;
const key = getDateKey(date);
if (!map.has(key)) map.set(key, []);
map.get(key)!.push(e);
});
@@ -157,7 +165,7 @@ const Calendar = () => {
deleteEvent(event.id);
// Close overlay if no more events for this date
if (selectedDate) {
const dateKey = `${selectedDate.getFullYear()}-${String(selectedDate.getMonth() + 1).padStart(2, "0")}-${String(selectedDate.getDate()).padStart(2, "0")}`;
const dateKey = getDateKey(selectedDate);
const remainingEvents = eventsByDate.get(dateKey) || [];
if (remainingEvents.length <= 1) {
setSelectedDate(null);
@@ -175,7 +183,7 @@ const Calendar = () => {
// Get events for selected date
const selectedDateEvents = useMemo(() => {
if (!selectedDate) return [];
const key = `${selectedDate.getFullYear()}-${String(selectedDate.getMonth() + 1).padStart(2, "0")}-${String(selectedDate.getDate()).padStart(2, "0")}`;
const key = getDateKey(selectedDate);
return eventsByDate.get(key) || [];
}, [selectedDate, eventsByDate]);
@@ -597,10 +605,6 @@ const CalendarGrid = (props: CalendarGridProps) => {
return date;
};
const getDateKey = (date: Date): string => {
return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, "0")}-${String(date.getDate()).padStart(2, "0")}`;
};
return (
<View
className="h-full flex-1 flex-col flex-wrap gap-2 p-2"