Skip to content

Commit da8c42e

Browse files
tjbcksilentoplayz
andcommitted
feat: Sort models by name in ascending/descending order
Co-Authored-By: silentoplayz <[email protected]>
1 parent ddb3058 commit da8c42e

File tree

2 files changed

+64
-10
lines changed

2 files changed

+64
-10
lines changed

src/lib/components/admin/Settings/Models/ConfigureModelsModal.svelte

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import Spinner from '$lib/components/common/Spinner.svelte';
1717
import Minus from '$lib/components/icons/Minus.svelte';
1818
import Plus from '$lib/components/icons/Plus.svelte';
19+
import ChevronUp from '$lib/components/icons/ChevronUp.svelte';
20+
import ChevronDown from '$lib/components/icons/ChevronDown.svelte';
1921
2022
export let show = false;
2123
export let initHandler = () => {};
@@ -26,6 +28,9 @@
2628
let defaultModelIds = [];
2729
let modelIds = [];
2830
31+
let sortKey = '';
32+
let sortOrder = '';
33+
2934
let loading = false;
3035
let showResetModal = false;
3136
@@ -71,6 +76,9 @@
7176
// Add remaining IDs not in MODEL_ORDER_LIST, sorted alphabetically
7277
...allModelIds.filter((id) => !orderedSet.has(id)).sort((a, b) => a.localeCompare(b))
7378
];
79+
80+
sortKey = '';
81+
sortOrder = '';
7482
};
7583
const submitHandler = async () => {
7684
loading = true;
@@ -145,9 +153,45 @@
145153
>
146154
<div>
147155
<div class="flex flex-col w-full">
148-
<div class="mb-1 flex justify-between">
156+
<button
157+
class="mb-1 flex gap-2"
158+
type="button"
159+
on:click={() => {
160+
sortKey = 'model';
161+
162+
if (sortOrder === 'asc') {
163+
sortOrder = 'desc';
164+
} else {
165+
sortOrder = 'asc';
166+
}
167+
168+
modelIds = modelIds
169+
.filter((id) => id !== '')
170+
.sort((a, b) => {
171+
const nameA = $models.find((model) => model.id === a)?.name || a;
172+
const nameB = $models.find((model) => model.id === b)?.name || b;
173+
return sortOrder === 'desc'
174+
? nameA.localeCompare(nameB)
175+
: nameB.localeCompare(nameA);
176+
});
177+
}}
178+
>
149179
<div class="text-xs text-gray-500">{$i18n.t('Reorder Models')}</div>
150-
</div>
180+
181+
{#if sortKey === 'model'}
182+
<span class="font-normal self-center">
183+
{#if sortOrder === 'asc'}
184+
<ChevronUp className="size-3" />
185+
{:else}
186+
<ChevronDown className="size-3" />
187+
{/if}
188+
</span>
189+
{:else}
190+
<span class="invisible">
191+
<ChevronUp className="size-3" />
192+
</span>
193+
{/if}
194+
</button>
151195
152196
<ModelList bind:modelIds />
153197
</div>

src/lib/components/admin/Settings/Models/ModelList.svelte

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,24 @@
2121
modelIds = modelList;
2222
};
2323
24-
onMount(() => {
25-
sortable = Sortable.create(modelListElement, {
26-
animation: 150,
27-
onUpdate: async (event) => {
28-
positionChangeHandler();
29-
}
30-
});
31-
});
24+
$: if (modelIds) {
25+
init();
26+
}
27+
28+
const init = () => {
29+
if (sortable) {
30+
sortable.destroy();
31+
}
32+
33+
if (modelListElement) {
34+
sortable = Sortable.create(modelListElement, {
35+
animation: 150,
36+
onUpdate: async (event) => {
37+
positionChangeHandler();
38+
}
39+
});
40+
}
41+
};
3242
</script>
3343

3444
{#if modelIds.length > 0}

0 commit comments

Comments
 (0)