Users.svelte 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. <script>
  2. import { WEBUI_BASE_URL } from '$lib/constants';
  3. import { WEBUI_NAME, config, user, showSidebar } from '$lib/stores';
  4. import { goto } from '$app/navigation';
  5. import { onMount, getContext } from 'svelte';
  6. import dayjs from 'dayjs';
  7. import relativeTime from 'dayjs/plugin/relativeTime';
  8. dayjs.extend(relativeTime);
  9. import { toast } from 'svelte-sonner';
  10. import { updateUserRole, getUsers, deleteUserById } from '$lib/apis/users';
  11. import EditUserModal from '$lib/components/admin/EditUserModal.svelte';
  12. import Pagination from '$lib/components/common/Pagination.svelte';
  13. import ChatBubbles from '$lib/components/icons/ChatBubbles.svelte';
  14. import Tooltip from '$lib/components/common/Tooltip.svelte';
  15. import UserChatsModal from '$lib/components/admin/UserChatsModal.svelte';
  16. import AddUserModal from '$lib/components/admin/AddUserModal.svelte';
  17. import ConfirmDialog from '$lib/components/common/ConfirmDialog.svelte';
  18. import Badge from '$lib/components/common/Badge.svelte';
  19. import Plus from '$lib/components/icons/Plus.svelte';
  20. import ChevronUp from '$lib/components/icons/ChevronUp.svelte';
  21. import ChevronDown from '$lib/components/icons/ChevronDown.svelte';
  22. import About from '$lib/components/chat/Settings/About.svelte';
  23. const i18n = getContext('i18n');
  24. let loaded = false;
  25. let tab = '';
  26. let users = [];
  27. let search = '';
  28. let selectedUser = null;
  29. let page = 1;
  30. let showDeleteConfirmDialog = false;
  31. let showAddUserModal = false;
  32. let showUserChatsModal = false;
  33. let showEditUserModal = false;
  34. const updateRoleHandler = async (id, role) => {
  35. const res = await updateUserRole(localStorage.token, id, role).catch((error) => {
  36. toast.error(error);
  37. return null;
  38. });
  39. if (res) {
  40. users = await getUsers(localStorage.token);
  41. }
  42. };
  43. const deleteUserHandler = async (id) => {
  44. const res = await deleteUserById(localStorage.token, id).catch((error) => {
  45. toast.error(error);
  46. return null;
  47. });
  48. if (res) {
  49. users = await getUsers(localStorage.token);
  50. }
  51. };
  52. onMount(async () => {
  53. if ($user?.role !== 'admin') {
  54. await goto('/');
  55. } else {
  56. users = await getUsers(localStorage.token);
  57. }
  58. loaded = true;
  59. });
  60. let sortKey = 'created_at'; // default sort key
  61. let sortOrder = 'asc'; // default sort order
  62. function setSortKey(key) {
  63. if (sortKey === key) {
  64. sortOrder = sortOrder === 'asc' ? 'desc' : 'asc';
  65. } else {
  66. sortKey = key;
  67. sortOrder = 'asc';
  68. }
  69. }
  70. let filteredUsers;
  71. $: filteredUsers = users
  72. .filter((user) => {
  73. if (search === '') {
  74. return true;
  75. } else {
  76. let name = user.name.toLowerCase();
  77. const query = search.toLowerCase();
  78. return name.includes(query);
  79. }
  80. })
  81. .sort((a, b) => {
  82. if (a[sortKey] < b[sortKey]) return sortOrder === 'asc' ? -1 : 1;
  83. if (a[sortKey] > b[sortKey]) return sortOrder === 'asc' ? 1 : -1;
  84. return 0;
  85. })
  86. .slice((page - 1) * 20, page * 20);
  87. </script>
  88. <ConfirmDialog
  89. bind:show={showDeleteConfirmDialog}
  90. on:confirm={() => {
  91. deleteUserHandler(selectedUser.id);
  92. }}
  93. />
  94. {#key selectedUser}
  95. <EditUserModal
  96. bind:show={showEditUserModal}
  97. {selectedUser}
  98. sessionUser={$user}
  99. on:save={async () => {
  100. users = await getUsers(localStorage.token);
  101. }}
  102. />
  103. {/key}
  104. <AddUserModal
  105. bind:show={showAddUserModal}
  106. on:save={async () => {
  107. users = await getUsers(localStorage.token);
  108. }}
  109. />
  110. <UserChatsModal bind:show={showUserChatsModal} user={selectedUser} />
  111. {#if loaded}
  112. <div class="mt-0.5 mb-2 gap-1 flex flex-col md:flex-row justify-between">
  113. <div class="flex md:self-center text-lg font-medium px-0.5">
  114. {$i18n.t('Users')}
  115. <div class="flex self-center w-[1px] h-6 mx-2.5 bg-gray-50 dark:bg-gray-850" />
  116. <span class="text-lg font-medium text-gray-500 dark:text-gray-300">{users.length}</span>
  117. </div>
  118. <div class="flex gap-1">
  119. <div class=" flex w-full space-x-2">
  120. <div class="flex flex-1">
  121. <div class=" self-center ml-1 mr-3">
  122. <svg
  123. xmlns="http://www.w3.org/2000/svg"
  124. viewBox="0 0 20 20"
  125. fill="currentColor"
  126. class="w-4 h-4"
  127. >
  128. <path
  129. fill-rule="evenodd"
  130. d="M9 3.5a5.5 5.5 0 100 11 5.5 5.5 0 000-11zM2 9a7 7 0 1112.452 4.391l3.328 3.329a.75.75 0 11-1.06 1.06l-3.329-3.328A7 7 0 012 9z"
  131. clip-rule="evenodd"
  132. />
  133. </svg>
  134. </div>
  135. <input
  136. class=" w-full text-sm pr-4 py-1 rounded-r-xl outline-none bg-transparent"
  137. bind:value={search}
  138. placeholder={$i18n.t('Search')}
  139. />
  140. </div>
  141. <div>
  142. <Tooltip content={$i18n.t('Add User')}>
  143. <button
  144. class=" p-2 rounded-xl hover:bg-gray-100 dark:bg-gray-900 dark:hover:bg-gray-850 transition font-medium text-sm flex items-center space-x-1"
  145. on:click={() => {
  146. showAddUserModal = !showAddUserModal;
  147. }}
  148. >
  149. <Plus className="size-3.5" />
  150. </button>
  151. </Tooltip>
  152. </div>
  153. </div>
  154. </div>
  155. </div>
  156. <div
  157. class="scrollbar-hidden relative whitespace-nowrap overflow-x-auto max-w-full rounded pt-0.5"
  158. >
  159. <table
  160. class="w-full text-sm text-left text-gray-500 dark:text-gray-400 table-auto max-w-full rounded"
  161. >
  162. <thead
  163. class="text-xs text-gray-700 uppercase bg-gray-50 dark:bg-gray-850 dark:text-gray-400 -translate-y-0.5"
  164. >
  165. <tr class="">
  166. <th
  167. scope="col"
  168. class="px-3 py-1.5 cursor-pointer select-none"
  169. on:click={() => setSortKey('role')}
  170. >
  171. <div class="flex gap-1.5 items-center">
  172. {$i18n.t('Role')}
  173. {#if sortKey === 'role'}
  174. <span class="font-normal"
  175. >{#if sortOrder === 'asc'}
  176. <ChevronUp className="size-2" />
  177. {:else}
  178. <ChevronDown className="size-2" />
  179. {/if}
  180. </span>
  181. {:else}
  182. <span class="invisible">
  183. <ChevronUp className="size-2" />
  184. </span>
  185. {/if}
  186. </div>
  187. </th>
  188. <th
  189. scope="col"
  190. class="px-3 py-1.5 cursor-pointer select-none"
  191. on:click={() => setSortKey('name')}
  192. >
  193. <div class="flex gap-1.5 items-center">
  194. {$i18n.t('Name')}
  195. {#if sortKey === 'name'}
  196. <span class="font-normal"
  197. >{#if sortOrder === 'asc'}
  198. <ChevronUp className="size-2" />
  199. {:else}
  200. <ChevronDown className="size-2" />
  201. {/if}
  202. </span>
  203. {:else}
  204. <span class="invisible">
  205. <ChevronUp className="size-2" />
  206. </span>
  207. {/if}
  208. </div>
  209. </th>
  210. <th
  211. scope="col"
  212. class="px-3 py-1.5 cursor-pointer select-none"
  213. on:click={() => setSortKey('email')}
  214. >
  215. <div class="flex gap-1.5 items-center">
  216. {$i18n.t('Email')}
  217. {#if sortKey === 'email'}
  218. <span class="font-normal"
  219. >{#if sortOrder === 'asc'}
  220. <ChevronUp className="size-2" />
  221. {:else}
  222. <ChevronDown className="size-2" />
  223. {/if}
  224. </span>
  225. {:else}
  226. <span class="invisible">
  227. <ChevronUp className="size-2" />
  228. </span>
  229. {/if}
  230. </div>
  231. </th>
  232. <th
  233. scope="col"
  234. class="px-3 py-1.5 cursor-pointer select-none"
  235. on:click={() => setSortKey('last_active_at')}
  236. >
  237. <div class="flex gap-1.5 items-center">
  238. {$i18n.t('Last Active')}
  239. {#if sortKey === 'last_active_at'}
  240. <span class="font-normal"
  241. >{#if sortOrder === 'asc'}
  242. <ChevronUp className="size-2" />
  243. {:else}
  244. <ChevronDown className="size-2" />
  245. {/if}
  246. </span>
  247. {:else}
  248. <span class="invisible">
  249. <ChevronUp className="size-2" />
  250. </span>
  251. {/if}
  252. </div>
  253. </th>
  254. <th
  255. scope="col"
  256. class="px-3 py-1.5 cursor-pointer select-none"
  257. on:click={() => setSortKey('created_at')}
  258. >
  259. <div class="flex gap-1.5 items-center">
  260. {$i18n.t('Created at')}
  261. {#if sortKey === 'created_at'}
  262. <span class="font-normal"
  263. >{#if sortOrder === 'asc'}
  264. <ChevronUp className="size-2" />
  265. {:else}
  266. <ChevronDown className="size-2" />
  267. {/if}
  268. </span>
  269. {:else}
  270. <span class="invisible">
  271. <ChevronUp className="size-2" />
  272. </span>
  273. {/if}
  274. </div>
  275. </th>
  276. <th
  277. scope="col"
  278. class="px-3 py-1.5 cursor-pointer select-none"
  279. on:click={() => setSortKey('oauth_sub')}
  280. >
  281. <div class="flex gap-1.5 items-center">
  282. {$i18n.t('OAuth ID')}
  283. {#if sortKey === 'oauth_sub'}
  284. <span class="font-normal"
  285. >{#if sortOrder === 'asc'}
  286. <ChevronUp className="size-2" />
  287. {:else}
  288. <ChevronDown className="size-2" />
  289. {/if}
  290. </span>
  291. {:else}
  292. <span class="invisible">
  293. <ChevronUp className="size-2" />
  294. </span>
  295. {/if}
  296. </div>
  297. </th>
  298. <th scope="col" class="px-3 py-2 text-right" />
  299. </tr>
  300. </thead>
  301. <tbody class="">
  302. {#each filteredUsers as user, userIdx}
  303. <tr class="bg-white dark:bg-gray-900 dark:border-gray-850 text-xs">
  304. <td class="px-3 py-1 min-w-[7rem] w-28">
  305. <button
  306. class=" translate-y-0.5"
  307. on:click={() => {
  308. if (user.role === 'user') {
  309. updateRoleHandler(user.id, 'admin');
  310. } else if (user.role === 'pending') {
  311. updateRoleHandler(user.id, 'user');
  312. } else {
  313. updateRoleHandler(user.id, 'pending');
  314. }
  315. }}
  316. >
  317. <Badge
  318. type={user.role === 'admin' ? 'info' : user.role === 'user' ? 'success' : 'muted'}
  319. content={$i18n.t(user.role)}
  320. />
  321. </button>
  322. </td>
  323. <td class="px-3 py-1 font-medium text-gray-900 dark:text-white w-max">
  324. <div class="flex flex-row w-max">
  325. <img
  326. class=" rounded-full w-6 h-6 object-cover mr-2.5"
  327. src={user.profile_image_url.startsWith(WEBUI_BASE_URL) ||
  328. user.profile_image_url.startsWith('https://www.gravatar.com/avatar/') ||
  329. user.profile_image_url.startsWith('data:')
  330. ? user.profile_image_url
  331. : `/user.png`}
  332. alt="user"
  333. />
  334. <div class=" font-medium self-center">{user.name}</div>
  335. </div>
  336. </td>
  337. <td class=" px-3 py-1"> {user.email} </td>
  338. <td class=" px-3 py-1">
  339. {dayjs(user.last_active_at * 1000).fromNow()}
  340. </td>
  341. <td class=" px-3 py-1">
  342. {dayjs(user.created_at * 1000).format($i18n.t('MMMM DD, YYYY'))}
  343. </td>
  344. <td class=" px-3 py-1"> {user.oauth_sub ?? ''} </td>
  345. <td class="px-3 py-1 text-right">
  346. <div class="flex justify-end w-full">
  347. {#if $config.features.enable_admin_chat_access && user.role !== 'admin'}
  348. <Tooltip content={$i18n.t('Chats')}>
  349. <button
  350. class="self-center w-fit text-sm px-2 py-2 hover:bg-black/5 dark:hover:bg-white/5 rounded-xl"
  351. on:click={async () => {
  352. showUserChatsModal = !showUserChatsModal;
  353. selectedUser = user;
  354. }}
  355. >
  356. <ChatBubbles />
  357. </button>
  358. </Tooltip>
  359. {/if}
  360. <Tooltip content={$i18n.t('Edit User')}>
  361. <button
  362. class="self-center w-fit text-sm px-2 py-2 hover:bg-black/5 dark:hover:bg-white/5 rounded-xl"
  363. on:click={async () => {
  364. showEditUserModal = !showEditUserModal;
  365. selectedUser = user;
  366. }}
  367. >
  368. <svg
  369. xmlns="http://www.w3.org/2000/svg"
  370. fill="none"
  371. viewBox="0 0 24 24"
  372. stroke-width="1.5"
  373. stroke="currentColor"
  374. class="w-4 h-4"
  375. >
  376. <path
  377. stroke-linecap="round"
  378. stroke-linejoin="round"
  379. d="m16.862 4.487 1.687-1.688a1.875 1.875 0 1 1 2.652 2.652L6.832 19.82a4.5 4.5 0 0 1-1.897 1.13l-2.685.8.8-2.685a4.5 4.5 0 0 1 1.13-1.897L16.863 4.487Zm0 0L19.5 7.125"
  380. />
  381. </svg>
  382. </button>
  383. </Tooltip>
  384. {#if user.role !== 'admin'}
  385. <Tooltip content={$i18n.t('Delete User')}>
  386. <button
  387. class="self-center w-fit text-sm px-2 py-2 hover:bg-black/5 dark:hover:bg-white/5 rounded-xl"
  388. on:click={async () => {
  389. showDeleteConfirmDialog = true;
  390. selectedUser = user;
  391. }}
  392. >
  393. <svg
  394. xmlns="http://www.w3.org/2000/svg"
  395. fill="none"
  396. viewBox="0 0 24 24"
  397. stroke-width="1.5"
  398. stroke="currentColor"
  399. class="w-4 h-4"
  400. >
  401. <path
  402. stroke-linecap="round"
  403. stroke-linejoin="round"
  404. d="m14.74 9-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 0 1-2.244 2.077H8.084a2.25 2.25 0 0 1-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 0 0-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 0 1 3.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 0 0-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 0 0-7.5 0"
  405. />
  406. </svg>
  407. </button>
  408. </Tooltip>
  409. {/if}
  410. </div>
  411. </td>
  412. </tr>
  413. {/each}
  414. </tbody>
  415. </table>
  416. </div>
  417. <div class=" text-gray-500 text-xs mt-1.5 text-right">
  418. ⓘ {$i18n.t("Click on the user role button to change a user's role.")}
  419. </div>
  420. <Pagination bind:page count={users.length} />
  421. {/if}