ArenaModelModal.svelte 12 KB

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