Calendar: - GET /api/domains/[domainId]/calendar/events?from=&to= — returns tasks, habits, projects, milestones - PATCH /api/domains/[domainId]/tasks/[id]/schedule — drag-to-reschedule with activity feed - Calendar UI with month/week/day views via react-big-calendar - Drag-to-reschedule with SSE updates - Filter by entity type and domain - Keyboard shortcuts: t=today, m/w/d=view, ←/→=navigate - Mobile: auto-switches to day view on small screens Dashboard: - GET/PUT /api/domains/[domainId]/dashboard — layout stored in domain custom_fields - 8 per-widget data endpoints (today-tasks, habit-checklist, weekly-stats, project-progress, upcoming-calendar, recent-notes, activity-feed, quick-capture) - react-grid-layout with responsive breakpoints (12/8/4 cols) - Drag-to-reorder, resize, add/remove widgets - Edit mode toggle, per-workspace layout persistence - Widget error boundary Search: - tsvector columns + GIN indexes on tasks, notes, projects, habits, domains - GET /api/search?q=&types=&domain= — ranked results with ts_headline snippets - Dedicated search page with grouped results, filters, recent searches (localStorage) - Empty state with hints Schema: - Added custom_fields jsonb column to domains table (migration 0002) - Removed stale root app/ directory Build: passes, typecheck: passes, tests: 18/18 wikilink-parser tests pass
153 lines
4.2 KiB
TypeScript
153 lines
4.2 KiB
TypeScript
import { db, sql } from '@project-e/db';
|
|
|
|
export interface SearchResult {
|
|
id: string;
|
|
type: 'task' | 'note' | 'project' | 'habit' | 'domain';
|
|
title: string;
|
|
snippet: string;
|
|
score: number;
|
|
workspaceId: string;
|
|
link: string;
|
|
}
|
|
|
|
export interface SearchOptions {
|
|
query: string;
|
|
types?: string[];
|
|
domainId?: string;
|
|
limit?: number;
|
|
offset?: number;
|
|
}
|
|
|
|
interface EntityConfig {
|
|
table: string;
|
|
titleColumn: string;
|
|
contentColumn: string | null;
|
|
linkPrefix: string;
|
|
workspaceColumn: string;
|
|
deletedColumn: string | null;
|
|
}
|
|
|
|
const entityConfigs: Record<string, EntityConfig> = {
|
|
task: {
|
|
table: 'tasks',
|
|
titleColumn: 'title',
|
|
contentColumn: 'description',
|
|
linkPrefix: '/tasks',
|
|
workspaceColumn: 'domain_id',
|
|
deletedColumn: 'deleted_at',
|
|
},
|
|
note: {
|
|
table: 'notes',
|
|
titleColumn: 'title',
|
|
contentColumn: 'content',
|
|
linkPrefix: '/notes',
|
|
workspaceColumn: 'domain_id',
|
|
deletedColumn: 'deleted_at',
|
|
},
|
|
project: {
|
|
table: 'projects',
|
|
titleColumn: 'name',
|
|
contentColumn: 'description',
|
|
linkPrefix: '/projects',
|
|
workspaceColumn: 'domain_id',
|
|
deletedColumn: 'deleted_at',
|
|
},
|
|
habit: {
|
|
table: 'habits',
|
|
titleColumn: 'name',
|
|
contentColumn: 'description',
|
|
linkPrefix: '/habits',
|
|
workspaceColumn: 'domain_id',
|
|
deletedColumn: 'deleted_at',
|
|
},
|
|
domain: {
|
|
table: 'domains',
|
|
titleColumn: 'name',
|
|
contentColumn: null,
|
|
linkPrefix: '/settings',
|
|
workspaceColumn: 'id',
|
|
deletedColumn: null,
|
|
},
|
|
};
|
|
|
|
/**
|
|
* Full-text search across all entity types using PostgreSQL tsvector/tsquery.
|
|
* Uses websearch_to_tsquery for user-friendly query syntax.
|
|
* Generates snippets via ts_headline for highlighted matches.
|
|
*/
|
|
export async function searchEntities(options: SearchOptions): Promise<{
|
|
results: SearchResult[];
|
|
totalCount: number;
|
|
}> {
|
|
const { query, types = ['task', 'note', 'project', 'habit', 'domain'], domainId, limit = 20, offset = 0 } = options;
|
|
|
|
if (!query.trim()) {
|
|
return { results: [], totalCount: 0 };
|
|
}
|
|
|
|
// Sanitize query for websearch
|
|
const sanitized = query.replace(/['"\\]/g, '').trim();
|
|
if (!sanitized) {
|
|
return { results: [], totalCount: 0 };
|
|
}
|
|
|
|
const results: SearchResult[] = [];
|
|
|
|
for (const type of types) {
|
|
const config = entityConfigs[type];
|
|
if (!config) continue;
|
|
|
|
const { table, titleColumn, contentColumn, linkPrefix, workspaceColumn, deletedColumn } = config;
|
|
|
|
// Build conditions
|
|
const conditions: string[] = [`search_vector @@ websearch_to_tsquery('english', '${sanitized.replace(/'/g, "''")}')`];
|
|
if (domainId && workspaceColumn !== 'id') {
|
|
conditions.push(`${workspaceColumn} = '${domainId}'::uuid`);
|
|
}
|
|
if (deletedColumn) {
|
|
conditions.push(`${deletedColumn} IS NULL`);
|
|
}
|
|
|
|
const whereClause = conditions.join(' AND ');
|
|
|
|
// Use the content column for headline if available, otherwise use title
|
|
const headlineColumn = contentColumn || titleColumn;
|
|
|
|
const queryStr = `
|
|
SELECT
|
|
id,
|
|
${titleColumn} AS title,
|
|
ts_headline('english', ${headlineColumn}, websearch_to_tsquery('english', '${sanitized.replace(/'/g, "''")}'),
|
|
'MaxWords=30, MinWords=15, ShortWord=3, HighlightAll=FALSE, StartSel=<mark>, StopSel=</mark>, FragmentDelimiter=...'
|
|
) AS snippet,
|
|
ts_rank(search_vector, websearch_to_tsquery('english', '${sanitized.replace(/'/g, "''")}')) AS score,
|
|
${workspaceColumn} AS workspace_id
|
|
FROM ${table}
|
|
WHERE ${whereClause}
|
|
ORDER BY score DESC
|
|
LIMIT 50
|
|
`;
|
|
|
|
const rows: any[] = await sql.unsafe(queryStr);
|
|
|
|
for (const row of rows) {
|
|
results.push({
|
|
id: String(row.id),
|
|
type: type as SearchResult['type'],
|
|
title: String(row.title || ''),
|
|
snippet: String(row.snippet || ''),
|
|
score: Number(row.score || 0),
|
|
workspaceId: String(row.workspace_id || ''),
|
|
link: `${linkPrefix}/${row.id}`,
|
|
});
|
|
}
|
|
}
|
|
|
|
// Sort by score descending, then apply pagination
|
|
results.sort((a, b) => b.score - a.score);
|
|
const totalCount = results.length;
|
|
const paginated = results.slice(offset, offset + limit);
|
|
|
|
return { results: paginated, totalCount };
|
|
}
|