File size: 3,238 Bytes
9e93b10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<script>
	import Sortable from "sortablejs";

	import { onDestroy, onMount, tick } from "svelte";

	import {
		chatId,
		config,
		mobile,
		models,
		settings,
		showSidebar,
	} from "$lib/stores";
	import { REXPRO_BASE_URL } from "$lib/constants";
	import { updateUserSettings } from "$lib/apis/users";
	import PinnedModelItem from "./PinnedModelItem.svelte";

	export let selectedChatId = null;
	export let shiftKey = false;

	let pinnedModels = [];

	const initPinnedModelsSortable = () => {
		const pinnedModelsList = document.getElementById("pinned-models-list");
		if (pinnedModelsList && !$mobile) {
			new Sortable(pinnedModelsList, {
				animation: 150,
				onUpdate: async (event) => {
					const modelId = event.item.dataset.id;
					const newIndex = event.newIndex;

					const pinnedModels = $settings.pinnedModels;
					const oldIndex = pinnedModels.indexOf(modelId);

					pinnedModels.splice(oldIndex, 1);
					pinnedModels.splice(newIndex, 0, modelId);

					settings.set({ ...$settings, pinnedModels: pinnedModels });
					await updateUserSettings(localStorage.token, {
						ui: $settings,
					});
				},
			});
		}
	};

	let unsubscribeSettings;

	const cleanupStalePinnedModels = async (modelIds) => {
		const validModels = modelIds.filter((id) => {
			const model = $models.find((m) => m.id === id);
			// Remove if model not found (deleted) or if hidden
			return model && !(model?.info?.meta?.hidden ?? false);
		});

		if (validModels.length !== modelIds.length) {
			pinnedModels = validModels;
			settings.set({ ...$settings, pinnedModels: validModels });
			await updateUserSettings(localStorage.token, { ui: $settings });
		}
	};

	onMount(async () => {
		pinnedModels = $settings?.pinnedModels ?? [];

		if (pinnedModels.length === 0 && $config?.default_pinned_models) {
			const defaultPinnedModels = ($config?.default_pinned_models)
				.split(",")
				.filter((id) => id);
			pinnedModels = defaultPinnedModels.filter((id) =>
				$models.find((model) => model.id === id),
			);

			settings.set({ ...$settings, pinnedModels });
			await updateUserSettings(localStorage.token, { ui: $settings });
		}

		// Auto-unpin hidden or deleted models
		if (pinnedModels.length > 0) {
			await cleanupStalePinnedModels(pinnedModels);
		}

		unsubscribeSettings = settings.subscribe((value) => {
			pinnedModels = value?.pinnedModels ?? [];
		});

		await tick();
		initPinnedModelsSortable();
	});

	onDestroy(() => {
		if (unsubscribeSettings) {
			unsubscribeSettings();
		}
	});
</script>

<div class="mt-0.5 pb-1.5" id="pinned-models-list">
	{#each pinnedModels as modelId (modelId)}
		{@const model = $models.find((model) => model.id === modelId)}
		{#if model}
			<PinnedModelItem
				{model}
				{shiftKey}
				onClick={() => {
					selectedChatId = null;
					chatId.set("");
					if ($mobile) {
						showSidebar.set(false);
					}
				}}
				onUnpin={($settings?.pinnedModels ?? []).includes(modelId)
					? () => {
							const pinnedModels = $settings.pinnedModels.filter(
								(id) => id !== modelId,
							);
							settings.set({ ...$settings, pinnedModels });
							updateUserSettings(localStorage.token, {
								ui: $settings,
							});
						}
					: null}
			/>
		{/if}
	{/each}
</div>