2022-04-13 12:46:52 +00:00
|
|
|
import { FilterList } from "./filter";
|
2022-04-21 11:57:45 +00:00
|
|
|
import { FilterOption, PageMeta } from "@silverbulletmd/common/types";
|
2022-03-20 08:56:28 +00:00
|
|
|
|
|
|
|
export function PageNavigator({
|
|
|
|
allPages,
|
|
|
|
onNavigate,
|
|
|
|
currentPage,
|
|
|
|
}: {
|
|
|
|
allPages: Set<PageMeta>;
|
|
|
|
onNavigate: (page: string | undefined) => void;
|
|
|
|
currentPage?: string;
|
|
|
|
}) {
|
2022-04-13 12:46:52 +00:00
|
|
|
let options: FilterOption[] = [];
|
2022-03-20 08:56:28 +00:00
|
|
|
for (let pageMeta of allPages) {
|
|
|
|
// Order by last modified date in descending order
|
|
|
|
let orderId = -pageMeta.lastModified;
|
2022-04-10 09:04:07 +00:00
|
|
|
// Unless it was opened in this session
|
2022-03-20 08:56:28 +00:00
|
|
|
if (pageMeta.lastOpened) {
|
|
|
|
orderId = -pageMeta.lastOpened;
|
|
|
|
}
|
2022-08-01 13:06:32 +00:00
|
|
|
// Or it's the currently open page
|
|
|
|
if (currentPage && currentPage === pageMeta.name) {
|
|
|
|
// ... then we put it all the way to the end
|
|
|
|
orderId = Infinity;
|
|
|
|
}
|
2022-03-20 08:56:28 +00:00
|
|
|
options.push({
|
|
|
|
...pageMeta,
|
|
|
|
orderId: orderId,
|
|
|
|
});
|
|
|
|
}
|
2022-04-01 13:02:35 +00:00
|
|
|
let completePrefix: string | undefined = undefined;
|
|
|
|
if (currentPage && currentPage.includes("/")) {
|
|
|
|
const pieces = currentPage.split("/");
|
|
|
|
completePrefix = pieces.slice(0, pieces.length - 1).join("/") + "/";
|
2022-07-04 09:38:16 +00:00
|
|
|
} else if (currentPage && currentPage.includes(" ")) {
|
|
|
|
completePrefix = currentPage.split(" ")[0] + " ";
|
2022-04-01 13:02:35 +00:00
|
|
|
}
|
2022-03-20 08:56:28 +00:00
|
|
|
return (
|
|
|
|
<FilterList
|
|
|
|
placeholder="Page"
|
|
|
|
label="Open"
|
|
|
|
options={options}
|
2022-04-10 09:04:07 +00:00
|
|
|
// icon={faFileLines}
|
2022-03-20 08:56:28 +00:00
|
|
|
allowNew={true}
|
|
|
|
helpText="Start typing the page name to filter results, press <code>Return</code> to open."
|
|
|
|
newHint="Create page"
|
2022-04-01 13:02:35 +00:00
|
|
|
completePrefix={completePrefix}
|
2022-03-20 08:56:28 +00:00
|
|
|
onSelect={(opt) => {
|
|
|
|
onNavigate(opt?.name);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|