-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathJournal.tsx
More file actions
349 lines (318 loc) · 13.3 KB
/
Journal.tsx
File metadata and controls
349 lines (318 loc) · 13.3 KB
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import React, { useState, useEffect } from 'react';
import { useNavigate } from 'react-router-dom';
import { motion } from 'framer-motion';
import {
ArrowLeft,
ChevronRight,
HelpCircle,
BookOpen,
Sun,
Moon
} from 'lucide-react';
import { Button } from '@/components/ui/button';
import { Card, CardContent } from '@/components/ui/card';
import { Slider } from '@/components/ui/slider';
import { Textarea } from '@/components/ui/textarea';
import { Badge } from '@/components/ui/badge';
import { useAuth } from '../utils/auth';
import { addJournalEntry, getJournalEntries } from '../utils/firebase';
import { toast } from 'sonner';
import { useTheme } from 'next-themes';
const DAILY_PROMPTS = [
"What's one thing you're grateful for today?",
"How did you handle urges or challenges today?",
"What's something that brought you joy today?",
"What's something you learned about yourself today?",
"What's one small victory you had today?",
"What's something you're looking forward to tomorrow?",
"What's a way you showed kindness to yourself today?",
"What's something that challenged you today and how did you respond?",
"What's one positive change you've noticed in yourself recently?",
"What did you do for self-care today?"
];
const NEGATIVE_EMOTIONS = [
"Angry", "Anxious", "Scared", "Overwhelmed", "Ashamed",
"Disgusted", "Embarrassed", "Frustrated", "Annoyed",
"Jealous", "Stressed", "Worried", "Guilty", "Surprised",
"Hopeless", "Irritated", "Lonely", "Discouraged",
"Disappointed", "Drained", "Sad"
];
const POSITIVE_EMOTIONS = [
"Amazed", "Excited", "Surprised", "Passionate", "Happy",
"Joyful", "Brave", "Proud", "Confident", "Hopeful",
"Amused", "Satisfied", "Relieved", "Grateful", "Content",
"Calm", "Peaceful", "Inspired", "Loved", "Refreshed"
];
const getMoodLabel = (score: number): string => {
if (score <= 2) return "Very Unpleasant";
if (score <= 4) return "Slightly Unpleasant";
if (score <= 6) return "Neutral";
if (score <= 8) return "Slightly Pleasant";
return "Very Pleasant";
};
const getMoodBackground = (score: number, theme: string): string => {
if (theme === 'light') {
if (score <= 2) return "bg-slate-200";
if (score <= 4) return "bg-slate-100";
if (score <= 6) return "bg-white";
if (score <= 8) return "bg-emerald-50";
return "bg-emerald-100";
} else {
if (score <= 2) return "bg-slate-800";
if (score <= 4) return "bg-slate-700";
if (score <= 6) return "bg-slate-600";
if (score <= 8) return "bg-emerald-900";
return "bg-emerald-800";
}
};
const getMoodColor = (score: number, theme: string): string => {
if (theme === 'light') {
if (score <= 4) return "text-blue-700";
if (score <= 6) return "text-slate-700";
return "text-green-700";
} else {
if (score <= 4) return "text-blue-300";
if (score <= 6) return "text-slate-200";
return "text-green-300";
}
};
const Journal: React.FC = () => {
const navigate = useNavigate();
const { currentUser } = useAuth();
const [step, setStep] = useState(1);
const [moodScore, setMoodScore] = useState(5);
const [selectedEmotions, setSelectedEmotions] = useState<string[]>([]);
const [journalText, setJournalText] = useState('');
const [prompt, setPrompt] = useState('');
const [isSubmitting, setIsSubmitting] = useState(false);
const { theme, setTheme } = useTheme();
const currentTheme = theme === 'system' ? (window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light') : theme;
useEffect(() => {
const randomIndex = Math.floor(Math.random() * DAILY_PROMPTS.length);
setPrompt(DAILY_PROMPTS[randomIndex]);
}, []);
const relevantEmotions = moodScore <= 5 ? NEGATIVE_EMOTIONS : POSITIVE_EMOTIONS;
const handleEmotionToggle = (emotion: string) => {
if (selectedEmotions.includes(emotion)) {
setSelectedEmotions(selectedEmotions.filter(e => e !== emotion));
} else {
if (selectedEmotions.length < 3) {
setSelectedEmotions([...selectedEmotions, emotion]);
} else {
toast.info("You can select up to 3 emotions");
}
}
};
const handleSubmit = async () => {
if (!currentUser) {
toast.error("You must be logged in to journal");
return;
}
if (journalText.trim() === '') {
toast.error("Please write something in your journal");
return;
}
setIsSubmitting(true);
try {
await addJournalEntry({
userId: currentUser.uid,
timestamp: new Date(),
question: prompt,
notes: journalText,
level: moodScore,
emotions: selectedEmotions
});
toast.success("Journal entry saved!");
navigate('/journal/entries');
} catch (error) {
console.error("Error saving journal entry:", error);
toast.error("Failed to save journal entry");
} finally {
setIsSubmitting(false);
}
};
const moodLabel = getMoodLabel(moodScore);
const backgroundClass = getMoodBackground(moodScore, currentTheme);
const moodColorClass = getMoodColor(moodScore, currentTheme);
const toggleTheme = () => {
setTheme(currentTheme === 'dark' ? 'light' : 'dark');
};
return (
<div className={`min-h-screen ${backgroundClass} transition-colors duration-300`}>
<div className="container max-w-md py-8 px-4 mx-auto">
<div className="flex justify-between items-center mb-8">
<Button
variant="ghost"
size="icon"
className={currentTheme === 'light' ? 'text-slate-800' : 'text-white'}
onClick={() => {
if (step > 1) {
setStep(step - 1);
} else {
navigate('/dashboard');
}
}}
>
<ArrowLeft className="h-6 w-6" />
</Button>
<div className={`text-xl font-medium ml-10 ${moodColorClass}`}>
{step === 1 ? 'How are you feeling?' : step === 2 ? 'Select Emotions' : 'Write in Journal'}
</div>
<div className="flex items-center gap-2">
<Button
variant="ghost"
className={currentTheme === 'light' ? 'text-slate-800' : 'text-white'}
disabled={step === 3 ? isSubmitting : false}
onClick={() => {
if (step < 3) {
setStep(step + 1);
} else {
handleSubmit();
}
}}
>
{step === 3 ? 'Save' : 'Next'}
{step < 3 && <ChevronRight className="ml-1 h-4 w-4" />}
</Button>
</div>
</div>
{step === 1 && (
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
className="space-y-8"
>
<div className="text-center">
<h1 className={`text-3xl font-bold mb-2 ${currentTheme === 'light' ? 'text-slate-800' : 'text-white'}`}>
Choose how you're feeling
</h1>
<p className={currentTheme === 'light' ? 'text-slate-600' : 'text-white/80'}>right now</p>
</div>
<div className="flex flex-col items-center justify-center py-2">
<div className="relative w-48 h-48 flex items-center justify-center mb-6">
<div className={`absolute inset-0 rounded-full blur-lg bg-${moodScore > 5 ? 'green' : 'blue'}-${currentTheme === 'light' ? '200' : '400'} opacity-20`}></div>
<div className={`w-36 h-36 rounded-full ${moodScore > 5 ? 'bg-green-400/30' : 'bg-blue-400/30'} flex items-center justify-center`}>
<div className={`w-24 h-24 rounded-full ${moodScore > 5 ? 'bg-green-400/50' : 'bg-blue-400/50'} flex items-center justify-center`}>
<div className={`w-16 h-16 rounded-full ${moodScore > 5 ? 'bg-green-400/70' : 'bg-blue-400/70'} flex items-center justify-center`}>
<div className={`w-8 h-8 rounded-full ${moodScore > 5 ? 'bg-green-400' : 'bg-blue-400'}`}></div>
</div>
</div>
</div>
</div>
<h2 className={`text-4xl font-bold mb-12 ${moodColorClass}`}>
{moodLabel}
</h2>
<div className="w-full px-4">
<Slider
value={[moodScore]}
min={1}
max={10}
step={1}
onValueChange={(values) => setMoodScore(values[0])}
className="my-6"
/>
<div className="flex justify-between text-sm">
<span className={currentTheme === 'light' ? 'text-slate-600' : 'text-white/70'}>VERY UNPLEASANT</span>
<span className={currentTheme === 'light' ? 'text-slate-600' : 'text-white/70'}>VERY PLEASANT</span>
</div>
</div>
</div>
</motion.div>
)}
{step === 2 && (
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
className="space-y-6"
>
<div className="text-center">
<h2 className={`text-4xl font-bold mb-2 ${moodColorClass}`}>
{moodLabel}
</h2>
<p className={`text-xl mb-8 ${currentTheme === 'light' ? 'text-slate-700' : 'text-white/80'}`}>
What best describes this feeling?
</p>
<p className={`text-sm ${currentTheme === 'light' ? 'text-slate-500' : 'text-white/60'}`}>
Select up to 3 emotions
</p>
</div>
<div className="flex flex-wrap gap-2 justify-center">
{relevantEmotions.map((emotion) => (
<Badge
key={emotion}
variant={selectedEmotions.includes(emotion) ? "default" : "outline"}
className={`text-md py-2 px-4 cursor-pointer ${
selectedEmotions.includes(emotion)
? moodScore > 5
? currentTheme === 'light' ? 'bg-green-600 hover:bg-green-700 text-white' : 'bg-green-600 hover:bg-green-700 text-white'
: currentTheme === 'light' ? 'bg-blue-600 hover:bg-blue-700 text-white' : 'bg-blue-600 hover:bg-blue-700 text-white'
: currentTheme === 'light' ? 'hover:bg-slate-200 text-slate-800' : 'hover:bg-white/10'
}`}
onClick={() => handleEmotionToggle(emotion)}
>
{emotion}
</Badge>
))}
</div>
</motion.div>
)}
{step === 3 && (
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
className="space-y-6"
>
<Card className={currentTheme === 'light' ? 'bg-white shadow-md border-slate-200' : 'bg-white/10 border-none'}>
<CardContent className="p-4">
<h3 className={`text-xl font-medium mb-2 ${currentTheme === 'light' ? 'text-slate-800' : 'text-white'}`}>Today's Prompt</h3>
<p className={currentTheme === 'light' ? 'text-slate-700 mb-4' : 'text-white/80 mb-4'}>{prompt}</p>
<Textarea
value={journalText}
onChange={(e) => setJournalText(e.target.value)}
placeholder="Write your thoughts here..."
className={currentTheme === 'light'
? 'min-h-[200px] bg-slate-50 border-slate-200 text-slate-800 placeholder:text-slate-400'
: 'min-h-[200px] bg-white/5 border-white/20 text-white placeholder:text-white/50'}
/>
</CardContent>
</Card>
<div className="space-y-4">
<h3 className={`text-xl font-medium ${currentTheme === 'light' ? 'text-slate-800' : 'text-white'}`}>Your mood</h3>
<div className="flex items-center gap-3">
<div className={`w-10 h-10 rounded-full ${moodScore > 5 ? 'bg-green-400/70' : 'bg-blue-400/70'} flex-shrink-0`}></div>
<div>
<p className={`font-medium ${moodColorClass}`}>{moodLabel}</p>
<div className="flex flex-wrap gap-1 mt-1">
{selectedEmotions.map(emotion => (
<Badge key={emotion} variant="outline" className={currentTheme === 'light' ? 'text-xs bg-slate-100 text-slate-700' : 'text-xs bg-white/10'}>
{emotion}
</Badge>
))}
</div>
</div>
</div>
</div>
</motion.div>
)}
<div className="mt-12 text-center">
<Button
variant="outline"
onClick={() => navigate('/journal/entries')}
className={`w-full py-6 journal-past-entries ${
currentTheme === 'light'
? 'border-slate-300 hover:bg-slate-100'
: 'border-white/20 hover:bg-white/10'
}`}
>
<BookOpen className="mr-2 h-5 w-5" />
Show past entries
</Button>
</div>
</div>
</div>
);
};
export default Journal;