-
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
// SPDX-FileCopyrightText: 2024 Shota FUJI <pockawoooh@gmail.com>
// SPDX-License-Identifier: AGPL-3.0-only
import { fromJsonString } from "@bufbuild/protobuf";
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 { 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(
"yamori-workspace-list",
class extends YamoriElement {
static get observedAttributes(): ObservedAttributes {
return ["pending", "list-response"];
}
#pending: boolean = this.hasAttribute("pending");
#data: ListResponse | null = this.#parseListResponseAttribute();
#shadow: ShadowRoot;
#startMarker: Comment;
#endMarker: Comment;
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;
}
}
constructor() {
super();
this.#shadow = this.attachShadow({
mode: "open",
});
const style = document.createElement("style");
style.textContent = css;
this.#shadow.appendChild(style);
this.#startMarker = document.createComment("");
this.#shadow.appendChild(this.#startMarker);
this.#endMarker = document.createComment("");
this.#shadow.appendChild(this.#endMarker);
this.#render();
}
setListResponse(listResponse: ListResponse): void {
this.#data = listResponse;
this.#render();
}
#render(): void {
const range = document.createRange();
range.setStartAfter(this.#startMarker);
range.setEndBefore(this.#endMarker);
range.deleteContents();
if (!this.#data) {
// データがない場合はどうしようもないので読込中と同様とする
const label = document.createElement("p");
label.textContent = "取得中...";
this.#shadow.insertBefore(label, this.#endMarker);
return;
}
if (this.#pending) {
const spinner = document.createElement("div");
spinner.classList.add("spinner");
this.#shadow.insertBefore(spinner, this.#endMarker);
}
switch (this.#data.result.case) {
case "systemError": {
const callout = document.createElement(YamoriCallout.tagName) as InstanceType<
typeof YamoriCallout.constructor
>;
const title = document.createElement("span");
title.slot = "title";
title.textContent = `取得エラー (${this.#data.result.value.code})`;
callout.appendChild(title);
const message = document.createTextNode(
`ワークスペース一覧の取得に失敗しました。`,
);
callout.appendChild(message);
const retry = document.createElement(YamoriButton.tagName) as InstanceType<
typeof YamoriButton.constructor
>;
retry.slot = "action";
retry.textContent = "再試行";
retry.addEventListener("click", (event) => {
event.preventDefault();
event.stopPropagation();
this.dispatchEvent(new RetryEvent());
});
callout.appendChild(retry);
this.#shadow.insertBefore(callout, this.#endMarker);
return;
}
case "ok": {
const list = document.createElement("ul");
list.classList.add("list");
this.#shadow.insertBefore(list, this.#endMarker);
const { workspaces } = this.#data.result.value;
customElements.whenDefined(YamoriWorkspaceListEntry.tagName).then(() => {
for (const workspace of workspaces) {
const item = document.createElement(
YamoriWorkspaceListEntry.tagName,
) as InstanceType<typeof YamoriWorkspaceListEntry.constructor>;
item.setWorkspace(workspace);
list.appendChild(item);
}
});
return;
}
default: {
const message = document.createElement("p");
message.textContent = "データが破損しています";
this.#shadow.insertBefore(message, this.#endMarker);
return;
}
}
}
},
);