File size: 3,922 Bytes
01e655b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { client } from "./client.mjs";
import { html, create, styled } from "./misc.mjs";

/**
 * 管理 speaker
 *
 * 1. 显示 speaker 列表
 * 2. 创建 speaker
 * 3. 可以删除修改 speaker
 */
const useStore = create((set, get) => ({
  /**
   * @type {{ name: string, params: string }[]}
   */
  speakers: [],

  setSpeakers: (speakers) => set({ speakers }),

  formData: {
    seed: 42,
    name: "",
  },
  setFormData: (data) => set({ formData: data }),
}));

window.addEventListener("load", async () => {
  const speakers = await client.listSpeakers();
  useStore.get().setSpeakers(speakers.data);
});

const SpeakerFactory = () => {
  // 调用接口创建 speaker
  // 创建speaker需要设定seed和name
  const { setSpeakers, formData, setFormData } = useStore();
  return html`
    <feildset class="speaker-factory">
      <div>
        <label
          >seed

          <input
            type="number"
            value=${formData.seed}
            oninput=${(e) => setFormData({ ...formData, seed: e.target.value })}
          />
        </label>
        <label
          >name
          <input
            type="text"
            value=${formData.name}
            oninput=${(e) => setFormData({ ...formData, name: e.target.value })}
          />
        </label>

        <button
          onclick=${async () => {
            const speaker = await client.createSpeaker(formData);
            setSpeakers([...useStore.get().speakers, speaker]);
            setFormData({ seed: 0, name: "" });
          }}
        >
          创建
        </button>
      </div>
    </feildset>
  `;
};

const SpeakerList = () => {
  // 显示 speaker 列表
  // 只是可以列出来,没有其他操作
  const { speakers } = useStore();
  // 以table
  return html`
    <fieldset class="spekaer-list">
      <legend>Speakers</legend>

      <table class="speaker-list">
        <thead>
          <tr>
            <th>id</th>
            <th>name</th>
          </tr>
        </thead>
        <tbody>
          ${speakers.map(
            (speaker) => html`
              <tr>
                <td>${speaker.id}</td>
                <td>${speaker.name}</td>
              </tr>
            `
          )}
        </tbody>
      </table>
    </fieldset>
  `;
};

const SpeakerPageContainer = styled.div`
  display: flex;
  flex-direction: row;

  textarea {
    width: 100%;
    height: 10rem;
    margin-bottom: 1rem;

    min-height: 10rem;

    resize: vertical;
  }

  button {
    padding: 0.5rem 1rem;
    background-color: #007bff;
    color: white;
    border: none;
    cursor: pointer;
  }

  button:hover {
    background-color: #0056b3;
  }

  button:disabled {
    background-color: #6c757d;
    cursor: not-allowed;
  }

  fieldset {
    margin-top: 1rem;
    padding: 1rem;
    border: 1px solid #333;
  }

  legend {
    font-weight: bold;
  }

  label {
    display: block;
    margin-bottom: 0.5rem;
  }

  select,
  input[type="range"],
  input[type="number"] {
    width: 100%;
    margin-top: 0.25rem;
  }

  input[type="range"] {
    width: calc(100% - 2rem);
  }

  input[type="number"] {
    width: calc(100% - 2rem);
    padding: 0.5rem;
  }

  input[type="text"] {
    width: 100%;
    padding: 0.5rem;
  }

  audio {
    margin-top: 1rem;
  }

  textarea,
  input,
  select {
    background-color: #333;
    color: white;
    border: 1px solid #333;
    border-radius: 0.25rem;
    padding: 0.5rem;
  }

  table {
    width: 100%;
    border-collapse: collapse;
  }

  th,
  td {
    padding: 0.5rem;
    border: 1px solid #333;
  }

  th {
    background-color: #333;
    color: white;
  }

  th:nth-child(2),
  td:nth-child(2) {
    width: 60%;
  }

  .speaker-factory {
    flex: 1;
  }

  .spekaer-list {
    width: 256px;
  }
`;

export const SpeakerPage = () => {
  return html`
    <${SpeakerPageContainer}>
      <${SpeakerList} />
      <${SpeakerFactory} />
    <//>
  `;
};