feat: diff tool to see discrepancies

This commit is contained in:
2026-02-15 21:29:38 +01:00
parent 8b70214d15
commit 3679750dbc
12 changed files with 1807 additions and 3 deletions

View File

@@ -0,0 +1,469 @@
/**
* MetadataDiffEngine
*
* Compares metadata between database records and filesystem files for posts and media.
* Used to detect and resolve differences that may have accumulated due to bugs or
* manual edits.
*/
import { EventEmitter } from 'events';
import { eq, and } from 'drizzle-orm';
import { getDatabase } from '../database';
import { posts, media } from '../database/schema';
import { readPostFile, PostFileData } from './postFileUtils';
import { getPostEngine } from './PostEngine';
import { taskManager } from './TaskManager';
/**
* A difference in a specific metadata field
*/
export interface FieldDifference<T = unknown> {
dbValue: T;
fileValue: T;
}
/**
* The fields that can have differences
*/
export type DiffField = 'tags' | 'categories' | 'title' | 'excerpt' | 'author';
/**
* Metadata differences for a single post
*/
export interface PostMetadataDiff {
postId: string;
title: string;
slug: string;
filePath?: string;
hasDifferences: boolean;
differences: Partial<Record<DiffField, FieldDifference>>;
}
/**
* A group of posts with the same type of difference
*/
export interface DiffGroup {
field: DiffField;
label: string;
posts: Array<{
postId: string;
title: string;
slug: string;
dbValue: unknown;
fileValue: unknown;
}>;
}
/**
* Result of scanning all published posts
*/
export interface ScanResult {
totalScanned: number;
postsWithDifferences: number;
differences: PostMetadataDiff[];
groups: DiffGroup[];
}
/**
* Statistics about posts/media tables
*/
export interface TableStats {
totalPosts: number;
publishedPosts: number;
draftPosts: number;
totalMedia: number;
}
export class MetadataDiffEngine extends EventEmitter {
private currentProjectId = 'default';
setProjectContext(projectId: string): void {
this.currentProjectId = projectId;
}
getProjectContext(): string {
return this.currentProjectId;
}
private getDb() {
return getDatabase().getLocal();
}
private getClient() {
return getDatabase().getLocalClient();
}
/**
* Get statistics about the posts and media tables
*/
async getTableStats(): Promise<TableStats> {
const db = this.getDb();
const client = this.getClient();
if (!client) throw new Error('Database not initialized');
// Get post counts
const allPostsResult = await client.execute({
sql: `SELECT COUNT(*) as count FROM posts WHERE project_id = ?`,
args: [this.currentProjectId],
});
const totalPosts = Number(allPostsResult.rows[0]?.count ?? 0);
const publishedResult = await client.execute({
sql: `SELECT COUNT(*) as count FROM posts WHERE project_id = ? AND status = 'published' AND file_path IS NOT NULL AND file_path != ''`,
args: [this.currentProjectId],
});
const publishedPosts = Number(publishedResult.rows[0]?.count ?? 0);
const draftResult = await client.execute({
sql: `SELECT COUNT(*) as count FROM posts WHERE project_id = ? AND status = 'draft'`,
args: [this.currentProjectId],
});
const draftPosts = Number(draftResult.rows[0]?.count ?? 0);
// Get media count
const mediaResult = await client.execute({
sql: `SELECT COUNT(*) as count FROM media WHERE project_id = ?`,
args: [this.currentProjectId],
});
const totalMedia = Number(mediaResult.rows[0]?.count ?? 0);
return {
totalPosts,
publishedPosts,
draftPosts,
totalMedia,
};
}
/**
* Compare metadata for a single post between database and file
*/
async comparePostMetadata(postId: string): Promise<PostMetadataDiff | null> {
const db = this.getDb();
// Get post from database
const dbPost = await db
.select()
.from(posts)
.where(and(eq(posts.id, postId), eq(posts.projectId, this.currentProjectId)))
.get();
if (!dbPost) {
return null;
}
// Skip drafts - they don't have files
if (!dbPost.filePath || dbPost.status === 'draft') {
return null;
}
// Read file metadata
const fileData = await readPostFile(dbPost.filePath);
if (!fileData) {
// File doesn't exist or can't be read
return {
postId: dbPost.id,
title: dbPost.title,
slug: dbPost.slug,
filePath: dbPost.filePath,
hasDifferences: true,
differences: {}, // File missing entirely
};
}
// Compare fields
const differences: Partial<Record<DiffField, FieldDifference>> = {};
// Parse JSON arrays from database
const dbTags: string[] = JSON.parse(dbPost.tags || '[]');
const dbCategories: string[] = JSON.parse(dbPost.categories || '[]');
const fileTags = fileData.tags || [];
const fileCategories = fileData.categories || [];
// Compare tags (order-independent)
if (!this.arraysEqual(dbTags, fileTags)) {
differences.tags = { dbValue: dbTags, fileValue: fileTags };
}
// Compare categories (order-independent)
if (!this.arraysEqual(dbCategories, fileCategories)) {
differences.categories = { dbValue: dbCategories, fileValue: fileCategories };
}
// Compare title
if (dbPost.title !== fileData.title) {
differences.title = { dbValue: dbPost.title, fileValue: fileData.title };
}
// Compare excerpt
if ((dbPost.excerpt || '') !== (fileData.excerpt || '')) {
differences.excerpt = { dbValue: dbPost.excerpt || '', fileValue: fileData.excerpt || '' };
}
// Compare author
if ((dbPost.author || '') !== (fileData.author || '')) {
differences.author = { dbValue: dbPost.author || '', fileValue: fileData.author || '' };
}
return {
postId: dbPost.id,
title: dbPost.title,
slug: dbPost.slug,
filePath: dbPost.filePath,
hasDifferences: Object.keys(differences).length > 0,
differences,
};
}
/**
* Compare arrays for equality (order-independent)
*/
private arraysEqual(a: string[], b: string[]): boolean {
if (a.length !== b.length) return false;
const sortedA = [...a].sort();
const sortedB = [...b].sort();
return sortedA.every((val, idx) => val === sortedB[idx]);
}
/**
* Scan all published posts and find metadata differences
*/
async scanAllPublishedPosts(
onProgress: (current: number, total: number, message: string) => void
): Promise<ScanResult> {
const client = this.getClient();
if (!client) throw new Error('Database not initialized');
// Get all published posts with file paths
const result = await client.execute({
sql: `SELECT id, title, slug, file_path, tags, categories, excerpt, author
FROM posts
WHERE project_id = ?
AND status = 'published'
AND file_path IS NOT NULL
AND file_path != ''`,
args: [this.currentProjectId],
});
const publishedPosts = result.rows;
const total = publishedPosts.length;
const differences: PostMetadataDiff[] = [];
onProgress(0, total, `Scanning ${total} published posts...`);
for (let i = 0; i < publishedPosts.length; i++) {
const row = publishedPosts[i];
const postId = row.id as string;
const diff = await this.comparePostMetadata(postId);
if (diff && diff.hasDifferences) {
differences.push(diff);
}
if ((i + 1) % 10 === 0 || i === total - 1) {
onProgress(i + 1, total, `Scanned ${i + 1}/${total} posts, found ${differences.length} with differences`);
}
}
// Group the differences
const groups = this.groupDifferencesByField(differences);
return {
totalScanned: total,
postsWithDifferences: differences.length,
differences,
groups,
};
}
/**
* Group differences by field type for easier display and bulk actions
*/
groupDifferencesByField(diffs: PostMetadataDiff[]): DiffGroup[] {
const groupMap = new Map<DiffField, DiffGroup>();
const fieldLabels: Record<DiffField, string> = {
tags: 'Tags',
categories: 'Categories',
title: 'Title',
excerpt: 'Excerpt',
author: 'Author',
};
for (const diff of diffs) {
for (const [field, fieldDiff] of Object.entries(diff.differences)) {
const fieldKey = field as DiffField;
if (!fieldDiff) continue;
if (!groupMap.has(fieldKey)) {
groupMap.set(fieldKey, {
field: fieldKey,
label: fieldLabels[fieldKey],
posts: [],
});
}
groupMap.get(fieldKey)!.posts.push({
postId: diff.postId,
title: diff.title,
slug: diff.slug,
dbValue: fieldDiff.dbValue,
fileValue: fieldDiff.fileValue,
});
}
}
return Array.from(groupMap.values()).sort((a, b) => b.posts.length - a.posts.length);
}
/**
* Sync database metadata to files for the given posts
* (DB -> File: writes current DB metadata to markdown files)
*/
async syncDbToFile(postIds: string[]): Promise<{ success: number; failed: number }> {
const postEngine = getPostEngine();
let success = 0;
let failed = 0;
for (const postId of postIds) {
try {
const synced = await postEngine.syncPublishedPostFile(postId);
if (synced) {
success++;
} else {
failed++;
}
} catch (error) {
console.error(`[MetadataDiffEngine] Failed to sync post ${postId} to file:`, error);
failed++;
}
}
return { success, failed };
}
/**
* Sync file metadata to database for the given posts
* (File -> DB: reads file metadata and updates DB)
*/
async syncFileToDb(postIds: string[], field?: DiffField): Promise<{ success: number; failed: number }> {
const db = this.getDb();
let success = 0;
let failed = 0;
for (const postId of postIds) {
try {
// Get the post from DB to get file path
const dbPost = await db
.select()
.from(posts)
.where(and(eq(posts.id, postId), eq(posts.projectId, this.currentProjectId)))
.get();
if (!dbPost || !dbPost.filePath) {
failed++;
continue;
}
// Read file metadata
const fileData = await readPostFile(dbPost.filePath);
if (!fileData) {
failed++;
continue;
}
// Build update object based on field or all fields
const updateData: Record<string, unknown> = {
updatedAt: new Date(),
};
if (!field || field === 'tags') {
updateData.tags = JSON.stringify(fileData.tags || []);
}
if (!field || field === 'categories') {
updateData.categories = JSON.stringify(fileData.categories || []);
}
if (!field || field === 'title') {
updateData.title = fileData.title;
}
if (!field || field === 'excerpt') {
updateData.excerpt = fileData.excerpt || null;
}
if (!field || field === 'author') {
updateData.author = fileData.author || null;
}
// Update database
await db
.update(posts)
.set(updateData)
.where(eq(posts.id, postId));
success++;
} catch (error) {
console.error(`[MetadataDiffEngine] Failed to sync post ${postId} to DB:`, error);
failed++;
}
}
return { success, failed };
}
/**
* Run a full scan as a background task
*/
async runScanTask(): Promise<ScanResult> {
return taskManager.runTask({
id: `metadata-diff-scan-${Date.now()}`,
name: 'Scanning for metadata differences',
execute: async (onProgress) => {
return this.scanAllPublishedPosts((current, total, message) => {
const percent = total > 0 ? (current / total) * 100 : 0;
onProgress(percent, message);
});
},
});
}
/**
* Run sync DB to File as a background task
*/
async runSyncDbToFileTask(postIds: string[], groupLabel: string): Promise<{ success: number; failed: number }> {
return taskManager.runTask({
id: `metadata-sync-db-to-file-${Date.now()}`,
name: `Syncing ${groupLabel} from DB to files`,
execute: async (onProgress) => {
onProgress(0, `Syncing ${postIds.length} posts...`);
const result = await this.syncDbToFile(postIds);
onProgress(100, `Completed: ${result.success} synced, ${result.failed} failed`);
return result;
},
});
}
/**
* Run sync File to DB as a background task
*/
async runSyncFileToDbTask(postIds: string[], field: DiffField, groupLabel: string): Promise<{ success: number; failed: number }> {
return taskManager.runTask({
id: `metadata-sync-file-to-db-${Date.now()}`,
name: `Syncing ${groupLabel} from files to DB`,
execute: async (onProgress) => {
onProgress(0, `Syncing ${postIds.length} posts...`);
const result = await this.syncFileToDb(postIds, field);
onProgress(100, `Completed: ${result.success} synced, ${result.failed} failed`);
return result;
},
});
}
}
// Singleton instance
let metadataDiffEngineInstance: MetadataDiffEngine | null = null;
export function getMetadataDiffEngine(): MetadataDiffEngine {
if (!metadataDiffEngineInstance) {
metadataDiffEngineInstance = new MetadataDiffEngine();
}
return metadataDiffEngineInstance;
}

View File

@@ -59,7 +59,17 @@ export {
export {
ImportDefinitionEngine,
type ImportDefinitionData,
} from './ImportDefinitionEngine';export {
} from './ImportDefinitionEngine';
export {
readPostFile,
type PostFileData,
} from './postFileUtils';
} from './postFileUtils';
export {
MetadataDiffEngine,
getMetadataDiffEngine,
type PostMetadataDiff,
type DiffGroup,
type DiffField,
type ScanResult,
type TableStats,
} from './MetadataDiffEngine';

View File

@@ -1027,6 +1027,65 @@ export function registerIpcHandlers(): void {
return engine.deleteDefinition(id);
});
// ============ Metadata Diff Handlers ============
safeHandle('metadataDiff:getStats', async () => {
const { getMetadataDiffEngine } = await import('../engine/MetadataDiffEngine');
const engine = getMetadataDiffEngine();
const projectEngine = getProjectEngine();
const activeProject = await projectEngine.getActiveProject();
if (activeProject) {
engine.setProjectContext(activeProject.id);
}
return engine.getTableStats();
});
safeHandle('metadataDiff:scan', async () => {
const { getMetadataDiffEngine } = await import('../engine/MetadataDiffEngine');
const engine = getMetadataDiffEngine();
const projectEngine = getProjectEngine();
const activeProject = await projectEngine.getActiveProject();
if (activeProject) {
engine.setProjectContext(activeProject.id);
}
// Forward progress events to renderer
const taskId = `metadata-diff-scan-${Date.now()}`;
return taskManager.runTask({
id: taskId,
name: 'Scanning for metadata differences',
execute: async (onProgress) => {
return engine.scanAllPublishedPosts((current, total, message) => {
const percent = total > 0 ? (current / total) * 100 : 0;
onProgress(percent, message);
});
},
});
});
safeHandle('metadataDiff:syncDbToFile', async (_, postIds: string[], groupLabel: string) => {
const { getMetadataDiffEngine } = await import('../engine/MetadataDiffEngine');
const engine = getMetadataDiffEngine();
const projectEngine = getProjectEngine();
const activeProject = await projectEngine.getActiveProject();
if (activeProject) {
engine.setProjectContext(activeProject.id);
}
return engine.runSyncDbToFileTask(postIds, groupLabel);
});
safeHandle('metadataDiff:syncFileToDb', async (_, postIds: string[], field: string, groupLabel: string) => {
const { getMetadataDiffEngine } = await import('../engine/MetadataDiffEngine');
const engine = getMetadataDiffEngine();
const projectEngine = getProjectEngine();
const activeProject = await projectEngine.getActiveProject();
if (activeProject) {
engine.setProjectContext(activeProject.id);
}
return engine.runSyncFileToDbTask(postIds, field as 'tags' | 'categories' | 'title' | 'excerpt' | 'author', groupLabel);
});
// ============ Event Forwarding ============
// Forward engine events to renderer

View File

@@ -253,6 +253,13 @@ function createApplicationMenu(): Menu {
mainWindow?.webContents.send('menu:reindexText');
},
},
{ type: 'separator' },
{
label: 'Metadata Diff Tool',
click: () => {
mainWindow?.webContents.send('menu:metadataDiff');
},
},
],
},
{

View File

@@ -194,6 +194,14 @@ contextBridge.exposeInMainWorld('electronAPI', {
},
},
// Metadata Diff Tool
metadataDiff: {
getStats: () => ipcRenderer.invoke('metadataDiff:getStats'),
scan: () => ipcRenderer.invoke('metadataDiff:scan'),
syncDbToFile: (postIds: string[], groupLabel: string) => ipcRenderer.invoke('metadataDiff:syncDbToFile', postIds, groupLabel),
syncFileToDb: (postIds: string[], field: string, groupLabel: string) => ipcRenderer.invoke('metadataDiff:syncFileToDb', postIds, field, groupLabel),
},
// AI Chat (OpenCode Zen API integration)
chat: {
// API Key Management
@@ -375,6 +383,39 @@ export interface ElectronAPI {
update: (id: string, updates: unknown) => Promise<unknown>;
delete: (id: string) => Promise<boolean>;
};
metadataDiff: {
getStats: () => Promise<{
totalPosts: number;
publishedPosts: number;
draftPosts: number;
totalMedia: number;
}>;
scan: () => Promise<{
totalScanned: number;
postsWithDifferences: number;
differences: Array<{
postId: string;
title: string;
slug: string;
filePath?: string;
hasDifferences: boolean;
differences: Record<string, { dbValue: unknown; fileValue: unknown }>;
}>;
groups: Array<{
field: string;
label: string;
posts: Array<{
postId: string;
title: string;
slug: string;
dbValue: unknown;
fileValue: unknown;
}>;
}>;
}>;
syncDbToFile: (postIds: string[], groupLabel: string) => Promise<{ success: number; failed: number }>;
syncFileToDb: (postIds: string[], field: string, groupLabel: string) => Promise<{ success: number; failed: number }>;
};
chat: {
// API Key Management
checkReady: () => Promise<{ ready: boolean; error?: string; backend?: string }>;

View File

@@ -276,6 +276,13 @@ const App: React.FC = () => {
}) || (() => {})
);
unsubscribers.push(
window.electronAPI?.on('menu:metadataDiff', () => {
// Open metadata diff tool tab
openTab({ id: 'metadata-diff', type: 'metadata-diff', title: 'Metadata Diff' });
}) || (() => {})
);
// Import completion event - refresh posts and media stores
unsubscribers.push(
window.electronAPI?.import.onComplete(async (data) => {

View File

@@ -13,6 +13,7 @@ import { TagsView } from '../TagsView';
import { TagInput } from '../TagInput';
import { ChatPanel } from '../ChatPanel';
import { ImportAnalysisView } from '../ImportAnalysisView';
import { MetadataDiffPanel } from '../MetadataDiffPanel';
import { AutoSaveManager } from '../../utils';
import { parseMacros, getMacro } from '../../macros/registry';
import { InsertModal } from '../InsertModal';
@@ -2319,6 +2320,7 @@ export const Editor: React.FC = () => {
const showTags = activeTab?.type === 'tags';
const showChat = activeTab?.type === 'chat';
const showImport = activeTab?.type === 'import';
const showMetadataDiff = activeTab?.type === 'metadata-diff';
// Clear selectedPostId if the post doesn't exist (e.g., after project switch)
useEffect(() => {
@@ -2407,6 +2409,17 @@ export const Editor: React.FC = () => {
);
}
// Show metadata diff if metadata-diff tab is active
if (showMetadataDiff) {
return (
<div className="editor">
<MetadataDiffPanel />
{renderErrorModal()}
{renderConfirmDeleteModal()}
</div>
);
}
// Show post editor if a post tab is active
if (showPost && activeTabId) {
return (

View File

@@ -0,0 +1,342 @@
.metadata-diff-panel {
display: flex;
flex-direction: column;
height: 100%;
padding: 16px;
overflow: auto;
background: var(--editor-background);
color: var(--editor-foreground);
}
.metadata-diff-panel h2 {
margin: 0 0 16px 0;
font-size: 18px;
font-weight: 600;
color: var(--editor-foreground);
}
.metadata-diff-panel h3 {
margin: 0 0 8px 0;
font-size: 14px;
font-weight: 600;
color: var(--editor-foreground);
}
/* Stats Section */
.diff-stats {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
gap: 12px;
margin-bottom: 20px;
padding: 12px;
background: var(--sidebar-background);
border-radius: 6px;
border: 1px solid var(--sidebar-border);
}
.stat-item {
display: flex;
flex-direction: column;
gap: 4px;
}
.stat-label {
font-size: 11px;
color: var(--descriptionForeground);
text-transform: uppercase;
letter-spacing: 0.5px;
}
.stat-value {
font-size: 20px;
font-weight: 600;
color: var(--editor-foreground);
}
/* Progress Section */
.diff-progress {
margin-bottom: 20px;
padding: 16px;
background: var(--sidebar-background);
border-radius: 6px;
border: 1px solid var(--sidebar-border);
}
.progress-bar-container {
height: 6px;
background: var(--input-background);
border-radius: 3px;
overflow: hidden;
margin-top: 8px;
}
.progress-bar {
height: 100%;
background: var(--button-background);
border-radius: 3px;
transition: width 0.2s ease;
}
.progress-text {
font-size: 12px;
color: var(--descriptionForeground);
margin-top: 8px;
}
/* Actions Section */
.diff-actions {
display: flex;
gap: 8px;
margin-bottom: 20px;
}
.diff-actions button {
display: flex;
align-items: center;
gap: 6px;
padding: 8px 16px;
border: none;
border-radius: 4px;
font-size: 13px;
cursor: pointer;
transition: background 0.2s;
}
.diff-actions button.primary {
background: var(--button-background);
color: var(--button-foreground);
}
.diff-actions button.primary:hover:not(:disabled) {
background: var(--button-hoverBackground);
}
.diff-actions button.secondary {
background: var(--input-background);
color: var(--editor-foreground);
border: 1px solid var(--input-border);
}
.diff-actions button.secondary:hover:not(:disabled) {
background: var(--list-hoverBackground);
}
.diff-actions button:disabled {
opacity: 0.5;
cursor: not-allowed;
}
/* Results Section */
.diff-results {
flex: 1;
display: flex;
flex-direction: column;
gap: 12px;
}
.diff-summary {
padding: 12px;
background: var(--sidebar-background);
border-radius: 6px;
border: 1px solid var(--sidebar-border);
}
.diff-summary.no-differences {
background: color-mix(in srgb, var(--testing-iconPassed) 10%, var(--sidebar-background));
border-color: var(--testing-iconPassed);
}
.diff-summary.has-differences {
background: color-mix(in srgb, var(--testing-iconFailed) 10%, var(--sidebar-background));
border-color: var(--testing-iconFailed);
}
/* Collapsible Groups */
.diff-group {
background: var(--sidebar-background);
border: 1px solid var(--sidebar-border);
border-radius: 6px;
overflow: hidden;
}
.diff-group-header {
display: flex;
align-items: center;
justify-content: space-between;
padding: 12px;
background: var(--list-hoverBackground);
cursor: pointer;
user-select: none;
}
.diff-group-header:hover {
background: var(--list-activeSelectionBackground);
}
.diff-group-title {
display: flex;
align-items: center;
gap: 8px;
font-weight: 600;
}
.diff-group-title .chevron {
font-size: 10px;
transition: transform 0.2s;
}
.diff-group-title .chevron.expanded {
transform: rotate(90deg);
}
.diff-group-count {
display: flex;
align-items: center;
gap: 8px;
}
.diff-group-count .badge {
padding: 2px 8px;
border-radius: 10px;
font-size: 11px;
font-weight: 600;
background: var(--badge-background);
color: var(--badge-foreground);
}
.diff-group-actions {
display: flex;
gap: 4px;
}
.diff-group-actions button {
padding: 4px 8px;
font-size: 11px;
border: 1px solid var(--input-border);
border-radius: 3px;
background: var(--input-background);
color: var(--editor-foreground);
cursor: pointer;
transition: background 0.2s;
}
.diff-group-actions button:hover:not(:disabled) {
background: var(--list-hoverBackground);
}
.diff-group-actions button:disabled {
opacity: 0.5;
cursor: not-allowed;
}
.diff-group-actions button.db-to-file {
border-color: var(--button-background);
color: var(--button-background);
}
.diff-group-actions button.file-to-db {
border-color: var(--testing-iconQueued);
color: var(--testing-iconQueued);
}
.diff-group-content {
padding: 12px;
border-top: 1px solid var(--sidebar-border);
}
.diff-group-content.collapsed {
display: none;
}
/* Post Items */
.diff-post-item {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
gap: 12px;
padding: 8px;
margin-bottom: 8px;
background: var(--editor-background);
border-radius: 4px;
font-size: 12px;
}
.diff-post-item:last-child {
margin-bottom: 0;
}
.diff-post-title {
font-weight: 500;
color: var(--editor-foreground);
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.diff-value {
padding: 4px 8px;
border-radius: 3px;
font-family: var(--editor-font-family);
font-size: 11px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.diff-value.db-value {
background: color-mix(in srgb, var(--button-background) 15%, transparent);
border: 1px solid var(--button-background);
}
.diff-value.file-value {
background: color-mix(in srgb, var(--testing-iconQueued) 15%, transparent);
border: 1px solid var(--testing-iconQueued);
}
.diff-value-label {
font-size: 10px;
color: var(--descriptionForeground);
margin-bottom: 2px;
}
/* Loading State */
.diff-loading {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 12px;
padding: 40px;
color: var(--descriptionForeground);
}
.diff-loading .spinner {
width: 24px;
height: 24px;
border: 2px solid var(--input-border);
border-top-color: var(--button-background);
border-radius: 50%;
animation: spin 0.8s linear infinite;
}
@keyframes spin {
to {
transform: rotate(360deg);
}
}
/* Empty State */
.diff-empty {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 12px;
padding: 40px;
color: var(--descriptionForeground);
text-align: center;
}
.diff-empty .icon {
font-size: 32px;
opacity: 0.5;
}

View File

@@ -0,0 +1,322 @@
import React, { useState, useEffect, useCallback } from 'react';
import { showToast } from '../Toast';
import './MetadataDiffPanel.css';
interface TableStats {
totalPosts: number;
publishedPosts: number;
draftPosts: number;
totalMedia: number;
}
interface DiffPost {
postId: string;
title: string;
slug: string;
dbValue: unknown;
fileValue: unknown;
}
interface DiffGroup {
field: string;
label: string;
posts: DiffPost[];
}
interface ScanResult {
totalScanned: number;
postsWithDifferences: number;
differences: Array<{
postId: string;
title: string;
slug: string;
filePath?: string;
hasDifferences: boolean;
differences: Record<string, { dbValue: unknown; fileValue: unknown }>;
}>;
groups: DiffGroup[];
}
type ScanPhase = 'idle' | 'loading-stats' | 'scanning' | 'complete';
export const MetadataDiffPanel: React.FC = () => {
const [stats, setStats] = useState<TableStats | null>(null);
const [scanResult, setScanResult] = useState<ScanResult | null>(null);
const [scanPhase, setScanPhase] = useState<ScanPhase>('idle');
const [progress, setProgress] = useState({ current: 0, total: 0, message: '' });
const [expandedGroups, setExpandedGroups] = useState<Set<string>>(new Set());
const [syncingGroups, setSyncingGroups] = useState<Set<string>>(new Set());
// Load initial stats
useEffect(() => {
const loadStats = async () => {
setScanPhase('loading-stats');
try {
const result = await window.electronAPI?.metadataDiff.getStats();
if (result) {
setStats(result);
}
} catch (error) {
console.error('Failed to load stats:', error);
showToast.error('Failed to load database statistics');
}
setScanPhase('idle');
};
loadStats();
}, []);
// Subscribe to task progress
useEffect(() => {
const unsubscribe = window.electronAPI?.on('task:progress', (data: unknown) => {
const progress = data as { id: string; progress: number; message?: string };
if (progress.id.startsWith('metadata-diff-scan')) {
setProgress({
current: Math.round(progress.progress),
total: 100,
message: progress.message || '',
});
}
});
return () => {
unsubscribe?.();
};
}, []);
const handleScan = useCallback(async () => {
setScanPhase('scanning');
setProgress({ current: 0, total: 100, message: 'Starting scan...' });
setScanResult(null);
try {
const result = await window.electronAPI?.metadataDiff.scan();
if (result) {
setScanResult(result);
// Auto-expand groups with differences
const groupsWithDiffs = new Set(result.groups.map(g => g.field));
setExpandedGroups(groupsWithDiffs);
}
setScanPhase('complete');
} catch (error) {
console.error('Scan failed:', error);
showToast.error('Failed to scan for differences');
setScanPhase('idle');
}
}, []);
const toggleGroup = (field: string) => {
setExpandedGroups(prev => {
const next = new Set(prev);
if (next.has(field)) {
next.delete(field);
} else {
next.add(field);
}
return next;
});
};
const handleSyncDbToFile = useCallback(async (group: DiffGroup) => {
const postIds = group.posts.map(p => p.postId);
setSyncingGroups(prev => new Set(prev).add(group.field));
try {
const result = await window.electronAPI?.metadataDiff.syncDbToFile(postIds, group.label);
if (result) {
showToast.success(`Synced ${result.success} posts to files${result.failed > 0 ? `, ${result.failed} failed` : ''}`);
// Re-scan to update the view
handleScan();
}
} catch (error) {
console.error('Sync failed:', error);
showToast.error('Failed to sync to files');
} finally {
setSyncingGroups(prev => {
const next = new Set(prev);
next.delete(group.field);
return next;
});
}
}, [handleScan]);
const handleSyncFileToDb = useCallback(async (group: DiffGroup) => {
const postIds = group.posts.map(p => p.postId);
setSyncingGroups(prev => new Set(prev).add(group.field));
try {
const result = await window.electronAPI?.metadataDiff.syncFileToDb(postIds, group.field, group.label);
if (result) {
showToast.success(`Synced ${result.success} files to database${result.failed > 0 ? `, ${result.failed} failed` : ''}`);
// Re-scan to update the view
handleScan();
}
} catch (error) {
console.error('Sync failed:', error);
showToast.error('Failed to sync to database');
} finally {
setSyncingGroups(prev => {
const next = new Set(prev);
next.delete(group.field);
return next;
});
}
}, [handleScan]);
const formatValue = (value: unknown): string => {
if (Array.isArray(value)) {
return value.length > 0 ? value.join(', ') : '(empty)';
}
if (value === null || value === undefined || value === '') {
return '(empty)';
}
return String(value);
};
return (
<div className="metadata-diff-panel">
<h2>Metadata Diff Tool</h2>
<p style={{ marginBottom: 16, color: 'var(--descriptionForeground)', fontSize: 13 }}>
Compare post metadata between database and markdown files. Fix inconsistencies caused by bugs or manual edits.
</p>
{/* Stats Section */}
{stats && (
<div className="diff-stats">
<div className="stat-item">
<span className="stat-label">Total Posts</span>
<span className="stat-value">{stats.totalPosts}</span>
</div>
<div className="stat-item">
<span className="stat-label">Published</span>
<span className="stat-value">{stats.publishedPosts}</span>
</div>
<div className="stat-item">
<span className="stat-label">Drafts</span>
<span className="stat-value">{stats.draftPosts}</span>
</div>
<div className="stat-item">
<span className="stat-label">Media Files</span>
<span className="stat-value">{stats.totalMedia}</span>
</div>
</div>
)}
{/* Progress Section */}
{scanPhase === 'scanning' && (
<div className="diff-progress">
<h3>Scanning published posts...</h3>
<div className="progress-bar-container">
<div
className="progress-bar"
style={{ width: `${progress.current}%` }}
/>
</div>
<div className="progress-text">{progress.message}</div>
</div>
)}
{/* Actions Section */}
<div className="diff-actions">
<button
className="primary"
onClick={handleScan}
disabled={scanPhase === 'scanning' || scanPhase === 'loading-stats'}
>
{scanPhase === 'scanning' ? (
<>
<span className="spinner" style={{ width: 14, height: 14 }} />
Scanning...
</>
) : scanResult ? (
'🔄 Re-scan'
) : (
'🔍 Scan for Differences'
)}
</button>
</div>
{/* Results Section */}
{scanPhase === 'complete' && scanResult && (
<div className="diff-results">
<div className={`diff-summary ${scanResult.postsWithDifferences > 0 ? 'has-differences' : 'no-differences'}`}>
{scanResult.postsWithDifferences === 0 ? (
<> No differences found! All {scanResult.totalScanned} published posts are in sync.</>
) : (
<>
Found <strong>{scanResult.postsWithDifferences}</strong> posts with differences
out of {scanResult.totalScanned} published posts.
</>
)}
</div>
{/* Groups */}
{scanResult.groups.map(group => (
<div key={group.field} className="diff-group">
<div
className="diff-group-header"
onClick={() => toggleGroup(group.field)}
>
<div className="diff-group-title">
<span className={`chevron ${expandedGroups.has(group.field) ? 'expanded' : ''}`}>
</span>
{group.label} Differences
</div>
<div className="diff-group-count">
<span className="badge">{group.posts.length} posts</span>
<div className="diff-group-actions" onClick={e => e.stopPropagation()}>
<button
className="db-to-file"
onClick={() => handleSyncDbToFile(group)}
disabled={syncingGroups.has(group.field)}
title="Update files with database values"
>
DB File
</button>
<button
className="file-to-db"
onClick={() => handleSyncFileToDb(group)}
disabled={syncingGroups.has(group.field)}
title="Update database with file values"
>
File DB
</button>
</div>
</div>
</div>
<div className={`diff-group-content ${!expandedGroups.has(group.field) ? 'collapsed' : ''}`}>
{group.posts.map(post => (
<div key={post.postId} className="diff-post-item">
<div className="diff-post-title" title={post.title}>
{post.title || post.slug}
</div>
<div>
<div className="diff-value-label">Database</div>
<div className="diff-value db-value" title={formatValue(post.dbValue)}>
{formatValue(post.dbValue)}
</div>
</div>
<div>
<div className="diff-value-label">File</div>
<div className="diff-value file-value" title={formatValue(post.fileValue)}>
{formatValue(post.fileValue)}
</div>
</div>
</div>
))}
</div>
</div>
))}
</div>
)}
{/* Empty state */}
{scanPhase === 'idle' && !scanResult && (
<div className="diff-empty">
<div className="icon">📊</div>
<div>Click "Scan for Differences" to compare database metadata with file metadata.</div>
</div>
)}
</div>
);
};

View File

@@ -0,0 +1 @@
export { MetadataDiffPanel } from './MetadataDiffPanel';

View File

@@ -6,7 +6,7 @@ import type { DeleteReference, ConfirmDeleteDetails } from '../components/Confir
const STORAGE_KEY = 'bds-app-state';
// Tab types
export type TabType = 'post' | 'media' | 'settings' | 'tags' | 'chat' | 'import';
export type TabType = 'post' | 'media' | 'settings' | 'tags' | 'chat' | 'import' | 'metadata-diff';
export interface Tab {
type: TabType;