Users.svelte 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <script lang="ts">
  2. import { getModelFilterConfig, updateModelFilterConfig } from '$lib/apis';
  3. import { getSignUpEnabledStatus, toggleSignUpEnabledStatus } from '$lib/apis/auths';
  4. import { getUserPermissions, updateUserPermissions } from '$lib/apis/users';
  5. import { models } from '$lib/stores';
  6. import { onMount } from 'svelte';
  7. export let saveHandler: Function;
  8. let whitelistEnabled = false;
  9. let whitelistModels = [''];
  10. let permissions = {
  11. chat: {
  12. deletion: true
  13. }
  14. };
  15. onMount(async () => {
  16. permissions = await getUserPermissions(localStorage.token);
  17. const res = await getModelFilterConfig(localStorage.token);
  18. if (res) {
  19. whitelistEnabled = res.enabled;
  20. whitelistModels = res.models.length > 0 ? res.models : [''];
  21. }
  22. });
  23. </script>
  24. <form
  25. class="flex flex-col h-full justify-between space-y-3 text-sm"
  26. on:submit|preventDefault={async () => {
  27. // console.log('submit');
  28. await updateUserPermissions(localStorage.token, permissions);
  29. await updateModelFilterConfig(localStorage.token, whitelistEnabled, whitelistModels);
  30. saveHandler();
  31. }}
  32. >
  33. <div class=" space-y-3 pr-1.5 overflow-y-scroll max-h-80">
  34. <div>
  35. <div class=" mb-2 text-sm font-medium">User Permissions</div>
  36. <div class=" flex w-full justify-between">
  37. <div class=" self-center text-xs font-medium">Allow Chat Deletion</div>
  38. <button
  39. class="p-1 px-3 text-xs flex rounded transition"
  40. on:click={() => {
  41. permissions.chat.deletion = !permissions.chat.deletion;
  42. }}
  43. type="button"
  44. >
  45. {#if permissions.chat.deletion}
  46. <svg
  47. xmlns="http://www.w3.org/2000/svg"
  48. viewBox="0 0 16 16"
  49. fill="currentColor"
  50. class="w-4 h-4"
  51. >
  52. <path
  53. d="M11.5 1A3.5 3.5 0 0 0 8 4.5V7H2.5A1.5 1.5 0 0 0 1 8.5v5A1.5 1.5 0 0 0 2.5 15h7a1.5 1.5 0 0 0 1.5-1.5v-5A1.5 1.5 0 0 0 9.5 7V4.5a2 2 0 1 1 4 0v1.75a.75.75 0 0 0 1.5 0V4.5A3.5 3.5 0 0 0 11.5 1Z"
  54. />
  55. </svg>
  56. <span class="ml-2 self-center">Allow</span>
  57. {:else}
  58. <svg
  59. xmlns="http://www.w3.org/2000/svg"
  60. viewBox="0 0 16 16"
  61. fill="currentColor"
  62. class="w-4 h-4"
  63. >
  64. <path
  65. fill-rule="evenodd"
  66. d="M8 1a3.5 3.5 0 0 0-3.5 3.5V7A1.5 1.5 0 0 0 3 8.5v5A1.5 1.5 0 0 0 4.5 15h7a1.5 1.5 0 0 0 1.5-1.5v-5A1.5 1.5 0 0 0 11.5 7V4.5A3.5 3.5 0 0 0 8 1Zm2 6V4.5a2 2 0 1 0-4 0V7h4Z"
  67. clip-rule="evenodd"
  68. />
  69. </svg>
  70. <span class="ml-2 self-center">Don't Allow</span>
  71. {/if}
  72. </button>
  73. </div>
  74. </div>
  75. <hr class=" dark:border-gray-700 my-2" />
  76. <div class="mt-2 space-y-3 pr-1.5">
  77. <div>
  78. <div class="mb-2">
  79. <div class="flex justify-between items-center text-xs">
  80. <div class=" text-sm font-medium">Manage Models</div>
  81. </div>
  82. </div>
  83. <div class=" space-y-3">
  84. <div>
  85. <div class="flex justify-between items-center text-xs">
  86. <div class=" text-xs font-medium">Model Whitelisting</div>
  87. <button
  88. class=" text-xs font-medium text-gray-500"
  89. type="button"
  90. on:click={() => {
  91. whitelistEnabled = !whitelistEnabled;
  92. }}>{whitelistEnabled ? 'On' : 'Off'}</button
  93. >
  94. </div>
  95. </div>
  96. {#if whitelistEnabled}
  97. <div>
  98. <div class=" space-y-1.5">
  99. {#each whitelistModels as modelId, modelIdx}
  100. <div class="flex w-full">
  101. <div class="flex-1 mr-2">
  102. <select
  103. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  104. bind:value={modelId}
  105. placeholder="Select a model"
  106. >
  107. <option value="" disabled selected>Select a model</option>
  108. {#each $models.filter((model) => model.id) as model}
  109. <option value={model.id} class="bg-gray-100 dark:bg-gray-700"
  110. >{model.name}</option
  111. >
  112. {/each}
  113. </select>
  114. </div>
  115. {#if modelIdx === 0}
  116. <button
  117. class="px-2.5 bg-gray-100 hover:bg-gray-200 text-gray-800 dark:bg-gray-900 dark:text-white rounded-lg transition"
  118. type="button"
  119. on:click={() => {
  120. if (whitelistModels.at(-1) !== '') {
  121. whitelistModels = [...whitelistModels, ''];
  122. }
  123. }}
  124. >
  125. <svg
  126. xmlns="http://www.w3.org/2000/svg"
  127. viewBox="0 0 16 16"
  128. fill="currentColor"
  129. class="w-4 h-4"
  130. >
  131. <path
  132. 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"
  133. />
  134. </svg>
  135. </button>
  136. {:else}
  137. <button
  138. class="px-2.5 bg-gray-100 hover:bg-gray-200 text-gray-800 dark:bg-gray-900 dark:text-white rounded-lg transition"
  139. type="button"
  140. on:click={() => {
  141. whitelistModels.splice(modelIdx, 1);
  142. whitelistModels = whitelistModels;
  143. }}
  144. >
  145. <svg
  146. xmlns="http://www.w3.org/2000/svg"
  147. viewBox="0 0 16 16"
  148. fill="currentColor"
  149. class="w-4 h-4"
  150. >
  151. <path d="M3.75 7.25a.75.75 0 0 0 0 1.5h8.5a.75.75 0 0 0 0-1.5h-8.5Z" />
  152. </svg>
  153. </button>
  154. {/if}
  155. </div>
  156. {/each}
  157. </div>
  158. <div class="flex justify-end items-center text-xs mt-1.5 text-right">
  159. <div class=" text-xs font-medium">
  160. {whitelistModels.length} Model(s) Whitelisted
  161. </div>
  162. </div>
  163. </div>
  164. {/if}
  165. </div>
  166. </div>
  167. </div>
  168. </div>
  169. <div class="flex justify-end pt-3 text-sm font-medium">
  170. <button
  171. class=" px-4 py-2 bg-emerald-600 hover:bg-emerald-700 text-gray-100 transition rounded"
  172. type="submit"
  173. >
  174. Save
  175. </button>
  176. </div>
  177. </form>