Users.svelte 6.0 KB

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