Skip to content

Commit

Permalink
fix(ViewTypes.js): Add a fallback widget in case of undefined widgets
Browse files Browse the repository at this point in the history
This fix addresses a potential issue where a widget created on a newer
version of argus will cause an error on an older one when it is loaded
from the database.
  • Loading branch information
k0machi committed Nov 19, 2024
1 parent f8ff3f0 commit 34b5b2b
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 4 deletions.
66 changes: 63 additions & 3 deletions frontend/AdminPanel/ViewsManager.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
import Fa from "svelte-fa";
import { faPlus, faTrash } from "@fortawesome/free-solid-svg-icons";
import ViewWidget from "./ViewWidget.svelte";
import { ADD_ALL_ID, Widget } from "../Common/ViewTypes";
import { ADD_ALL_ID, Widget, WIDGET_TYPES } from "../Common/ViewTypes";
import queryString from "query-string";
import * as urlSlug from "url-slug";
import Select from "svelte-select";
import ViewSelectItem from "./ViewSelectItem.svelte";
import { titleCase } from "../Common/TextUtils";
import ViewListItem from "./ViewListItem.svelte";
import ModalWindow from "../Common/ModalWindow.svelte";
let allViews = [];
Expand All @@ -32,6 +33,11 @@
let lockForm = false;
let editingExistingView = false;
let testSearcherValue;
let errorPopup = false;
let errorTitle = "";
let errorMessage = "";
let errorCallback;
const fetchAllViews = async function() {
try {
Expand Down Expand Up @@ -62,10 +68,34 @@
}
};
const handleViewUpdateRequest = function(e) {
/**
*
* @param {{type: string}[]} viewWidgets
*/
const checkIfViewSupported = function(viewWidgets) {
for (const widget of viewWidgets) {
if (!WIDGET_TYPES[widget.type]) {
return false;
}
}
return true;
};
const handleViewUpdateRequest = function(e, force = false) {
resetForm();
let viewForUpdate = e.detail;
newWidgets = JSON.parse(viewForUpdate.widget_settings);
let widgets = JSON.parse(viewForUpdate.widget_settings);
if (!checkIfViewSupported(widgets) && !force) {
errorTitle = "Unsupported widget found";
errorMessage = "This view contains unsupported widgets. Editing said view will result in the loss of those widgets.";
errorPopup = true;
errorCallback = handleViewUpdateRequest.bind(undefined, e, true);
return;
} else if (force) {
widgets = widgets.filter(w => WIDGET_TYPES[w.type]);
widgets.forEach((w, idx) => w.position = idx + 1);
}
newWidgets = widgets;
newView = {
id: viewForUpdate.id,
name: viewForUpdate.name,
Expand Down Expand Up @@ -333,6 +363,36 @@
});
</script>
{#if errorPopup}
<ModalWindow on:modalClose={() => {
errorTitle = "";
errorMessage = "";
errorPopup = false;
}}>
<div slot="title">
{errorTitle}
</div>
<div slot="body">
<p>{errorMessage}</p>
<div>
<button class="btn btn-danger" on:click={() => {
errorTitle = "";
errorMessage = "";
errorPopup = false;
errorCallback();
errorCallback = undefined;
}}>Edit Anyway</button>
<button class="btn btn-secondary" on:click={() => {
errorTitle = "";
errorMessage = "";
errorPopup = false;
errorCallback = undefined;
}}>Cancel</button>
</div>
</div>
</ModalWindow>
{/if}
<div class="bg-white rounded p-2 shadow-sm my-2">
<div>
<h4>View Manager</h4>
Expand Down
8 changes: 8 additions & 0 deletions frontend/Common/ViewTypes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ViewGithubIssues from "../Views/Widgets/ViewGithubIssues.svelte";
import ViewReleaseStats from "../Views/Widgets/ViewReleaseStats.svelte";
import ViewTestDashboard from "../Views/Widgets/ViewTestDashboard.svelte";
import ViewUnsupportedPlaceholder from "../Views/Widgets/ViewUnsupportedPlaceholder.svelte";
import CheckValue from "../Views/WidgetSettingTypes/CheckValue.svelte";
import MultiSelectValue from "../Views/WidgetSettingTypes/MultiSelectValue.svelte";
import StringValue from "../Views/WidgetSettingTypes/StringValue.svelte";
Expand All @@ -17,6 +18,13 @@ export class Widget {


export const WIDGET_TYPES = {
UNSUPPORTED: {
type: ViewUnsupportedPlaceholder,
hidden: true,
friendlyName: "Dummy widget",
settingDefinitions: {
}
},
testDashboard: {
type: ViewTestDashboard,
friendlyName: "Test Dashboard",
Expand Down
2 changes: 1 addition & 1 deletion frontend/Views/ViewDashboard.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
{#each view.widget_settings as widget}
<div class="mb-2">
<svelte:component
this={WIDGET_TYPES[widget.type].type}
this={WIDGET_TYPES[widget.type]?.type ?? WIDGET_TYPES.UNSUPPORTED.type}
dashboardObject={view}
dashboardObjectType="view"
settings={widget.settings}
Expand Down
3 changes: 3 additions & 0 deletions frontend/Views/Widgets/ViewUnsupportedPlaceholder.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="alert alert-warning p-2 m-2 text-center">
This widget is not supported by this version of argus.
</div>

0 comments on commit 34b5b2b

Please sign in to comment.