File size: 3,827 Bytes
9d57416
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import {errorText} from './text.ts';
import {useEffect, useState} from 'react';
import {getJSON} from './api.ts';

export type NoteScope = {segment_id: string} | {batch_id: string};
type Note = {id: string; created_at: string; scope: NoteScope; span_s: [number, number] | null; actor_ids: string[]; category: string; text: string; reviewer: string; provenance: {segment_id: string; artifact_sha256: string; generator_version: string}[]};
const CATEGORIES = {unnatural_motion: '运动不自然', unusable_view: '画面不可用', situation_missed: '情形没达成', insufficient_interaction: '交互不足', other: '其他'};
export function NotesPanel({scope, fingerprints, span, actors}: {scope: NoteScope; fingerprints: Record<string, string>; span: [number, number] | null; actors: string[]}) {
  const [notes, setNotes] = useState<Note[]>([]), [text, setText] = useState(''), [reviewer, setReviewer] = useState(''), [category, setCategory] = useState('unnatural_motion');
  const [error, setError] = useState(''), [saving, setSaving] = useState(false), [revision, setRevision] = useState(0), [open, setOpen] = useState(false);
  const key = 'segment_id' in scope ? 'segment_id' : 'batch_id', id = 'segment_id' in scope ? scope.segment_id : scope.batch_id;
  useEffect(() => {
    const controller = new AbortController();
    getJSON<{notes: Note[]; errors: unknown[]}>('/api/v1/notes?' + new URLSearchParams({[key]: id}), controller.signal).then(data => {setNotes(data.notes); if (data.errors.length) setError('有保存记录不能读取,已保留原文件。');}).catch(e => {if (!controller.signal.aborted) setError(errorText(e));});
    return () => controller.abort();
  }, [key, id, revision]);
  async function save() {
    setSaving(true); setError('');
    try {
      const response = await fetch('/api/v1/notes', {method: 'POST', credentials: 'same-origin', headers: {'Content-Type': 'application/json'},
        body: JSON.stringify({scope, span_s: span, actor_ids: actors, category, text, reviewer, expected_fingerprints: fingerprints})});
      const value = await response.json(); if (!response.ok) throw Error(value.error?.message || String(response.status));
      setText(''); setRevision(r => r + 1); setOpen(false);
    } catch (e) {setError(errorText(e));} finally {setSaving(false);}
  }
  return <section className="notes-panel"><div className="notes-heading"><strong>审阅意见 · {notes.length}</strong><button data-open-note onClick={() => setOpen(x => !x)}>记意见</button></div>
    {open && <div className="note-form"><p>{span ? `${span[0].toFixed(2)}–${span[1].toFixed(2)} 秒` : '整批'} · {actors.join('、') || '未指定主体'}</p>
      <label>类别<select data-note-category value={category} onChange={e => setCategory(e.target.value)}>{Object.entries(CATEGORIES).map(([id, label]) => <option key={id} value={id}>{label}</option>)}</select></label>
      <label>审阅者<input data-note-reviewer value={reviewer} maxLength={128} onChange={e => setReviewer(e.target.value)} /></label>
      <label>意见<textarea data-note-text value={text} maxLength={8000} onChange={e => setText(e.target.value)} /></label>
      <button data-save-note disabled={saving || !text.trim() || !reviewer.trim()} onClick={() => void save()}>{saving ? '保存中…' : '保存意见'}</button></div>}
    {error && <p role="alert" className="note-error">{error}</p>}
    {notes.map(n => <article className="saved-note" data-note-id={n.id} key={n.id}><strong>{CATEGORIES[n.category as keyof typeof CATEGORIES] || n.category}</strong> · {n.reviewer} · {n.span_s ? `${n.span_s[0].toFixed(2)}–${n.span_s[1].toFixed(2)} 秒` : '整批'}<p>{n.text}</p><small>{n.created_at} · 保存时剧本 {n.provenance.map(p => p.artifact_sha256.slice(0, 8)).join('、')}</small></article>)}
  </section>;
}