|
| 1 | +<script setup lang="ts"> |
| 2 | +import type { GetOrCache, IndexerType } from '../constants.ts' |
| 3 | +import { asyncComputed } from '@vueuse/core' |
| 4 | +import { CdxButton, CdxTextInput } from '@wikimedia/codex' |
| 5 | +import { computed, inject, nextTick, ref } from 'vue' |
| 6 | +import { useI18n } from 'vue-i18n' |
| 7 | +import { getAsPrimitiveProtocol, indexed, isActionKey } from '../constants.ts' |
| 8 | +import TypeChoice from './TypeChoice.vue' |
| 9 | +
|
| 10 | +const props = defineProps<{ data: object; version: number }>() |
| 11 | +const { t } = useI18n() |
| 12 | +const cacheGet = inject('cache-get') as GetOrCache |
| 13 | +const getIndexFor = inject('get-index-for') as IndexerType |
| 14 | +
|
| 15 | +const errorState = |
| 16 | + !Array.isArray(props.data) || props.data[0] !== 'direct_holder' || props.data.length !== 3 |
| 17 | +const registry = (props.data as any[])[1] as string |
| 18 | +const content = (props.data as any[])[2] as string | object |
| 19 | +const primitive = getAsPrimitiveProtocol(content) |
| 20 | +const desc = primitive |
| 21 | + ? t('protocol.type.direct_holder.primitive', { registry, type: t(`protocol.type.${primitive}`) }) |
| 22 | + : t('protocol.type.direct_holder.complex', { registry }) |
| 23 | +
|
| 24 | +const showSubType = ref(false) |
| 25 | +const loadingState = ref(true) |
| 26 | +const data = asyncComputed<string[]>( |
| 27 | + async () => { |
| 28 | + if (!showSubType.value) return Promise.resolve([]) // lazy |
| 29 | + const index = getIndexFor(props.version, registry) |
| 30 | + if (index === -1) return Promise.resolve([]) |
| 31 | + return await cacheGet(props.version, `registry.${registry}`, async () => { |
| 32 | + return (await (await fetch(indexed(index, `registries/${registry}.json`))).json()) || [] |
| 33 | + }) |
| 34 | + }, |
| 35 | + [], |
| 36 | + loadingState, |
| 37 | +) |
| 38 | +const writeableRegistry = computed(() => data.value.length === 0) |
| 39 | +
|
| 40 | +const mode = ref(true) |
| 41 | +const input = ref('0') |
| 42 | +const result = computed(() => { |
| 43 | + if (mode.value) { |
| 44 | + const num = Number(input.value) |
| 45 | + if (Number.isNaN(num)) return 'Not a number' |
| 46 | + return data.value[num] || 'Invalid network ID' |
| 47 | + } else { |
| 48 | + const index = data.value.findIndex((i) => i === input.value) |
| 49 | + if (index >= 0) return `${index}` |
| 50 | + return 'Invalid ID' |
| 51 | + } |
| 52 | +}) |
| 53 | +
|
| 54 | +function changeMode() { |
| 55 | + const lastResult = result.value |
| 56 | + mode.value = !mode.value |
| 57 | + nextTick(() => (input.value = lastResult)) |
| 58 | +} |
| 59 | +</script> |
| 60 | +<template> |
| 61 | + <div class="complex-padding flex"> |
| 62 | + <span v-if="errorState" class="error-state">{{ t('protocol.error.data') }}</span> |
| 63 | + <span v-else class="flex-1">{{ desc }}</span> |
| 64 | + <span |
| 65 | + v-if="!primitive && !errorState" |
| 66 | + class="action-text" |
| 67 | + tabindex="0" |
| 68 | + @click="showSubType = !showSubType" |
| 69 | + @keyup="(e: KeyboardEvent) => isActionKey(e) && (showSubType = !showSubType)" |
| 70 | + > |
| 71 | + [{{ showSubType ? t('protocol.action.collapse') : t('protocol.action.expand') }}] |
| 72 | + </span> |
| 73 | + </div> |
| 74 | + <div v-if="loadingState && showSubType" class="px-[0.4em]">{{ t('protocol.loading') }}</div> |
| 75 | + <div v-else-if="showSubType && writeableRegistry" class="px-[0.4em] pb-[0.2em] italic"> |
| 76 | + {{ t('protocol.type.registry.writeable') }} |
| 77 | + </div> |
| 78 | + <div v-else-if="showSubType" class="sub-type w-full flex flex-row items-center"> |
| 79 | + <CdxButton @click="changeMode"> |
| 80 | + {{ t(`protocol.action.search_registry.${mode ? 'network_id' : 'id_network'}`) }} |
| 81 | + </CdxButton> |
| 82 | + <CdxTextInput v-model="input" :input-type="mode ? 'number' : 'text'" translate="no" /> |
| 83 | + <span class="px-[0.4em] py-[0.2em] min-w-16 text-center" translate="no">{{ result }}</span> |
| 84 | + </div> |
| 85 | + <TypeChoice v-if="showSubType" :data="content" :version="version" type="div" class="sub-type" /> |
| 86 | +</template> |
0 commit comments