-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathResourceManagementComponent.cs
More file actions
269 lines (238 loc) · 11.3 KB
/
ResourceManagementComponent.cs
File metadata and controls
269 lines (238 loc) · 11.3 KB
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
// Copyright © 2024-Present The Synapse Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"),
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using Json.Schema;
namespace Synapse.Dashboard.Components;
/// <summary>
/// Represents the base class for all components used to manage <see cref="IResource"/>s
/// </summary>
/// <typeparam name="TComponent">The type of component inheriting the <see cref="ResourceManagementComponent{TComponent, TStore, TState, TResource}"/></typeparam>
/// <typeparam name="TStore">The type of <see cref="ResourceManagementComponentStoreBase{TResource}"/> to use</typeparam>
/// <typeparam name="TState">The type of the component's state</typeparam>
/// <typeparam name="TResource">The type of <see cref="IResource"/> to manage</typeparam>
public abstract class ResourceManagementComponent<TComponent, TStore, TState, TResource>
: StatefulComponent<TComponent, TStore, TState>
where TComponent : ResourceManagementComponent<TComponent, TStore, TState, TResource>
where TStore : ResourceManagementComponentStoreBase<TState, TResource>
where TState : ResourceManagementComponentState<TResource>, new()
where TResource : Resource, new()
{
/// <summary>
/// Gets/sets the service to build a bridge with the monaco interop extension
/// </summary>
[Inject]
protected MonacoInterop? MonacoInterop { get; set; }
/// <summary>
/// Gets/sets the service used for JS interop
/// </summary>
[Inject]
protected JSInterop JsInterop { get; set; } = default!;
/// <summary>
/// Gets the service used to serialize/deserialize objects to/from JSON
/// </summary>
[Inject]
protected IJsonSerializer Serializer { get; set; } = null!;
/// <summary>
/// Gets/sets the list of displayed <see cref="Resource"/>s
/// </summary>
protected EquatableList<TResource>? Resources { get; set; }
/// <summary>
/// Gets/sets the list of selected <see cref="Resource"/>s
/// </summary>
protected EquatableList<string> SelectedResourceNames { get; set; } = [];
/// <summary>
/// Gets/sets the <see cref="Offcanvas"/> used to show the <see cref="Resource"/>'s details
/// </summary>
protected Offcanvas? DetailsOffCanvas { get; set; }
/// <summary>
/// Gets/sets the <see cref="Offcanvas"/> used to edit the <see cref="Resource"/>
/// </summary>
protected Offcanvas? EditorOffCanvas { get; set; }
/// <summary>
/// Gets/sets the <see cref="ConfirmDialog"/> used to confirm the <see cref="Resource"/>'s deletion
/// </summary>
protected ConfirmDialog? Dialog { get; set; }
/// <summary>
/// Gets/sets the <see cref="Resource"/>'s <see cref="ResourceDefinition"/>
/// </summary>
protected ResourceDefinition? Definition { get; set; }
/// <summary>
/// Gets/sets the search term to filter the resources with
/// </summary>
protected string? SearchTerm { get; set; }
/// <summary>
/// Gets/sets a boolean value that indicates whether data is currently being gathered
/// </summary>
protected bool Loading { get; set; } = false;
/// <summary>
/// Gets/sets the checkbox used to (un)select all resources
/// </summary>
protected ElementReference? CheckboxAll { get; set; } = null;
string activeResourceName = null!;
/// <summary>
/// Gets/sets the name of the active resource
/// </summary>
[Parameter] public string? Name { get; set; }
/// <inheritdoc/>
protected override async Task OnInitializedAsync()
{
Observable.CombineLatest(
this.Store.Resources,
this.Store.SelectedResourceNames,
(resources, selectedResourceNames) => (resources, selectedResourceNames)
).SubscribeAsync(async (values) => {
var (resources, selectedResourceNames) = values;
this.OnStateChanged(_ =>
{
this.Resources = resources;
this.SelectedResourceNames = selectedResourceNames;
});
if (this.CheckboxAll.HasValue)
{
if (selectedResourceNames.Count == 0)
{
await this.JsInterop.SetCheckboxStateAsync(this.CheckboxAll.Value, CheckboxState.Unchecked);
}
else if (selectedResourceNames.Count == (resources?.Count ?? 0))
{
await this.JsInterop.SetCheckboxStateAsync(this.CheckboxAll.Value, CheckboxState.Checked);
}
else
{
await this.JsInterop.SetCheckboxStateAsync(this.CheckboxAll.Value, CheckboxState.Indeterminate);
}
}
}, cancellationToken: this.CancellationTokenSource.Token);
this.Store.ActiveResourceName.Subscribe(value => this.OnStateChanged(_ => this.activeResourceName = value), token: this.CancellationTokenSource.Token);
this.Store.SearchTerm.Subscribe(value => this.OnStateChanged(_ => this.SearchTerm = value), token: this.CancellationTokenSource.Token);
this.Store.Loading.Subscribe(value => this.OnStateChanged(_ => this.Loading = value), token: this.CancellationTokenSource.Token);
this.Store.Definition.SubscribeAsync(async definition =>
{
if (this.Definition != definition)
{
this.Definition = definition;
if (this.Definition != null && this.MonacoInterop != null)
{
await this.MonacoInterop.AddValidationSchemaAsync(this.Serializer.SerializeToText(this.Definition.Spec.Versions.First().Schema.OpenAPIV3Schema ?? new JsonSchemaBuilder().Build()), $"https://synapse.io/schemas/{typeof(TResource).Name.ToLower()}.json", $"{typeof(TResource).Name.ToLower()}*").ConfigureAwait(false);
}
}
}, cancellationToken: this.CancellationTokenSource.Token);
this.Store.ActiveResource.SubscribeAsync(async resource =>
{
if (resource != null)
{
await this.OnShowResourceDetailsAsync(resource);
}
}, cancellationToken: CancellationTokenSource.Token);
await base.OnInitializedAsync().ConfigureAwait(false);
}
/// <inheritdoc/>
protected override async Task OnParametersSetAsync()
{
if (!string.IsNullOrEmpty(Name) && Name != activeResourceName)
{
Store.SetActiveResourceName(Name);
}
await base.OnParametersSetAsync();
}
/// <summary>
/// Handles search input value changes
/// </summary>
/// <param name="e">the <see cref="ChangeEventArgs"/> to handle</param>
protected void OnSearchInput(ChangeEventArgs e)
{
this.Store.SetSearchTerm(e.Value?.ToString());
}
/// <summary>
/// Handles the deletion of the targeted <see cref="Resource"/>
/// </summary>
/// <param name="resource">The <see cref="Resource"/> to delete</param>
protected async Task OnDeleteResourceAsync(TResource resource)
{
if (this.Dialog == null) return;
var confirmation = await this.Dialog.ShowAsync(
title: $"Are you sure you want to delete '{resource.Metadata.Name}'?",
message1: $"The {typeof(TResource).Name.ToLower()} will be permanently deleted. Are you sure you want to proceed ?",
confirmDialogOptions: new ConfirmDialogOptions()
{
YesButtonColor = ButtonColor.Danger,
YesButtonText = "Delete",
NoButtonText = "Abort",
IsVerticallyCentered = true
}
);
if (!confirmation) return;
await this.Store.DeleteResourceAsync(resource);
}
/// <summary>
/// Handles the deletion of the selected <see cref="Resource"/>s
/// </summary>
protected async Task OnDeleteSelectedResourcesAsync()
{
if (this.Dialog == null) return;
if (this.SelectedResourceNames.Count == 0) return;
var confirmation = await this.Dialog.ShowAsync(
title: $"Are you sure you want to delete {SelectedResourceNames.Count} resource{(SelectedResourceNames.Count > 1 ? "s" : "")}?",
message1: $"The resource{(SelectedResourceNames.Count > 1 ? "s" : "")} will be permanently deleted. Are you sure you want to proceed ?",
confirmDialogOptions: new ConfirmDialogOptions()
{
YesButtonColor = ButtonColor.Danger,
YesButtonText = "Delete",
NoButtonText = "Abort",
IsVerticallyCentered = true
}
);
if (!confirmation) return;
await this.Store.DeleteSelectedResourcesAsync();
}
/// <summary>
/// Opens the targeted <see cref="Resource"/>'s details
/// </summary>
/// <param name="resource">The <see cref="Resource"/> to show the details for</param>
protected virtual Task OnShowResourceDetailsAsync(TResource resource)
{
if (this.DetailsOffCanvas == null) return Task.CompletedTask;
var parameters = new Dictionary<string, object>
{
{ nameof(ResourceDetails<TResource>.Resource), resource }
};
return this.DetailsOffCanvas.ShowAsync<ResourceDetails<TResource>>(title: typeof(TResource).Name + " details", parameters: parameters);
}
/// <summary>
/// Opens the targeted <see cref="Resource"/>'s edition
/// </summary>
/// <param name="resource">The <see cref="Resource"/> to edit</param>
protected virtual async Task OnShowResourceEditorAsync(TResource? resource = null)
{
if (this.EditorOffCanvas == null) return;
var parameters = new Dictionary<string, object>
{
{ nameof(ResourceEditor<TResource>.Resource), resource! }
};
string actionType = resource == null ? "creation" : "edition";
await this.EditorOffCanvas.ShowAsync<ResourceEditor<TResource>>(title: typeof(TResource).Name + " " + actionType);
await this.EditorOffCanvas.ShowAsync<ResourceEditor<TResource>>(title: typeof(TResource).Name + " " + actionType, parameters: parameters);
}
}
/// <summary>
/// Represents the base class for all components used to manage <see cref="IResource"/>s
/// </summary>
/// <typeparam name="TComponent">The type of component inheriting the <see cref="ResourceManagementComponent{TComponent, TStore, TResource}"/></typeparam>
/// <typeparam name="TStore">The type of <see cref="ResourceManagementComponentStoreBase{TResource}"/> to use</typeparam>
/// <typeparam name="TResource">The type of <see cref="IResource"/> to manage</typeparam>
public abstract class ResourceManagementComponent<TComponent, TStore, TResource>
: ResourceManagementComponent<TComponent, TStore, ResourceManagementComponentState<TResource>, TResource>
where TComponent : ResourceManagementComponent<TComponent, TStore, TResource>
where TStore : ResourceManagementComponentStoreBase<TResource>
where TResource : Resource, new()
{
}