"use client"; import { useEffect, useState } from "react"; import { LogoutButton } from "@/components/logout-button"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card"; import { Checkbox } from "@/components/ui/checkbox"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { clearDailyReminderSentToday, getNotificationPermissionLabel, getNotificationPermissionState, isNotificationSupported, requestNotificationPermission, showTestDailyReminderNotification, } from "@/lib/daily-reminders"; import { getDailyReminderStatus } from "@/lib/daily-reminder-status"; import { getTodayDateKey } from "@/lib/habits-storage"; import { avatarVibeOptions, defaultProfilePreferences, focusTopicOptions, getProfilePreferences, saveProfilePreferences, type ProfilePreferences, } from "@/lib/profile-preferences-storage"; type ProfilePreferencesFormProps = { email?: string | null; }; export function ProfilePreferencesForm({ email }: ProfilePreferencesFormProps) { const [preferences, setPreferences] = useState( defaultProfilePreferences, ); const [isReady, setIsReady] = useState(false); const [error, setError] = useState(null); const [isSaving, setIsSaving] = useState(false); const [notificationPermission, setNotificationPermission] = useState( getNotificationPermissionState(), ); const [notificationMessage, setNotificationMessage] = useState( null, ); useEffect(() => { setNotificationPermission(getNotificationPermissionState()); }, []); useEffect(() => { async function loadPreferences() { try { setPreferences(await getProfilePreferences()); } catch (loadError) { setError( loadError instanceof Error ? loadError.message : "Could not load profile preferences.", ); } finally { setIsReady(true); } } void loadPreferences(); }, []); const updatePreferences = async (updates: Partial) => { const nextPreferences = { ...preferences, ...updates }; setPreferences(nextPreferences); setIsSaving(true); setError(null); try { const savedPreferences = await saveProfilePreferences(nextPreferences); setPreferences(savedPreferences); if ( "dailyReminderEnabled" in updates || "dailyReminderTime" in updates ) { clearDailyReminderSentToday(getTodayDateKey()); } } catch (saveError) { setPreferences(preferences); setError( saveError instanceof Error ? saveError.message : "Could not save profile preferences.", ); } finally { setIsSaving(false); } }; const handleDailyReminderToggle = async (checked: boolean) => { if (checked) { if (!isNotificationSupported()) { setError("This browser does not support notifications."); return; } const permission = await requestNotificationPermission(); setNotificationPermission(permission); if (permission !== "granted") { setError( permission === "denied" ? "Notifications are blocked. Enable them in your browser settings to use daily reminders." : "Notification permission was not granted.", ); return; } } setError(null); await updatePreferences({ dailyReminderEnabled: checked }); }; const handleTestNotification = async () => { setNotificationMessage(null); setError(null); if (!isNotificationSupported()) { setError("This browser does not support notifications."); return; } const permission = await requestNotificationPermission(); setNotificationPermission(permission); if (permission !== "granted") { setError( permission === "denied" ? "Notifications are blocked. Enable them in your browser settings." : "Notification permission was not granted.", ); return; } const status = await getDailyReminderStatus(); const shown = showTestDailyReminderNotification(status); if (!shown) { setError("Could not show a test notification."); return; } setNotificationMessage( "Test notification sent. Check the corner of your screen or Windows notification center.", ); }; if (!isReady) { return (

Loading profile...

); } return (
{error ?

{error}

: null} Profile information Your account details.
Focus topic preference Choose the one area you want your pet and daily tasks to focus on. {focusTopicOptions.map((topic) => ( ))} Avatar preferences Personalize your pet's vibe. {avatarVibeOptions.map((vibe) => ( ))} Notification preferences Control reminders for your daily quiz and habits.
void handleDailyReminderToggle(checked === true) } />
Browser permission {getNotificationPermissionLabel(notificationPermission)}
void updatePreferences({ dailyReminderTime: event.target.value }) } />

Reminders use your browser's local time and notify you once per day when the quiz or habits are still incomplete. Keep this app open in a tab for reminders to fire. On Windows, check Focus Assist if notifications are hidden.

{notificationMessage ? (

{notificationMessage}

) : null}
Account settings Session and onboarding status.
Onboarding Complete
); }