-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathProfilesList.tsx
More file actions
130 lines (119 loc) · 4.02 KB
/
Copy pathProfilesList.tsx
File metadata and controls
130 lines (119 loc) · 4.02 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
import { ProfileCard } from "./ProfileCard";
import { Search } from "lucide-react";
import { CreateProfileButton } from "../action-buttons/CreateProfileDialog";
import { useGetProfiles } from "@/hooks/profiles/use-get-profiles";
import { PROFILES } from "@/lib/constants/profileConstants";
import type { Profile } from "@/lib/types/profiles";
import { useMemo, useState } from "react";
import { useGetAttestations } from "@/hooks/attestations/use-get-attestations";
import { Input } from "../../ui/input";
import { AiOutlineLoading3Quarters } from "react-icons/ai";
import ErrorDisplay from "@/components/displayError/index";
export function ProfilesList() {
const { data, isLoading, error } = useGetProfiles();
const [searchQuery, setSearchQuery] = useState("");
const attestations = useGetAttestations();
const baseProfiles: Profile[] =
data && data.length > 0
? data.map((p) => ({
address: p.address,
name: p.name || "",
description: p.description || "",
githubLogin: p.github_login,
attestationCount: 0,
attestations: [],
}))
: PROFILES;
const attestationsByRecipient = useMemo(() => {
const map = new Map<
string,
{ id: string; badgeName: string; justification: string; issuer: string }[]
>();
const list = attestations.data ?? [];
for (let i = 0; i < list.length; i++) {
const a = list[i];
const arr = map.get(a.recipient.toLowerCase()) ?? [];
arr.push({
id: String(i),
badgeName: a.badgeName,
justification: a.attestationJustification,
issuer: a.issuer,
});
map.set(a.recipient.toLowerCase(), arr);
}
return map;
}, [attestations.data]);
const profiles: Profile[] = useMemo(() => {
return baseProfiles.map((p) => {
const list =
attestationsByRecipient.get((p.address || "").toLowerCase()) ?? [];
return {
...p,
attestationCount: list.length,
attestations: list,
};
});
}, [baseProfiles, attestationsByRecipient]);
const filtered = useMemo(() => {
const q = searchQuery.trim().toLowerCase();
if (!q) return profiles;
return profiles.filter((p) => (p.name || "").toLowerCase().includes(q));
}, [profiles, searchQuery]);
if (isLoading || attestations.isLoading) {
return (
<div className="flex justify-start ">
<AiOutlineLoading3Quarters className="h-10 w-10 animate-spin" />
</div>
);
}
if (data && data.length === 0) {
return (
<>
<div className="flex gap-4 items-center pb-8">
<CreateProfileButton />
</div>
<p className="text-2xl text-yellow-600 flex items-center gap-2">
<span>⚠️</span>
{"No Profile Found"}
</p>
</>
);
}
return (
<>
{error ? (
<ErrorDisplay error={error} />
) : (
<main className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
<div className="flex gap-4 items-center pb-8">
<div className="relative">
<Search className="absolute left-3 top-1/2 transform -translate-y-1/2 text-gray-400 h-4 w-4" />
<Input
type="text"
placeholder="Search profiles..."
className="pl-10"
value={searchQuery}
onChange={(e) => setSearchQuery(e.target.value)}
/>
</div>
<CreateProfileButton />
</div>
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
{filtered.map((profile) => (
<ProfileCard
key={profile.address}
address={profile.address}
name={profile.name}
description={profile.description}
githubLogin={profile.githubLogin}
attestationCount={profile.attestationCount}
attestations={profile.attestations}
/>
))}
</div>
</main>
)}
</>
);
}
export default ProfilesList;