-
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
// SPDX-FileCopyrightText: 2024 Shota FUJI <pockawoooh@gmail.com>
// SPDX-License-Identifier: AGPL-3.0-only
import { fromJsonString } from "@bufbuild/protobuf";
import { html, nothing, render, type TemplateResult } from "lit-html";
import { repeat } from "lit-html/directives/repeat.js";
import {
type ListResponse,
ListResponseSchema,
} from "@yamori/proto/yamori/workspace/v1/list_response_pb.js";
import { wrapElement, YamoriElement } from "../../element.ts";
import { YamoriButton } from "../../components/button/button.ts";
import { YamoriCallout } from "../../components/callout/callout.ts";
import { YamoriEmpty } from "../../components/empty/empty.ts";
import { LucideBadgeX } from "../../lucide/icons.ts";
import { YamoriWorkspaceListEntry } from "../workspace-list-entry/workspace-list-entry.ts";
import css from "./workspace-list.css?inline";
export { OpenEvent } from "../workspace-list-entry/workspace-list-entry.ts";
export class RetryEvent extends Event {
constructor() {
super("retry", {
cancelable: true,
});
}
}
type ObservedAttributes = readonly ["pending", "list-response"];
export const YamoriWorkspaceList = wrapElement({
tagName: "yamori-workspace-list",
dependencies: [
YamoriButton,
YamoriCallout,
YamoriEmpty,
LucideBadgeX,
YamoriWorkspaceListEntry,
],
constructor: class extends YamoriElement {
static get observedAttributes(): ObservedAttributes {
return ["pending", "list-response"];
}
#pending: boolean = this.hasAttribute("pending");
#data: ListResponse | null = this.#parseListResponseAttribute();
#shadow: ShadowRoot;
attributeChangedCallback(
name: ObservedAttributes[number],
oldValue: string | null,
newValue: string | null,
) {
if (oldValue === newValue) {
return;
}
switch (name) {
case "pending":
this.#pending = typeof newValue === "string";
this.#render();
return;
case "list-response":
this.#data = this.#parseListResponseAttribute();
this.#render();
return;
}
}
#parseListResponseAttribute(): ListResponse | null {
const value = this.getAttribute("list-response");
if (!value) {
return null;
}
try {
return fromJsonString(ListResponseSchema, value);
} catch (error) {
console.error(`The value of "list-response" attribute is invalid.`, error);
return null;
}
}
override connectedCallback(): void {
super.connectedCallback();
this.#render();
}
constructor() {
super();
this.#shadow = this.attachShadow({
mode: "open",
});
}
setListResponse(listResponse: ListResponse): void {
this.#data = listResponse;
this.#render();
}
#pendingIndicator(): TemplateResult {
if (!this.#pending) {
return html`${nothing}`;
}
return html`
<span class="revalidating">データ取得中...</span>
`;
}
#view(): TemplateResult {
// データがない場合はどうしようもないので読込中としてお茶を濁す
if (!this.#data) {
return html`
${this.#pendingIndicator()}
<p>取得中...</p>
`;
}
switch (this.#data.result.case) {
case "systemError": {
const onRetry = (event: MouseEvent) => {
event.preventDefault();
event.stopPropagation();
this.dispatchEvent(new RetryEvent());
};
return html`
${this.#pendingIndicator()}
<yamori-empty>
<lucide-badge-x slot="icon"></lucide-badge-x>
<span slot="title">取得エラー</span>
ワークスペース一覧の取得に失敗しました。<br/>
エラーコード: ${this.#data.result.value.code}
<yamori-button slot="action" ?pending=${this.#pending} @click=${onRetry}>
再取得
</yamori-button>
</yamori-empty>
`;
}
case "ok": {
const { workspaces } = this.#data.result.value;
if (workspaces.length === 0) {
return html`
${this.#pendingIndicator()}
<slot name="empty-fallback" class="fallback"></slot>
`;
}
return html`
${this.#pendingIndicator()}
<ul class="list">
${repeat(
workspaces,
(workspace) => workspace.id?.value,
(workspace) => html`
<yamori-workspace-list-entry .workspace=${workspace}></yamori-workspace-list-entry>
`,
)}
</ul>
`;
}
default: {
return html`<p>データが破損しています</p>`;
}
}
}
#render(): void {
render(
html`
<style>${css}</style>
${this.#view()}
`,
this.#shadow,
);
}
},
});