qualiteer/src/ctx/StoreContext.jsx

150 lines
3.7 KiB
React
Raw Normal View History

2022-05-05 12:35:47 +00:00
import React, { useReducer, createContext, useMemo } from "react";
2022-05-23 00:24:21 +00:00
2022-05-05 12:35:47 +00:00
const StoreContext = createContext();
const ACTIONS = {
UPDATE: "u",
};
2022-08-05 13:03:48 +00:00
const pipelineMappingsMock = [["primary", "secondary1","tertiary1"],
["primary", "secondary1", "tertiary2"],
["primary", "secondary2", "tertiary3"]];
2022-06-22 00:47:19 +00:00
const silencedMock = new Array(10).fill(0).map((v, i) => ({
name: `Test${i + 1}`,
class: `SomeTestClass${i % 2 ? i - 1 : i / 2}`,
method: "someMethod",
id: Date.now(),
silencedUntil: `2022-05-10T16:${2 + i}:33.810Z`,
}));
const catalogMock = new Array(10).fill(0).map((v, i) => ({
name: `Test${i + 1}`,
class: `SomeTestClass${i % 2 ? i - 1 : i / 2}`,
repo: "Repo",
isCompound: i % 5 ? false : true,
type: i % 3 ? "api" : "ui",
}));
2022-07-06 20:20:27 +00:00
const failingMock = new Array(12).fill(0).map((v, i) => ({
2022-05-23 00:24:21 +00:00
class: `SomeTestClass${i % 2 ? i - 1 : i / 2}`,
name: `TestThatDoesOneThing${i + 1}`,
2022-07-06 20:20:27 +00:00
timestamp: `2022-05-10T16:0${i < 10 ? i : i - 10}:33.810Z`,
2022-06-22 00:47:19 +00:00
method: `SomeMethod`,
2022-07-06 20:20:27 +00:00
silencedUntil: i % 4 ? null : `2022-05-10T16:0${i}:33.810Z`,
2022-05-23 00:24:21 +00:00
frequency: "1hour",
type: i % 3 ? "api" : "ui",
dailyFails: i + 1,
2022-07-06 20:20:27 +00:00
screenshot: "https://picsum.photos/1920/1080",
2022-05-23 00:24:21 +00:00
recentResults: [1, 0, 0, 1, 0],
isCompound: i % 5 ? false : true,
failedMessage: `Some Test FailureMessage ${i}`,
jobStatus: (() => {
switch (i) {
case 1:
2022-08-05 13:03:48 +00:00
return "o";
2022-05-23 00:24:21 +00:00
case 3:
2022-08-05 13:03:48 +00:00
return "e";
2022-05-23 00:24:21 +00:00
case 4:
2022-08-05 13:03:48 +00:00
return "p";
2022-05-23 00:24:21 +00:00
case 5:
2022-08-05 13:03:48 +00:00
return "a";
2022-05-23 00:24:21 +00:00
case 6:
2022-08-05 13:03:48 +00:00
return "c";
2022-05-23 00:24:21 +00:00
case 8:
2022-08-05 13:03:48 +00:00
return "q";
2022-05-23 00:24:21 +00:00
default:
return null;
}
})(),
}));
2022-08-05 13:03:48 +00:00
const localStorage = {setItem: ()=>{}, getItem: ()=>{}};
const localSettings = localStorage.getItem("settings");
const defaultSettings = {
focusJob: true,
simplifiedControls: true,
logAppDetails: true,
defaultRegion: "us",
defaultPage: "failing",
};
const settings = localSettings ? JSON.parse(localSettings) : defaultSettings;
const settingsKeys = Object.keys(defaultSettings);
2022-05-17 12:32:04 +00:00
const initialState = {
2022-07-06 20:20:27 +00:00
pages: ["failing", "alerting", "jobs", "catalog", "settings", "about"],
2022-05-17 12:32:04 +00:00
intervals: [],
2022-06-22 00:47:19 +00:00
catalog: catalogMock,
failing: failingMock,
silenced: silencedMock,
2022-08-05 13:03:48 +00:00
pipelineMappings: pipelineMappingsMock,
2022-05-17 12:32:04 +00:00
regions: [],
2022-05-23 00:24:21 +00:00
catalogSearch: "",
2022-08-05 13:03:48 +00:00
...settings,
};
const settingsUpdater = (oldState, storeUpdate) => {
const settingsToUpdate = {};
for (var k of settingsKeys) {
settingsToUpdate[k] = oldState[k];
if (storeUpdate[k] === undefined) continue;
settingsToUpdate[k] = storeUpdate[k];
}
localStorage.setItem("settings", JSON.stringify(settingsToUpdate));
2022-05-17 12:32:04 +00:00
};
2022-05-05 12:35:47 +00:00
const reducer = (state, action) => {
2022-05-17 12:32:04 +00:00
const { store } = action;
2022-05-05 12:35:47 +00:00
// Actions
switch (action.type) {
case ACTIONS.UPDATE:
2022-08-05 13:03:48 +00:00
settingsUpdater(state, store);
2022-05-05 12:35:47 +00:00
return { ...state, ...store };
default:
return state;
}
};
export const StoreProvider = ({ children }) => {
const [state, dispatch] = useReducer(reducer, initialState);
2022-06-22 00:47:19 +00:00
function silenceRequest(silenceInfo) {
2022-07-06 20:20:27 +00:00
const req = {
name: silenceInfo.name ?? "*",
class: silenceInfo.class ?? "*",
method: silenceInfo.method ?? "*",
silencedUntil: silenceInfo.silencedUntil,
};
2022-06-22 00:47:19 +00:00
console.log("Would upsert silence", req);
}
2022-07-06 20:20:27 +00:00
function removeFailure(failure) {
const req = {
name: failure.name,
class: failure.class,
};
console.log("Would remove failure", req);
}
2022-05-05 12:35:47 +00:00
const context = {
state,
dispatch,
2022-06-22 00:47:19 +00:00
silenceRequest,
2022-07-06 20:20:27 +00:00
removeFailure,
2022-05-17 12:32:04 +00:00
updateStore: (store) => dispatch({ type: ACTIONS.UPDATE, store }),
2022-05-05 12:35:47 +00:00
};
const contextValue = useMemo(() => context, [state, dispatch]);
return (
<StoreContext.Provider value={contextValue}>
{children}
</StoreContext.Provider>
);
};
export default StoreContext;