Files
Momento/memento-note/hooks/use-auto-tagging.ts
Sepehr Ramezani aa6a214f37 feat: rename keep-notes to memento-note, migrate to PostgreSQL, fix MCP bugs
- Rename directory keep-notes -> memento-note with all code references
- Prisma: SQLite -> PostgreSQL (both app and MCP server schemas)
- Sync MCP schema with main app (add missing fields, relations, indexes)
- Delete 17 SQLite migrations (clean slate for PostgreSQL)
- Remove SQLite dependencies (@libsql/client, better-sqlite3, etc.)
- Fix MCP server: hardcoded Windows DB paths -> DATABASE_URL env var
- Fix MCP server: .dockerignore excluded index-sse.js (SSE mode broken)
- MCP Dockerfile: node:20 -> node:22
- Docker Compose: add postgres service, remove SQLite volume
- Generate favicon.ico, icon-192.png, icon-512.png, apple-icon.png
- Update layout.tsx icons and manifest.json for PNG icons
- Update all .env files for PostgreSQL
- Rewrite README.md with updated sections
- Remove mcp-server/node_modules and prisma/client-generated from git tracking

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-04-20 20:58:04 +02:00

95 lines
2.7 KiB
TypeScript

import { useState, useEffect, useRef } from 'react';
import { useDebounce } from './use-debounce';
import { TagSuggestion } from '@/lib/ai/types';
interface UseAutoTaggingProps {
content: string;
notebookId?: string | null;
enabled?: boolean;
}
export function useAutoTagging({ content, notebookId, enabled = true }: UseAutoTaggingProps) {
const [suggestions, setSuggestions] = useState<TagSuggestion[]>([]);
const [isAnalyzing, setIsAnalyzing] = useState(false);
const [error, setError] = useState<string | null>(null);
// Debounce le contenu de 1.5s
const debouncedContent = useDebounce(content, 1500);
// Track previous notebookId to detect when note is moved to a notebook
const previousNotebookId = useRef<string | null | undefined>(notebookId);
const analyzeContent = async (contentToAnalyze: string) => {
// CRITICAL: Don't suggest labels in "Notes générales" (notebookId is null)
// Labels should ONLY appear within notebooks, not in the general notes section
if (!notebookId) {
setSuggestions([]);
return;
}
if (!contentToAnalyze || contentToAnalyze.length < 10) {
setSuggestions([]);
return;
}
setIsAnalyzing(true);
setError(null);
try {
const response = await fetch('/api/ai/tags', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
content: contentToAnalyze,
notebookId: notebookId || undefined,
language: document.documentElement.lang || 'en',
}),
});
if (!response.ok) {
throw new Error('Erreur lors de l\'analyse');
}
const data = await response.json();
setSuggestions(data.tags || []);
} catch (err) {
setError('Impossible de générer des suggestions');
} finally {
setIsAnalyzing(false);
}
};
// Trigger on content change
useEffect(() => {
if (!enabled) {
setSuggestions([]);
return;
}
analyzeContent(debouncedContent);
}, [debouncedContent, enabled]);
// CRITICAL: Also trigger when notebookId changes from null/undefined to a value (note moved to notebook)
useEffect(() => {
if (!enabled) return;
const prev = previousNotebookId.current;
previousNotebookId.current = notebookId;
// Detect when note is moved FROM "Notes générales" (null) TO a notebook
const wasMovedToNotebook = (prev === null || prev === undefined) && notebookId;
if (wasMovedToNotebook && content && content.length >= 10) {
// Use current content immediately (no debounce) when moving to notebook
analyzeContent(content);
}
}, [notebookId, content, enabled]);
return {
suggestions,
isAnalyzing,
error,
clearSuggestions: () => setSuggestions([]),
};
}