some cleanup

This commit is contained in:
Paul Fitzpatrick
2023-10-06 21:38:37 -04:00
parent fd1734de69
commit 4f3d0d41a0
18 changed files with 305 additions and 438 deletions

View File

@@ -31,7 +31,13 @@ export interface ICustomWidget {
*/
renderAfterReady?: boolean;
fromPlugin?: string;
/**
* If the widget came from a plugin, we track that here.
*/
source?: {
pluginId: string;
name: string;
};
}
/**
@@ -64,64 +70,20 @@ export function isSatisfied(current: AccessLevel, minimum: AccessLevel) {
return ordered(current) >= ordered(minimum);
}
/**
* Find the best match for a widgetId/pluginId combination among the
* given widgets. An exact widgetId match is required. A pluginId match
* is preferred but not required.
*/
export function matchWidget(widgets: ICustomWidget[], options: {
widgetId?: string,
widgetId: string,
pluginId?: string,
}): ICustomWidget|undefined {
console.log("MATCHING", {
widgets,
options,
});
const prefs = sortBy(widgets, (w) => {
return [w.widgetId !== options.widgetId,
(w.fromPlugin||'') !== options.pluginId]
(w.source?.pluginId||'') !== options.pluginId]
});
if (prefs.length === 0) { return; }
if (options.widgetId && prefs[0].widgetId !== options.widgetId) {
return;
}
console.log("ORDERED", prefs);
console.log("MATCHED", prefs[0]);
if (prefs[0].widgetId !== options.widgetId) { return };
return prefs[0];
}
export function filterWidgets(widgets: ICustomWidget[], options: {
preferPlugin?: boolean,
keepWidgetIdUnique?: boolean,
}) {
const folders = new Map<string, ICustomWidget[]>();
for (const widget of widgets) {
const widgetId = widget.widgetId;
if (!folders.has(widgetId)) { folders.set(widgetId, []); }
const widgetFolder = folders.get(widgetId)!;
widgetFolder.push(widget);
}
let finalResults: ICustomWidget[] = widgets;
if (options.preferPlugin !== undefined) {
const results = [];
const seen = new Set<string>();
for (const widget of widgets) {
const folder = folders.get(widget.widgetId)!;
if (folder.length === 1) {
results.push(widget);
continue;
}
if (seen.has(widget.widgetId)) { continue; }
seen.add(widget.widgetId);
const folderSorted = sortBy(folder, (w) => Boolean(w.fromPlugin) !== options.preferPlugin);
results.push(folderSorted[0]!);
}
finalResults = results;
}
if (options.keepWidgetIdUnique) {
const results = [];
const seen = new Set<string>();
for (const widget of widgets) {
if (seen.has(widget.widgetId)) { continue; }
seen.add(widget.widgetId);
results.push(widget);
}
finalResults = results;
}
return finalResults;
}