Added the Refresh All button to invalidate caches and force requests to be sent again
This commit is contained in:
50
src/features/opportunities/refresh-all.js
Normal file
50
src/features/opportunities/refresh-all.js
Normal file
@@ -0,0 +1,50 @@
|
||||
function createRefreshAll({ messageTypes, sendRuntimeMessage, activityCache, resetRelatedData,
|
||||
opportunitiesQueryState, productsContentState, consumptionPlansState, consumptionTrendState,
|
||||
aiSummaryState, reload, onFailure, notify, createRefreshIcon, environment }) {
|
||||
let pending = null;
|
||||
function refresh(period) {
|
||||
if (pending) return pending;
|
||||
opportunitiesQueryState.periodRequestVersion += 1;
|
||||
productsContentState.currentProductsRequestId = "";
|
||||
productsContentState.opptyProductsByNumber = new Map();
|
||||
productsContentState.expandedProductOptyNumbers = new Set();
|
||||
productsContentState.productsLastCachedAt = 0;
|
||||
productsContentState.productsProgress = { state: "idle", completed: 0, total: 0, failed: 0, cacheHits: 0 };
|
||||
consumptionPlansState.accountPlansRequestState = new Map();
|
||||
consumptionTrendState.consumptionTrendCache = new Map();
|
||||
consumptionTrendState.consumptionTrendWindowOffsets = new Map();
|
||||
consumptionTrendState.consumptionTrendSeriesFilters = new Map();
|
||||
aiSummaryState.aiSummariesByOptyNumber = new Map();
|
||||
resetRelatedData();
|
||||
pending = (async () => {
|
||||
await activityCache.invalidate();
|
||||
const response = await sendRuntimeMessage({ type: messageTypes.invalidateProductsCache });
|
||||
if (!response?.ok) throw new Error(response?.error || "Unable to clear product cache.");
|
||||
await reload(period);
|
||||
})().catch(error => { onFailure(error); throw error; }).finally(() => { pending = null; });
|
||||
return pending;
|
||||
}
|
||||
function createButton(getPeriod) {
|
||||
const button = environment.document.createElement("button");
|
||||
button.type = "button";
|
||||
button.className = "opportunities-extension-icon-button opportunities-extension-refresh-all";
|
||||
button.append(createRefreshIcon());
|
||||
button.title = "Refresh all";
|
||||
button.setAttribute("aria-label", "Refresh all");
|
||||
button.addEventListener("click", async () => {
|
||||
if (button.disabled) return;
|
||||
button.disabled = true;
|
||||
button.title = "Refreshing...";
|
||||
button.setAttribute("aria-busy", "true");
|
||||
try { await refresh(getPeriod()); }
|
||||
catch (error) { notify(error.message || "Unable to refresh cached data.", true); }
|
||||
finally {
|
||||
button.disabled = false;
|
||||
button.title = "Refresh all";
|
||||
button.setAttribute("aria-busy", "false");
|
||||
}
|
||||
});
|
||||
return button;
|
||||
}
|
||||
return { refresh, createButton };
|
||||
}
|
||||
Reference in New Issue
Block a user