1
0
silverbullet/web/reducer.ts

196 lines
4.8 KiB
TypeScript
Raw Normal View History

import { Action, AppViewState } from "./types.ts";
export default function reducer(
state: AppViewState,
action: Action,
): AppViewState {
// console.log("Got action", action);
switch (action.type) {
case "page-loading":
return {
...state,
isLoading: true,
currentPage: action.name,
2023-12-21 17:37:50 +00:00
panels: state.currentPage === action.name ? state.panels : {
2023-12-19 16:20:47 +00:00
...state.panels,
// Hide these by default to avoid flickering
top: {},
bottom: {},
},
};
case "page-loaded":
return {
...state,
isLoading: false,
allPages: state.allPages.map((pageMeta) =>
pageMeta.name === action.meta.name
? { ...pageMeta, lastOpened: Date.now() }
: pageMeta
),
2022-05-17 09:53:17 +00:00
currentPage: action.meta.name,
2023-01-16 10:28:59 +00:00
currentPageMeta: action.meta,
};
case "page-changed":
return {
...state,
unsavedChanges: true,
};
case "page-saved":
return {
...state,
unsavedChanges: false,
};
case "sync-change":
return {
...state,
2023-08-16 09:40:31 +00:00
syncFailures: action.syncSuccess ? 0 : state.syncFailures + 1,
};
2023-12-22 14:55:50 +00:00
case "update-page-list": {
// Let's move over any "lastOpened" times to the "allPages" list
2022-10-15 17:02:56 +00:00
const oldPageMeta = new Map(
[...state.allPages].map((pm) => [pm.name, pm]),
);
2023-12-22 14:55:50 +00:00
for (const pageMeta of action.allPages) {
2022-10-15 17:02:56 +00:00
const oldPageMetaItem = oldPageMeta.get(pageMeta.name);
if (oldPageMetaItem && oldPageMetaItem.lastOpened) {
pageMeta.lastOpened = oldPageMetaItem.lastOpened;
}
}
2023-12-22 14:55:50 +00:00
return {
...state,
allPages: action.allPages,
};
}
case "start-navigate": {
return {
...state,
2023-12-21 17:37:50 +00:00
showPageNavigator: true,
showCommandPalette: false,
showFilterBox: false,
};
2022-10-15 17:02:56 +00:00
}
2023-12-21 17:37:50 +00:00
case "stop-navigate":
return {
...state,
showPageNavigator: false,
};
2022-10-15 17:02:56 +00:00
case "show-palette": {
return {
...state,
showCommandPalette: true,
showPageNavigator: false,
showFilterBox: false,
showCommandPaletteContext: action.context,
};
2022-10-15 17:02:56 +00:00
}
case "hide-palette":
return {
...state,
showCommandPalette: false,
showCommandPaletteContext: undefined,
};
2022-05-16 13:09:36 +00:00
case "command-run":
return {
...state,
recentCommands: state.recentCommands.set(action.command, new Date()),
};
case "update-commands":
return {
...state,
commands: action.commands,
};
case "show-notification":
return {
...state,
2022-05-09 12:59:12 +00:00
notifications: [...state.notifications, action.notification],
};
case "dismiss-notification":
return {
...state,
notifications: state.notifications.filter((n) => n.id !== action.id),
};
case "show-panel":
2022-03-28 13:25:05 +00:00
return {
...state,
panels: {
...state.panels,
[action.id]: action.config,
},
2022-03-28 13:25:05 +00:00
};
case "hide-panel":
2022-03-28 13:25:05 +00:00
return {
...state,
panels: {
...state.panels,
[action.id]: {},
},
2022-04-26 17:04:36 +00:00
};
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: "",
};
2022-12-21 15:08:51 +00:00
case "show-prompt":
return {
...state,
showPrompt: true,
promptDefaultValue: action.defaultValue,
promptMessage: action.message,
promptCallback: action.callback,
};
case "hide-prompt":
return {
...state,
showPrompt: false,
promptDefaultValue: undefined,
promptMessage: undefined,
promptCallback: undefined,
};
case "show-confirm":
return {
...state,
showConfirm: true,
confirmMessage: action.message,
confirmCallback: action.callback,
};
case "hide-confirm":
return {
...state,
showConfirm: false,
confirmMessage: undefined,
confirmCallback: undefined,
};
case "set-ui-option":
2022-09-16 12:26:47 +00:00
return {
...state,
uiOptions: {
...state.uiOptions,
[action.key]: action.value,
},
2022-09-16 12:26:47 +00:00
};
2023-06-14 18:58:08 +00:00
case "set-progress":
return {
...state,
progressPerc: action.progressPerc,
};
}
return state;
}