1
0
silverbullet/web/components/page_navigator.tsx

63 lines
1.8 KiB
TypeScript
Raw Normal View History

import { FilterList } from "./filter.tsx";
import { FilterOption, PageMeta } from "../../common/types.ts";
import { CompletionContext, CompletionResult } from "../deps.ts";
export function PageNavigator({
allPages,
onNavigate,
completer,
vimMode,
darkMode,
currentPage,
}: {
allPages: Set<PageMeta>;
vimMode: boolean;
darkMode: boolean;
onNavigate: (page: string | undefined) => void;
completer: (context: CompletionContext) => Promise<CompletionResult | null>;
currentPage?: string;
}) {
const options: FilterOption[] = [];
for (const 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
if (pageMeta.lastOpened) {
orderId = -pageMeta.lastOpened;
}
// Or it's the currently open page
if (currentPage && currentPage === pageMeta.name) {
// ... then we put it all the way to the end
orderId = Infinity;
}
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
}
return (
<FilterList
placeholder="Page"
label="Open"
options={options}
vimMode={vimMode}
darkMode={darkMode}
completer={completer}
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}
onSelect={(opt) => {
onNavigate(opt?.name);
}}
/>
);
}