+page.svelte 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. <script>
  2. import { WEBUI_API_BASE_URL } from '$lib/constants';
  3. import { WEBUI_NAME, config, user } 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 { getSignUpEnabledStatus, toggleSignUpEnabledStatus } from '$lib/apis/auths';
  12. import EditUserModal from '$lib/components/admin/EditUserModal.svelte';
  13. import SettingsModal from '$lib/components/admin/SettingsModal.svelte';
  14. import Pagination from '$lib/components/common/Pagination.svelte';
  15. import ChatBubbles from '$lib/components/icons/ChatBubbles.svelte';
  16. import Tooltip from '$lib/components/common/Tooltip.svelte';
  17. import UserChatsModal from '$lib/components/admin/UserChatsModal.svelte';
  18. import AddUserModal from '$lib/components/admin/AddUserModal.svelte';
  19. const i18n = getContext('i18n');
  20. let loaded = false;
  21. let users = [];
  22. let search = '';
  23. let selectedUser = null;
  24. let page = 1;
  25. let showSettingsModal = false;
  26. let showAddUserModal = false;
  27. let showUserChatsModal = false;
  28. let showEditUserModal = false;
  29. const updateRoleHandler = async (id, role) => {
  30. const res = await updateUserRole(localStorage.token, id, role).catch((error) => {
  31. toast.error(error);
  32. return null;
  33. });
  34. if (res) {
  35. users = await getUsers(localStorage.token);
  36. }
  37. };
  38. const editUserPasswordHandler = async (id, password) => {
  39. const res = await deleteUserById(localStorage.token, id).catch((error) => {
  40. toast.error(error);
  41. return null;
  42. });
  43. if (res) {
  44. users = await getUsers(localStorage.token);
  45. toast.success($i18n.t('Successfully updated.'));
  46. }
  47. };
  48. const deleteUserHandler = async (id) => {
  49. const res = await deleteUserById(localStorage.token, id).catch((error) => {
  50. toast.error(error);
  51. return null;
  52. });
  53. if (res) {
  54. users = await getUsers(localStorage.token);
  55. }
  56. };
  57. onMount(async () => {
  58. if ($user?.role !== 'admin') {
  59. await goto('/');
  60. } else {
  61. users = await getUsers(localStorage.token);
  62. }
  63. loaded = true;
  64. });
  65. </script>
  66. <svelte:head>
  67. <title>{$i18n.t('Admin Panel')} | {$WEBUI_NAME}</title>
  68. </svelte:head>
  69. {#key selectedUser}
  70. <EditUserModal
  71. bind:show={showEditUserModal}
  72. {selectedUser}
  73. sessionUser={$user}
  74. on:save={async () => {
  75. users = await getUsers(localStorage.token);
  76. }}
  77. />
  78. {/key}
  79. <AddUserModal
  80. bind:show={showAddUserModal}
  81. on:save={async () => {
  82. users = await getUsers(localStorage.token);
  83. }}
  84. />
  85. <UserChatsModal bind:show={showUserChatsModal} user={selectedUser} />
  86. <SettingsModal bind:show={showSettingsModal} />
  87. <div class="min-h-screen max-h-[100dvh] w-full flex justify-center dark:text-white">
  88. {#if loaded}
  89. <div class=" flex flex-col justify-between w-full overflow-y-auto">
  90. <div class=" mx-auto w-full">
  91. <div class="w-full">
  92. <div class=" flex flex-col justify-center">
  93. <div class=" px-6 pt-4">
  94. <div class=" flex justify-between items-center">
  95. <div class="flex items-center text-2xl font-semibold">Dashboard</div>
  96. <div>
  97. <Tooltip content={$i18n.t('Admin Settings')}>
  98. <button
  99. class="flex items-center space-x-1 p-2 md:px-3 md:py-1.5 rounded-xl bg-gray-50 hover:bg-gray-100 dark:bg-gray-800 dark:hover:bg-gray-700 transition"
  100. type="button"
  101. on:click={() => {
  102. showSettingsModal = !showSettingsModal;
  103. }}
  104. >
  105. <svg
  106. xmlns="http://www.w3.org/2000/svg"
  107. viewBox="0 0 16 16"
  108. fill="currentColor"
  109. class="w-4 h-4"
  110. >
  111. <path
  112. fill-rule="evenodd"
  113. d="M6.955 1.45A.5.5 0 0 1 7.452 1h1.096a.5.5 0 0 1 .497.45l.17 1.699c.484.12.94.312 1.356.562l1.321-1.081a.5.5 0 0 1 .67.033l.774.775a.5.5 0 0 1 .034.67l-1.08 1.32c.25.417.44.873.561 1.357l1.699.17a.5.5 0 0 1 .45.497v1.096a.5.5 0 0 1-.45.497l-1.699.17c-.12.484-.312.94-.562 1.356l1.082 1.322a.5.5 0 0 1-.034.67l-.774.774a.5.5 0 0 1-.67.033l-1.322-1.08c-.416.25-.872.44-1.356.561l-.17 1.699a.5.5 0 0 1-.497.45H7.452a.5.5 0 0 1-.497-.45l-.17-1.699a4.973 4.973 0 0 1-1.356-.562L4.108 13.37a.5.5 0 0 1-.67-.033l-.774-.775a.5.5 0 0 1-.034-.67l1.08-1.32a4.971 4.971 0 0 1-.561-1.357l-1.699-.17A.5.5 0 0 1 1 8.548V7.452a.5.5 0 0 1 .45-.497l1.699-.17c.12-.484.312-.94.562-1.356L2.629 4.107a.5.5 0 0 1 .034-.67l.774-.774a.5.5 0 0 1 .67-.033L5.43 3.71a4.97 4.97 0 0 1 1.356-.561l.17-1.699ZM6 8c0 .538.212 1.026.558 1.385l.057.057a2 2 0 0 0 2.828-2.828l-.058-.056A2 2 0 0 0 6 8Z"
  114. clip-rule="evenodd"
  115. />
  116. </svg>
  117. <div class="hidden md:inline text-xs">{$i18n.t('Admin Settings')}</div>
  118. </button>
  119. </Tooltip>
  120. </div>
  121. </div>
  122. </div>
  123. <div class="px-6 flex text-sm gap-2.5">
  124. <div class="py-3 border-b font-medium text-gray-100 cursor-pointer">Overview</div>
  125. <!-- <div class="py-3 text-gray-300 cursor-pointer">Users</div> -->
  126. </div>
  127. <hr class=" mb-3 dark:border-gray-800" />
  128. <div class="px-6">
  129. <div class="mt-0.5 mb-3 gap-1 flex flex-col md:flex-row justify-between">
  130. <div class="flex text-lg font-medium px-0.5">
  131. {$i18n.t('All Users')}
  132. <div class="flex self-center w-[1px] h-6 mx-2.5 bg-gray-200 dark:bg-gray-700" />
  133. <span class="text-lg font-medium text-gray-500 dark:text-gray-300"
  134. >{users.length}</span
  135. >
  136. </div>
  137. <div class="flex gap-1">
  138. <input
  139. class="w-full md:w-60 rounded-xl py-1.5 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  140. placeholder={$i18n.t('Search')}
  141. bind:value={search}
  142. />
  143. <div>
  144. <Tooltip content="Add User">
  145. <button
  146. class=" px-2 py-2 rounded-xl border border-gray-200 dark:border-gray-600 dark:border-0 hover:bg-gray-100 dark:bg-gray-850 dark:hover:bg-gray-800 transition font-medium text-sm flex items-center space-x-1"
  147. on:click={() => {
  148. showAddUserModal = !showAddUserModal;
  149. }}
  150. >
  151. <svg
  152. xmlns="http://www.w3.org/2000/svg"
  153. viewBox="0 0 16 16"
  154. fill="currentColor"
  155. class="w-4 h-4"
  156. >
  157. <path
  158. d="M8.75 3.75a.75.75 0 0 0-1.5 0v3.5h-3.5a.75.75 0 0 0 0 1.5h3.5v3.5a.75.75 0 0 0 1.5 0v-3.5h3.5a.75.75 0 0 0 0-1.5h-3.5v-3.5Z"
  159. />
  160. </svg>
  161. </button>
  162. </Tooltip>
  163. </div>
  164. </div>
  165. </div>
  166. <div class="scrollbar-hidden relative overflow-x-auto whitespace-nowrap">
  167. <table class="w-full text-sm text-left text-gray-500 dark:text-gray-400 table-auto">
  168. <thead
  169. class="text-xs text-gray-700 uppercase bg-gray-50 dark:bg-gray-850 dark:text-gray-400"
  170. >
  171. <tr>
  172. <th scope="col" class="px-3 py-2"> {$i18n.t('Role')} </th>
  173. <th scope="col" class="px-3 py-2"> {$i18n.t('Name')} </th>
  174. <th scope="col" class="px-3 py-2"> {$i18n.t('Email')} </th>
  175. <th scope="col" class="px-3 py-2"> {$i18n.t('Last Active')} </th>
  176. <th scope="col" class="px-3 py-2"> {$i18n.t('Created at')} </th>
  177. <th scope="col" class="px-3 py-2 text-right" />
  178. </tr>
  179. </thead>
  180. <tbody>
  181. {#each users
  182. .filter((user) => {
  183. if (search === '') {
  184. return true;
  185. } else {
  186. let name = user.name.toLowerCase();
  187. const query = search.toLowerCase();
  188. return name.includes(query);
  189. }
  190. })
  191. .slice((page - 1) * 20, page * 20) as user}
  192. <tr class="bg-white border-b dark:bg-gray-900 dark:border-gray-700 text-xs">
  193. <td class="px-3 py-2 min-w-[7rem] w-28">
  194. <button
  195. class=" flex items-center gap-2 text-xs px-3 py-0.5 rounded-lg {user.role ===
  196. 'admin' &&
  197. 'text-sky-600 dark:text-sky-200 bg-sky-200/30'} {user.role ===
  198. 'user' &&
  199. 'text-green-600 dark:text-green-200 bg-green-200/30'} {user.role ===
  200. 'pending' && 'text-gray-600 dark:text-gray-200 bg-gray-200/30'}"
  201. on:click={() => {
  202. if (user.role === 'user') {
  203. updateRoleHandler(user.id, 'admin');
  204. } else if (user.role === 'pending') {
  205. updateRoleHandler(user.id, 'user');
  206. } else {
  207. updateRoleHandler(user.id, 'pending');
  208. }
  209. }}
  210. >
  211. <div
  212. class="w-1 h-1 rounded-full {user.role === 'admin' &&
  213. 'bg-sky-600 dark:bg-sky-300'} {user.role === 'user' &&
  214. 'bg-green-600 dark:bg-green-300'} {user.role === 'pending' &&
  215. 'bg-gray-600 dark:bg-gray-300'}"
  216. />
  217. {$i18n.t(user.role)}</button
  218. >
  219. </td>
  220. <td class="px-3 py-2 font-medium text-gray-900 dark:text-white w-max">
  221. <div class="flex flex-row w-max">
  222. <img
  223. class=" rounded-full w-6 h-6 object-cover mr-2.5"
  224. src={user.profile_image_url}
  225. alt="user"
  226. />
  227. <div class=" font-medium self-center">{user.name}</div>
  228. </div>
  229. </td>
  230. <td class=" px-3 py-2"> {user.email} </td>
  231. <td class=" px-3 py-2">
  232. {dayjs(user.last_active_at * 1000).fromNow()}
  233. </td>
  234. <td class=" px-3 py-2">
  235. {dayjs(user.created_at * 1000).format($i18n.t('MMMM DD, YYYY'))}
  236. </td>
  237. <td class="px-3 py-2 text-right">
  238. <div class="flex justify-end w-full">
  239. {#if user.role !== 'admin'}
  240. <Tooltip content="Chats">
  241. <button
  242. class="self-center w-fit text-sm px-2 py-2 hover:bg-black/5 dark:hover:bg-white/5 rounded-xl"
  243. on:click={async () => {
  244. showUserChatsModal = !showUserChatsModal;
  245. selectedUser = user;
  246. }}
  247. >
  248. <ChatBubbles />
  249. </button>
  250. </Tooltip>
  251. {/if}
  252. <Tooltip content={$i18n.t('Edit User')}>
  253. <button
  254. class="self-center w-fit text-sm px-2 py-2 hover:bg-black/5 dark:hover:bg-white/5 rounded-xl"
  255. on:click={async () => {
  256. showEditUserModal = !showEditUserModal;
  257. selectedUser = user;
  258. }}
  259. >
  260. <svg
  261. xmlns="http://www.w3.org/2000/svg"
  262. fill="none"
  263. viewBox="0 0 24 24"
  264. stroke-width="1.5"
  265. stroke="currentColor"
  266. class="w-4 h-4"
  267. >
  268. <path
  269. stroke-linecap="round"
  270. stroke-linejoin="round"
  271. 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"
  272. />
  273. </svg>
  274. </button>
  275. </Tooltip>
  276. <Tooltip content={$i18n.t('Delete User')}>
  277. <button
  278. class="self-center w-fit text-sm px-2 py-2 hover:bg-black/5 dark:hover:bg-white/5 rounded-xl"
  279. on:click={async () => {
  280. deleteUserHandler(user.id);
  281. }}
  282. >
  283. <svg
  284. xmlns="http://www.w3.org/2000/svg"
  285. fill="none"
  286. viewBox="0 0 24 24"
  287. stroke-width="1.5"
  288. stroke="currentColor"
  289. class="w-4 h-4"
  290. >
  291. <path
  292. stroke-linecap="round"
  293. stroke-linejoin="round"
  294. 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"
  295. />
  296. </svg>
  297. </button>
  298. </Tooltip>
  299. </div>
  300. </td>
  301. </tr>
  302. {/each}
  303. </tbody>
  304. </table>
  305. </div>
  306. <div class=" text-gray-500 text-xs mt-2 text-right">
  307. ⓘ {$i18n.t("Click on the user role button to change a user's role.")}
  308. </div>
  309. <Pagination bind:page count={users.length} />
  310. </div>
  311. </div>
  312. </div>
  313. </div>
  314. </div>
  315. {/if}
  316. </div>
  317. <style>
  318. .font-mona {
  319. font-family: 'Mona Sans';
  320. }
  321. .scrollbar-hidden::-webkit-scrollbar {
  322. display: none; /* for Chrome, Safari and Opera */
  323. }
  324. .scrollbar-hidden {
  325. -ms-overflow-style: none; /* IE and Edge */
  326. scrollbar-width: none; /* Firefox */
  327. }
  328. </style>