TaskPage.vue 3.97 KB
<script setup>
import { onMounted, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import { useRoute, useRouter } from 'vue-router'
import { Badge, Button, Card } from '@drama/ui'

import { api, download, getToken } from '../api.js'

const { t } = useI18n()
const route = useRoute()
const router = useRouter()
const detail = ref(null)
const error = ref('')

const when = (/** @type {string} */ iso) => new Date(iso).toLocaleString()

/** @param {{ file_id: string, filename: string }} artifact */
async function save(artifact) {
  error.value = ''
  try {
    await download(`/api/voko/agents/${route.params.id}/files/${artifact.file_id}`, artifact.filename)
  } catch (err) {
    error.value = err instanceof Error ? err.message : String(err)
  }
}

/** @param {number} bytes */
const size = (bytes) => (bytes < 1024 ? `${bytes} B` : bytes < 1024 * 1024 ? `${(bytes / 1024).toFixed(1)} KB` : `${(bytes / 1024 / 1024).toFixed(1)} MB`)

onMounted(async () => {
  if (getToken() === null) {
    await router.push({ path: '/login', query: { next: route.fullPath } })
    return
  }
  try {
    detail.value = await api('GET', `/api/voko/agents/${route.params.id}/tasks/${route.params.taskId}`)
  } catch (err) {
    error.value = err instanceof Error ? err.message : String(err)
  }
})
</script>

<template>
  <p><router-link :to="`/agents/${route.params.id}/board`">{{ t('task.back') }}</router-link></p>
  <p v-if="error" role="alert">{{ error }}</p>
  <template v-if="detail">
    <h1>{{ detail.task.title }} <Badge>{{ t(`board.status.${detail.task.status}`) }}</Badge></h1>
    <p>{{ t(`board.templates.${detail.task.template}`) }} · {{ t('board.reward', { reward: detail.task.reward }) }} · {{ t('board.rarity', { rarity: detail.task.rarity }) }} · {{ t('board.deadline', { deadline: when(detail.task.deadline) }) }}</p>
    <Card class="voko-section">
      <p class="voko-pre">{{ detail.task.spec }}</p>
      <h2>{{ t('task.acceptance') }}</h2>
      <ol><li v-for="item in detail.acceptance" :key="item.id">{{ item.text }}</li></ol>
    </Card>
    <Card class="voko-section">
      <h2>{{ t('task.checkpoints') }}</h2>
      <p v-if="detail.checkpoints.length === 0">{{ t('task.none') }}</p>
      <p v-for="item in detail.checkpoints" :key="item.id">{{ when(item.created_at) }} · {{ item.progress }}% · {{ item.note }}</p>
    </Card>
    <Card class="voko-section">
      <h2>{{ t('task.submissions') }}</h2>
      <p v-if="detail.submissions.length === 0">{{ t('task.noSubmissions') }}</p>
      <div v-for="item in detail.submissions" :key="item.id">
        <p>{{ t('task.round', { round: item.round }) }} · {{ when(item.created_at) }}</p>
        <p class="voko-pre">{{ item.summary }}</p>
        <ul class="voko-artifacts">
          <li v-for="artifact in item.artifacts" :key="artifact.file_id || artifact.url">
            <template v-if="artifact.type === 'file'">
              <span>{{ artifact.filename }} · {{ size(artifact.size_bytes) }}<template v-if="artifact.note"> · {{ artifact.note }}</template></span>
              <Button variant="secondary" size="sm" :aria-label="t('task.downloadFile', { filename: artifact.filename })" @click="save(artifact)">{{ t('task.download') }}</Button>
            </template>
            <a v-else :href="artifact.url" rel="noopener noreferrer" target="_blank">{{ artifact.note || artifact.url }}</a>
          </li>
        </ul>
      </div>
    </Card>
    <Card class="voko-section">
      <h2>{{ t('task.comments') }}</h2>
      <p v-if="detail.comments.length === 0">{{ t('task.none') }}</p>
      <p v-for="item in detail.comments" :key="item.id" class="voko-pre">{{ when(item.created_at) }} · {{ item.body }}</p>
    </Card>
  </template>
</template>

<style scoped>
.voko-section {
  margin-bottom: var(--drama-space-md);
}
.voko-pre {
  white-space: pre-wrap;
}
.voko-artifacts {
  padding-left: var(--drama-space-md);
}
.voko-artifacts li {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--drama-space-sm);
  margin-bottom: var(--drama-space-sm);
}
</style>