File size: 5,961 Bytes
9b53f70
 
00d2c12
9b53f70
 
d94104e
9b53f70
 
412390b
9b53f70
 
 
 
 
d94104e
9b53f70
 
 
 
 
 
 
 
 
 
 
 
 
00d2c12
 
 
 
9b53f70
d94104e
9b53f70
00d2c12
9b53f70
00d2c12
9b53f70
 
 
 
 
 
 
 
 
 
d94104e
9b53f70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c55c292
9b53f70
 
 
 
 
 
 
 
c55c292
 
9b53f70
c55c292
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b53f70
 
 
 
 
 
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
import { useState, useEffect } from "react";
import { useVideoGen } from "../stores/videoGenStore";
import track from "../lib/track";
import { useDirty } from "../stores/dirtyStore";
import { IcVideo } from "../components/Icons";
import { jget, jpost, request } from "../api";

export default function CreateVideo() {
  const { connected, url, connect, disconnect } = useVideoGen();
  const { makeDirty, makeClean } = useDirty();
  const [connectUrl, setConnectUrl] = useState(url || "");
  const [busy, setBusy] = useState(false);

  useEffect(() => {
    jget("/api/video/status")
      .then(data => {
        if (data.connected && data.url) {
          setConnectUrl(data.url);
          connect(data.url, data);
        }
      })
      .catch(() => {});
  }, []);

  const handleConnect = async () => {
    makeDirty("Bạn đang kết nối tạo video. Mất tiến trình nếu rời đi.");
    if (!connectUrl.trim()) return;
    setBusy(true);
    // Tool này chỉ bàn giao sang app ngoài: đo được tới đây là hết, KHÔNG có
    // output nội bộ để đo (spec §4.8).
    const t0 = Date.now();
    track.runClick({ phase: "connect" });
    try {
      const data = await jpost("/api/video/connect", { url: connectUrl });
      connect(connectUrl, data);
      track.step("connect_success", "output", { latency_ms_client: Date.now() - t0 });
    } catch (e) {
      track.abandon("connect_failed", "run");
      alert("Lỗi kết nối: " + e.message);
    } finally {
      setBusy(false);
    }
  };

  const handleDisconnect = async () => {
    makeClean();
    setBusy(true);
    try {
      await request("/api/video/disconnect", { method: "POST" });
      disconnect();
    } catch (e) {
      alert("Lỗi: " + e.message);
    } finally {
      setBusy(false);
    }
  };

  return (
    <>
      <div style={{
        display: "flex", alignItems: "center", gap: 12,
        padding: "10px 36px", background: "var(--white)",
        borderBottom: "1px solid var(--line)",
        flex: "0 0 auto",
      }}>
        <IcVideo size={18} style={{ color: "var(--orange)" }} />
        <span style={{
          display: "inline-flex", alignItems: "center", gap: 5,
          fontSize: 12, fontWeight: 600, padding: "4px 10px", borderRadius: 20,
          background: connected ? "var(--green-soft)" : "var(--red-soft)",
          color: connected ? "var(--green)" : "var(--red)",
        }}>
          <span style={{ width: 6, height: 6, borderRadius: "50%", background: "currentColor" }} />
          {connected ? "Đã kết nối" : "Chưa kết nối"}
        </span>

        {!connected ? (
          <>
            <input type="text" value={connectUrl}
              onChange={e => setConnectUrl(e.target.value)}
              placeholder="https://xxxx.gradio.live/"
              style={{ flex: 1, maxWidth: 400, padding: "7px 12px", fontSize: 13, border: "2px solid var(--line)", borderRadius: "var(--radius-sm)", background: "#fff" }} />
            <button className="btn primary sm" disabled={busy || !connectUrl.trim()}
              onClick={handleConnect}>
              {busy ? "⏳" : "🔌 Kết nối"}
            </button>
          </>
        ) : (
          <>
            <span className="small muted" style={{ flex: 1 }}>{url}</span>
            <button className="btn sm" disabled={busy} onClick={handleDisconnect}>
              🔌 Ngắt kết nối
            </button>
          </>
        )}
      </div>

      {/* Hướng dẫn khi chưa kết nối */}
      {!connected && (
        <div style={{
          flex: 1, display: "flex", alignItems: "center", justifyContent: "center",
          background: "var(--cream)",
        }}>
          <div style={{ textAlign: "center", maxWidth: 480 }}>
            <div style={{
              width: 64, height: 64, borderRadius: "50%",
              background: "var(--orange-soft)", color: "var(--orange)",
              display: "grid", placeItems: "center", margin: "0 auto 16px",
            }}>
              <IcVideo size={28} />
            </div>
            <h2 style={{ margin: "0 0 8px", fontSize: 20 }}>Tạo video với AI</h2>
            <p style={{ color: "var(--ink-2)", fontSize: 14, lineHeight: 1.6, margin: "0 0 20px" }}>
              Kết nối tới server WanGP đang chạy để dùng giao diện tạo video gốc ngay trong trang.
            </p>
            <ol style={{
              textAlign: "left", margin: "0 auto", padding: 0, listStyle: "none",
              display: "flex", flexDirection: "column", gap: 12,
            }}>
              {[
                ["Chạy WanGP", "Khởi động công cụ sinh video WanGP và lấy link Gradio công khai của nó (dạng https://xxxx.gradio.live/)."],
                ["Dán link & Kết nối", "Dán link vào ô phía trên rồi bấm “🔌 Kết nối”. Trạng thái sẽ chuyển sang “Đã kết nối”."],
                ["Tạo video", "Giao diện WanGP gốc hiện ngay trong trang — nhập prompt và tạo video như bình thường."],
                ["Ngắt kết nối", "Xong việc bấm “🔌 Ngắt kết nối”. Link được ghi nhớ để lần sau vào lại nhanh."],
              ].map(([t, d], i) => (
                <li key={i} style={{ display: "flex", gap: 12, alignItems: "flex-start" }}>
                  <span style={{
                    flex: "0 0 24px", width: 24, height: 24, borderRadius: "50%",
                    background: "var(--orange)", color: "#fff", fontSize: 12, fontWeight: 700,
                    display: "grid", placeItems: "center", marginTop: 1,
                  }}>{i + 1}</span>
                  <span style={{ fontSize: 13, lineHeight: 1.5 }}>
                    <b>{t}.</b> <span style={{ color: "var(--ink-2)" }}>{d}</span>
                  </span>
                </li>
              ))}
            </ol>
          </div>
        </div>
      )}
    </>
  );
}