File size: 18,617 Bytes
b180cdf
 
 
 
 
0ef5c60
 
 
 
 
 
b180cdf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0ef5c60
 
 
 
 
 
 
 
 
 
 
 
 
 
b180cdf
0ef5c60
 
 
 
 
 
 
b180cdf
 
 
 
 
0ef5c60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b180cdf
 
 
0ef5c60
 
 
 
b180cdf
 
 
 
0ef5c60
 
b180cdf
 
 
 
 
 
 
 
 
 
 
 
0ef5c60
 
 
 
b180cdf
0ef5c60
 
 
b180cdf
 
0ef5c60
 
 
b180cdf
0ef5c60
 
 
 
b180cdf
0ef5c60
 
b180cdf
 
0ef5c60
 
 
b180cdf
0ef5c60
 
b180cdf
0ef5c60
 
 
 
 
 
b180cdf
 
 
 
0ef5c60
b180cdf
0ef5c60
 
 
 
 
 
b180cdf
0ef5c60
 
b180cdf
0ef5c60
 
 
 
 
b180cdf
 
 
 
 
 
 
 
0ef5c60
 
 
 
 
 
 
 
 
 
 
b180cdf
0ef5c60
 
 
 
b180cdf
 
0ef5c60
b180cdf
 
0ef5c60
b180cdf
 
0ef5c60
 
 
b180cdf
 
0ef5c60
 
b180cdf
0ef5c60
 
 
 
b180cdf
 
 
 
 
0ef5c60
b180cdf
 
0ef5c60
 
b180cdf
 
 
 
 
 
 
 
 
 
 
 
0ef5c60
b180cdf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0ef5c60
b180cdf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0ef5c60
b180cdf
 
 
 
 
 
 
 
 
0ef5c60
 
 
b180cdf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0ef5c60
b180cdf
 
 
 
 
 
 
0ef5c60
b180cdf
 
 
 
0ef5c60
b180cdf
 
 
 
 
 
 
 
 
 
0ef5c60
b180cdf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0ef5c60
b180cdf
 
 
 
0ef5c60
 
 
b180cdf
0ef5c60
 
b180cdf
 
 
0ef5c60
b180cdf
 
 
 
 
0ef5c60
b180cdf
0ef5c60
 
 
 
 
 
 
b180cdf
0ef5c60
 
 
b180cdf
 
 
 
 
 
0ef5c60
b180cdf
0ef5c60
 
b180cdf
 
 
 
 
0ef5c60
 
 
 
 
 
 
 
 
 
b180cdf
 
0ef5c60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b180cdf
0ef5c60
 
 
b180cdf
 
 
 
 
 
 
 
 
 
 
 
 
 
0ef5c60
b180cdf
0ef5c60
 
b180cdf
0ef5c60
 
b180cdf
 
 
 
 
 
 
0ef5c60
 
 
 
 
 
 
 
b180cdf
0ef5c60
b180cdf
 
 
0ef5c60
 
 
 
 
 
 
 
 
 
b180cdf
0ef5c60
 
 
 
 
 
 
b180cdf
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
// web/src/components/Header.tsx
import React, { useState } from "react";
import { Button } from "./ui/button";
import { Menu, Sun, Moon, Languages, ChevronDown, LogOut, Plus, X, Edit, Star } from "lucide-react";
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "./ui/tooltip";
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuTrigger,
  DropdownMenuSeparator,
} from "./ui/dropdown-menu";
import clareAvatar from "../assets/dfe44dab3ad8cd93953eac4a3e68bd1a5f999653.png";
import type { Workspace, CourseInfo } from "../App";
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter } from "./ui/dialog";
import { Input } from "./ui/input";
import { Label } from "./ui/label";
import { RadioGroup, RadioGroupItem } from "./ui/radio-group";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "./ui/select";
import { toast } from "sonner";
import { ProfileEditor } from "./ProfileEditor";

type UserType = {
  name: string;
  email: string;
};

type Language = "auto" | "en" | "zh";

interface HeaderProps {
  user: UserType | null;
  onMenuClick: () => void;
  onUserClick: () => void;
  isDarkMode: boolean;
  onToggleDarkMode: () => void;
  language: Language;
  onLanguageChange: (lang: Language) => void;
  workspaces: Workspace[];
  currentWorkspace: Workspace | undefined;
  onWorkspaceChange: (workspaceId: string) => void;
  onCreateWorkspace?: (payload: {
    name: string;
    category: "course" | "personal";
    courseId?: string;
    invites: string[];
  }) => void;
  onLogout: () => void;
  availableCourses?: CourseInfo[];
  onUserUpdate?: (user: UserType) => void;

  // ✅ NEW: controlled review-star display + click behavior
  reviewStarOpacity?: number; // 0..1
  reviewEnergyPct?: number;   // 0..100
  onStarClick?: () => void;   // recommended: switch to Review
}

export function Header({
  user,
  onMenuClick,
  onUserClick,
  isDarkMode,
  onToggleDarkMode,
  language,
  onLanguageChange,
  workspaces,
  currentWorkspace,
  onWorkspaceChange,
  onLogout,
  onCreateWorkspace,
  availableCourses = [],
  onUserUpdate,
  reviewStarOpacity,
  reviewEnergyPct,
  onStarClick,
}: HeaderProps) {
  const [showProfileEditor, setShowProfileEditor] = useState(false);

  const [createOpen, setCreateOpen] = useState(false);
  const [workspaceName, setWorkspaceName] = useState("");
  const [category, setCategory] = useState<"course" | "personal">("course");
  const [courseId, setCourseId] = useState("");
  const [inviteEmail, setInviteEmail] = useState("");
  const [invites, setInvites] = useState<string[]>([]);

  const opacity = typeof reviewStarOpacity === "number" ? reviewStarOpacity : 0.15;
  const energy = typeof reviewEnergyPct === "number" ? reviewEnergyPct : Math.round(opacity * 100);

  const getStarStyle = () => {
    if (energy <= 0) {
      return { fill: "transparent", stroke: "white", strokeWidth: 1.5, strokeDasharray: "2 2" };
    }
    if (energy >= 60) return { fill: "#fbbf24", stroke: "#fbbf24", strokeWidth: 0 };
    if (energy >= 25) return { fill: "#fcd34d", stroke: "#fcd34d", strokeWidth: 0 };
    return { fill: "#fde68a", stroke: "#fde68a", strokeWidth: 0 };
  };

  const addInvite = () => {
    const email = inviteEmail.trim();
    if (!email) return;
    if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
      toast.error("Please enter a valid email");
      return;
    }
    if (invites.includes(email)) return;
    setInvites((prev) => [...prev, email]);
    setInviteEmail("");
  };

  const removeInvite = (email: string) => {
    setInvites((prev) => prev.filter((e) => e !== email));
  };

  const handleCreate = () => {
    if (!workspaceName.trim()) {
      toast.error("Please enter a workspace name");
      return;
    }
    if (category === "course" && !courseId) {
      toast.error("Please select a course");
      return;
    }
    if (invites.length === 0) {
      toast.error("Please add at least one member");
      return;
    }

    onCreateWorkspace?.({
      name: workspaceName.trim(),
      category,
      courseId: courseId || undefined,
      invites,
    });

    setWorkspaceName("");
    setCourseId("");
    setCategory("course");
    setInvites([]);
    setInviteEmail("");
    setCreateOpen(false);
  };

  return (
    <header className="h-16 border-b border-border bg-card px-4 lg:px-6 flex items-center justify-between sticky top-0 z-[100]">
      <div className="flex items-center gap-4">
        <Button variant="ghost" size="icon" className="lg:hidden" onClick={onMenuClick}>
          <Menu className="h-5 w-5" />
        </Button>

        <div className="flex items-center gap-3">
          <div className="w-10 h-10 rounded-full overflow-hidden bg-white flex items-center justify-center">
            <img src={clareAvatar} alt="Clare AI" className="w-full h-full object-cover" />
          </div>
          <div>
            <h1
              className="text-lg sm:text-xl tracking-tight"
              style={{ fontFamily: "Inter, sans-serif", fontWeight: 600, letterSpacing: "-0.02em" }}
            >
              Clare{" "}
              <span className="text-sm font-bold text-muted-foreground hidden sm:inline ml-2">
                Your Personalized AI Tutor
              </span>
            </h1>
            <p className="text-xs text-muted-foreground hidden sm:block">
              Personalized guidance, review, and intelligent reinforcement
            </p>
          </div>
        </div>
      </div>

      <div className="flex items-center gap-2">
        <DropdownMenu>
          <DropdownMenuTrigger asChild>
            <Button variant="ghost" size="icon" aria-label="Change language">
              <Languages className="h-5 w-5" />
            </Button>
          </DropdownMenuTrigger>
          <DropdownMenuContent align="end">
            <DropdownMenuItem onClick={() => onLanguageChange("auto")}>
              {language === "auto" && "✓ "}Auto
            </DropdownMenuItem>
            <DropdownMenuItem onClick={() => onLanguageChange("en")}>
              {language === "en" && "✓ "}English
            </DropdownMenuItem>
            <DropdownMenuItem onClick={() => onLanguageChange("zh")}>
              {language === "zh" && "✓ "}简体中文
            </DropdownMenuItem>
          </DropdownMenuContent>
        </DropdownMenu>

        <Button variant="ghost" size="icon" onClick={onToggleDarkMode} aria-label="Toggle dark mode">
          {isDarkMode ? <Sun className="h-5 w-5" /> : <Moon className="h-5 w-5" />}
        </Button>

        {user && currentWorkspace ? (
          <>
            <DropdownMenu>
              <DropdownMenuTrigger asChild>
                <Button variant="outline" className="gap-2 pl-2 pr-3" aria-label="Switch workspace">
                  <img
                    src={currentWorkspace.avatar}
                    alt={currentWorkspace.name}
                    className="w-6 h-6 rounded-full object-cover"
                  />
                  <span className="hidden sm:inline max-w-[120px] truncate">{currentWorkspace.name}</span>
                  <ChevronDown className="h-4 w-4 opacity-50" />
                </Button>
              </DropdownMenuTrigger>

              <DropdownMenuContent align="end" className="min-w-[14rem]">
                {workspaces.map((workspace) => (
                  <DropdownMenuItem
                    key={workspace.id}
                    onClick={() => onWorkspaceChange(workspace.id)}
                    className={`gap-3 ${currentWorkspace.id === workspace.id ? "bg-accent" : ""}`}
                  >
                    <img
                      src={workspace.avatar}
                      alt={workspace.name}
                      className="w-6 h-6 rounded-full object-cover flex-shrink-0"
                    />
                    <span className="truncate">{workspace.name}</span>
                    {currentWorkspace.id === workspace.id && <span className="ml-auto text-primary"></span>}
                  </DropdownMenuItem>
                ))}
                <DropdownMenuSeparator />
                <DropdownMenuItem className="gap-2" onClick={() => setCreateOpen(true)}>
                  <Plus className="h-4 w-4" />
                  <span>New Group Workspace</span>
                </DropdownMenuItem>
              </DropdownMenuContent>
            </DropdownMenu>

            {/* Profile Avatar Button */}
            <div className="relative inline-block">
              <DropdownMenu>
                <DropdownMenuTrigger asChild>
                  <Button variant="ghost" size="icon" className="rounded-full" aria-label="User profile">
                    <img
                      src={`https://api.dicebear.com/7.x/avataaars/svg?seed=${encodeURIComponent(user.email)}`}
                      alt={user.name}
                      className="w-8 h-8 rounded-full object-cover"
                    />
                  </Button>
                </DropdownMenuTrigger>

                <DropdownMenuContent align="end" className="w-56">
                  <div className="px-2 py-1.5">
                    <div className="flex items-center justify-between gap-2">
                      <div className="flex-1 min-w-0">
                        <p className="text-sm font-medium truncate">{user.name}</p>
                        <p className="text-xs text-muted-foreground truncate">
                          ID: {user.email.split("@")[0] || user.email}
                        </p>
                      </div>

                      <div className="flex items-center gap-2 flex-shrink-0">
                        <Star
                          className="w-4 h-4"
                          style={{
                            ...getStarStyle(),
                            opacity,
                            filter: energy >= 85 ? "drop-shadow(0 0 2px rgba(251, 191, 36, 0.8))" : "none",
                          }}
                        />
                        <div className="flex flex-col items-end">
                          <span className="text-xs font-medium">{energy}%</span>
                          <div className="w-12 h-1.5 bg-muted rounded-full overflow-hidden">
                            <div
                              className="h-full bg-gradient-to-r from-amber-400 to-yellow-500 transition-all duration-300"
                              style={{ width: `${energy}%` }}
                            />
                          </div>
                        </div>
                      </div>
                    </div>
                  </div>

                  <DropdownMenuSeparator />

                  <DropdownMenuItem onClick={() => setShowProfileEditor(true)}>
                    <Edit className="h-4 w-4 mr-2" />
                    Edit Profile
                  </DropdownMenuItem>

                  <DropdownMenuSeparator />

                  <DropdownMenuItem onClick={onLogout} className="text-destructive focus:text-destructive">
                    <LogOut className="h-4 w-4 mr-2" />
                    Log out
                  </DropdownMenuItem>
                </DropdownMenuContent>
              </DropdownMenu>

              {/* Star badge in top-right corner of avatar */}
              <TooltipProvider>
                <Tooltip>
                  <TooltipTrigger asChild>
                    <button
                      type="button"
                      className="absolute cursor-pointer z-20 pointer-events-auto bg-transparent border-0 p-0"
                      style={{
                        top: "-8px",
                        right: "-16px",
                        opacity,
                        transition: "opacity 0.3s ease-in-out",
                        filter: energy >= 85
                          ? "drop-shadow(0 0 4px rgba(251, 191, 36, 0.8)) drop-shadow(0 0 8px rgba(251, 191, 36, 0.4))"
                          : "none",
                      }}
                      onClick={(e) => {
                        e.preventDefault();
                        e.stopPropagation();
                        onStarClick?.();
                      }}
                      aria-label="Review energy"
                      title="Review energy"
                    >
                      <Star
                        className="w-5 h-5"
                        style={{
                          ...getStarStyle(),
                          opacity,
                          filter: energy >= 85 ? "drop-shadow(0 0 2px rgba(251, 191, 36, 1))" : "none",
                        }}
                      />
                    </button>
                  </TooltipTrigger>

                  <TooltipContent
                    className="z-[200] border border-amber-300/30 shadow-md"
                    style={{
                      zIndex: 200,
                      backgroundColor: "rgba(251, 191, 36, 0.95)",
                      color: "rgb(28, 25, 23)",
                    }}
                    sideOffset={5}
                  >
                    <div className="space-y-1">
                      <p className="text-sm font-medium">Energy: {energy}%</p>
                      <div className="w-32 h-2 bg-muted rounded-full overflow-hidden">
                        <div
                          className="h-full bg-gradient-to-r from-amber-400 to-yellow-500 transition-all duration-300"
                          style={{ width: `${energy}%` }}
                        />
                      </div>
                      <p className="text-xs opacity-80">Enter Review and complete at least 1 action today to recharge.</p>
                    </div>
                  </TooltipContent>
                </Tooltip>
              </TooltipProvider>
            </div>
          </>
        ) : null}
      </div>

      {/* Create Group Workspace Dialog */}
      <Dialog open={createOpen} onOpenChange={setCreateOpen}>
        <DialogContent
          className="w-[600px] max-w-[600px] sm:max-w-[600px] z-[1001] pointer-events-auto"
          style={{ width: 600, maxWidth: 600, zIndex: 1001 }}
          overlayClassName="!z-[99]"
          overlayStyle={{
            top: "64px",
            left: 0,
            right: 0,
            bottom: 0,
            zIndex: 99,
            position: "fixed",
          }}
          onPointerDownOutside={(e) => e.preventDefault()}
          onInteractOutside={(e) => e.preventDefault()}
        >
          <DialogHeader>
            <DialogTitle>Create Group Workspace</DialogTitle>
          </DialogHeader>

          <div className="space-y-6">
            <div className="space-y-2">
              <Label htmlFor="ws-name">Workspace Name</Label>
              <Input
                id="ws-name"
                value={workspaceName}
                onChange={(e) => setWorkspaceName(e.target.value)}
                placeholder="e.g., CS 101 Study Group"
              />
            </div>

            <div className="space-y-2">
              <Label>Category</Label>
              <RadioGroup
                value={category}
                onValueChange={(val) => setCategory(val as "course" | "personal")}
                className="flex gap-4"
              >
                <div className="flex items-center space-x-2">
                  <RadioGroupItem id="cat-course" value="course" />
                  <Label htmlFor="cat-course">Course</Label>
                </div>
                <div className="flex items-center space-x-2">
                  <RadioGroupItem id="cat-personal" value="personal" />
                  <Label htmlFor="cat-personal">Personal Interest</Label>
                </div>
              </RadioGroup>
            </div>

            {category === "course" && (
              <div className="space-y-2">
                <Label htmlFor="course-select">Course Name</Label>
                <Select value={courseId} onValueChange={setCourseId}>
                  <SelectTrigger id="course-select">
                    <SelectValue placeholder="Select a course" />
                  </SelectTrigger>
                  <SelectContent>
                    {availableCourses.map((course) => (
                      <SelectItem key={course.id} value={course.id}>
                        {course.name}
                      </SelectItem>
                    ))}
                  </SelectContent>
                </Select>
              </div>
            )}

            <div className="space-y-2">
              <Label>Invite Members (emails)</Label>
              <div className="flex gap-2">
                <Input
                  value={inviteEmail}
                  onChange={(e) => setInviteEmail(e.target.value)}
                  placeholder="Enter email and click Add"
                  onKeyDown={(e) => {
                    if (e.key === "Enter") {
                      e.preventDefault();
                      addInvite();
                    }
                  }}
                />
                <Button variant="secondary" onClick={addInvite}>
                  Add
                </Button>
              </div>

              {invites.length > 0 && (
                <div className="flex flex-wrap gap-2">
                  {invites.map((email) => (
                    <span key={email} className="inline-flex items-center px-2 py-1 rounded-full bg-muted text-sm">
                      {email}
                      <Button
                        variant="ghost"
                        size="icon"
                        className="h-4 w-4 ml-1"
                        onClick={() => removeInvite(email)}
                        aria-label={`Remove ${email}`}
                      >
                        <X className="h-3 w-3" />
                      </Button>
                    </span>
                  ))}
                </div>
              )}
            </div>
          </div>

          <DialogFooter>
            <Button variant="outline" onClick={() => setCreateOpen(false)}>
              Cancel
            </Button>
            <Button onClick={handleCreate}>Create</Button>
          </DialogFooter>
        </DialogContent>
      </Dialog>

      {/* Profile Editor Dialog */}
      {user && showProfileEditor && (
        <ProfileEditor
          user={user}
          onSave={(updatedUser) => {
            if (onUserUpdate) onUserUpdate(updatedUser);
            setShowProfileEditor(false);
          }}
          onClose={() => setShowProfileEditor(false)}
        />
      )}
    </header>
  );
}