summaryrefslogtreecommitdiffstats
path: root/src/routes/search/+page.svelte
blob: 2df902d7719306092d02ba53632595fb65614d5a (plain) (blame)
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<script lang="ts">
    import { browser } from '$app/environment';
    import { goto } from '$app/navigation';
    import { page } from '$app/state';
    import { onMount } from 'svelte';

    import ExtensionRow from '$lib/components/ExtensionRow.svelte';
    import { debounce } from '$lib/search/debounce.js';
    import {
        getFilterOptions,
        initMeilisearch,
        searchExtensions,
        transformMeilisearchHit
    } from '$lib/search/meilisearch.js';
    import type { EnrichedExtension } from '$lib/search/types.js';
    import { findSourceByFormattedName, formatSourceName } from '$lib/search/utils.js';

    // Component state (must be declared before derived state that uses them)
    let loading = $state(true);
    let error = $state<string | null>(null);
    let results = $state<EnrichedExtension[]>([]);
    let sources = $state<string[]>(['all']);
    let categories = $state<string[]>(['all']);
    let languages = $state<string[]>(['all']);
    let currentPage = $state(1);
    let totalPages = $state(1);
    let totalHits = $state(0);
    let resultsPerPage = $state(10);
    let hasSearched = $state(false);

    // Derived state from URL parameters
    let query = $derived(browser ? (page.url.searchParams.get('q') ?? '') : '');
    let selectedSource = $derived(
        browser
            ? findSourceByFormattedName(page.url.searchParams.get('source') ?? 'all', sources)
            : 'all'
    );
    let selectedCategory = $derived(
        browser ? (page.url.searchParams.get('category') ?? 'all') : 'all'
    );
    let selectedLanguage = $derived(browser ? (page.url.searchParams.get('lang') ?? 'all') : 'all');
    let showNSFW = $derived(browser ? page.url.searchParams.get('nsfw') !== '0' : true);
    let pageParam = $derived(browser ? parseInt(page.url.searchParams.get('page') ?? '1') : 1);

    // URL parameter management
    function updateParams(updates: Record<string, string | null>) {
        const params = new URLSearchParams(page.url.searchParams);

        // Reset to page 1 if any search filter changed (not page parameter)
        const filterChanges = Object.keys(updates).filter((key) => key !== 'page');
        if (filterChanges.length > 0) {
            params.set('page', '1');
        }

        for (const [key, value] of Object.entries(updates)) {
            if (value === null) params.delete(key);
            else params.set(key, value);
        }
        goto(`?${params.toString()}`, { replaceState: true, keepFocus: true, noScroll: true });
    }

    // Initialize Meilisearch
    onMount(async () => {
        try {
            const meiliConfig = {
                host: import.meta.env.VITE_MEILISEARCH_HOST || '',
                apiKey: import.meta.env.VITE_MEILISEARCH_DEFAULT_SEARCH_KEY
            };

            if (!meiliConfig.host) {
                error = 'Meilisearch is not configured.';
                return;
            }

            initMeilisearch(meiliConfig);
        } catch (e) {
            console.error(e);
            error = 'Failed to initialize Meilisearch.';
        } finally {
            loading = false;
        }
    });

    // Debounced search with 300ms delay
    const debouncedSearch = debounce(
        (
            query: string,
            source: string,
            category: string,
            lang: string,
            nsfw: boolean,
            page: number
        ) => {
            searchExtensions({
                query: query || undefined,
                source: source !== 'all' ? formatSourceName(source) : undefined,
                category: category !== 'all' ? category : undefined,
                lang: lang !== 'all' ? lang : undefined,
                nsfw: nsfw,
                page,
                limit: resultsPerPage
            })
                .then((searchResults) => {
                    results = searchResults.hits.map(transformMeilisearchHit);
                    totalHits = searchResults.estimatedTotalHits || searchResults.hits.length;
                    totalPages = Math.ceil(totalHits / resultsPerPage);
                    currentPage = page;
                    hasSearched = true;
                })
                .catch((err) => {
                    console.error('Meilisearch error:', err);
                    error = 'Search failed. Please try again.';
                    hasSearched = true;
                })
                .finally(() => {
                    loading = false;
                });
        },
        300
    );

    // Reactive search effect
    $effect(() => {
        if (!browser) return;

        currentPage = pageParam;

        // Set loading immediately, then execute debounced search
        loading = true;
        debouncedSearch(
            query,
            selectedSource,
            selectedCategory,
            selectedLanguage,
            showNSFW,
            pageParam
        );
    });

    // Load filter options from Meilisearch
    $effect(() => {
        if (!browser) return;
        getFilterOptions()
            .then((options) => {
                sources = [...new Set(['all', ...options.sources.sort()])];
                categories = [...new Set(['all', ...options.categories.sort()])];
                languages = [...new Set(['all', ...options.languages.sort()])];
            })
            .catch((err) => {
                console.error('Failed to load filter options:', err);
            });
    });
</script>

<div class="container">
    <div class="page-header">
        <h1>Search Extensions</h1>
        <a href="/" class="btn btn-secondary header-btn"> Home </a>
    </div>
    <div class="search-container">
        <input
            type="text"
            class="search-input"
            placeholder="Search by name or package..."
            value={query}
            oninput={(e) => updateParams({ q: e.currentTarget.value || null })}
        />
    </div>
    <div class="filter-bar">
        <div class="filter-group">
            <label for="category-filter">Category:</label>
            <select
                id="category-filter"
                value={selectedCategory}
                onchange={(e) =>
                    updateParams({
                        category: e.currentTarget.value === 'all' ? null : e.currentTarget.value
                    })}
            >
                {#each categories as category (category)}
                    <option value={category}>
                        {category}
                    </option>
                {/each}
            </select>
        </div>
        <div class="filter-group">
            <label for="source-filter">Source:</label>
            <select
                id="source-filter"
                value={selectedSource}
                onchange={(e) => {
                    const val = formatSourceName(e.currentTarget.value);
                    updateParams({ source: val === 'all' ? null : val });
                }}
            >
                {#each sources as source (source)}
                    <option value={source}>
                        {source}
                    </option>
                {/each}
            </select>
        </div>
        <div class="filter-group">
            <label for="language-filter">Language:</label>
            <select
                id="language-filter"
                value={selectedLanguage}
                onchange={(e) =>
                    updateParams({
                        lang: e.currentTarget.value === 'all' ? null : e.currentTarget.value
                    })}
            >
                {#each languages as lang (lang)}
                    <option value={lang}>
                        {lang}
                    </option>
                {/each}
            </select>
        </div>
        <div class="filter-group filter-checkbox">
            <label>
                <input
                    type="checkbox"
                    checked={showNSFW}
                    onchange={(e) => updateParams({ nsfw: e.currentTarget.checked ? null : '0' })}
                />
                <span>Show NSFW</span>
            </label>
        </div>
    </div>
    <div class="table-container">
        <table class="extensions-table">
            <thead>
                <tr>
                    <th style="width: 60px;">Icon</th>
                    <th>Name / Package</th>
                    <th>Version / Lang</th>
                    <th style="width: 100px;">Action</th>
                </tr>
            </thead>
            <tbody>
                {#each results as ext (ext.formattedSourceName + ';' + ext.pkg)}
                    <ExtensionRow extension={ext} repoUrl={ext.repoUrl} />
                {/each}
            </tbody>
        </table>
    </div>
    {#if totalPages > 1}
        {@const startPage = Math.max(1, currentPage - 2)}
        {@const endPage = Math.min(totalPages, startPage + 4)}
        <div class="pagination-container">
            <div class="pagination-info">
                Showing {Math.min((currentPage - 1) * resultsPerPage + 1, totalHits)} to {Math.min(
                    currentPage * resultsPerPage,
                    totalHits
                )} of {totalHits} results
            </div>
            <div class="pagination-controls">
                <button
                    class="btn btn-secondary btn-sm"
                    disabled={currentPage === 1}
                    onclick={() => updateParams({ page: '1' })}
                    title="First page"
                >
                    First
                </button>

                <button
                    class="btn btn-secondary btn-sm"
                    disabled={currentPage === 1}
                    onclick={() =>
                        updateParams({
                            page: currentPage === 1 ? null : (currentPage - 1).toString()
                        })}
                >
                    Previous
                </button>

                <div class="page-numbers">
                    {#each Array.from({ length: endPage - startPage + 1 }, (_, i) => startPage + i) as pageNum}
                        <button
                            class="btn btn-sm {pageNum === currentPage
                                ? 'btn-primary'
                                : 'btn-secondary'}"
                            onclick={() =>
                                updateParams({ page: pageNum === 1 ? null : pageNum.toString() })}
                        >
                            {pageNum}
                        </button>
                    {/each}
                </div>

                <button
                    class="btn btn-secondary btn-sm"
                    disabled={currentPage === totalPages}
                    onclick={() => updateParams({ page: (currentPage + 1).toString() })}
                >
                    Next
                </button>

                <button
                    class="btn btn-secondary btn-sm"
                    disabled={currentPage === totalPages}
                    onclick={() => updateParams({ page: totalPages.toString() })}
                    title="Last page"
                >
                    Last
                </button>
            </div>
        </div>
    {/if}

    {#if loading}
        <div style="text-align: center; padding: 20px;">Loading extensions...</div>
    {:else if results.length === 0 && hasSearched}
        <div style="text-align: center; padding: 20px;">No results found.</div>
    {/if}
    {#if error}
        <div style="text-align: center; margin-top: 50px; color: red;">{error}</div>
    {/if}
</div>