2024-07-13 19:56:00 +08:00
|
|
|
import { PageMeta } from "../plug-api/types.ts";
|
2024-02-09 04:12:23 +08:00
|
|
|
import { Action, AppViewState } from "../type/web.ts";
|
2024-07-13 19:56:00 +08:00
|
|
|
import { PageState } from "./navigator.ts";
|
2022-03-20 16:56:28 +08:00
|
|
|
|
|
|
|
export default function reducer(
|
|
|
|
state: AppViewState,
|
2022-10-10 20:50:21 +08:00
|
|
|
action: Action,
|
2022-03-20 16:56:28 +08:00
|
|
|
): AppViewState {
|
|
|
|
// console.log("Got action", action);
|
|
|
|
switch (action.type) {
|
2022-09-06 22:21:33 +08:00
|
|
|
case "page-loading":
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
isLoading: true,
|
|
|
|
currentPage: action.name,
|
2023-12-22 01:37:50 +08:00
|
|
|
panels: state.currentPage === action.name ? state.panels : {
|
2023-12-20 00:20:47 +08:00
|
|
|
...state.panels,
|
|
|
|
// Hide these by default to avoid flickering
|
|
|
|
top: {},
|
|
|
|
bottom: {},
|
|
|
|
},
|
2022-09-06 22:21:33 +08:00
|
|
|
};
|
2024-02-07 16:33:47 +08:00
|
|
|
case "page-loaded": {
|
2024-07-11 12:03:33 +08:00
|
|
|
const mouseDetected = window.matchMedia("(pointer:fine)").matches;
|
2024-07-13 19:56:00 +08:00
|
|
|
const pageMeta = state.allPages.find(p => p.name == action.meta.name);
|
|
|
|
const decor = state.settings.decorations?.filter(d => pageMeta?.tags?.some(t => d.tag === t));
|
|
|
|
if (decor && decor.length > 0) {
|
|
|
|
const mergedDecorations = decor.reduceRight((accumulator, el) => {
|
|
|
|
accumulator = {...accumulator, ...el};
|
|
|
|
return accumulator;
|
|
|
|
});
|
|
|
|
if (mergedDecorations) {
|
|
|
|
const { tag, ...currPageDecorations } = mergedDecorations;
|
|
|
|
action.meta.pageDecorations = currPageDecorations;
|
|
|
|
}
|
|
|
|
}
|
2022-03-20 16:56:28 +08:00
|
|
|
return {
|
|
|
|
...state,
|
2022-09-06 22:21:33 +08:00
|
|
|
isLoading: false,
|
2024-02-07 16:33:47 +08:00
|
|
|
isMobile: !mouseDetected,
|
2023-05-24 02:53:53 +08:00
|
|
|
allPages: state.allPages.map((pageMeta) =>
|
|
|
|
pageMeta.name === action.meta.name
|
|
|
|
? { ...pageMeta, lastOpened: Date.now() }
|
|
|
|
: pageMeta
|
2022-03-20 16:56:28 +08:00
|
|
|
),
|
2022-05-17 17:53:17 +08:00
|
|
|
currentPage: action.meta.name,
|
2023-01-16 18:28:59 +08:00
|
|
|
currentPageMeta: action.meta,
|
2022-03-20 16:56:28 +08:00
|
|
|
};
|
2024-02-07 16:33:47 +08:00
|
|
|
}
|
2022-03-31 20:28:07 +08:00
|
|
|
case "page-changed":
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
unsavedChanges: true,
|
|
|
|
};
|
|
|
|
case "page-saved":
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
unsavedChanges: false,
|
|
|
|
};
|
2023-05-24 02:53:53 +08:00
|
|
|
case "sync-change":
|
|
|
|
return {
|
|
|
|
...state,
|
2023-08-16 17:40:31 +08:00
|
|
|
syncFailures: action.syncSuccess ? 0 : state.syncFailures + 1,
|
2023-05-24 02:53:53 +08:00
|
|
|
};
|
2024-01-25 18:42:36 +08:00
|
|
|
case "settings-loaded":
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
settings: action.settings,
|
|
|
|
};
|
2023-12-22 22:55:50 +08:00
|
|
|
case "update-page-list": {
|
2022-08-01 21:06:32 +08:00
|
|
|
// Let's move over any "lastOpened" times to the "allPages" list
|
2022-10-16 01:02:56 +08:00
|
|
|
const oldPageMeta = new Map(
|
|
|
|
[...state.allPages].map((pm) => [pm.name, pm]),
|
|
|
|
);
|
2024-07-13 19:56:00 +08:00
|
|
|
let currPageMeta = oldPageMeta.get(state.currentPage!);
|
|
|
|
if (currPageMeta === undefined) {
|
|
|
|
currPageMeta = {} as PageMeta;
|
|
|
|
}
|
2023-12-22 22:55:50 +08:00
|
|
|
for (const pageMeta of action.allPages) {
|
2022-10-16 01:02:56 +08:00
|
|
|
const oldPageMetaItem = oldPageMeta.get(pageMeta.name);
|
2022-08-01 21:06:32 +08:00
|
|
|
if (oldPageMetaItem && oldPageMetaItem.lastOpened) {
|
|
|
|
pageMeta.lastOpened = oldPageMetaItem.lastOpened;
|
|
|
|
}
|
2024-07-13 19:56:00 +08:00
|
|
|
const decor = state.settings.decorations?.filter(d => pageMeta.tags?.some((t: any) => d.tag === t));
|
|
|
|
// Page can have multiple decorations applied via different tags, accumulate them.
|
|
|
|
// The decorations higher in the decorations list defined in SETTINGS gets
|
|
|
|
// higher precedence.
|
|
|
|
if (decor && decor.length > 0) {
|
|
|
|
const mergedDecorations = decor.reduceRight((accumulator, el) => {
|
|
|
|
accumulator = {...accumulator, ...el};
|
|
|
|
return accumulator;
|
|
|
|
});
|
|
|
|
if (mergedDecorations) {
|
|
|
|
const { tag, ...currPageDecorations} = mergedDecorations;
|
|
|
|
pageMeta.pageDecorations = currPageDecorations;
|
|
|
|
if (pageMeta.name === state.currentPage) {
|
|
|
|
currPageMeta!.pageDecorations = currPageDecorations;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-01 21:06:32 +08:00
|
|
|
}
|
2023-12-22 22:55:50 +08:00
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
allPages: action.allPages,
|
2024-07-13 19:56:00 +08:00
|
|
|
currentPageMeta: currPageMeta,
|
2023-12-22 22:55:50 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
case "start-navigate": {
|
2022-03-20 16:56:28 +08:00
|
|
|
return {
|
|
|
|
...state,
|
2023-12-22 01:37:50 +08:00
|
|
|
showPageNavigator: true,
|
2024-01-21 02:16:07 +08:00
|
|
|
pageNavigatorMode: action.mode,
|
2023-12-22 01:37:50 +08:00
|
|
|
showCommandPalette: false,
|
|
|
|
showFilterBox: false,
|
2022-03-20 16:56:28 +08:00
|
|
|
};
|
2022-10-16 01:02:56 +08:00
|
|
|
}
|
2023-12-22 01:37:50 +08:00
|
|
|
case "stop-navigate":
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
showPageNavigator: false,
|
|
|
|
};
|
|
|
|
|
2022-10-16 01:02:56 +08:00
|
|
|
case "show-palette": {
|
2022-04-21 17:46:33 +08:00
|
|
|
return {
|
|
|
|
...state,
|
2022-03-20 16:56:28 +08:00
|
|
|
showCommandPalette: true,
|
2023-06-14 02:47:05 +08:00
|
|
|
showPageNavigator: false,
|
|
|
|
showFilterBox: false,
|
2023-01-04 23:37:09 +08:00
|
|
|
showCommandPaletteContext: action.context,
|
2022-03-20 16:56:28 +08:00
|
|
|
};
|
2022-10-16 01:02:56 +08:00
|
|
|
}
|
2022-03-20 16:56:28 +08:00
|
|
|
case "hide-palette":
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
showCommandPalette: false,
|
2023-01-04 23:37:09 +08:00
|
|
|
showCommandPaletteContext: undefined,
|
2022-03-20 16:56:28 +08:00
|
|
|
};
|
2022-05-16 21:09:36 +08:00
|
|
|
case "command-run":
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
recentCommands: state.recentCommands.set(action.command, new Date()),
|
|
|
|
};
|
2022-03-20 16:56:28 +08:00
|
|
|
case "update-commands":
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
commands: action.commands,
|
|
|
|
};
|
|
|
|
case "show-notification":
|
|
|
|
return {
|
|
|
|
...state,
|
2022-05-09 20:59:12 +08:00
|
|
|
notifications: [...state.notifications, action.notification],
|
2022-03-20 16:56:28 +08:00
|
|
|
};
|
|
|
|
case "dismiss-notification":
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
notifications: state.notifications.filter((n) => n.id !== action.id),
|
|
|
|
};
|
2022-09-30 22:59:57 +08:00
|
|
|
case "show-panel":
|
2022-03-28 21:25:05 +08:00
|
|
|
return {
|
|
|
|
...state,
|
2022-09-30 22:59:57 +08:00
|
|
|
panels: {
|
|
|
|
...state.panels,
|
|
|
|
[action.id]: action.config,
|
|
|
|
},
|
2022-03-28 21:25:05 +08:00
|
|
|
};
|
2022-09-30 22:59:57 +08:00
|
|
|
case "hide-panel":
|
2022-03-28 21:25:05 +08:00
|
|
|
return {
|
|
|
|
...state,
|
2022-09-30 22:59:57 +08:00
|
|
|
panels: {
|
|
|
|
...state.panels,
|
|
|
|
[action.id]: {},
|
|
|
|
},
|
2022-04-27 01:04:36 +08:00
|
|
|
};
|
2022-09-30 22:59:57 +08:00
|
|
|
|
2022-04-13 20:46:52 +08:00
|
|
|
case "show-filterbox":
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
showFilterBox: true,
|
|
|
|
filterBoxOnSelect: action.onSelect,
|
|
|
|
filterBoxPlaceHolder: action.placeHolder,
|
|
|
|
filterBoxOptions: action.options,
|
2022-04-21 17:46:33 +08:00
|
|
|
filterBoxLabel: action.label,
|
2022-04-13 20:46:52 +08:00
|
|
|
filterBoxHelpText: action.helpText,
|
|
|
|
};
|
|
|
|
case "hide-filterbox":
|
|
|
|
return {
|
|
|
|
...state,
|
2024-01-21 02:16:07 +08:00
|
|
|
showCommandPalette: false,
|
|
|
|
showPageNavigator: false,
|
2022-04-13 20:46:52 +08:00
|
|
|
showFilterBox: false,
|
|
|
|
filterBoxOnSelect: () => {},
|
|
|
|
filterBoxPlaceHolder: "",
|
|
|
|
filterBoxOptions: [],
|
|
|
|
filterBoxHelpText: "",
|
|
|
|
};
|
2022-12-21 23:08:51 +08: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 21:55:24 +08:00
|
|
|
case "set-ui-option":
|
2022-09-16 20:26:47 +08:00
|
|
|
return {
|
|
|
|
...state,
|
2022-12-21 21:55:24 +08:00
|
|
|
uiOptions: {
|
|
|
|
...state.uiOptions,
|
|
|
|
[action.key]: action.value,
|
|
|
|
},
|
2022-09-16 20:26:47 +08:00
|
|
|
};
|
2023-06-15 02:58:08 +08:00
|
|
|
case "set-progress":
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
progressPerc: action.progressPerc,
|
|
|
|
};
|
2022-03-20 16:56:28 +08:00
|
|
|
}
|
|
|
|
return state;
|
|
|
|
}
|