UserList.svelte 13 KB

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