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
|
"use client";
import { ActionButton } from "@/components/ui/action-button";
import { ButtonWithTooltip } from "@/components/ui/button";
import LoadingSpinner from "@/components/ui/spinner";
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "@/components/ui/table";
import { toast } from "@/components/ui/use-toast";
import { useTranslation } from "@/lib/i18n/client";
import { api } from "@/lib/trpc";
import { Check, KeyRound, Pencil, Trash, UserPlus, X } from "lucide-react";
import { useSession } from "next-auth/react";
import ActionConfirmingDialog from "../ui/action-confirming-dialog";
import AddUserDialog from "./AddUserDialog";
import ChangeRoleDialog from "./ChangeRoleDialog";
import ResetPasswordDialog from "./ResetPasswordDialog";
function toHumanReadableSize(size: number) {
const sizes = ["Bytes", "KB", "MB", "GB", "TB"];
if (size === 0) return "0 Bytes";
const i = Math.floor(Math.log(size) / Math.log(1024));
return (size / Math.pow(1024, i)).toFixed(2) + " " + sizes[i];
}
export default function UsersSection() {
const { t } = useTranslation();
const { data: session } = useSession();
const invalidateUserList = api.useUtils().users.list.invalidate;
const { data: users } = api.users.list.useQuery();
const { data: userStats } = api.admin.userStats.useQuery();
const { mutateAsync: deleteUser, isPending: isDeletionPending } =
api.users.delete.useMutation({
onSuccess: () => {
toast({
description: "User deleted",
});
invalidateUserList();
},
onError: (e) => {
toast({
variant: "destructive",
description: `Something went wrong: ${e.message}`,
});
},
});
if (!users || !userStats) {
return <LoadingSpinner />;
}
return (
<div className="flex flex-col gap-4">
<div className="mb-2 flex items-center justify-between text-xl font-medium">
<span>{t("admin.users_list.users_list")}</span>
<AddUserDialog>
<ButtonWithTooltip tooltip="Create User" variant="outline">
<UserPlus size={16} />
</ButtonWithTooltip>
</AddUserDialog>
</div>
<Table>
<TableHeader className="bg-gray-200">
<TableHead>{t("common.name")}</TableHead>
<TableHead>{t("common.email")}</TableHead>
<TableHead>{t("admin.users_list.num_bookmarks")}</TableHead>
<TableHead>{t("admin.users_list.asset_sizes")}</TableHead>
<TableHead>{t("common.role")}</TableHead>
<TableHead>{t("admin.users_list.local_user")}</TableHead>
<TableHead>{t("common.actions")}</TableHead>
</TableHeader>
<TableBody>
{users.users.map((u) => (
<TableRow key={u.id}>
<TableCell className="py-1">{u.name}</TableCell>
<TableCell className="py-1">{u.email}</TableCell>
<TableCell className="py-1">
{userStats[u.id].numBookmarks}
</TableCell>
<TableCell className="py-1">
{toHumanReadableSize(userStats[u.id].assetSizes)}
</TableCell>
<TableCell className="py-1">
{u.role && t(`common.roles.${u.role}`)}
</TableCell>
<TableCell className="py-1">
{u.localUser ? <Check /> : <X />}
</TableCell>
<TableCell className="flex gap-1 py-1">
<ActionConfirmingDialog
title={t("admin.users_list.delete_user")}
description={t(
"admin.users_list.delete_user_confirm_description",
{
name: u.name ?? "this user",
},
)}
actionButton={(setDialogOpen) => (
<ActionButton
variant="destructive"
loading={isDeletionPending}
onClick={async () => {
await deleteUser({ userId: u.id });
setDialogOpen(false);
}}
>
Delete
</ActionButton>
)}
>
<ButtonWithTooltip
tooltip={t("admin.users_list.delete_user")}
variant="outline"
disabled={session!.user.id == u.id}
>
<Trash size={16} color="red" />
</ButtonWithTooltip>
</ActionConfirmingDialog>
<ResetPasswordDialog userId={u.id}>
<ButtonWithTooltip
tooltip={t("admin.users_list.reset_password")}
variant="outline"
disabled={session!.user.id == u.id || !u.localUser}
>
<KeyRound size={16} color="red" />
</ButtonWithTooltip>
</ResetPasswordDialog>
<ChangeRoleDialog userId={u.id} currentRole={u.role!}>
<ButtonWithTooltip
tooltip={t("admin.users_list.change_role")}
variant="outline"
disabled={session!.user.id == u.id}
>
<Pencil size={16} color="red" />
</ButtonWithTooltip>
</ChangeRoleDialog>
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</div>
);
}
|