1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
import {
faBoxOpen,
faPen,
faPlus,
faSpinner,
faTrash,
} from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { useCallback, useEffect, useState } from "react";
import { Link } from "wouter";
import { ApiClientError, apiClient } from "../api";
import { CreateDeckModal } from "../components/CreateDeckModal";
import { DeleteDeckModal } from "../components/DeleteDeckModal";
import { EditDeckModal } from "../components/EditDeckModal";
import { SyncButton } from "../components/SyncButton";
import { SyncStatusIndicator } from "../components/SyncStatusIndicator";
import { useAuth } from "../stores";
interface Deck {
id: string;
name: string;
description: string | null;
newCardsPerDay: number;
createdAt: string;
updatedAt: string;
}
export function HomePage() {
const { logout } = useAuth();
const [decks, setDecks] = useState<Deck[]>([]);
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const [isCreateModalOpen, setIsCreateModalOpen] = useState(false);
const [editingDeck, setEditingDeck] = useState<Deck | null>(null);
const [deletingDeck, setDeletingDeck] = useState<Deck | null>(null);
const fetchDecks = useCallback(async () => {
setIsLoading(true);
setError(null);
try {
const res = await apiClient.rpc.api.decks.$get(undefined, {
headers: apiClient.getAuthHeader(),
});
if (!res.ok) {
const errorBody = await res.json().catch(() => ({}));
throw new ApiClientError(
(errorBody as { error?: string }).error ||
`Request failed with status ${res.status}`,
res.status,
);
}
const data = await res.json();
setDecks(data.decks);
} catch (err) {
if (err instanceof ApiClientError) {
setError(err.message);
} else {
setError("Failed to load decks. Please try again.");
}
} finally {
setIsLoading(false);
}
}, []);
useEffect(() => {
fetchDecks();
}, [fetchDecks]);
return (
<div className="min-h-screen bg-cream">
{/* Header */}
<header className="bg-white border-b border-border/50 sticky top-0 z-10">
<div className="max-w-4xl mx-auto px-4 py-4 flex items-center justify-between">
<h1 className="font-display text-2xl font-semibold text-ink">
Kioku
</h1>
<div className="flex items-center gap-3">
<SyncStatusIndicator />
<SyncButton />
<button
type="button"
onClick={logout}
className="text-sm text-muted hover:text-slate transition-colors px-3 py-1.5 rounded-lg hover:bg-ivory"
>
Logout
</button>
</div>
</div>
</header>
{/* Main Content */}
<main className="max-w-4xl mx-auto px-4 py-8">
{/* Section Header */}
<div className="flex items-center justify-between mb-6">
<h2 className="font-display text-xl font-medium text-slate">
Your Decks
</h2>
<button
type="button"
onClick={() => setIsCreateModalOpen(true)}
className="inline-flex items-center gap-2 bg-primary hover:bg-primary-dark text-white font-medium py-2 px-4 rounded-lg transition-all duration-200 active:scale-[0.98] shadow-sm hover:shadow-md"
>
<FontAwesomeIcon
icon={faPlus}
className="w-5 h-5"
aria-hidden="true"
/>
New Deck
</button>
</div>
{/* Loading State */}
{isLoading && (
<div className="flex items-center justify-center py-12">
<FontAwesomeIcon
icon={faSpinner}
className="h-8 w-8 text-primary animate-spin"
aria-hidden="true"
/>
</div>
)}
{/* Error State */}
{error && (
<div
role="alert"
className="bg-error/5 border border-error/20 rounded-xl p-4 flex items-center justify-between"
>
<span className="text-error">{error}</span>
<button
type="button"
onClick={fetchDecks}
className="text-error hover:text-error/80 font-medium text-sm"
>
Retry
</button>
</div>
)}
{/* Empty State */}
{!isLoading && !error && decks.length === 0 && (
<div className="text-center py-16 animate-fade-in">
<div className="w-16 h-16 mx-auto mb-4 bg-ivory rounded-2xl flex items-center justify-center">
<FontAwesomeIcon
icon={faBoxOpen}
className="w-8 h-8 text-muted"
aria-hidden="true"
/>
</div>
<h3 className="font-display text-lg font-medium text-slate mb-2">
No decks yet
</h3>
<p className="text-muted text-sm mb-6">
Create your first deck to start learning
</p>
<button
type="button"
onClick={() => setIsCreateModalOpen(true)}
className="inline-flex items-center gap-2 bg-primary hover:bg-primary-dark text-white font-medium py-2.5 px-5 rounded-lg transition-all duration-200"
>
<FontAwesomeIcon
icon={faPlus}
className="w-5 h-5"
aria-hidden="true"
/>
Create Your First Deck
</button>
</div>
)}
{/* Deck List */}
{!isLoading && !error && decks.length > 0 && (
<div className="space-y-3 animate-fade-in">
{decks.map((deck, index) => (
<div
key={deck.id}
className="bg-white rounded-xl border border-border/50 p-5 shadow-card hover:shadow-md transition-all duration-200 group"
style={{ animationDelay: `${index * 50}ms` }}
>
<div className="flex items-start justify-between gap-4">
<div className="flex-1 min-w-0">
<Link
href={`/decks/${deck.id}`}
className="block group-hover:text-primary transition-colors"
>
<h3 className="font-display text-lg font-medium text-slate truncate">
{deck.name}
</h3>
</Link>
{deck.description && (
<p className="text-muted text-sm mt-1 line-clamp-2">
{deck.description}
</p>
)}
</div>
<div className="flex items-center gap-2 shrink-0">
<button
type="button"
onClick={() => setEditingDeck(deck)}
className="p-2 text-muted hover:text-slate hover:bg-ivory rounded-lg transition-colors"
title="Edit deck"
>
<FontAwesomeIcon
icon={faPen}
className="w-4 h-4"
aria-hidden="true"
/>
</button>
<button
type="button"
onClick={() => setDeletingDeck(deck)}
className="p-2 text-muted hover:text-error hover:bg-error/5 rounded-lg transition-colors"
title="Delete deck"
>
<FontAwesomeIcon
icon={faTrash}
className="w-4 h-4"
aria-hidden="true"
/>
</button>
</div>
</div>
</div>
))}
</div>
)}
</main>
{/* Modals */}
<CreateDeckModal
isOpen={isCreateModalOpen}
onClose={() => setIsCreateModalOpen(false)}
onDeckCreated={fetchDecks}
/>
<EditDeckModal
isOpen={editingDeck !== null}
deck={editingDeck}
onClose={() => setEditingDeck(null)}
onDeckUpdated={fetchDecks}
/>
<DeleteDeckModal
isOpen={deletingDeck !== null}
deck={deletingDeck}
onClose={() => setDeletingDeck(null)}
onDeckDeleted={fetchDecks}
/>
</div>
);
}
|