ValvesModal.svelte 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <script lang="ts">
  2. import { toast } from 'svelte-sonner';
  3. import { createEventDispatcher } from 'svelte';
  4. import { onMount, getContext } from 'svelte';
  5. import { addUser } from '$lib/apis/auths';
  6. import Modal from '../../common/Modal.svelte';
  7. import {
  8. getFunctionValvesById,
  9. getFunctionValvesSpecById,
  10. updateFunctionValvesById
  11. } from '$lib/apis/functions';
  12. import { getToolValvesById, getToolValvesSpecById, updateToolValvesById } from '$lib/apis/tools';
  13. import Spinner from '../../common/Spinner.svelte';
  14. import Switch from '$lib/components/common/Switch.svelte';
  15. import Valves from '$lib/components/common/Valves.svelte';
  16. const i18n = getContext('i18n');
  17. const dispatch = createEventDispatcher();
  18. export let show = false;
  19. export let type = 'tool';
  20. export let id = null;
  21. let saving = false;
  22. let loading = false;
  23. let valvesSpec = null;
  24. let valves = {};
  25. const submitHandler = async () => {
  26. saving = true;
  27. if (valvesSpec) {
  28. // Convert string to array
  29. for (const property in valvesSpec.properties) {
  30. if (valvesSpec.properties[property]?.type === 'array') {
  31. valves[property] = (valves[property] ?? '').split(',').map((v) => v.trim());
  32. }
  33. }
  34. let res = null;
  35. if (type === 'tool') {
  36. res = await updateToolValvesById(localStorage.token, id, valves).catch((error) => {
  37. toast.error(`${error}`);
  38. });
  39. } else if (type === 'function') {
  40. res = await updateFunctionValvesById(localStorage.token, id, valves).catch((error) => {
  41. toast.error(`${error}`);
  42. });
  43. }
  44. if (res) {
  45. toast.success('Valves updated successfully');
  46. dispatch('save');
  47. }
  48. }
  49. saving = false;
  50. };
  51. const initHandler = async () => {
  52. loading = true;
  53. valves = {};
  54. valvesSpec = null;
  55. if (type === 'tool') {
  56. valves = await getToolValvesById(localStorage.token, id);
  57. valvesSpec = await getToolValvesSpecById(localStorage.token, id);
  58. } else if (type === 'function') {
  59. valves = await getFunctionValvesById(localStorage.token, id);
  60. valvesSpec = await getFunctionValvesSpecById(localStorage.token, id);
  61. }
  62. if (!valves) {
  63. valves = {};
  64. }
  65. if (valvesSpec) {
  66. // Convert array to string
  67. for (const property in valvesSpec.properties) {
  68. if (valvesSpec.properties[property]?.type === 'array') {
  69. valves[property] = (valves[property] ?? []).join(',');
  70. }
  71. }
  72. }
  73. loading = false;
  74. };
  75. $: if (show) {
  76. initHandler();
  77. }
  78. </script>
  79. <Modal size="sm" bind:show>
  80. <div>
  81. <div class=" flex justify-between dark:text-gray-300 px-5 pt-4 pb-2">
  82. <div class=" text-lg font-medium self-center">{$i18n.t('Valves')}</div>
  83. <button
  84. class="self-center"
  85. on:click={() => {
  86. show = false;
  87. }}
  88. >
  89. <svg
  90. xmlns="http://www.w3.org/2000/svg"
  91. viewBox="0 0 20 20"
  92. fill="currentColor"
  93. class="w-5 h-5"
  94. >
  95. <path
  96. 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"
  97. />
  98. </svg>
  99. </button>
  100. </div>
  101. <div class="flex flex-col md:flex-row w-full px-5 pb-4 md:space-x-4 dark:text-gray-200">
  102. <div class=" flex flex-col w-full sm:flex-row sm:justify-center sm:space-x-6">
  103. <form
  104. class="flex flex-col w-full"
  105. on:submit|preventDefault={() => {
  106. submitHandler();
  107. }}
  108. >
  109. <div class="px-1">
  110. {#if !loading}
  111. <Valves {valvesSpec} bind:valves />
  112. {:else}
  113. <Spinner className="size-5" />
  114. {/if}
  115. </div>
  116. <div class="flex justify-end pt-3 text-sm font-medium">
  117. <button
  118. class="px-3.5 py-1.5 text-sm font-medium bg-black hover:bg-gray-900 text-white dark:bg-white dark:text-black dark:hover:bg-gray-100 transition rounded-full {saving
  119. ? ' cursor-not-allowed'
  120. : ''}"
  121. type="submit"
  122. disabled={saving}
  123. >
  124. {$i18n.t('Save')}
  125. {#if saving}
  126. <div class="ml-2 self-center">
  127. <svg
  128. class=" w-4 h-4"
  129. viewBox="0 0 24 24"
  130. fill="currentColor"
  131. xmlns="http://www.w3.org/2000/svg"
  132. ><style>
  133. .spinner_ajPY {
  134. transform-origin: center;
  135. animation: spinner_AtaB 0.75s infinite linear;
  136. }
  137. @keyframes spinner_AtaB {
  138. 100% {
  139. transform: rotate(360deg);
  140. }
  141. }
  142. </style><path
  143. d="M12,1A11,11,0,1,0,23,12,11,11,0,0,0,12,1Zm0,19a8,8,0,1,1,8-8A8,8,0,0,1,12,20Z"
  144. opacity=".25"
  145. /><path
  146. d="M10.14,1.16a11,11,0,0,0-9,8.92A1.59,1.59,0,0,0,2.46,12,1.52,1.52,0,0,0,4.11,10.7a8,8,0,0,1,6.66-6.61A1.42,1.42,0,0,0,12,2.69h0A1.57,1.57,0,0,0,10.14,1.16Z"
  147. class="spinner_ajPY"
  148. /></svg
  149. >
  150. </div>
  151. {/if}
  152. </button>
  153. </div>
  154. </form>
  155. </div>
  156. </div>
  157. </div>
  158. </Modal>
  159. <style>
  160. input::-webkit-outer-spin-button,
  161. input::-webkit-inner-spin-button {
  162. /* display: none; <- Crashes Chrome on hover */
  163. -webkit-appearance: none;
  164. margin: 0; /* <-- Apparently some margin are still there even though it's hidden */
  165. }
  166. .tabs::-webkit-scrollbar {
  167. display: none; /* for Chrome, Safari and Opera */
  168. }
  169. .tabs {
  170. -ms-overflow-style: none; /* IE and Edge */
  171. scrollbar-width: none; /* Firefox */
  172. }
  173. input[type='number'] {
  174. -moz-appearance: textfield; /* Firefox */
  175. }
  176. </style>