1
0
silverbullet/packages/web/reducer.ts

140 lines
3.1 KiB
TypeScript
Raw Normal View History

2022-04-05 15:02:17 +00:00
import { Action, AppViewState } from "./types";
export default function reducer(
state: AppViewState,
action: Action
): AppViewState {
// console.log("Got action", action);
switch (action.type) {
case "page-loaded":
return {
...state,
allPages: new Set(
2022-03-31 15:25:34 +00:00
[...state.allPages].map((pageMeta) =>
pageMeta.name === action.name
? { ...pageMeta, lastOpened: Date.now() }
: pageMeta
)
),
currentPage: action.name,
};
case "page-changed":
return {
...state,
unsavedChanges: true,
};
case "page-saved":
return {
...state,
unsavedChanges: false,
};
case "start-navigate":
return {
...state,
showPageNavigator: true,
};
case "stop-navigate":
return {
...state,
showPageNavigator: false,
};
case "pages-listed":
return {
...state,
allPages: action.pages,
};
case "show-palette":
let commands = new Map(state.commands);
for (let [k, v] of state.commands.entries()) {
if (
v.command.contexts &&
(!action.context || !v.command.contexts.includes(action.context))
) {
commands.delete(k);
}
}
return {
...state,
commands,
showCommandPalette: true,
};
case "hide-palette":
return {
...state,
showCommandPalette: false,
};
case "update-commands":
return {
...state,
commands: action.commands,
2022-05-06 16:55:04 +00:00
actionButtons: action.actionButtons,
};
case "show-notification":
return {
...state,
notifications: [action.notification, ...state.notifications],
};
case "dismiss-notification":
return {
...state,
notifications: state.notifications.filter((n) => n.id !== action.id),
};
2022-03-28 13:25:05 +00:00
case "show-rhs":
return {
...state,
2022-04-04 16:33:13 +00:00
showRHS: action.flex,
2022-03-28 13:25:05 +00:00
rhsHTML: action.html,
};
case "hide-rhs":
return {
...state,
2022-04-04 16:33:13 +00:00
showRHS: 0,
2022-03-28 13:25:05 +00:00
rhsHTML: "",
};
2022-04-04 16:33:13 +00:00
case "show-lhs":
return {
...state,
showLHS: action.flex,
lhsHTML: action.html,
};
case "hide-lhs":
return {
...state,
showLHS: 0,
lhsHTML: "",
};
2022-04-26 17:04:36 +00:00
case "show-bhs":
return {
...state,
showBHS: action.flex,
bhsHTML: action.html,
};
case "hide-bhs":
return {
...state,
showBHS: 0,
bhsHTML: "",
};
case "show-filterbox":
return {
...state,
showFilterBox: true,
filterBoxOnSelect: action.onSelect,
filterBoxPlaceHolder: action.placeHolder,
filterBoxOptions: action.options,
filterBoxLabel: action.label,
filterBoxHelpText: action.helpText,
};
case "hide-filterbox":
return {
...state,
showFilterBox: false,
filterBoxOnSelect: () => {},
filterBoxPlaceHolder: "",
filterBoxOptions: [],
filterBoxHelpText: "",
};
}
return state;
}