2022-10-10 12:50:21 +00:00
|
|
|
import { Action, AppViewState } from "./types.ts";
|
2022-03-20 08:56:28 +00:00
|
|
|
|
|
|
|
export default function reducer(
|
|
|
|
state: AppViewState,
|
2022-10-10 12:50:21 +00:00
|
|
|
action: Action,
|
2022-03-20 08:56:28 +00:00
|
|
|
): AppViewState {
|
|
|
|
// console.log("Got action", action);
|
|
|
|
switch (action.type) {
|
2022-09-06 14:21:33 +00:00
|
|
|
case "page-loading":
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
isLoading: true,
|
|
|
|
currentPage: action.name,
|
|
|
|
};
|
2022-03-20 08:56:28 +00:00
|
|
|
case "page-loaded":
|
|
|
|
return {
|
|
|
|
...state,
|
2022-09-06 14:21:33 +00:00
|
|
|
isLoading: false,
|
2023-05-23 18:53:53 +00:00
|
|
|
allPages: state.allPages.map((pageMeta) =>
|
|
|
|
pageMeta.name === action.meta.name
|
|
|
|
? { ...pageMeta, lastOpened: Date.now() }
|
|
|
|
: pageMeta
|
2022-03-20 08:56:28 +00:00
|
|
|
),
|
2022-05-17 09:53:17 +00:00
|
|
|
currentPage: action.meta.name,
|
2023-01-16 10:28:59 +00:00
|
|
|
currentPageMeta: action.meta,
|
2022-03-20 08:56:28 +00:00
|
|
|
};
|
2022-03-31 12:28:07 +00:00
|
|
|
case "page-changed":
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
unsavedChanges: true,
|
|
|
|
};
|
|
|
|
case "page-saved":
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
unsavedChanges: false,
|
|
|
|
};
|
2023-05-23 18:53:53 +00:00
|
|
|
case "sync-change":
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
synced: action.synced,
|
|
|
|
};
|
2022-03-20 08:56:28 +00:00
|
|
|
case "start-navigate":
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
showPageNavigator: true,
|
2023-06-13 18:47:05 +00:00
|
|
|
showCommandPalette: false,
|
|
|
|
showFilterBox: false,
|
2022-03-20 08:56:28 +00:00
|
|
|
};
|
|
|
|
case "stop-navigate":
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
showPageNavigator: false,
|
|
|
|
};
|
2022-10-15 17:02:56 +00:00
|
|
|
case "pages-listed": {
|
2022-08-01 13:06:32 +00:00
|
|
|
// 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]),
|
|
|
|
);
|
|
|
|
for (const pageMeta of action.pages) {
|
|
|
|
const oldPageMetaItem = oldPageMeta.get(pageMeta.name);
|
2022-08-01 13:06:32 +00:00
|
|
|
if (oldPageMetaItem && oldPageMetaItem.lastOpened) {
|
|
|
|
pageMeta.lastOpened = oldPageMetaItem.lastOpened;
|
|
|
|
}
|
|
|
|
}
|
2022-03-20 08:56:28 +00:00
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
allPages: action.pages,
|
|
|
|
};
|
2022-10-15 17:02:56 +00:00
|
|
|
}
|
|
|
|
case "show-palette": {
|
2022-04-21 09:46:33 +00:00
|
|
|
return {
|
|
|
|
...state,
|
2022-03-20 08:56:28 +00:00
|
|
|
showCommandPalette: true,
|
2023-06-13 18:47:05 +00:00
|
|
|
showPageNavigator: false,
|
|
|
|
showFilterBox: false,
|
2023-01-04 15:37:09 +00:00
|
|
|
showCommandPaletteContext: action.context,
|
2022-03-20 08:56:28 +00:00
|
|
|
};
|
2022-10-15 17:02:56 +00:00
|
|
|
}
|
2022-03-20 08:56:28 +00:00
|
|
|
case "hide-palette":
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
showCommandPalette: false,
|
2023-01-04 15:37:09 +00:00
|
|
|
showCommandPaletteContext: undefined,
|
2022-03-20 08:56:28 +00:00
|
|
|
};
|
2022-05-16 13:09:36 +00:00
|
|
|
case "command-run":
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
recentCommands: state.recentCommands.set(action.command, new Date()),
|
|
|
|
};
|
2022-03-20 08:56:28 +00:00
|
|
|
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],
|
2022-03-20 08:56:28 +00:00
|
|
|
};
|
|
|
|
case "dismiss-notification":
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
notifications: state.notifications.filter((n) => n.id !== action.id),
|
|
|
|
};
|
2022-09-30 14:59:57 +00:00
|
|
|
case "show-panel":
|
2022-03-28 13:25:05 +00:00
|
|
|
return {
|
|
|
|
...state,
|
2022-09-30 14:59:57 +00:00
|
|
|
panels: {
|
|
|
|
...state.panels,
|
|
|
|
[action.id]: action.config,
|
|
|
|
},
|
2022-03-28 13:25:05 +00:00
|
|
|
};
|
2022-09-30 14:59:57 +00:00
|
|
|
case "hide-panel":
|
2022-03-28 13:25:05 +00:00
|
|
|
return {
|
|
|
|
...state,
|
2022-09-30 14:59:57 +00:00
|
|
|
panels: {
|
|
|
|
...state.panels,
|
|
|
|
[action.id]: {},
|
|
|
|
},
|
2022-04-26 17:04:36 +00:00
|
|
|
};
|
2022-09-30 14:59:57 +00:00
|
|
|
|
2022-04-13 12:46:52 +00:00
|
|
|
case "show-filterbox":
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
showFilterBox: true,
|
|
|
|
filterBoxOnSelect: action.onSelect,
|
|
|
|
filterBoxPlaceHolder: action.placeHolder,
|
|
|
|
filterBoxOptions: action.options,
|
2022-04-21 09:46:33 +00:00
|
|
|
filterBoxLabel: action.label,
|
2022-04-13 12:46:52 +00:00
|
|
|
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,
|
|
|
|
};
|
2022-12-21 13:55:24 +00:00
|
|
|
case "set-ui-option":
|
2022-09-16 12:26:47 +00:00
|
|
|
return {
|
|
|
|
...state,
|
2022-12-21 13:55:24 +00:00
|
|
|
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,
|
|
|
|
};
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|
|
|
|
return state;
|
|
|
|
}
|