UserList.svelte 13 KB

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