ModelModal.svelte 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. <script>
  2. import { createEventDispatcher, getContext, onMount } from 'svelte';
  3. const i18n = getContext('i18n');
  4. const dispatch = createEventDispatcher();
  5. import Modal from '$lib/components/common/Modal.svelte';
  6. import { models } from '$lib/stores';
  7. import Plus from '$lib/components/icons/Plus.svelte';
  8. import Minus from '$lib/components/icons/Minus.svelte';
  9. import PencilSolid from '$lib/components/icons/PencilSolid.svelte';
  10. import { toast } from 'svelte-sonner';
  11. export let show = false;
  12. export let edit = false;
  13. export let model = null;
  14. let name = '';
  15. let id = '';
  16. $: if (name) {
  17. generateId();
  18. }
  19. const generateId = () => {
  20. if (!edit) {
  21. id = name
  22. .toLowerCase()
  23. .replace(/[^a-z0-9]/g, '-')
  24. .replace(/-+/g, '-')
  25. .replace(/^-|-$/g, '');
  26. }
  27. };
  28. let profileImageUrl = '/favicon.png';
  29. let description = '';
  30. let selectedModelId = '';
  31. let modelIds = [];
  32. let filterMode = 'include';
  33. let imageInputElement;
  34. let loading = false;
  35. const addModelHandler = () => {
  36. if (selectedModelId) {
  37. modelIds = [...modelIds, selectedModelId];
  38. selectedModelId = '';
  39. }
  40. };
  41. const submitHandler = () => {
  42. loading = true;
  43. if (!name || !id) {
  44. loading = false;
  45. toast.error('Name and ID are required, please fill them out');
  46. return;
  47. }
  48. if (!edit) {
  49. if ($models.find((model) => model.name === name)) {
  50. loading = false;
  51. name = '';
  52. toast.error('Model name already exists, please choose a different one');
  53. return;
  54. }
  55. }
  56. const model = {
  57. id: id,
  58. name: name,
  59. meta: {
  60. profile_image_url: profileImageUrl,
  61. description: description || null,
  62. model_ids: modelIds.length > 0 ? modelIds : null,
  63. filter_mode: modelIds.length > 0 ? (filterMode ? filterMode : null) : null
  64. }
  65. };
  66. dispatch('submit', model);
  67. loading = false;
  68. show = false;
  69. name = '';
  70. id = '';
  71. profileImageUrl = '/favicon.png';
  72. description = '';
  73. modelIds = [];
  74. selectedModelId = '';
  75. };
  76. const initModel = () => {
  77. if (model) {
  78. name = model.name;
  79. id = model.id;
  80. profileImageUrl = model.meta.profile_image_url;
  81. description = model.meta.description;
  82. modelIds = model.meta.model_ids || [];
  83. filterMode = model.meta?.filter_mode ?? 'include';
  84. }
  85. };
  86. $: if (show) {
  87. initModel();
  88. }
  89. onMount(() => {
  90. initModel();
  91. });
  92. </script>
  93. <Modal size="sm" bind:show>
  94. <div>
  95. <div class=" flex justify-between dark:text-gray-100 px-5 pt-4 pb-2">
  96. <div class=" text-lg font-medium self-center font-primary">
  97. {#if edit}
  98. {$i18n.t('Edit Arena Model')}
  99. {:else}
  100. {$i18n.t('Add Arena Model')}
  101. {/if}
  102. </div>
  103. <button
  104. class="self-center"
  105. on:click={() => {
  106. show = false;
  107. }}
  108. >
  109. <svg
  110. xmlns="http://www.w3.org/2000/svg"
  111. viewBox="0 0 20 20"
  112. fill="currentColor"
  113. class="w-5 h-5"
  114. >
  115. <path
  116. 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"
  117. />
  118. </svg>
  119. </button>
  120. </div>
  121. <div class="flex flex-col md:flex-row w-full px-4 pb-4 md:space-x-4 dark:text-gray-200">
  122. <div class=" flex flex-col w-full sm:flex-row sm:justify-center sm:space-x-6">
  123. <form
  124. class="flex flex-col w-full"
  125. on:submit|preventDefault={() => {
  126. submitHandler();
  127. }}
  128. >
  129. <div class="px-1">
  130. <div class="flex justify-center pb-3">
  131. <input
  132. bind:this={imageInputElement}
  133. type="file"
  134. hidden
  135. accept="image/*"
  136. on:change={(e) => {
  137. const files = e.target.files ?? [];
  138. let reader = new FileReader();
  139. reader.onload = (event) => {
  140. let originalImageUrl = `${event.target.result}`;
  141. const img = new Image();
  142. img.src = originalImageUrl;
  143. img.onload = function () {
  144. const canvas = document.createElement('canvas');
  145. const ctx = canvas.getContext('2d');
  146. // Calculate the aspect ratio of the image
  147. const aspectRatio = img.width / img.height;
  148. // Calculate the new width and height to fit within 250x250
  149. let newWidth, newHeight;
  150. if (aspectRatio > 1) {
  151. newWidth = 250 * aspectRatio;
  152. newHeight = 250;
  153. } else {
  154. newWidth = 250;
  155. newHeight = 250 / aspectRatio;
  156. }
  157. // Set the canvas size
  158. canvas.width = 250;
  159. canvas.height = 250;
  160. // Calculate the position to center the image
  161. const offsetX = (250 - newWidth) / 2;
  162. const offsetY = (250 - newHeight) / 2;
  163. // Draw the image on the canvas
  164. ctx.drawImage(img, offsetX, offsetY, newWidth, newHeight);
  165. // Get the base64 representation of the compressed image
  166. const compressedSrc = canvas.toDataURL('image/jpeg');
  167. // Display the compressed image
  168. profileImageUrl = compressedSrc;
  169. e.target.files = null;
  170. };
  171. };
  172. if (
  173. files.length > 0 &&
  174. ['image/gif', 'image/webp', 'image/jpeg', 'image/png'].includes(
  175. files[0]['type']
  176. )
  177. ) {
  178. reader.readAsDataURL(files[0]);
  179. }
  180. }}
  181. />
  182. <button
  183. class="relative rounded-full w-fit h-fit shrink-0"
  184. type="button"
  185. on:click={() => {
  186. imageInputElement.click();
  187. }}
  188. >
  189. <img
  190. src={profileImageUrl}
  191. class="size-16 rounded-full object-cover shrink-0"
  192. alt="Profile"
  193. />
  194. <div
  195. class="absolute flex justify-center rounded-full bottom-0 left-0 right-0 top-0 h-full w-full overflow-hidden bg-gray-700 bg-fixed opacity-0 transition duration-300 ease-in-out hover:opacity-50"
  196. >
  197. <div class="my-auto text-white">
  198. <PencilSolid className="size-4" />
  199. </div>
  200. </div>
  201. </button>
  202. </div>
  203. <div class="flex gap-2">
  204. <div class="flex flex-col w-full">
  205. <div class=" mb-0.5 text-xs text-gray-500">{$i18n.t('Name')}</div>
  206. <div class="flex-1">
  207. <input
  208. class="w-full text-sm bg-transparent placeholder:text-gray-300 dark:placeholder:text-gray-700 outline-none"
  209. type="text"
  210. bind:value={name}
  211. placeholder={$i18n.t('Model Name')}
  212. autocomplete="off"
  213. required
  214. />
  215. </div>
  216. </div>
  217. <div class="flex flex-col w-full">
  218. <div class=" mb-0.5 text-xs text-gray-500">{$i18n.t('ID')}</div>
  219. <div class="flex-1">
  220. <input
  221. class="w-full text-sm bg-transparent placeholder:text-gray-300 dark:placeholder:text-gray-700 outline-none"
  222. type="text"
  223. bind:value={id}
  224. placeholder={$i18n.t('Model ID')}
  225. autocomplete="off"
  226. required
  227. disabled={edit}
  228. />
  229. </div>
  230. </div>
  231. </div>
  232. <div class="flex flex-col w-full mt-2">
  233. <div class=" mb-1 text-xs text-gray-500">{$i18n.t('Description')}</div>
  234. <div class="flex-1">
  235. <input
  236. class="w-full text-sm bg-transparent placeholder:text-gray-300 dark:placeholder:text-gray-700 outline-none"
  237. type="text"
  238. bind:value={description}
  239. placeholder={$i18n.t('Enter description')}
  240. autocomplete="off"
  241. />
  242. </div>
  243. </div>
  244. <hr class=" border-gray-100 dark:border-gray-700/10 my-2.5 w-full" />
  245. <div class="flex flex-col w-full">
  246. <div class="mb-1 flex justify-between">
  247. <div class="text-xs text-gray-500">{$i18n.t('Models')}</div>
  248. <div>
  249. <button
  250. class=" text-xs text-gray-500"
  251. type="button"
  252. on:click={() => {
  253. filterMode = filterMode === 'include' ? 'exclude' : 'include';
  254. }}
  255. >
  256. {#if filterMode === 'include'}
  257. {$i18n.t('Include')}
  258. {:else}
  259. {$i18n.t('Exclude')}
  260. {/if}
  261. </button>
  262. </div>
  263. </div>
  264. {#if modelIds.length > 0}
  265. <div class="flex flex-col">
  266. {#each modelIds as modelId, modelIdx}
  267. <div class=" flex gap-2 w-full justify-between items-center">
  268. <div class=" text-sm flex-1 py-1 rounded-lg">
  269. {$models.find((model) => model.id === modelId)?.name}
  270. </div>
  271. <div class="flex-shrink-0">
  272. <button
  273. type="button"
  274. on:click={() => {
  275. modelIds = modelIds.filter((_, idx) => idx !== modelIdx);
  276. }}
  277. >
  278. <Minus strokeWidth="2" className="size-3.5" />
  279. </button>
  280. </div>
  281. </div>
  282. {/each}
  283. </div>
  284. {:else}
  285. <div class="text-gray-500 text-xs text-center py-2">
  286. {$i18n.t('Leave empty to include all models or select specific models')}
  287. </div>
  288. {/if}
  289. </div>
  290. <hr class=" border-gray-100 dark:border-gray-700/10 my-2.5 w-full" />
  291. <div class="flex items-center">
  292. <select
  293. class="w-full py-1 text-sm rounded-lg bg-transparent {selectedModelId
  294. ? ''
  295. : 'text-gray-500'} placeholder:text-gray-300 dark:placeholder:text-gray-700 outline-none"
  296. bind:value={selectedModelId}
  297. >
  298. <option value="">{$i18n.t('Select a model')}</option>
  299. {#each $models.filter((m) => m?.owned_by !== 'arena') as model}
  300. <option value={model.id} class="bg-gray-50 dark:bg-gray-700">{model.name}</option>
  301. {/each}
  302. </select>
  303. <div>
  304. <button
  305. type="button"
  306. on:click={() => {
  307. addModelHandler();
  308. }}
  309. >
  310. <Plus className="size-3.5" strokeWidth="2" />
  311. </button>
  312. </div>
  313. </div>
  314. </div>
  315. <div class="flex justify-end pt-3 text-sm font-medium gap-1.5">
  316. {#if edit}
  317. <button
  318. class="px-3.5 py-1.5 text-sm font-medium dark:bg-black dark:hover:bg-gray-900 dark:text-white bg-white text-black hover:bg-gray-100 transition rounded-full flex flex-row space-x-1 items-center"
  319. type="button"
  320. on:click={() => {
  321. dispatch('delete', model);
  322. show = false;
  323. }}
  324. >
  325. {$i18n.t('Delete')}
  326. </button>
  327. {/if}
  328. <button
  329. 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 flex flex-row space-x-1 items-center {loading
  330. ? ' cursor-not-allowed'
  331. : ''}"
  332. type="submit"
  333. disabled={loading}
  334. >
  335. {$i18n.t('Save')}
  336. {#if loading}
  337. <div class="ml-2 self-center">
  338. <svg
  339. class=" w-4 h-4"
  340. viewBox="0 0 24 24"
  341. fill="currentColor"
  342. xmlns="http://www.w3.org/2000/svg"
  343. ><style>
  344. .spinner_ajPY {
  345. transform-origin: center;
  346. animation: spinner_AtaB 0.75s infinite linear;
  347. }
  348. @keyframes spinner_AtaB {
  349. 100% {
  350. transform: rotate(360deg);
  351. }
  352. }
  353. </style><path
  354. 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"
  355. opacity=".25"
  356. /><path
  357. 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"
  358. class="spinner_ajPY"
  359. /></svg
  360. >
  361. </div>
  362. {/if}
  363. </button>
  364. </div>
  365. </form>
  366. </div>
  367. </div>
  368. </div>
  369. </Modal>