Files
Artveris/apps/admin/src/components/OnchainProofModal.vue
T

282 lines
9.7 KiB
Vue

<template>
<n-modal
v-model:show="visible"
preset="card"
:title="t('pages.blockchain.title')"
style="width: 680px; max-width: 94vw"
>
<n-descriptions :column="1" label-placement="left" bordered size="small">
<n-descriptions-item :label="t('pages.traceCode.shortCode')">
{{ traceCode?.shortCode || '-' }}
</n-descriptions-item>
<n-descriptions-item :label="t('pages.artwork.title')">
{{ traceCode?.artwork?.title || '-' }}
</n-descriptions-item>
<n-descriptions-item :label="t('pages.blockchain.status')">
<n-tag v-if="proof" size="small" :type="statusType(proof.status)">{{ proofStatusText }}</n-tag>
<n-text v-else depth="3">{{ t('pages.traceCode.noOnchain') }}</n-text>
</n-descriptions-item>
<n-descriptions-item v-if="proof?.txHash" :label="t('pages.blockchain.txHash')">
<copyable-value :value="proof.txHash" />
</n-descriptions-item>
<n-descriptions-item v-if="proof?.chainName" :label="t('pages.blockchain.chainName')">
{{ proof.chainName }}
</n-descriptions-item>
<n-descriptions-item v-if="proof?.blockHeight" :label="t('pages.blockchain.blockHeight')">
<copyable-value :value="proof.blockHeight" />
</n-descriptions-item>
<n-descriptions-item v-if="proof?.contractAddress" :label="t('pages.blockchain.contract')">
<copyable-value :value="proof.contractAddress" />
</n-descriptions-item>
<n-descriptions-item v-if="proof?.onchainAt" :label="t('pages.blockchain.onchainAt')">
{{ fmt(proof.onchainAt) }}
</n-descriptions-item>
<n-descriptions-item v-if="proof?.explorerUrl" :label="t('pages.blockchain.explorer')">
<a :href="proof.explorerUrl" target="_blank" rel="noopener" class="proof-link">
{{ t('pages.blockchain.openExplorer') }}
<n-icon :component="OpenOutline" />
</a>
</n-descriptions-item>
<n-descriptions-item v-if="failureReason" :label="t('pages.operations.resultReason')">
<n-text type="error">{{ failureReason }}</n-text>
</n-descriptions-item>
</n-descriptions>
<n-alert v-if="isUnknown" type="warning" :bordered="false" class="unknown-alert">
{{ t('pages.blockchain.unknownTip') }}
</n-alert>
<n-alert v-else-if="unavailableReason" type="warning" :bordered="false" class="unknown-alert">
{{ unavailableReason }}
</n-alert>
<template #footer>
<div class="footer-actions">
<template v-if="auth.canWrite">
<n-button v-if="canRun" type="primary" :loading="working" @click="runOnchain">
{{ proof?.status === 'FAILED' ? t('pages.blockchain.retry') : t('pages.blockchain.trigger') }}
</n-button>
<n-button v-if="canConfirm" :loading="working" @click="confirmProof">
{{ t('pages.traceCode.reconfirmOnchain') }}
</n-button>
<template v-if="isUnknown">
<n-button v-if="proof?.onchainChunkId" :loading="working" @click="resync">
{{ t('pages.blockchain.resync') }}
</n-button>
<n-popover v-if="proof?.onchainChunkId" trigger="click" placement="top-end">
<template #trigger>
<n-button>{{ t('pages.blockchain.recordTransaction') }}</n-button>
</template>
<div class="proof-action-form">
<n-input v-model:value="transactionHash" :maxlength="191" :placeholder="t('pages.blockchain.txHash')" />
<n-button type="primary" :disabled="transactionHash.trim().length < 8" :loading="working" @click="recordTransaction">
{{ t('common.action.confirm') }}
</n-button>
</div>
</n-popover>
<n-popover v-if="auth.isSuperAdmin && proof?.id && proof?.onchainChunkId" trigger="click" placement="top-end">
<template #trigger>
<n-button type="error">{{ t('pages.blockchain.markFailed') }}</n-button>
</template>
<div class="proof-action-form">
<n-input v-model:value="failedReason" :maxlength="2000" :placeholder="t('pages.operations.resultReason')" />
<n-button type="error" :disabled="failedReason.trim().length < 5" :loading="working" @click="markFailed">
{{ t('common.action.confirm') }}
</n-button>
</div>
</n-popover>
</template>
</template>
<n-button @click="visible = false">{{ t('common.action.close') }}</n-button>
</div>
</template>
</n-modal>
</template>
<script setup lang="ts">
import { computed, defineComponent, h, ref, watch } from 'vue';
import { useI18n } from 'vue-i18n';
import { NButton, NIcon } from 'naive-ui';
import { CopyOutline, OpenOutline } from '@vicons/ionicons5';
import { onchainApi, traceCodeApi } from '@/api';
import { useAuthStore } from '@/stores/auth';
import { fmt, statusType } from '@/utils/format';
import { dialog, message } from '@/utils/feedback';
import { localizeOperationReason } from '@/utils/operationReason';
const props = defineProps<{
show: boolean;
traceCode: any | null;
}>();
const emit = defineEmits<{
(event: 'update:show', value: boolean): void;
(event: 'run', traceCode: any): void;
(event: 'changed'): void;
}>();
const { t } = useI18n();
const auth = useAuthStore();
const visible = computed({
get: () => props.show,
set: (value) => emit('update:show', value),
});
const proof = computed(() => props.traceCode?.latestAnyProof || props.traceCode?.proof || null);
const working = ref(false);
const transactionHash = ref('');
const failedReason = ref('');
const proofStatusText = computed(() => {
const status = proof.value?.status;
if (!status) return '-';
const key = `common.status.${status}`;
const translated = t(key);
return translated === key ? status : translated;
});
const isUnknown = computed(() => proof.value?.status === 'UNKNOWN');
const canRun = computed(() => {
const eligible = props.traceCode?.onchainEligible ?? props.traceCode?.eligible;
return eligible !== false && (!proof.value || proof.value.status === 'FAILED');
});
const canConfirm = computed(() =>
Boolean(proof.value?.id && proof.value?.txHash && proof.value.status === 'CONFIRMING'),
);
const failureReason = computed(() => {
const raw = proof.value?.rawResponse;
return localizeOperationReason(
proof.value?.failureReason || proof.value?.reason || proof.value?.error || raw?.error || raw?.message || raw?.enqueueError,
);
});
const unavailableReason = computed(() =>
!proof.value && !canRun.value
? localizeOperationReason(props.traceCode?.onchainReason || props.traceCode?.reason) || t('pages.operations.onchainUnavailable')
: '',
);
const CopyableValue = defineComponent({
props: { value: { type: [String, Number], required: true } },
setup(componentProps) {
return () => h('span', { class: 'copyable-value' }, [
h('span', { class: 'copyable-text' }, String(componentProps.value)),
h(
NButton,
{
quaternary: true,
circle: true,
size: 'tiny',
title: t('common.action.copy'),
onClick: () => copyValue(componentProps.value),
},
{ icon: () => h(NIcon, null, { default: () => h(CopyOutline) }) },
),
]);
},
});
async function copyValue(value: unknown) {
try {
await navigator.clipboard.writeText(String(value));
message.success(t('common.msg.copied'));
} catch {
message.error(t('common.msg.copyFailed'));
}
}
function runOnchain() {
if (!props.traceCode) return;
emit('run', props.traceCode);
}
async function execute(action: () => Promise<unknown>) {
if (working.value) return;
working.value = true;
try {
await action();
message.success(t('common.msg.operationSuccess'));
visible.value = false;
emit('changed');
} finally {
working.value = false;
}
}
function confirmProof() {
if (!proof.value?.id) return;
void execute(() => traceCodeApi.confirmProof(Number(proof.value.id)));
}
function resync() {
if (!proof.value?.onchainChunkId) return;
void execute(() => onchainApi.resync(String(proof.value.onchainChunkId)));
}
function recordTransaction() {
if (!proof.value?.id || transactionHash.value.trim().length < 8) return;
void execute(() => onchainApi.recordTransaction(Number(proof.value.id), transactionHash.value.trim()));
}
function markFailed() {
if (!proof.value?.onchainChunkId || failedReason.value.trim().length < 5) return;
const chunkId = String(proof.value.onchainChunkId);
const reason = failedReason.value.trim();
dialog.warning({
title: t('pages.blockchain.markFailed'),
content: t('pages.blockchain.markFailedConfirm'),
positiveText: t('common.action.confirm'),
negativeText: t('common.action.cancel'),
onPositiveClick: () => execute(() => onchainApi.markFailed(chunkId, reason)),
});
}
watch(
() => [props.show, proof.value?.id] as const,
([show]) => {
if (!show) return;
transactionHash.value = proof.value?.txHash || '';
failedReason.value = '';
},
);
</script>
<style scoped>
.proof-link {
display: inline-flex;
align-items: center;
gap: 5px;
color: var(--brand);
text-decoration: none;
}
.unknown-alert {
margin-top: 12px;
}
.footer-actions {
display: flex;
justify-content: flex-end;
align-items: center;
flex-wrap: wrap;
gap: 8px;
}
.proof-action-form {
display: flex;
gap: 8px;
width: min(460px, calc(100vw - 72px));
}
.proof-action-form :deep(.n-input) {
min-width: 0;
flex: 1 1 auto;
}
:deep(.copyable-value) {
display: inline-flex;
align-items: center;
gap: 4px;
max-width: 100%;
}
:deep(.copyable-text) {
overflow-wrap: anywhere;
font-family: Consolas, monospace;
}
@media (max-width: 520px) {
.proof-action-form {
flex-direction: column;
}
}
</style>