File size: 10,560 Bytes
806144f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import { useEffect, useMemo, useState } from 'react';
import type { DemoLocale } from './types';

type ChatDemoScene = {
  id: 'prompt' | 'workflow' | 'output';
  title: string;
  caption: string;
  prompt: string;
  tool: string;
  result: string;
  durationMs: number;
};

type ChatFeatureDemoProps = {
  locale: DemoLocale;
  isBusy: boolean;
};

const COPY = {
  en: {
    title: 'Feature Walkthrough',
    subtitle: 'Video-style preview of how the Chat Agent works',
    stateBusy: 'Live run active',
    statePlaying: 'Playing demo',
    statePaused: 'Demo paused',
    play: 'Play',
    pause: 'Pause',
    replay: 'Replay',
    hide: 'Hide',
    show: 'Show Demo',
    hiddenBusy: 'Walkthrough hidden while the agent is running.',
    hiddenIdle: 'Walkthrough hidden.',
    reducedMotion: 'Autoplay is disabled because reduced motion is enabled.',
    videoName: 'chat_agent_walkthrough.mp4',
    lanePrompt: 'Prompt',
    laneWorkflow: 'Workflow',
    laneOutput: 'Output',
    quickPromptLabel: 'quick prompt',
    workflowProgress: 'progress panel update',
    workflowTrace: 'trace event recorded',
    resultCardTitle: 'Result Card',
    payloadCardTitle: 'Structured Payload',
    payloadCardText: 'JSON preview + action chips',
  },
  'zh-TW': {
    title: '功能導覽',
    subtitle: '以影片風格快速示範 Chat Agent 使用方式',
    stateBusy: '執行中',
    statePlaying: '導覽播放中',
    statePaused: '導覽已暫停',
    play: '播放',
    pause: '暫停',
    replay: '重播',
    hide: '隱藏',
    show: '顯示導覽',
    hiddenBusy: '模型執行中,已暫時隱藏導覽。',
    hiddenIdle: '導覽已隱藏。',
    reducedMotion: '你啟用了減少動畫,已停用自動播放。',
    videoName: '聊天導覽影片.mp4',
    lanePrompt: '輸入',
    laneWorkflow: '流程',
    laneOutput: '輸出',
    quickPromptLabel: '快速提示',
    workflowProgress: '進度面板更新',
    workflowTrace: '追蹤事件記錄',
    resultCardTitle: '結果卡片',
    payloadCardTitle: '結構化載荷',
    payloadCardText: 'JSON 預覽 + 動作標籤',
  },
} as const;

const SCENES = {
  en: [
    {
      id: 'prompt',
      title: 'Step 1: Prompt and context',
      caption: 'User chooses a quick prompt or types intent in natural language.',
      prompt: 'Find eggs under NT$200 and rank by freshness.',
      tool: 'Prompt parser prepares query tokens and constraints',
      result: 'Input normalized and queued',
      durationMs: 1700,
    },
    {
      id: 'workflow',
      title: 'Step 2: Tool orchestration',
      caption: 'Planner selects tools and the progress panel updates in real time.',
      prompt: 'Routing: product_search_public + score ranking',
      tool: 'Planner validates filters and runs tool chain',
      result: 'Structured candidate set is ready',
      durationMs: 1750,
    },
    {
      id: 'output',
      title: 'Step 3: Structured answer',
      caption: 'Assistant responds with cards, metrics, actions, and JSON payload.',
      prompt: 'Rendering response bundle for UI cards',
      tool: 'Renderer composes summary and recommendation cards',
      result: 'Chat output card with payload attached',
      durationMs: 1750,
    },
  ],
  'zh-TW': [
    {
      id: 'prompt',
      title: '步驟 1:輸入需求',
      caption: '使用者可點擊快速提示,或直接輸入自然語句。',
      prompt: '找出 200 元以下雞蛋,並依新鮮度排序。',
      tool: '系統先解析條件與語意',
      result: '已建立查詢上下文',
      durationMs: 1700,
    },
    {
      id: 'workflow',
      title: '步驟 2:工具編排',
      caption: '規劃器決定工具路徑,進度面板同步顯示執行狀態。',
      prompt: '路由:product_search_public + 排名計算',
      tool: '規劃器驗證條件並串接工具',
      result: '已取得結構化候選結果',
      durationMs: 1750,
    },
    {
      id: 'output',
      title: '步驟 3:輸出答案',
      caption: '最後輸出摘要、卡片與 JSON 結構化資料。',
      prompt: '生成可視化回覆內容',
      tool: 'Renderer 整理摘要與推薦卡片',
      result: '可直接展示的結果已完成',
      durationMs: 1750,
    },
  ],
} as const satisfies Record<DemoLocale, ChatDemoScene[]>;

function ChatFeatureDemo({ locale, isBusy }: ChatFeatureDemoProps) {
  const copy = COPY[locale];
  const scenes = useMemo(() => SCENES[locale], [locale]);

  const [sceneIndex, setSceneIndex] = useState(0);
  const [elapsedMs, setElapsedMs] = useState(0);
  const [isPlaying, setIsPlaying] = useState(true);
  const [isCollapsed, setIsCollapsed] = useState(false);
  const [reducedMotion, setReducedMotion] = useState(false);

  useEffect(() => {
    const media = window.matchMedia('(prefers-reduced-motion: reduce)');
    const apply = () => setReducedMotion(media.matches);

    apply();
    if (media.addEventListener) {
      media.addEventListener('change', apply);
      return () => media.removeEventListener('change', apply);
    }

    media.addListener(apply);
    return () => media.removeListener(apply);
  }, []);

  useEffect(() => {
    if (reducedMotion) {
      setIsPlaying(false);
    }
  }, [reducedMotion]);

  useEffect(() => {
    setSceneIndex(0);
    setElapsedMs(0);
    setIsCollapsed(false);
    if (!reducedMotion) {
      setIsPlaying(true);
    }
  }, [locale, reducedMotion]);

  useEffect(() => {
    if (!isBusy) return;
    setIsCollapsed(true);
    setIsPlaying(false);
  }, [isBusy]);

  useEffect(() => {
    if (!isPlaying || isCollapsed || isBusy || reducedMotion) return;

    const timer = window.setInterval(() => {
      setElapsedMs((prev) => {
        const scene = scenes[sceneIndex];
        if (!scene) return 0;
        const next = prev + 120;
        if (next < scene.durationMs) return next;
        setSceneIndex((current) => (current + 1) % scenes.length);
        return 0;
      });
    }, 120);

    return () => window.clearInterval(timer);
  }, [isPlaying, isCollapsed, isBusy, reducedMotion, scenes, sceneIndex]);

  const activeScene = scenes[sceneIndex] ?? scenes[0];
  const totalDuration = scenes.reduce((sum, scene) => sum + scene.durationMs, 0);
  const previousDuration = scenes.slice(0, sceneIndex).reduce((sum, scene) => sum + scene.durationMs, 0);
  const progress = totalDuration > 0 ? Math.round(((previousDuration + elapsedMs) / totalDuration) * 100) : 0;

  const replay = () => {
    setSceneIndex(0);
    setElapsedMs(0);
    if (!reducedMotion && !isBusy) {
      setIsPlaying(true);
    }
  };

  if (isCollapsed) {
    return (
      <section className="chat-demo-collapsed" aria-live="polite">
        <p>{isBusy ? copy.hiddenBusy : copy.hiddenIdle}</p>
        {!isBusy ? (
          <button
            type="button"
            className="ghost-btn"
            onClick={() => {
              setIsCollapsed(false);
              replay();
            }}
          >
            {copy.show}
          </button>
        ) : null}
      </section>
    );
  }

  return (
    <section className="chat-demo-video" aria-label={copy.title}>
      <header className="chat-demo-head">
        <div>
          <h3>{copy.title}</h3>
          <p>{copy.subtitle}</p>
          {reducedMotion ? <small>{copy.reducedMotion}</small> : null}
        </div>
        <div className="chat-demo-head-actions">
          <span className={`chat-demo-state ${isBusy ? 'busy' : isPlaying ? 'playing' : 'paused'}`}>
            {isBusy ? copy.stateBusy : isPlaying ? copy.statePlaying : copy.statePaused}
          </span>
          <button
            type="button"
            className="ghost-btn"
            onClick={() => setIsPlaying((prev) => !prev)}
            disabled={isBusy || reducedMotion}
          >
            {isPlaying ? copy.pause : copy.play}
          </button>
          <button type="button" className="ghost-btn" onClick={replay} disabled={isBusy}>
            {copy.replay}
          </button>
          <button type="button" className="ghost-btn" onClick={() => setIsCollapsed(true)}>
            {copy.hide}
          </button>
        </div>
      </header>

      <div className="chat-demo-frame" data-scene={activeScene.id}>
        <div className="chat-demo-windowbar">
          <span />
          <span />
          <span />
          <small>{copy.videoName}</small>
        </div>

        <div className="chat-demo-canvas">
          <section className="chat-demo-lane prompts">
            <h4>{copy.lanePrompt}</h4>
            <div className="chat-demo-prompt-chip">{copy.quickPromptLabel}</div>
            <div className="chat-demo-prompt-bubble">{activeScene.prompt}</div>
          </section>

          <section className="chat-demo-lane workflow">
            <h4>{copy.laneWorkflow}</h4>
            <div className="chat-demo-step active">{activeScene.tool}</div>
            <div className="chat-demo-step">{copy.workflowProgress}</div>
            <div className="chat-demo-step">{copy.workflowTrace}</div>
          </section>

          <section className="chat-demo-lane output">
            <h4>{copy.laneOutput}</h4>
            <article className="chat-demo-output-card">
              <strong>{copy.resultCardTitle}</strong>
              <p>{activeScene.result}</p>
            </article>
            <article className="chat-demo-output-card">
              <strong>{copy.payloadCardTitle}</strong>
              <p>{copy.payloadCardText}</p>
            </article>
          </section>

          <span className="chat-demo-cursor" aria-hidden="true" />
        </div>
      </div>

      <footer className="chat-demo-meta">
        <div className="chat-demo-caption">
          <strong>{activeScene.title}</strong>
          <p>{activeScene.caption}</p>
        </div>

        <div className="chat-demo-progress-track" aria-hidden="true">
          <span style={{ width: `${Math.min(100, Math.max(0, progress))}%` }} />
        </div>

        <div className="chat-demo-scene-row">
          {scenes.map((scene, index) => (
            <button
              key={scene.id}
              type="button"
              className={`chat-demo-scene-pill ${index === sceneIndex ? 'active' : index < sceneIndex ? 'complete' : ''}`}
              onClick={() => {
                setSceneIndex(index);
                setElapsedMs(0);
              }}
            >
              {scene.title}
            </button>
          ))}
        </div>
      </footer>
    </section>
  );
}

export default ChatFeatureDemo;