docs/.vitepress/theme/components/extrepo/ExtensionRepos.vue
427 lines · 11207 bytes
1<script setup lang="ts">
2import { refDebounced } from '@vueuse/core'
3import { computed, nextTick, onUnmounted, ref, watch } from 'vue'
4import { SEARCH_DEBOUNCE_MS, STATUS_TICK_MS } from './constants'
5import { groupSites } from './groupSites'
6import { escapeHtml, stripHtml } from './helpers'
7import { mangayomiCopyValue } from './install'
8import RepoRow from './RepoRow.vue'
9import SettingsMenu from './SettingsMenu.vue'
10import SiteGrid from './SiteGrid.vue'
11import type { MatchedSite, RatingFilter, Repo } from './types'
12import { dedupeSites, indexSites, useRepoFilter } from './useRepoFilter'
13import { provideRepoStore } from './useRepoStore'
14import { provideTooltip } from './useTooltip'
15
16const { tooltip } = provideTooltip()
17
18const props = defineProps<{
19 repos: Repo[]
20 scheme: string
21 from?: string
22 manual?: boolean
23}>()
24
25const {
26 store,
27 loading,
28 refreshing,
29 refreshFailed,
30 loadFailed,
31 updatedAt,
32 loadMs,
33 refresh,
34 retry
35} = provideRepoStore(props.scheme)
36
37const ticked = ref(false)
38let tickTimer: ReturnType<typeof setTimeout> | undefined
39
40onUnmounted(() => {
41 if (tickTimer) clearTimeout(tickTimer)
42})
43
44let lastCorrectedHash: string | null = null
45
46function correctAnchorScroll() {
47 if (typeof window === 'undefined') return
48 const hash = window.location.hash
49 if (!hash || hash.length < 2 || hash === lastCorrectedHash) return
50 const raw = hash.slice(1)
51 let id = raw
52 try {
53 id = decodeURIComponent(raw)
54 } catch {
55 }
56 const target = document.getElementById(id) ?? document.getElementById(raw)
57 if (!target) return
58 lastCorrectedHash = hash
59 void nextTick(() => {
60 requestAnimationFrame(() => {
61 requestAnimationFrame(() => {
62 const navBar = document.querySelector<HTMLElement>('.VPNavBar')
63 const navOffset = navBar ? navBar.offsetHeight : 0
64 window.scrollTo({
65 top: target.getBoundingClientRect().top + window.scrollY - navOffset,
66 behavior: 'auto'
67 })
68 })
69 })
70 })
71}
72
73watch(loading, (value, oldValue) => {
74 if (oldValue && !value) correctAnchorScroll()
75})
76
77async function onStatusClick() {
78 if (loading.value || refreshing.value) return
79 if (loadFailed.value) {
80 await retry()
81 return
82 }
83 ticked.value = false
84 if (tickTimer) clearTimeout(tickTimer)
85 await refresh()
86 if (!refreshFailed.value) {
87 ticked.value = true
88 tickTimer = setTimeout(() => {
89 ticked.value = false
90 }, STATUS_TICK_MS)
91 }
92}
93
94const busy = computed(() => loading.value || refreshing.value)
95const failed = computed(() => loadFailed.value || refreshFailed.value)
96
97const statusIcon = computed(() => {
98 if (busy.value) return 'i-lucide:loader-circle'
99 if (failed.value) return 'i-lucide:x'
100 if (ticked.value) return 'i-lucide:check'
101 return 'i-lucide:refresh-cw'
102})
103
104const statusTitle = computed(() => {
105 const ms = loadMs.value !== null ? ` (${loadMs.value}ms)` : ''
106 if (loading.value) return `Loading sites…${ms}`
107 if (refreshing.value) return 'Refreshing…'
108 if (failed.value) return `Loading failed${ms} — click to retry`
109 if (ticked.value) return 'Database refreshed'
110 if (updatedAt.value) {
111 return `Database refreshed at ${new Date(updatedAt.value).toLocaleTimeString()}`
112 }
113 return `Refresh data${ms}`
114})
115
116const repoData = computed(() => store.value?.sites ?? {})
117const soraAuthors = computed(() => store.value?.soraAuthors ?? {})
118const echoAuthors = computed(() => store.value?.echoAuthors ?? {})
119const repoLists = computed(() => store.value?.repoLists ?? {})
120
121const baseRepos = computed<Repo[]>(() => {
122 const list = props.from ? repoLists.value[props.from] ?? [] : []
123 const auto: Repo[] = list.map(entry => ({
124 name: entry.repoUrl
125 ? `<a href="${entry.repoUrl}" target="_blank" rel="noopener noreferrer">${
126 escapeHtml(entry.label)
127 }</a>`
128 : escapeHtml(entry.label),
129 indexUrl: entry.indexUrl,
130 repoName: entry.label,
131 repoUrl: entry.repoUrl
132 }))
133 const manualByUrl = new Map(props.repos.map(repo => [repo.indexUrl, repo]))
134 const merged = [
135 ...auto.filter(repo => !manualByUrl.has(repo.indexUrl)),
136 ...props.repos
137 ]
138 if (!props.manual) {
139 merged.sort((a, b) => stripHtml(a.name).localeCompare(stripHtml(b.name)))
140 }
141 return merged
142})
143
144const resolvedRepos = computed<Repo[]>(() => {
145 const result: Repo[] = []
146 for (const repo of baseRepos.value) {
147 const authors = soraAuthors.value[repo.indexUrl] ??
148 echoAuthors.value[repo.indexUrl]
149 if (!authors?.length) {
150 result.push(repo)
151 continue
152 }
153 for (const author of authors) {
154 const name = author.repoUrl
155 ? `<a href="${author.repoUrl}" target="_blank" rel="noopener noreferrer">${
156 escapeHtml(author.label)
157 }</a>`
158 : escapeHtml(author.label)
159 result.push({ name, indexUrl: author.indexUrl, repoName: author.label })
160 }
161 }
162 return result
163})
164
165const query = ref('')
166const debouncedQuery = refDebounced(query, SEARCH_DEBOUNCE_MS)
167const langFilter = ref('')
168const ratingFilter = ref<RatingFilter>('all')
169const contentTypeFilter = ref('')
170const showBroken = ref(false)
171
172const allSites = computed(() => {
173 const sites: MatchedSite[] = []
174 for (const repo of resolvedRepos.value) {
175 if (repo.variants?.length) {
176 const sameRepoName = repo.repoName ? stripHtml(repo.name) : undefined
177 for (const variant of repo.variants) {
178 const url = mangayomiCopyValue(variant)
179 if (!url) continue
180 const repoName = sameRepoName ?? variant.label
181 for (const site of repoData.value[url]?.sites ?? []) {
182 sites.push({ ...site, repoName })
183 }
184 }
185 continue
186 }
187 const repoName = stripHtml(repo.name)
188 const dataUrl = repo.data ?? repo.indexUrl
189 for (const site of repoData.value[dataUrl]?.sites ?? []) {
190 sites.push({ ...site, repoName })
191 }
192 }
193 return indexSites(sites)
194})
195
196const { facets, isFiltering, filteredSites } = useRepoFilter(allSites, {
197 query: debouncedQuery,
198 langFilter,
199 ratingFilter,
200 contentTypeFilter,
201 showBroken
202})
203
204const totalExtensions = computed(() => {
205 const visible = showBroken.value
206 ? allSites.value
207 : allSites.value.filter(site => !site.isBroken)
208 return groupSites(dedupeSites(visible)).length
209})
210
211const searchPlaceholder = computed(() => {
212 const total = totalExtensions.value
213 return total
214 ? `Search all extensions (${total.toLocaleString()})…`
215 : 'Search all extensions…'
216})
217</script>
218
219<template>
220 <div class="ext-repos">
221 <div class="ext-toolbar">
222 <div class="ext-search">
223 <span class="i-lucide:search" />
224 <input v-model="query" type="text" :placeholder="searchPlaceholder" />
225 <button
226 type="button"
227 class="ext-search-clear"
228 :disabled="!query"
229 title="Clear search"
230 aria-label="Clear search"
231 @click="query = ''"
232 >
233 <span class="i-lucide:x" />
234 </button>
235 </div>
236 <button
237 type="button"
238 class="ext-status-btn"
239 :class="{
240 spinning: busy,
241 'is-failed': !busy && failed,
242 'is-ok': !busy && !failed && ticked
243 }"
244 :disabled="busy"
245 :title="statusTitle"
246 aria-label="Reload site data"
247 @click="onStatusClick"
248 >
249 <span :class="statusIcon" />
250 </button>
251 <SettingsMenu
252 v-model:lang-filter="langFilter"
253 v-model:rating-filter="ratingFilter"
254 v-model:content-type-filter="contentTypeFilter"
255 v-model:show-broken="showBroken"
256 :available-langs="facets.availableLangs"
257 :has-multi-language="facets.hasMultiLanguage"
258 :has-ratings="facets.hasRatings"
259 :has-mixed="facets.hasMixed"
260 :has-suggestive="facets.hasSuggestive"
261 :available-content-types="facets.availableContentTypes"
262 :has-content-types="facets.availableContentTypes.length > 1"
263 :has-broken="facets.hasBroken"
264 />
265 </div>
266
267 <SiteGrid
268 v-if="isFiltering"
269 class="ext-search-results"
270 :sites="filteredSites"
271 :scheme="scheme"
272 empty-text="No sites match your filters."
273 />
274
275 <div v-else>
276 <RepoRow
277 v-for="repo in resolvedRepos"
278 :key="repo.indexUrl"
279 :repo="repo"
280 :scheme="scheme"
281 :show-broken="showBroken"
282 />
283 </div>
284 </div>
285
286 <Teleport to="body">
287 <div
288 v-if="tooltip"
289 class="ext-fixed-tooltip"
290 :style="{ left: tooltip.x + 'px', top: tooltip.y + 'px' }"
291 >
292 {{ tooltip.text }}
293 </div>
294 </Teleport>
295</template>
296
297<style scoped>
298.ext-repos {
299 margin: 16px 0;
300 border: 1px solid var(--vp-c-divider);
301 border-radius: var(--radius-xl, 12px);
302 overflow: hidden;
303}
304.ext-toolbar {
305 display: flex;
306 flex-wrap: nowrap;
307 align-items: center;
308 gap: 8px;
309 padding: 10px;
310 background-color: var(--vp-c-bg-soft);
311 border-bottom: 1px solid var(--vp-c-divider);
312}
313.ext-search {
314 display: flex;
315 align-items: center;
316 gap: 6px;
317 flex: 1 1 auto;
318 min-width: 0;
319 padding: 6px 10px;
320 border-radius: 8px;
321 border: 1px solid var(--vp-c-divider);
322 background-color: var(--vp-c-bg);
323 color: var(--vp-c-text-2);
324}
325.ext-search input {
326 flex: 1;
327 min-width: 0;
328 border: none;
329 outline: none;
330 background: transparent;
331 color: var(--vp-c-text-1);
332 font-size: 13px;
333}
334.ext-search > span {
335 flex-shrink: 0;
336}
337.ext-search-clear {
338 display: flex;
339 align-items: center;
340 justify-content: center;
341 flex-shrink: 0;
342 padding: 0;
343 border: none;
344 border-radius: 0;
345 background: transparent;
346 color: var(--vp-c-text-1);
347 opacity: 0.55;
348 cursor: pointer;
349 transition: opacity 0.25s, transform 0.2s cubic-bezier(0.16, 1, 0.3, 1);
350}
351.ext-search-clear:hover:not(:disabled) {
352 opacity: 1;
353}
354.ext-search-clear:disabled {
355 opacity: 0.37;
356 cursor: default;
357}
358.ext-search-clear:not(:disabled):active {
359 transform: scale(0.94);
360}
361.ext-search-clear span {
362 font-size: 18px;
363}
364html.effects-disabled .ext-search-clear {
365 transition: none;
366}
367html.effects-disabled .ext-search-clear:active {
368 transform: none;
369}
370.ext-status-btn {
371 display: flex;
372 align-items: center;
373 justify-content: center;
374 flex-shrink: 0;
375 width: 30px;
376 height: 30px;
377 padding: 0;
378 border: none;
379 border-radius: 0;
380 background: transparent;
381 color: var(--vp-c-text-1);
382 opacity: 0.55;
383 cursor: pointer;
384 transition: opacity 0.25s;
385}
386.ext-status-btn:hover:not(:disabled) {
387 opacity: 1;
388}
389.ext-status-btn:disabled {
390 cursor: default;
391}
392.ext-status-btn span {
393 font-size: 15px;
394}
395.ext-status-btn.spinning span {
396 animation: ext-spin 1s linear infinite;
397}
398.ext-status-btn.is-failed {
399 color: var(--vp-c-red-2, #e5484d);
400 opacity: 1;
401}
402.ext-status-btn.is-ok {
403 color: var(--vp-c-green-1, #3dd68c);
404 opacity: 1;
405}
406@keyframes ext-spin {
407 to {
408 transform: rotate(360deg);
409 }
410}
411html.effects-disabled .ext-status-btn.spinning span {
412 animation: none;
413}
414.ext-fixed-tooltip {
415 position: fixed;
416 z-index: 9999;
417 transform: translate(-50%, calc(-100% - 15px));
418 background: var(--vp-c-bg-soft);
419 color: var(--vp-c-text-1);
420 border: 1px solid var(--vp-c-brand-1);
421 border-radius: 5px;
422 padding: 3px 8px;
423 font-size: 11px;
424 white-space: nowrap;
425 pointer-events: none;
426}
427</style>