File size: 2,533 Bytes
b074e91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React, { useEffect, useState } from "react";
import apiclient from "../apiclient";
import type { GetMyProfileData } from "../apiclient/data-contracts";
import { useUser } from "@stackframe/react";
import { useNavigate } from "react-router-dom";
import { toast } from "sonner";

export interface Props {}

export const ProfileCard: React.FC<Props> = () => {
  const user = useUser();
  const navigate = useNavigate();
  const [loading, setLoading] = useState(false);
  const [profile, setProfile] = useState<GetMyProfileData | null>(null);
  const [error, setError] = useState<string | null>(null);

  useEffect(() => {
    const load = async () => {
      if (!user) return; // not signed in yet
      setLoading(true);
      setError(null);
      try {
        const res = await apiclient.get_my_profile();
        const data = await res.json();
        setProfile(data);
      } catch (e: any) {
        setError("Failed to load profile");
      } finally {
        setLoading(false);
      }
    };
    load();
  }, [user?.id]);

  if (!user) {
    return (
      <div className="bg-card border border-border rounded-lg p-4">
        <div className="flex items-center justify-between gap-3">
          <p className="text-sm text-muted-foreground">Sign in to manage your profile.</p>
          <button
            onClick={() => navigate("/auth/sign-in")}
            className="px-3 py-1.5 bg-primary text-primary-foreground rounded-md text-sm hover:bg-primary/90"
          >
            Sign in
          </button>
        </div>
      </div>
    );
  }

  return (
    <div className="bg-card border border-border rounded-lg p-4">
      <div className="flex items-center justify-between mb-3">
        <h3 className="text-lg font-medium">Your Profile</h3>
        {loading && <span className="text-xs text-muted-foreground">Loading…</span>}
      </div>
      {error && <p className="text-sm text-red-400 mb-2">{error}</p>}
      <div className="space-y-2">
        <div>
          <label className="block text-xs text-muted-foreground mb-1">Display Name</label>
          <input
            value={profile?.display_name || user?.displayName || 'Anonymous'}
            readOnly
            className="w-full px-3 py-2 bg-muted/50 border border-input rounded-md text-foreground cursor-not-allowed"
          />
        </div>
        {profile && (
          <p className="text-xs text-muted-foreground">User ID: <span className="text-foreground">{profile.user_id}</span></p>
        )}
      </div>
    </div>
  );
};