-
Notifications
You must be signed in to change notification settings - Fork 51
/
member-view.tsx
160 lines (154 loc) · 4.47 KB
/
member-view.tsx
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
"use client";
import {
Loader2,
MoreVertical,
Shield,
ShieldAlert,
ShieldCheck,
ShieldQuestion,
Trash,
Users2,
} from "lucide-react";
import UserAvatar from "./user-avatar";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuPortal,
DropdownMenuSeparator,
DropdownMenuSub,
DropdownMenuSubContent,
DropdownMenuSubTrigger,
DropdownMenuTrigger,
} from "./ui/dropdown-menu";
import axios from "axios";
import { useRouter } from "next/navigation";
import { useModal } from "@/hooks/use-modal-store";
import { useState } from "react";
import { cn } from "@/lib/utils";
interface UserMemberProps {
member?: memberProps;
editMode?: boolean;
className?: string;
role?: boolean;
serverId?: string;
loadingId?: { loadingId: string; setLoadingId: any };
}
interface memberProps {
Profile: {
imgUrl: string;
name: string;
id: string;
email: string;
};
role: string;
id: string;
serverId: string;
}
const roleIconMap = {
GUEST: <Users2 className="h-5 w-5 ml-2 text-gray-700" />,
MODERATOR: <ShieldCheck className="h-5 w-5 ml-2 text-indigo-500" />,
ADMIN: <ShieldAlert className="h-5 w-5 ml-2 text-rose-500" />,
};
const MemberView = ({
member,
className,
role,
editMode,
serverId,
}: UserMemberProps) => {
const [loading, setLoading] = useState(false);
const router = useRouter();
const { onOpen } = useModal();
const onRoleChange = async (role: string) => {
try {
setLoading(true);
const response = await axios.patch(`/api/members/${member?.id}`, {
role,
serverId: member?.serverId,
});
router.refresh();
// onClose();
onOpen("members", { members: response.data });
} catch (error) {
alert(error);
} finally {
setLoading(false);
}
};
return (
<div
onClick={() => {
if (!editMode)
router.push(`/servers/${serverId}/conversations/${member?.id}`);
}}
className={cn(
"flex items-center gap-x-2 mb-6",
!editMode &&
"group cursor-pointer px-2 py-2 rounded-md w-full hover:bg-zinc-700/10 dark:hover:bg-zinc-700/50 transition mb-1"
)}
>
<UserAvatar
src={member?.Profile?.imgUrl}
className={cn("h-6 w-6 md:h-8 md:w-8", editMode && className)}
/>
<div className="flex flex-col gap-y-1">
<div className="text-xs font-semibold items-center flex gap-x-1">
{member?.Profile?.name}
{/* @ts-ignore */}
{roleIconMap[member.role]}
</div>
{editMode && (
<p className="text-xs text-zinc-500">{member?.Profile?.email}</p>
)}
</div>
{editMode && member?.role !== "ADMIN" && !loading && (
<div className="ml-auto">
<DropdownMenu>
<DropdownMenuTrigger>
<MoreVertical className="h-4 w-4 text-zinc-600" />
</DropdownMenuTrigger>
<DropdownMenuContent side="left">
<DropdownMenuSub>
<DropdownMenuSubTrigger className="flex items-center">
<ShieldQuestion className="w-4 h-4 mr-2" />
<span>Role</span>
</DropdownMenuSubTrigger>
<DropdownMenuPortal>
<DropdownMenuSubContent>
<DropdownMenuItem
disabled={member?.role === "GUEST"}
onClick={() => onRoleChange("GUEST")}
>
<ShieldCheck className="h-4 w-4 mr-2" />
GUEST
</DropdownMenuItem>
<DropdownMenuItem
disabled={member?.role === "MODERATOR"}
onClick={() => onRoleChange("MODERATOR")}
>
<Shield className="h-4 w-4 mr-2" />
MODERATOR
</DropdownMenuItem>
</DropdownMenuSubContent>
</DropdownMenuPortal>
</DropdownMenuSub>
<DropdownMenuSeparator />
<DropdownMenuItem
className="text-rose-500"
onClick={() => setLoading(true)}
>
<Trash className="h-4 w-4 mr-2" />
Kick
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</div>
)}
{loading && (
<Loader2 className="animate-spin text-zinc-500 ml-auto h-4 w-4" />
)}
</div>
);
};
export default MemberView;