示例工程 28-project-approval-panel

28-project-approval-panel/app/src/views/ApprovalVue.vue

vue · 3.5 KB

<script setup lang="ts">
import { computed, onMounted, onUnmounted, shallowRef } from "vue";
import type { Decision } from "../../shared/ipc";
import { remainingSeconds, riskLabel, type ApprovalStore } from "../lib/store";

const props = defineProps<{ store: ApprovalStore }>();
const state = shallowRef(props.store.getState());

let unsubscribe: (() => void) | undefined;
onMounted(() => {
  state.value = props.store.getState();
  unsubscribe = props.store.subscribe(() => {
    state.value = props.store.getState();
  });
});
onUnmounted(() => unsubscribe?.());

const current = computed(() => state.value.pending[0]);
const queue = computed(() => state.value.pending.slice(1));
const remaining = computed(() =>
  current.value
    ? remainingSeconds(current.value.expiresAt, state.value.now)
    : 0,
);

function decide(decision: Decision): void {
  if (current.value) void props.store.decide(current.value.id, decision);
}
</script>

<template>
  <div class="panel">
    <div class="panel-head">
      <div>
        <div class="muted small">当前请求</div>
        <h2 data-testid="approval-title">
          {{ current ? current.tool : "没有待审批请求" }}
        </h2>
      </div>
      <div v-if="current" class="countdown" data-testid="approval-countdown">
        {{ remaining }}s
      </div>
    </div>

    <template v-if="current">
      <div class="request-meta">
        <span :class="`risk risk-${current.risk}`">
          {{ riskLabel(current.risk) }}
        </span>
        <span class="muted small" data-testid="approval-detail">
          {{ current.detail }}
        </span>
      </div>
      <div class="actions">
        <button
          class="btn primary"
          data-testid="approval-allow"
          @click="decide('allow')"
        >
          允许一次(1)
        </button>
        <button
          class="btn danger"
          data-testid="approval-deny"
          @click="decide('deny')"
        >
          拒绝(2)
        </button>
        <button
          class="btn"
          data-testid="approval-always"
          @click="decide('always')"
        >
          永久允许(3)
        </button>
      </div>
    </template>
    <p v-else class="empty muted" data-testid="approval-empty">
      队列已清空。用上方按钮模拟新的工具调用。
    </p>

    <div class="columns">
      <div>
        <div class="muted small">
          排队
          <span data-testid="approval-queue-count">{{ queue.length }}</span>
        </div>
        <ul class="queue">
          <li v-for="item in queue" :key="item.id">
            <code>{{ item.tool }}</code>
            <span class="muted">{{ riskLabel(item.risk) }}</span>
          </li>
        </ul>
      </div>
      <div>
        <div class="muted small">
          永久允许
          <span data-testid="approval-policies">
            {{ state.policies.length }}
          </span>
        </div>
        <ul class="queue" data-testid="approval-policy-list">
          <li v-for="tool in state.policies" :key="tool">
            <code>{{ tool }}</code>
          </li>
        </ul>
      </div>
    </div>

    <div class="history">
      <div class="muted small">最近决定</div>
      <ul data-testid="approval-history">
        <li v-for="record in state.history.slice(0, 6)" :key="record.id">
          <code>{{ record.tool }}</code>
          <span :class="`outcome outcome-${record.outcome}`">
            {{ record.outcome }}
          </span>
          <span v-if="record.auto" class="muted">自动</span>
        </li>
      </ul>
    </div>
  </div>
</template>