chore: last refactors finished

This commit is contained in:
2026-02-21 18:29:25 +01:00
parent f9663b07b1
commit 3fb0085b11
6 changed files with 196 additions and 286 deletions

View File

@@ -5,6 +5,7 @@ import { loadTabsForProject, saveTabsForProject } from './utils';
import { openSingletonToolTab } from './navigation/tabPolicy';
import { persistSiteValidationReport } from './navigation/siteValidationPersistence';
import { executeActivityClick } from './navigation/activityExecution';
import { createAndFocusPost } from './navigation/postCreation';
import { ensureRendererPicoThemeStylesheet, getRendererPicoTheme } from './utils/picoTheme';
import { useI18n } from './i18n';
import './App.css';
@@ -186,14 +187,30 @@ const App: React.FC = () => {
// Menu events
unsubscribers.push(
window.electronAPI?.on('menu:newPost', async () => {
const post = await window.electronAPI?.posts.create({
title: '',
content: '',
const state = useAppStore.getState();
await createAndFocusPost({
createPost: async (input) => (await window.electronAPI?.posts.create(input)) as { id: string } | null | undefined,
setSelectedPost: state.setSelectedPost,
ensurePostsSidebar: () => {
const next = useAppStore.getState();
executeActivityClick(
{
activeView: next.activeView,
sidebarVisible: next.sidebarVisible,
tabs: next.tabs,
activeTabId: next.activeTabId,
},
'posts',
{
setActiveView: next.setActiveView,
toggleSidebar: next.toggleSidebar,
},
);
},
onError: (error) => {
console.error('Failed to create post:', error);
},
});
if (post) {
setSelectedPost((post as PostData).id);
setActiveView('posts');
}
}) || (() => {})
);

View File

@@ -9,6 +9,7 @@ import { scrollToTagsSection, TagsCategory } from '../TagsView';
import { activateSidebarSection } from '../../navigation/sectionActivation';
import { getPersistedSidebarSection, setPersistedSidebarSection } from '../../navigation/sidebarUiPersistence';
import { openChatTab, openEntityTab, openImportTab, openSingletonToolTab } from '../../navigation/tabPolicy';
import { createAndFocusPost } from '../../navigation/postCreation';
import type { SidebarView } from '../../navigation/sidebarViewRegistry';
import { useI18n } from '../../i18n';
import './Sidebar.css';
@@ -743,21 +744,14 @@ const PostsList: React.FC<PostsListProps> = ({ mode, isActive }) => {
}, [posts, searchQuery, selectedYear, selectedMonth, selectedTags, selectedCategories, isPagesMode]);
const handleCreatePost = async () => {
// Create a real post immediately in the database with default empty content
try {
const { setSelectedPost: selectPost } = useAppStore.getState();
const newPost = await window.electronAPI?.posts.create({
title: '',
content: '',
tags: [],
categories: [],
});
if (newPost) {
selectPost(newPost.id);
}
} catch (error) {
console.error('Failed to create post:', error);
}
const { setSelectedPost: selectPost } = useAppStore.getState();
await createAndFocusPost({
createPost: async (input) => (await window.electronAPI?.posts.create(input)) as { id: string } | null | undefined,
setSelectedPost: selectPost,
onError: (error) => {
console.error('Failed to create post:', error);
},
});
};
const handleLoadMore = async () => {

View File

@@ -0,0 +1,33 @@
export interface CreateAndFocusPostOptions {
createPost: (input: {
title: string;
content: string;
tags: string[];
categories: string[];
}) => Promise<{ id: string } | null | undefined>;
setSelectedPost: (postId: string) => void;
ensurePostsSidebar?: () => void;
onError?: (error: unknown) => void;
}
export async function createAndFocusPost(options: CreateAndFocusPostOptions): Promise<string | null> {
try {
const post = await options.createPost({
title: '',
content: '',
tags: [],
categories: [],
});
if (!post) {
return null;
}
options.setSelectedPost(post.id);
options.ensurePostsSidebar?.();
return post.id;
} catch (error) {
options.onError?.(error);
return null;
}
}