-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnav-user.tsx
More file actions
213 lines (203 loc) · 6 KB
/
Copy pathnav-user.tsx
File metadata and controls
213 lines (203 loc) · 6 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
"use client";
import { type KyselyNotNull, sqliteTrue } from "@evolu/common";
import { IconPlus } from "@tabler/icons-react";
import { useAtomValue, useSetAtom } from "jotai";
import {
ChevronsUpDownIcon,
HardDriveDownloadIcon,
LogOutIcon,
} from "lucide-react";
import Link from "next/link";
import { useCallback, useMemo } from "react";
import { useTranslation } from "react-i18next";
import { deviceEvoluAtom } from "@/atoms/device-evolu";
import { evoluCounterAtom } from "@/atoms/evolu-counter";
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuGroup,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import {
SidebarMenu,
SidebarMenuButton,
SidebarMenuItem,
useSidebar,
} from "@/components/ui/sidebar";
import { useDeviceEvoluQuery } from "@/hooks/use-device-evolu-query";
import { useGlobalDialog } from "@/hooks/use-global-dialog";
import { useInstallPwa } from "@/hooks/use-install-pwa";
import { createDeviceQuery } from "@/lib/evolu/device";
import { TimestampMs } from "@/lib/shared/types";
const accountsQuery = createDeviceQuery((db) =>
db
.selectFrom("account")
.select(["account.id as id", "account.name as name"])
.where("isDeleted", "is not", sqliteTrue)
.where("name", "is not", null)
.orderBy("lastUseAt", "desc")
.$narrowType<{
name: KyselyNotNull;
}>(),
);
export function NavUser({
user,
}: {
user: {
name: string;
email: string;
avatar: string;
};
}) {
const { t } = useTranslation();
const { setOpenMobile } = useSidebar();
const { onClick, isPwaSupported } = useInstallPwa();
const { withConfirm } = useGlobalDialog();
const deviceEvolu = useAtomValue(deviceEvoluAtom);
const setEvoluCounter = useSetAtom(evoluCounterAtom);
const { data: accounts } = useDeviceEvoluQuery(accountsQuery);
const activeAccountId = accounts[0]?.id;
const activeAccountName =
accounts[0]?.name ?? t("navigation:account.unknown");
const logout = useCallback(async () => {
const currentAccount = accounts[0];
if (!currentAccount) {
return;
}
await new Promise<void>((resolve) => {
deviceEvolu.update(
"account",
{
id: currentAccount.id,
isDeleted: sqliteTrue,
},
{
onComplete: resolve,
},
);
});
setEvoluCounter((value) => value + 1);
}, [accounts, deviceEvolu, setEvoluCounter]);
const logoutWithConfirm = useMemo(
() =>
withConfirm(logout, {
title: t("navigation:account.confirmLogout.title"),
description: t("navigation:account.confirmLogout.description"),
confirmText: t("navigation:account.actions.logout"),
cancelText: t("navigation:account.actions.cancel"),
confirmVariant: "destructive",
}),
[logout, t, withConfirm],
);
return (
<SidebarMenu>
<SidebarMenuItem>
<DropdownMenu>
<DropdownMenuTrigger
render={
<SidebarMenuButton
size="lg"
className="data-[state=open]:bg-sidebar-accent data-[state=open]:text-sidebar-accent-foreground"
/>
}
>
<Avatar className="h-8 w-8 rounded-lg grayscale">
<AvatarImage src={user.avatar} alt={user.name} />
<AvatarFallback className="rounded-lg">
{t("navigation:account.initials")}
</AvatarFallback>
</Avatar>
<div className="grid flex-1 text-left text-sm leading-tight">
<span className="truncate font-medium">{activeAccountName}</span>
</div>
<ChevronsUpDownIcon className="ml-auto" />
</DropdownMenuTrigger>
<DropdownMenuContent
className="w-(--radix-dropdown-menu-trigger-width) min-w-56 rounded-lg"
side={"top"}
align="end"
sideOffset={4}
>
<DropdownMenuGroup>
<DropdownMenuLabel>
{t("navigation:account.accounts")}
</DropdownMenuLabel>
{accounts.map((account) => {
return (
<DropdownMenuItem
disabled={account.id === activeAccountId}
key={account.id}
className="p-0 font-normal"
onClick={async () => {
await new Promise<void>((resolve) => {
deviceEvolu.update(
"account",
{
id: account.id,
lastUseAt: TimestampMs(Date.now()),
},
{
onComplete: resolve,
},
);
});
setEvoluCounter((value) => value + 1); // Reload
}}
>
<div className="flex items-center gap-2 px-1 py-1.5 text-left text-sm">
<Avatar className="h-8 w-8 rounded-lg">
<AvatarImage src={user.avatar} alt={account.name} />
<AvatarFallback className="rounded-lg">
{t("navigation:account.initials")}
</AvatarFallback>
</Avatar>
<div className="grid flex-1 text-left text-sm leading-tight">
<span className="truncate font-medium">
{account.name}
</span>
</div>
</div>
</DropdownMenuItem>
);
})}
<DropdownMenuItem
render={
<Link
href={"/admin/switch-account"}
onClick={() => setOpenMobile(false)}
/>
}
>
<IconPlus />
{t("navigation:account.actions.addAccount")}
</DropdownMenuItem>
<DropdownMenuSeparator
title={t("navigation:account.actions.label")}
/>
<DropdownMenuLabel>
{t("navigation:account.currentAccount")}
</DropdownMenuLabel>
<DropdownMenuItem
onClick={() => void logoutWithConfirm()}
disabled={accounts.length <= 1}
>
<LogOutIcon />
{t("navigation:account.actions.logout")}
</DropdownMenuItem>
{isPwaSupported && (
<DropdownMenuItem>
<HardDriveDownloadIcon onClick={onClick} />
{t("navigation:account.actions.install")}
</DropdownMenuItem>
)}
</DropdownMenuGroup>
</DropdownMenuContent>
</DropdownMenu>
</SidebarMenuItem>
</SidebarMenu>
);
}