|
| 1 | +<script lang="ts" setup> |
| 2 | +import { ref, onMounted } from "vue"; |
| 3 | +import { useApolloClient } from "@vue/apollo-composable"; |
| 4 | +import gql from "graphql-tag"; |
| 5 | +import { Boxes, Check, X } from "lucide-vue-next"; |
| 6 | +
|
| 7 | +type MapRow = { name: string }; |
| 8 | +
|
| 9 | +const { client: apolloClient } = useApolloClient(); |
| 10 | +const meshCdn = (useRuntimeConfig().public.mapMeshCdn as string) || ""; |
| 11 | +
|
| 12 | +const loading = ref(true); |
| 13 | +const availableMaps = ref<string[]>([]); |
| 14 | +const missingMaps = ref<string[]>([]); |
| 15 | +
|
| 16 | +const CACHE_KEY = `mesh-coverage:v1:${meshCdn}`; |
| 17 | +
|
| 18 | +const MAPS_QUERY = gql` |
| 19 | + query MeshKnownMaps { |
| 20 | + maps( |
| 21 | + where: { workshop_map_id: { _is_null: true }, enabled: { _eq: true } } |
| 22 | + ) { |
| 23 | + name |
| 24 | + } |
| 25 | + } |
| 26 | +`; |
| 27 | +
|
| 28 | +function meshName(name: string): string { |
| 29 | + let n = name.toLowerCase().trim(); |
| 30 | + const slash = n.lastIndexOf("/"); |
| 31 | + if (slash >= 0) { |
| 32 | + n = n.slice(slash + 1); |
| 33 | + } |
| 34 | + return n.replace(/_night$/, ""); |
| 35 | +} |
| 36 | +
|
| 37 | +async function probe(name: string): Promise<boolean | null> { |
| 38 | + if (!meshCdn) { |
| 39 | + return null; |
| 40 | + } |
| 41 | + try { |
| 42 | + const res = await fetch(`${meshCdn}/${name}.tri`, { method: "HEAD" }); |
| 43 | + if (res.status === 404) { |
| 44 | + return false; |
| 45 | + } |
| 46 | + if (res.ok) { |
| 47 | + return true; |
| 48 | + } |
| 49 | + return null; |
| 50 | + } catch { |
| 51 | + return null; |
| 52 | + } |
| 53 | +} |
| 54 | +
|
| 55 | +async function probeRetry(name: string): Promise<boolean | null> { |
| 56 | + for (let attempt = 0; attempt < 3; attempt++) { |
| 57 | + const result = await probe(name); |
| 58 | + if (result !== null) { |
| 59 | + return result; |
| 60 | + } |
| 61 | + } |
| 62 | + return null; |
| 63 | +} |
| 64 | +
|
| 65 | +function readCache(): { available: string[]; missing: string[] } | null { |
| 66 | + try { |
| 67 | + const raw = localStorage.getItem(CACHE_KEY); |
| 68 | + return raw ? JSON.parse(raw) : null; |
| 69 | + } catch { |
| 70 | + return null; |
| 71 | + } |
| 72 | +} |
| 73 | +
|
| 74 | +function writeCache(value: { available: string[]; missing: string[] }) { |
| 75 | + try { |
| 76 | + localStorage.setItem(CACHE_KEY, JSON.stringify(value)); |
| 77 | + } catch { |
| 78 | + loading.value = false; |
| 79 | + } |
| 80 | +} |
| 81 | +
|
| 82 | +onMounted(async () => { |
| 83 | + const cached = readCache(); |
| 84 | + if (cached) { |
| 85 | + availableMaps.value = cached.available; |
| 86 | + missingMaps.value = cached.missing; |
| 87 | + loading.value = false; |
| 88 | + return; |
| 89 | + } |
| 90 | +
|
| 91 | + try { |
| 92 | + const { data } = await apolloClient.query({ |
| 93 | + query: MAPS_QUERY, |
| 94 | + fetchPolicy: "network-only", |
| 95 | + }); |
| 96 | + const names = new Set<string>(); |
| 97 | + for (const m of ((data as any)?.maps ?? []) as MapRow[]) { |
| 98 | + const key = meshName(m.name); |
| 99 | + if (key) { |
| 100 | + names.add(key); |
| 101 | + } |
| 102 | + } |
| 103 | + const unique = [...names].sort(); |
| 104 | + const results = await Promise.all( |
| 105 | + unique.map(async (name) => ({ name, has: await probeRetry(name) })), |
| 106 | + ); |
| 107 | +
|
| 108 | + availableMaps.value = results |
| 109 | + .filter((r) => r.has === true) |
| 110 | + .map((r) => r.name); |
| 111 | + missingMaps.value = results |
| 112 | + .filter((r) => r.has !== true) |
| 113 | + .map((r) => r.name); |
| 114 | +
|
| 115 | + if (!results.some((r) => r.has === null)) { |
| 116 | + writeCache({ |
| 117 | + available: availableMaps.value, |
| 118 | + missing: missingMaps.value, |
| 119 | + }); |
| 120 | + } |
| 121 | + } catch { |
| 122 | + availableMaps.value = []; |
| 123 | + missingMaps.value = []; |
| 124 | + } finally { |
| 125 | + loading.value = false; |
| 126 | + } |
| 127 | +}); |
| 128 | +</script> |
| 129 | + |
| 130 | +<template> |
| 131 | + <div |
| 132 | + v-if="loading || availableMaps.length || missingMaps.length" |
| 133 | + class="flex flex-col gap-3" |
| 134 | + > |
| 135 | + <span |
| 136 | + class="flex items-center gap-2 font-mono text-[0.7rem] font-bold tracking-[0.16em] uppercase text-[hsl(var(--tac-amber))]" |
| 137 | + > |
| 138 | + <Boxes class="h-4 w-4" /> |
| 139 | + {{ $t("glossary.mesh_coverage_title") }} |
| 140 | + </span> |
| 141 | + <p class="text-xs leading-snug text-muted-foreground/80"> |
| 142 | + {{ $t("glossary.mesh_coverage_note") }} |
| 143 | + </p> |
| 144 | + |
| 145 | + <div v-if="loading" class="text-xs text-muted-foreground"> |
| 146 | + {{ $t("glossary.mesh_coverage_loading") }} |
| 147 | + </div> |
| 148 | + |
| 149 | + <div v-else class="grid gap-4 sm:grid-cols-2"> |
| 150 | + <div class="flex flex-col gap-2"> |
| 151 | + <span |
| 152 | + class="flex items-center gap-1.5 text-xs font-semibold text-success" |
| 153 | + > |
| 154 | + <Check class="h-3.5 w-3.5" /> |
| 155 | + {{ $t("glossary.mesh_have", { count: availableMaps.length }) }} |
| 156 | + </span> |
| 157 | + <div class="flex flex-wrap gap-1.5"> |
| 158 | + <span |
| 159 | + v-for="name of availableMaps" |
| 160 | + :key="name" |
| 161 | + class="rounded border border-success/40 bg-success/10 px-1.5 py-0.5 font-mono text-[0.7rem] text-foreground/90" |
| 162 | + > |
| 163 | + {{ name }} |
| 164 | + </span> |
| 165 | + </div> |
| 166 | + </div> |
| 167 | + |
| 168 | + <div class="flex flex-col gap-2"> |
| 169 | + <span |
| 170 | + class="flex items-center gap-1.5 text-xs font-semibold text-destructive" |
| 171 | + > |
| 172 | + <X class="h-3.5 w-3.5" /> |
| 173 | + {{ $t("glossary.mesh_missing", { count: missingMaps.length }) }} |
| 174 | + </span> |
| 175 | + <div v-if="missingMaps.length" class="flex flex-wrap gap-1.5"> |
| 176 | + <span |
| 177 | + v-for="name of missingMaps" |
| 178 | + :key="name" |
| 179 | + class="rounded border border-destructive/40 bg-destructive/10 px-1.5 py-0.5 font-mono text-[0.7rem] text-foreground/90" |
| 180 | + > |
| 181 | + {{ name }} |
| 182 | + </span> |
| 183 | + </div> |
| 184 | + <span v-else class="text-xs text-muted-foreground"> |
| 185 | + {{ $t("glossary.mesh_missing_none") }} |
| 186 | + </span> |
| 187 | + </div> |
| 188 | + </div> |
| 189 | + </div> |
| 190 | +</template> |
0 commit comments