feat: Phase 5 - Calendar + Dashboard + Search
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
This commit is contained in:
@@ -4,73 +4,39 @@
|
||||
// See AGENTS.md for full rules.
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { withAuth } from '@/lib/auth';
|
||||
import { createPocketBaseClient } from '@/lib/pocketbase';
|
||||
import { withAuth, createErrorResponse } from '@/lib/auth';
|
||||
import { searchEntities } from '@/lib/search-service';
|
||||
|
||||
// GET /api/search — Cross-entity full-text search
|
||||
//
|
||||
// Implementation note: the underlying data layer (`lib/database.ts`) uses a
|
||||
// JavaScript filter parser that only supports `=, !=, <=, >=, <, >` — it does
|
||||
// NOT understand PocketBase's `~` (contains) or `||` (or) operators. To make
|
||||
// search actually return results we fetch each collection's full list and
|
||||
// filter in-process with a case-insensitive substring match on the searchable
|
||||
// fields. This is fine at the current data scale and avoids the silent
|
||||
// zero-result bug.
|
||||
export const GET = withAuth(async (request: NextRequest, _user) => {
|
||||
// GET /api/search?q=&type=&domain=&limit=&offset=
|
||||
// Full-text search across all entity types using PostgreSQL tsvector/tsquery
|
||||
export const GET = withAuth(async (request: NextRequest, user) => {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const query = (searchParams.get('q') || '').trim();
|
||||
const types = (
|
||||
searchParams.get('types')?.split(',') || ['tasks', 'habits', 'projects', 'notes', 'reports']
|
||||
).filter((t) =>
|
||||
['tasks', 'habits', 'projects', 'notes', 'reports'].includes(t)
|
||||
);
|
||||
const limit = Math.max(1, Math.min(50, parseInt(searchParams.get('limit') || '10')));
|
||||
const q = (searchParams.get('q') || '').trim();
|
||||
const types = searchParams.get('types')?.split(',').filter(Boolean) || ['task', 'note', 'project', 'habit', 'domain'];
|
||||
const domain = searchParams.get('domain') || undefined;
|
||||
const limit = Math.max(1, Math.min(50, parseInt(searchParams.get('limit') || '20')));
|
||||
const offset = Math.max(0, parseInt(searchParams.get('offset') || '0'));
|
||||
|
||||
if (!query) {
|
||||
return NextResponse.json({ results: [] });
|
||||
if (!q) {
|
||||
return NextResponse.json({ results: [], totalCount: 0 });
|
||||
}
|
||||
|
||||
const needle = query.toLowerCase();
|
||||
const pb = createPocketBaseClient();
|
||||
const results: Array<{ type: string; items: unknown[] }> = [];
|
||||
try {
|
||||
const { results, totalCount } = await searchEntities({
|
||||
query: q,
|
||||
types,
|
||||
domainId: domain,
|
||||
limit,
|
||||
offset,
|
||||
});
|
||||
|
||||
type Searchable = Record<string, unknown> & { id: string };
|
||||
const matches = (record: Searchable, fields: string[]): boolean => {
|
||||
for (const f of fields) {
|
||||
const value = record[f];
|
||||
if (typeof value === 'string' && value.toLowerCase().includes(needle)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
const searchableFields: Record<string, string[]> = {
|
||||
tasks: ['title', 'description'],
|
||||
habits: ['name', 'description'],
|
||||
projects: ['name', 'description'],
|
||||
notes: ['title', 'content'],
|
||||
reports: ['title', 'content'],
|
||||
};
|
||||
|
||||
for (const type of types) {
|
||||
try {
|
||||
const items = (await pb.collection(type).getFullList()) as Searchable[];
|
||||
const filtered = items
|
||||
.filter((record) => matches(record, searchableFields[type] || []))
|
||||
.slice(0, limit)
|
||||
.map((record) => ({ id: record.id, title: getTitle(record, type) }));
|
||||
results.push({ type, items: filtered });
|
||||
} catch {
|
||||
// Skip collections that fail (e.g. missing or inaccessible)
|
||||
}
|
||||
return NextResponse.json({
|
||||
results,
|
||||
totalCount,
|
||||
query: q,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('[search GET] error:', error);
|
||||
return createErrorResponse('INTERNAL_ERROR', 'Search failed', 500);
|
||||
}
|
||||
|
||||
return NextResponse.json({ results });
|
||||
});
|
||||
|
||||
function getTitle(record: Record<string, unknown>, type: string): string {
|
||||
const title = record.title ?? record.name;
|
||||
if (typeof title === 'string' && title.length > 0) return title;
|
||||
return `Untitled ${type.slice(0, -1)}`;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user