ArenaModelModal.svelte 11 KB

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