docs/.vitepress/theme/components/crossapp/CrossAppSupport.vue
727 lines · 16865 bytes
1<script setup lang="ts">
2import {
3 computed,
4 nextTick,
5 onBeforeUnmount,
6 onMounted,
7 ref,
8 watch
9} from 'vue'
10import { data as platformMap } from './platforms.data'
11
12interface Repo {
13 name: string
14 icon: string | null
15 href: string | null
16}
17
18interface AppEntry {
19 id: string
20 name: string
21 platforms: string[]
22 icon: string | null
23 repos: string[]
24 comp?: string[]
25 notes: string[]
26}
27
28interface OpenRepo extends Repo {
29 id: string
30 comp: boolean
31}
32
33interface Payload {
34 version: number
35 repos: Record<string, Repo>
36 apps: AppEntry[]
37}
38
39const DEFAULT_SRC = 'https://wotaku-ext.duckling.workers.dev/data.json'
40const CACHE_KEY = 'wotaku:crossapp:v1'
41const CACHE_TTL_MS = 5 * 60 * 1000
42const FETCH_TIMEOUT_MS = 10000
43
44const props = withDefaults(defineProps<{ src?: string }>(), {
45 src: DEFAULT_SRC
46})
47
48const payload = ref<Payload | null>(null)
49const loading = ref(true)
50const failed = ref(false)
51const openId = ref<string | null>(null)
52const brokenIcons = ref(new Set<string>())
53const brokenRepoIcons = ref(new Set<string>())
54const gridRef = ref<HTMLElement | null>(null)
55const columnCount = ref(1)
56
57const apps = computed(() => payload.value?.apps ?? [])
58const openApp = computed(() =>
59 apps.value.find(app => app.id === openId.value) ?? null
60)
61
62const openRepos = computed<OpenRepo[]>(() => {
63 const app = openApp.value
64 const repos = payload.value?.repos
65 if (!app || !repos) return []
66 const comp = new Set(app.comp ?? [])
67 return app.repos.flatMap(id => {
68 const repo = repos[id]
69 return repo ? [{ ...repo, id, comp: comp.has(id) }] : []
70 })
71})
72
73const hasCompRepo = computed(() => openRepos.value.some(repo => repo.comp))
74
75const openRowEnd = computed(() => {
76 const app = openApp.value
77 if (!app) return -1
78 const index = apps.value.findIndex(item => item.id === app.id)
79 if (index < 0) return -1
80 const cols = Math.max(1, columnCount.value)
81 return Math.min(Math.floor(index / cols) * cols + cols, apps.value.length) - 1
82})
83
84function syncColumns() {
85 const el = gridRef.value
86 if (!el) return
87 const tracks = getComputedStyle(el).gridTemplateColumns
88 const count = tracks && tracks !== 'none' ? tracks.split(' ').filter(Boolean).length : 1
89 if (count > 0) columnCount.value = count
90}
91
92function platformInfo(token: string) {
93 return platformMap[token] ?? { icon: 'lucide-monitor', label: token }
94}
95
96function iconUrl(path: string) {
97 try {
98 return new URL(path, props.src).toString()
99 } catch {
100 return path
101 }
102}
103
104function readCache(): Payload | null {
105 try {
106 const raw = sessionStorage.getItem(CACHE_KEY)
107 if (!raw) return null
108 const cached = JSON.parse(raw) as { at: number; data: Payload }
109 if (!cached?.data || Date.now() - cached.at > CACHE_TTL_MS) return null
110 return cached.data
111 } catch {
112 return null
113 }
114}
115
116function writeCache(data: Payload) {
117 try {
118 sessionStorage.setItem(CACHE_KEY, JSON.stringify({ at: Date.now(), data }))
119 } catch {}
120}
121
122let inFlight = false
123
124async function load(force = false) {
125 if (inFlight) return
126 inFlight = true
127 loading.value = true
128 failed.value = false
129
130 if (!force) {
131 const cached = readCache()
132 if (cached) {
133 payload.value = cached
134 loading.value = false
135 }
136 }
137
138 const controller = new AbortController()
139 const timer = setTimeout(() => controller.abort(), FETCH_TIMEOUT_MS)
140 try {
141 const response = await fetch(props.src, {
142 signal: controller.signal,
143 cache: 'no-cache'
144 })
145 if (!response.ok) {
146 throw new Error(`${response.status} ${response.statusText}`)
147 }
148 const data = (await response.json()) as Payload
149 if (!Array.isArray(data?.apps)) throw new Error('invalid payload')
150 payload.value = data
151 writeCache(data)
152 } catch {
153 if (!payload.value) failed.value = true
154 } finally {
155 clearTimeout(timer)
156 loading.value = false
157 inFlight = false
158 }
159}
160
161function toggle(id: string) {
162 openId.value = openId.value === id ? null : id
163 if (!openId.value) return
164 void nextTick(() => {
165 const panel = gridRef.value?.querySelector<HTMLElement>('.ca-panel')
166 if (!panel) return
167 const rect = panel.getBoundingClientRect()
168 if (rect.bottom > window.innerHeight || rect.top < 0) {
169 panel.scrollIntoView({ block: 'nearest', behavior: 'smooth' })
170 }
171 })
172}
173
174function onIconError(id: string) {
175 brokenIcons.value = new Set(brokenIcons.value).add(id)
176}
177
178function onRepoIconError(name: string) {
179 brokenRepoIcons.value = new Set(brokenRepoIcons.value).add(name)
180}
181
182let resizeObserver: ResizeObserver | null = null
183
184onMounted(() => {
185 void load()
186 void nextTick(syncColumns)
187 if (typeof ResizeObserver !== 'undefined') {
188 resizeObserver = new ResizeObserver(syncColumns)
189 if (gridRef.value) resizeObserver.observe(gridRef.value)
190 }
191})
192
193watch(apps, () => {
194 void nextTick(() => {
195 syncColumns()
196 if (resizeObserver) {
197 resizeObserver.disconnect()
198 if (gridRef.value) resizeObserver.observe(gridRef.value)
199 }
200 })
201})
202
203onBeforeUnmount(() => resizeObserver?.disconnect())
204</script>
205
206<template>
207 <div class="ca-root" @keydown.esc="openId = null">
208 <div v-if="loading && !apps.length" class="ca-box" aria-busy="true">
209 <div class="ca-grid">
210 <div v-for="n in 8" :key="n" class="ca-app ca-skeleton">
211 <span class="ca-sk-row">
212 <span class="ca-sk-icon" />
213 <span class="ca-sk-line" />
214 </span>
215 <span class="ca-sk-line short" />
216 </div>
217 </div>
218 </div>
219
220 <div v-else-if="failed && !apps.length" class="ca-error" role="status">
221 <span>Couldn't load cross-app support.</span>
222 <button type="button" @click="load(true)">Retry</button>
223 </div>
224
225 <template v-else>
226 <div class="ca-box">
227 <div ref="gridRef" class="ca-grid">
228 <template v-for="(app, index) in apps" :key="app.id">
229 <button
230 type="button"
231 class="ca-app"
232 :class="{
233 'is-open': openId === app.id,
234 'is-dimmed': openId && openId !== app.id
235 }"
236 :aria-expanded="openId === app.id"
237 :aria-controls="openId === app.id ? 'ca-panel' : undefined"
238 @click="toggle(app.id)"
239 >
240 <span class="ca-app-row">
241 <span class="ca-app-icon">
242 <span class="ca-app-mono">{{ app.name.charAt(0) }}</span>
243 <img
244 v-if="app.icon && !brokenIcons.has(app.id)"
245 class="ca-app-img no-zoom"
246 :src="iconUrl(app.icon)"
247 alt=""
248 loading="lazy"
249 decoding="async"
250 @error="onIconError(app.id)"
251 />
252 </span>
253 <span class="ca-app-name" :title="app.name">{{ app.name }}</span>
254 <span v-if="app.platforms.length" class="ca-app-platforms">
255 <span
256 v-for="token in app.platforms"
257 :key="token"
258 class="icon-tip ca-platform"
259 :data-tip="platformInfo(token).label"
260 >
261 <span :class="`i-${platformInfo(token).icon}`" />
262 </span>
263 </span>
264 </span>
265 <span class="ca-app-sub">
266 {{ app.repos.length }}
267 {{ app.repos.length === 1 ? 'type' : 'types' }}
268 </span>
269 </button>
270
271 <section
272 v-if="openApp && index === openRowEnd"
273 id="ca-panel"
274 class="ca-panel"
275 role="region"
276 :aria-label="`${openApp.name} supported repos`"
277 >
278 <header class="ca-panel-head">
279 <h3 class="ca-panel-title">{{ openApp.name }}</h3>
280 <button
281 type="button"
282 class="ca-panel-close"
283 aria-label="Close"
284 @click="openId = null"
285 >
286 <svg
287 xmlns="http://www.w3.org/2000/svg"
288 width="16"
289 height="16"
290 viewBox="0 0 24 24"
291 aria-hidden="true"
292 >
293 <path
294 fill="currentColor"
295 d="M6.4 5 5 6.4 10.6 12 5 17.6 6.4 19 12 13.4 17.6 19 19 17.6 13.4 12 19 6.4 17.6 5 12 10.6z"
296 />
297 </svg>
298 </button>
299 </header>
300
301 <ul v-if="openApp.notes.length" class="ca-notes">
302 <li v-for="note in openApp.notes" :key="note">{{ note }}</li>
303 </ul>
304
305 <div class="ca-repos">
306 <component
307 :is="repo.href ? 'a' : 'div'"
308 v-for="repo in openRepos"
309 :key="repo.id"
310 class="ca-repo"
311 :class="{
312 'is-disabled': !repo.href,
313 'is-comp': repo.comp
314 }"
315 :href="repo.href ?? undefined"
316 :title="repo.href ? repo.name : 'No page yet'"
317 >
318 <span class="ca-repo-icon">
319 <span class="ca-repo-mono">{{ repo.name.charAt(0) }}</span>
320 <img
321 v-if="repo.icon && !brokenRepoIcons.has(repo.name)"
322 class="ca-repo-img no-zoom"
323 :src="iconUrl(repo.icon)"
324 alt=""
325 loading="lazy"
326 decoding="async"
327 @error="onRepoIconError(repo.name)"
328 />
329 </span>
330 <span class="ca-repo-name">{{ repo.name }}</span>
331 </component>
332 </div>
333
334 <p v-if="hasCompRepo" class="ca-comp-foot">
335 <span
336 class="i-lucide-square-dashed ca-comp-icon"
337 aria-hidden="true"
338 />
339 <span>
340 App uses an extension as a compatibility layer. All the extensions / features may not work properly.
341 </span>
342 </p>
343 </section>
344 </template>
345 </div>
346 </div>
347 </template>
348 </div>
349</template>
350
351<style scoped>
352.ca-root {
353 margin: 16px 0;
354}
355
356.ca-box {
357 padding: 8px;
358 border: 2px solid #3d3d3d;
359 border-radius: var(--radius-xl, 12px);
360 background-color: var(--vp-c-bg);
361}
362
363@media (max-width: 767px) {
364 .ca-box {
365 max-height: min(360px, 55vh);
366 overflow-y: auto;
367 overscroll-behavior: contain;
368 -webkit-overflow-scrolling: touch;
369 scrollbar-width: thin;
370 }
371}
372
373.ca-grid {
374 display: grid;
375 grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
376 gap: 6px;
377}
378
379.ca-app {
380 display: flex;
381 flex-direction: column;
382 gap: 0;
383 padding: 6px;
384 border: 1.5px solid #3d3d3d;
385 border-radius: 6px;
386 background-color: var(--vp-c-bg-soft);
387 color: var(--vp-c-text-1);
388 text-align: left;
389 cursor: pointer;
390 transition: border-color 0.2s, background-color 0.2s, opacity 0.2s;
391}
392
393.ca-app.is-dimmed {
394 opacity: 0.45;
395}
396
397.ca-app.is-dimmed:hover,
398.ca-app.is-dimmed:focus-visible {
399 opacity: 0.85;
400}
401
402.ca-app:hover {
403 border-color: var(--vp-c-brand-1);
404 background-color: var(--vp-c-bg);
405}
406
407.ca-app:focus-visible {
408 outline: 2px solid var(--vp-c-brand-1);
409 outline-offset: 2px;
410}
411
412.ca-app.is-open {
413 border-color: var(--vp-c-brand-1);
414 background-color: var(--vp-c-bg);
415}
416
417.ca-app-row {
418 display: flex;
419 align-items: center;
420 gap: 6px;
421 min-width: 0;
422}
423
424.ca-app-icon {
425 position: relative;
426 display: flex;
427 align-items: center;
428 justify-content: center;
429 flex-shrink: 0;
430 width: 18px;
431 height: 18px;
432 border-radius: 4px;
433 overflow: hidden;
434 background-color: var(--vp-c-bg-alt);
435}
436
437.ca-app-img {
438 position: absolute;
439 inset: 0;
440 width: 18px;
441 height: 18px;
442 object-fit: contain;
443 background-color: var(--vp-c-bg-alt);
444}
445
446.ca-app-mono {
447 font-size: 10px;
448 font-weight: 700;
449 line-height: 1;
450 color: var(--vp-c-text-2);
451}
452
453.ca-app-name {
454 flex: 1 1 auto;
455 min-width: 0;
456 font-size: 12.5px;
457 font-weight: 600;
458 line-height: 1.2;
459 white-space: nowrap;
460 overflow: hidden;
461 text-overflow: ellipsis;
462}
463
464.ca-app-platforms {
465 display: flex;
466 align-items: center;
467 gap: 1px;
468 flex-shrink: 0;
469 font-size: 12px;
470 line-height: 1;
471 color: var(--vp-c-text-2);
472}
473
474.ca-platform {
475 line-height: 1;
476}
477
478.ca-platform > span {
479 min-width: 0;
480 width: 13px;
481 height: 13px;
482 font-size: 13px;
483 line-height: 1;
484}
485
486.ca-app-sub {
487 display: block;
488 padding-left: 24px;
489 font-size: 10.5px;
490 font-weight: 400;
491 line-height: 1.1;
492 color: var(--vp-c-text-2);
493 white-space: nowrap;
494 overflow: hidden;
495 text-overflow: ellipsis;
496}
497
498.ca-panel {
499 grid-column: 1 / -1;
500 padding: 8px;
501 border: 1.5px solid var(--vp-c-brand-1);
502 border-radius: 10px;
503 background-color: var(--vp-c-bg);
504 animation: ca-panel-in 0.18s ease-out;
505}
506
507@keyframes ca-panel-in {
508 from {
509 opacity: 0;
510 transform: translateY(-4px);
511 }
512}
513
514.ca-panel-head {
515 display: flex;
516 align-items: center;
517 justify-content: space-between;
518 gap: 8px;
519 padding: 2px 4px 0;
520}
521
522.ca-panel-title {
523 margin: 0;
524 padding: 0;
525 border: none;
526 font-size: 13px;
527 font-weight: 600;
528 line-height: 1.4;
529 text-decoration: none;
530}
531
532.ca-panel-close {
533 display: flex;
534 align-items: center;
535 justify-content: center;
536 width: 26px;
537 height: 26px;
538 padding: 0;
539 border: none;
540 border-radius: 6px;
541 background: transparent;
542 color: var(--vp-c-text-2);
543 cursor: pointer;
544 transition: background-color 0.2s, color 0.2s;
545}
546
547.ca-panel-close:hover {
548 background-color: var(--vp-c-bg-alt);
549 color: var(--vp-c-text-1);
550}
551
552.ca-notes {
553 margin: 8px 4px 0;
554 padding: 8px 10px 8px 24px;
555 border: 1px solid var(--vp-custom-block-warning-border);
556 border-radius: 8px;
557 background-color: var(--vp-custom-block-warning-bg);
558 color: var(--vp-custom-block-warning-text);
559 font-size: 12px;
560 line-height: 1.5;
561}
562
563.ca-notes li + li {
564 margin-top: 2px;
565}
566
567.ca-repos {
568 display: grid;
569 grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
570 gap: 6px;
571 margin-top: 8px;
572 padding: 0 4px 4px;
573}
574
575.ca-repo {
576 display: flex;
577 align-items: center;
578 justify-content: flex-start;
579 min-height: 32px;
580 padding: 6px 8px;
581 border: 1.5px solid #3d3d3d;
582 border-radius: 6px;
583 background-color: var(--vp-c-bg-soft);
584 color: var(--vp-c-text-1);
585 text-align: left;
586 text-decoration: none !important;
587 transition: border-color 0.2s, background-color 0.2s;
588}
589
590.ca-repo:hover {
591 border-color: var(--vp-c-brand-1);
592 background-color: var(--vp-c-bg);
593}
594
595.ca-repo.is-disabled {
596 opacity: 0.55;
597}
598
599.ca-repo.is-comp {
600 border-style: dashed;
601}
602
603.ca-comp-foot {
604 display: flex;
605 align-items: flex-start;
606 gap: 6px;
607 margin: 6px 4px 2px;
608 font-size: 10.5px;
609 line-height: 1.4;
610 color: var(--vp-c-text-2);
611}
612
613.ca-comp-icon {
614 flex-shrink: 0;
615 width: 12px;
616 height: 12px;
617 min-width: 0;
618 margin-top: 1px;
619 font-size: 12px;
620 line-height: 1;
621}
622
623.ca-repo-name {
624 font-size: 12.5px;
625 font-weight: 600;
626 line-height: 1.2;
627 color: var(--vp-c-text-1);
628 white-space: nowrap;
629 overflow: hidden;
630 text-overflow: ellipsis;
631}
632
633.ca-repo-icon {
634 position: relative;
635 display: flex;
636 align-items: center;
637 justify-content: center;
638 flex-shrink: 0;
639 width: 18px;
640 height: 18px;
641 margin-right: 6px;
642 border-radius: 4px;
643 overflow: hidden;
644 background-color: var(--vp-c-bg-alt);
645}
646
647.ca-repo-img {
648 position: absolute;
649 inset: 0;
650 width: 18px;
651 height: 18px;
652 object-fit: contain;
653 background-color: var(--vp-c-bg-alt);
654}
655
656.ca-repo-mono {
657 font-size: 10px;
658 font-weight: 700;
659 line-height: 1;
660 color: var(--vp-c-text-2);
661}
662
663.ca-error {
664 display: flex;
665 align-items: center;
666 gap: 10px;
667 padding: 14px;
668 border: 1px solid var(--vp-c-divider);
669 border-radius: var(--radius-xl, 12px);
670 background-color: var(--vp-c-bg-soft);
671 font-size: 13px;
672 color: var(--vp-c-text-2);
673}
674
675.ca-error button {
676 padding: 4px 10px;
677 border: 1px solid var(--vp-c-divider);
678 border-radius: 6px;
679 background-color: var(--vp-c-bg);
680 color: var(--vp-c-text-1);
681 font-size: 12px;
682 cursor: pointer;
683}
684
685.ca-skeleton {
686 cursor: default;
687}
688
689.ca-sk-row {
690 display: flex;
691 align-items: center;
692 gap: 6px;
693}
694
695.ca-sk-icon {
696 flex-shrink: 0;
697 width: 18px;
698 height: 18px;
699 border-radius: 4px;
700 background-color: var(--vp-c-bg-alt);
701}
702
703.ca-sk-line {
704 flex: 1;
705 height: 9px;
706 border-radius: 3px;
707 background-color: var(--vp-c-bg-alt);
708}
709
710.ca-sk-line.short {
711 display: block;
712 width: 38%;
713 height: 7px;
714 margin-top: 3px;
715 margin-left: 24px;
716}
717
718html.effects-disabled .ca-panel {
719 animation: none;
720}
721
722@media (prefers-reduced-motion: reduce) {
723 .ca-panel {
724 animation: none;
725 }
726}
727</style>