ValvesModal.svelte 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 {
  14. getUserValvesSpecById as getToolUserValvesSpecById,
  15. getUserValvesById as getToolUserValvesById,
  16. updateUserValvesById as updateToolUserValvesById,
  17. getTools
  18. } from '$lib/apis/tools';
  19. import {
  20. getUserValvesSpecById as getFunctionUserValvesSpecById,
  21. getUserValvesById as getFunctionUserValvesById,
  22. updateUserValvesById as updateFunctionUserValvesById,
  23. getFunctions
  24. } from '$lib/apis/functions';
  25. import Spinner from '../../common/Spinner.svelte';
  26. import Switch from '$lib/components/common/Switch.svelte';
  27. import Valves from '$lib/components/common/Valves.svelte';
  28. import XMark from '$lib/components/icons/XMark.svelte';
  29. const i18n = getContext('i18n');
  30. const dispatch = createEventDispatcher();
  31. export let show = false;
  32. export let type = 'tool';
  33. export let id = null;
  34. export let userValves = false;
  35. let saving = false;
  36. let loading = false;
  37. let valvesSpec = null;
  38. let valves = {};
  39. const submitHandler = async () => {
  40. saving = true;
  41. if (valvesSpec) {
  42. // Convert string to array
  43. for (const property in valvesSpec.properties) {
  44. if (valvesSpec.properties[property]?.type === 'array') {
  45. valves[property] = (valves[property] ?? '').split(',').map((v) => v.trim());
  46. }
  47. }
  48. let res = null;
  49. if (userValves) {
  50. if (type === 'tool') {
  51. res = await updateToolUserValvesById(localStorage.token, id, valves).catch((error) => {
  52. toast.error(`${error}`);
  53. });
  54. } else if (type === 'function') {
  55. res = await updateFunctionUserValvesById(localStorage.token, id, valves).catch(
  56. (error) => {
  57. toast.error(`${error}`);
  58. }
  59. );
  60. }
  61. } else {
  62. if (type === 'tool') {
  63. res = await updateToolValvesById(localStorage.token, id, valves).catch((error) => {
  64. toast.error(`${error}`);
  65. });
  66. } else if (type === 'function') {
  67. res = await updateFunctionValvesById(localStorage.token, id, valves).catch((error) => {
  68. toast.error(`${error}`);
  69. });
  70. }
  71. }
  72. if (res) {
  73. toast.success($i18n.t('Valves updated successfully'));
  74. dispatch('save');
  75. }
  76. }
  77. saving = false;
  78. };
  79. const initHandler = async () => {
  80. loading = true;
  81. valves = {};
  82. valvesSpec = null;
  83. try {
  84. if (userValves) {
  85. if (type === 'tool') {
  86. valves = await getToolUserValvesById(localStorage.token, id);
  87. valvesSpec = await getToolUserValvesSpecById(localStorage.token, id);
  88. } else if (type === 'function') {
  89. valves = await getFunctionUserValvesById(localStorage.token, id);
  90. valvesSpec = await getFunctionUserValvesSpecById(localStorage.token, id);
  91. }
  92. } else {
  93. if (type === 'tool') {
  94. valves = await getToolValvesById(localStorage.token, id);
  95. valvesSpec = await getToolValvesSpecById(localStorage.token, id);
  96. } else if (type === 'function') {
  97. valves = await getFunctionValvesById(localStorage.token, id);
  98. valvesSpec = await getFunctionValvesSpecById(localStorage.token, id);
  99. }
  100. }
  101. if (!valves) {
  102. valves = {};
  103. }
  104. if (valvesSpec) {
  105. // Convert array to string
  106. for (const property in valvesSpec.properties) {
  107. if (valvesSpec.properties[property]?.type === 'array') {
  108. valves[property] = (valves[property] ?? []).join(',');
  109. }
  110. }
  111. }
  112. loading = false;
  113. } catch (e) {
  114. toast.error(`Error fetching valves`);
  115. show = false;
  116. }
  117. };
  118. $: if (show) {
  119. initHandler();
  120. }
  121. </script>
  122. <Modal size="sm" bind:show>
  123. <div>
  124. <div class=" flex justify-between dark:text-gray-300 px-5 pt-4 pb-2">
  125. <div class=" text-lg font-medium self-center">{$i18n.t('Valves')}</div>
  126. <button
  127. class="self-center"
  128. on:click={() => {
  129. show = false;
  130. }}
  131. >
  132. <XMark className={'size-5'} />
  133. </button>
  134. </div>
  135. <div class="flex flex-col md:flex-row w-full px-5 pb-4 md:space-x-4 dark:text-gray-200">
  136. <div class=" flex flex-col w-full sm:flex-row sm:justify-center sm:space-x-6">
  137. <form
  138. class="flex flex-col w-full"
  139. on:submit|preventDefault={() => {
  140. submitHandler();
  141. }}
  142. >
  143. <div class="px-1">
  144. {#if !loading}
  145. <Valves {valvesSpec} bind:valves />
  146. {:else}
  147. <Spinner className="size-5" />
  148. {/if}
  149. </div>
  150. <div class="flex justify-end pt-3 text-sm font-medium">
  151. <button
  152. 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
  153. ? ' cursor-not-allowed'
  154. : ''}"
  155. type="submit"
  156. disabled={saving}
  157. >
  158. {$i18n.t('Save')}
  159. {#if saving}
  160. <div class="ml-2 self-center">
  161. <Spinner />
  162. </div>
  163. {/if}
  164. </button>
  165. </div>
  166. </form>
  167. </div>
  168. </div>
  169. </div>
  170. </Modal>
  171. <style>
  172. input::-webkit-outer-spin-button,
  173. input::-webkit-inner-spin-button {
  174. /* display: none; <- Crashes Chrome on hover */
  175. -webkit-appearance: none;
  176. margin: 0; /* <-- Apparently some margin are still there even though it's hidden */
  177. }
  178. .tabs::-webkit-scrollbar {
  179. display: none; /* for Chrome, Safari and Opera */
  180. }
  181. .tabs {
  182. -ms-overflow-style: none; /* IE and Edge */
  183. scrollbar-width: none; /* Firefox */
  184. }
  185. input[type='number'] {
  186. -moz-appearance: textfield; /* Firefox */
  187. }
  188. </style>