ModelModal.svelte 11 KB

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