ArenaModelModal.svelte 12 KB

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