Interface.svelte 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <script lang="ts">
  2. import { getBackendConfig } from '$lib/apis';
  3. import { setDefaultPromptSuggestions } from '$lib/apis/configs';
  4. import { config, user } from '$lib/stores';
  5. import { createEventDispatcher, onMount } from 'svelte';
  6. const dispatch = createEventDispatcher();
  7. // Interface
  8. let promptSuggestions = [];
  9. const updateInterfaceHandler = async () => {
  10. promptSuggestions = await setDefaultPromptSuggestions(localStorage.token, promptSuggestions);
  11. await config.set(await getBackendConfig());
  12. };
  13. onMount(async () => {
  14. if ($user.role === 'admin') {
  15. promptSuggestions = $config?.default_prompt_suggestions;
  16. }
  17. });
  18. </script>
  19. <form
  20. class="flex flex-col h-full justify-between space-y-3 text-sm"
  21. on:submit|preventDefault={() => {
  22. updateInterfaceHandler();
  23. dispatch('save');
  24. }}
  25. >
  26. <div class=" space-y-3 pr-1.5 overflow-y-scroll max-h-80">
  27. <div class="flex w-full justify-between mb-2">
  28. <div class=" self-center text-sm font-semibold">Default Prompt Suggestions</div>
  29. <button
  30. class="p-1 px-3 text-xs flex rounded transition"
  31. type="button"
  32. on:click={() => {
  33. if (promptSuggestions.length === 0 || promptSuggestions.at(-1).content !== '') {
  34. promptSuggestions = [...promptSuggestions, { content: '', title: ['', ''] }];
  35. }
  36. }}
  37. >
  38. <svg
  39. xmlns="http://www.w3.org/2000/svg"
  40. viewBox="0 0 20 20"
  41. fill="currentColor"
  42. class="w-4 h-4"
  43. >
  44. <path
  45. d="M10.75 4.75a.75.75 0 00-1.5 0v4.5h-4.5a.75.75 0 000 1.5h4.5v4.5a.75.75 0 001.5 0v-4.5h4.5a.75.75 0 000-1.5h-4.5v-4.5z"
  46. />
  47. </svg>
  48. </button>
  49. </div>
  50. <div class="flex flex-col space-y-1">
  51. {#each promptSuggestions as prompt, promptIdx}
  52. <div class=" flex border dark:border-gray-600 rounded-lg">
  53. <div class="flex flex-col flex-1">
  54. <div class="flex border-b dark:border-gray-600 w-full">
  55. <input
  56. class="px-3 py-1.5 text-xs w-full bg-transparent outline-none border-r dark:border-gray-600"
  57. placeholder="Title (e.g. Tell me a fun fact)"
  58. bind:value={prompt.title[0]}
  59. />
  60. <input
  61. class="px-3 py-1.5 text-xs w-full bg-transparent outline-none border-r dark:border-gray-600"
  62. placeholder="Subtitle (e.g. about the Roman Empire)"
  63. bind:value={prompt.title[1]}
  64. />
  65. </div>
  66. <input
  67. class="px-3 py-1.5 text-xs w-full bg-transparent outline-none border-r dark:border-gray-600"
  68. placeholder="Prompt (e.g. Tell me a fun fact about the Roman Empire)"
  69. bind:value={prompt.content}
  70. />
  71. </div>
  72. <button
  73. class="px-2"
  74. type="button"
  75. on:click={() => {
  76. promptSuggestions.splice(promptIdx, 1);
  77. promptSuggestions = promptSuggestions;
  78. }}
  79. >
  80. <svg
  81. xmlns="http://www.w3.org/2000/svg"
  82. viewBox="0 0 20 20"
  83. fill="currentColor"
  84. class="w-4 h-4"
  85. >
  86. <path
  87. d="M6.28 5.22a.75.75 0 00-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 101.06 1.06L10 11.06l3.72 3.72a.75.75 0 101.06-1.06L11.06 10l3.72-3.72a.75.75 0 00-1.06-1.06L10 8.94 6.28 5.22z"
  88. />
  89. </svg>
  90. </button>
  91. </div>
  92. {/each}
  93. </div>
  94. {#if promptSuggestions.length > 0}
  95. <div class="text-xs text-left w-full mt-2">
  96. Adjusting these settings will apply changes universally to all users.
  97. </div>
  98. {/if}
  99. </div>
  100. <div class="flex justify-end pt-3 text-sm font-medium">
  101. <button
  102. class=" px-4 py-2 bg-emerald-600 hover:bg-emerald-700 text-gray-100 transition rounded"
  103. type="submit"
  104. >
  105. Save
  106. </button>
  107. </div>
  108. </form>