Sort pages locally
All checks were successful
thehideout/TheHideout-API/pipeline/head This commit looks good
All checks were successful
thehideout/TheHideout-API/pipeline/head This commit looks good
This commit is contained in:
parent
13d89e2d52
commit
f39c8c1981
@ -2,6 +2,7 @@ import { queryNotionDataSource, getNotionPage } from "../utils/notion.js";
|
||||
import { transformPageData, storePages, getPages } from "../utils/pages.js";
|
||||
import { getProperties } from "../utils/properties.js";
|
||||
import { getSettings } from "../utils/settings.js";
|
||||
import { unionBy } from "lodash";
|
||||
|
||||
// Fetch Notion pages (raw data)
|
||||
export async function importNotionPages(env, notionId = null) {
|
||||
@ -29,22 +30,10 @@ export async function importNotionPages(env, notionId = null) {
|
||||
const cachedPages = await getPages(env);
|
||||
console.log("Cached pages:", cachedPages);
|
||||
console.log("Pages:", pages);
|
||||
// Create a map of new pages by notionId for quick lookup
|
||||
const newPagesMap = new Map(pages.map((page) => [page.notionId, page]));
|
||||
|
||||
// Merge: preserve cached order, but replace with new data if available
|
||||
const mergedPages = cachedPages.map((cachedPage) => {
|
||||
return newPagesMap.has(cachedPage.notionId)
|
||||
? newPagesMap.get(cachedPage.notionId)
|
||||
: cachedPage;
|
||||
});
|
||||
|
||||
// Add any new pages that weren't in cache
|
||||
const existingNotionIds = new Set(cachedPages.map((page) => page.notionId));
|
||||
const newPages = pages.filter(
|
||||
(page) => !existingNotionIds.has(page.notionId)
|
||||
);
|
||||
mergedPages.push(...newPages);
|
||||
// Use unionBy to merge pages by notionId (new pages take precedence)
|
||||
const mergedPages = unionBy(pages, cachedPages, "notionId");
|
||||
// Sort by order
|
||||
mergedPages.sort((a, b) => (a.order || 0) - (b.order || 0));
|
||||
await storePages(env, mergedPages);
|
||||
console.log("Pages imported from Notion and merged with cache.");
|
||||
return mergedPages;
|
||||
|
||||
@ -134,6 +134,9 @@ export async function transformPageData(
|
||||
|
||||
const showScroll = properties["Show Scroll Icon"]?.checkbox || false;
|
||||
|
||||
// Extract order property
|
||||
const order = properties["Order"]?.number || 0;
|
||||
|
||||
if (showProperties == true) {
|
||||
content.push({
|
||||
type: "properties",
|
||||
@ -168,5 +171,6 @@ export async function transformPageData(
|
||||
showContactForm: showContactForm,
|
||||
hideMobileImage: hideMobileImage,
|
||||
invertHeader: invertHeader,
|
||||
order: order,
|
||||
};
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user