fix: publish/draft status. handlign better

This commit is contained in:
2026-02-14 21:51:04 +01:00
parent 4b31d9d421
commit c429fb6087
5 changed files with 343 additions and 6 deletions

View File

@@ -1,2 +1,3 @@
export { AutoSaveManager, type AutoSaveConfig } from './autoSave';
export { unescapeMacroSyntax } from './markdownEscape';
export { groupPostsByStatus, type GroupedPosts, type PostStatus } from './postGrouping';

View File

@@ -0,0 +1,39 @@
import type { PostData } from '../store';
export type PostStatus = 'draft' | 'published' | 'archived';
export interface GroupedPosts {
draft: PostData[];
published: PostData[];
archived: PostData[];
}
/**
* Groups posts by status, freshening cached filter results with current store data.
*
* This ensures that when a post's status changes in the store, the change is
* reflected in all sections even when filters are active and have cached stale data.
*
* @param storePosts - Fresh posts from the Zustand store
* @param filteredPosts - Cached posts from search/filter results (may be stale), or null if no filter active
* @returns Posts grouped by status with freshened data
*/
export function groupPostsByStatus(
storePosts: PostData[],
filteredPosts: PostData[] | null
): GroupedPosts {
// Create a map for O(1) lookup of fresh post data from the store
const postsById = new Map(storePosts.map(p => [p.id, p]));
// Freshen filtered posts by using current store data when available
const freshenedFilteredPosts = filteredPosts?.map(cachedPost => {
const freshPost = postsById.get(cachedPost.id);
return freshPost ?? cachedPost;
}) ?? null;
return {
draft: storePosts.filter(p => p.status === 'draft'),
published: (freshenedFilteredPosts ?? storePosts).filter(p => p.status === 'published'),
archived: (freshenedFilteredPosts ?? storePosts).filter(p => p.status === 'archived'),
};
}