File size: 1,888 Bytes
6a30288
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"use client";

import { Button } from "@/components/ui/button";
import { useToast } from "@/components/ui/use-toast";
import { routes } from "@/lib/routes";
import { signOut } from "@/server-actions/auth";
import { Loader2, LogOut, User } from "lucide-react";
import { useRouter } from "next/navigation";
import { useState } from "react";

export function UserMenu(props: {
  name: string | null;
  email: string | null;
  compact?: boolean;
}) {
  const router = useRouter();
  const { toast } = useToast();
  const [isLoading, setIsLoading] = useState(false);

  return (
    <div
      className={
        props.compact
          ? "flex items-center gap-3"
          : "rounded-md border border-border bg-secondary px-6 py-6"
      }
    >
      <div className="flex items-center gap-3">
        <div className="flex h-10 w-10 items-center justify-center rounded-full bg-slate-900 text-white">
          <User size={20} />
        </div>
        <div>
          <p className="font-medium">{props.name ?? "ShopSmart User"}</p>
          <p className="text-sm text-muted-foreground">{props.email}</p>
        </div>
      </div>
      <Button
        variant="outline"
        size="sm"
        className={props.compact ? "ml-2" : "mt-5 flex gap-2"}
        disabled={isLoading}
        onClick={() => {
          setIsLoading(true);
          signOut()
            .then((result) => {
              toast({
                title: result.message,
                description: result.action,
              });
              router.push(routes.signIn);
              router.refresh();
            })
            .finally(() => setIsLoading(false));
        }}
      >
        {isLoading ? (
          <Loader2 size={16} className="animate-spin" />
        ) : (
          <LogOut size={16} />
        )}
        {!props.compact && <span>Sign out</span>}
      </Button>
    </div>
  );
}