import React, { useState, useEffect } from 'react'; import { useNavigate } from 'react-router-dom'; import { motion } from 'framer-motion'; import { ArrowLeft, Moon, Sun, Bell, Download, Upload, Trash2, Plus, Trash, GitBranch } from 'lucide-react'; import { Button } from '../components/ui/button'; import { Switch } from '../components/ui/switch'; import { Label } from '../components/ui/label'; import { useToast } from '../components/ui/use-toast'; import { exportData, importData, clearAllData } from '../lib/storage'; import { addIntegration, getIntegrations, removeIntegration, getGitEnabled, setGitEnabled, fetchAllGitActivity, getCachedGitActivity } from '../lib/git'; const SettingsPage = () => { const navigate = useNavigate(); const { toast } = useToast(); const [darkMode, setDarkMode] = useState(() => { return localStorage.getItem('theme') === 'dark'; }); const [notifications, setNotifications] = useState(false); const [gitEnabled, setGitEnabledState] = useState(getGitEnabled()); const [sources, setSources] = useState(() => getIntegrations()); const [form, setForm] = useState({ provider: 'github', baseUrl: '', username: '', token: '' }); const [{ lastSync }, setCacheInfo] = useState(getCachedGitActivity()); const [syncing, setSyncing] = useState(false); useEffect(() => { if (darkMode) { document.documentElement.classList.add('dark'); localStorage.setItem('theme', 'dark'); } else { document.documentElement.classList.remove('dark'); localStorage.setItem('theme', 'light'); } }, [darkMode]); const toggleDarkMode = (enabled) => { setDarkMode(enabled); }; const toggleGitEnabled = (enabled) => { setGitEnabledState(enabled); setGitEnabled(enabled); }; const handleAddSource = async () => { if (!form.username) return; const baseUrl = form.baseUrl || (form.provider === 'github' ? 'https://api.github.com' : form.provider === 'gitlab' ? 'https://gitlab.com' : ''); await addIntegration({ provider: form.provider, baseUrl, username: form.username, token: form.token }); setSources(getIntegrations()); setForm({ provider: 'github', baseUrl: '', username: '', token: '' }); }; const handleRemoveSource = (id) => { removeIntegration(id); setSources(getIntegrations()); }; const handleSyncGit = async () => { setSyncing(true); const data = await fetchAllGitActivity({ force: true }); setCacheInfo(data); setSyncing(false); }; const handleExport = () => { const data = exportData(); const blob = new Blob([data], { type: 'application/json' }); const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = `habitgrid-backup-${new Date().toISOString().split('T')[0]}.json`; a.click(); URL.revokeObjectURL(url); toast({ title: "✅ Data exported", description: "Your habits have been exported successfully.", }); }; const handleImport = () => { const input = document.createElement('input'); input.type = 'file'; input.accept = 'application/json'; input.onchange = (e) => { const file = e.target.files[0]; if (file) { const reader = new FileReader(); reader.onload = (event) => { try { importData(event.target.result); toast({ title: "✅ Data imported", description: "Your habits have been imported successfully.", }); setTimeout(() => navigate('/'), 500); } catch (error) { toast({ title: "Import failed", description: "Invalid backup file format.", variant: "destructive", }); } }; reader.readAsText(file); } }; input.click(); }; const handleClearData = () => { if (window.confirm('Are you sure you want to delete all habits? This action cannot be undone.')) { clearAllData(); toast({ title: "✅ Data cleared", description: "All habits have been deleted.", }); setTimeout(() => navigate('/'), 500); } }; const handleNotificationToggle = (enabled) => { setNotifications(enabled); toast({ title: "🚧 Feature coming soon", description: "Daily reminders will be available in a future update!", }); }; return (
{/* Header */}

Settings

Customize your experience

{/* Appearance */}

Appearance

{darkMode ? : }

Toggle dark theme

{/* Notifications */}

Notifications

Get reminded to track habits

{/* Data Management */}

Data Management

{/* Integrations */}

Integrations

Display a unified Git activity grid

setForm({ ...form, baseUrl: e.target.value })} />
setForm({ ...form, username: e.target.value })} />
setForm({ ...form, token: e.target.value })} />
{lastSync ? `Last sync: ${new Date(lastSync).toLocaleString()}` : ''}
{sources.length > 0 && (
{sources.map(src => (
{src.provider} • {src.username}
{src.baseUrl}
))}
)}
{/* About */}

About HabitGrid

Version 1.0.0 • Built with ❤️ for habit builders

Track your habits with a beautiful GitHub-style contribution grid. Build streaks, visualize progress, and commit to yourself daily.

); }; export default SettingsPage;