Rend les liens entre notes visibles et persistants (sync NoteLink au save, auto-save, graphe réseau rafraîchi), ajoute living blocks, Memory Echo, recherche globale, consentement IA explicite et consolide les prototypes design en architectural-grid. Co-authored-by: Cursor <cursoragent@cursor.com>
100 lines
2.6 KiB
TypeScript
100 lines
2.6 KiB
TypeScript
import { useState, useEffect, useRef } from 'react'
|
|
import { useDebounce } from './use-debounce'
|
|
import { useAiConsent } from '@/components/legal/ai-consent-provider'
|
|
|
|
export interface TitleSuggestion {
|
|
title: string
|
|
confidence: number
|
|
reasoning?: string
|
|
}
|
|
|
|
interface UseTitleSuggestionsProps {
|
|
content: string
|
|
enabled?: boolean
|
|
}
|
|
|
|
export function useTitleSuggestions({ content, enabled = true }: UseTitleSuggestionsProps) {
|
|
const { requestAiConsent, hasAiConsent } = useAiConsent()
|
|
const [suggestions, setSuggestions] = useState<TitleSuggestion[]>([])
|
|
const [isAnalyzing, setIsAnalyzing] = useState(false)
|
|
const [error, setError] = useState<string | null>(null)
|
|
const abortRef = useRef<AbortController | null>(null)
|
|
|
|
// Debounce content by 2s to avoid excessive API calls
|
|
const debouncedContent = useDebounce(content, 2000)
|
|
|
|
useEffect(() => {
|
|
if (!enabled || !debouncedContent) {
|
|
setSuggestions([])
|
|
return
|
|
}
|
|
|
|
const wordCount = debouncedContent.split(/\s+/).length
|
|
|
|
// Need at least 10 words
|
|
if (wordCount < 10) {
|
|
setSuggestions([])
|
|
return
|
|
}
|
|
|
|
// Cancel previous request
|
|
abortRef.current?.abort()
|
|
const controller = new AbortController()
|
|
abortRef.current = controller
|
|
|
|
const generateTitles = async () => {
|
|
if (!hasAiConsent) {
|
|
const consented = await requestAiConsent()
|
|
if (!consented) {
|
|
setSuggestions([])
|
|
return
|
|
}
|
|
}
|
|
|
|
setIsAnalyzing(true)
|
|
setError(null)
|
|
|
|
try {
|
|
const response = await fetch('/api/ai/title-suggestions', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ content: debouncedContent }),
|
|
signal: controller.signal,
|
|
})
|
|
|
|
if (controller.signal.aborted) return
|
|
|
|
if (!response.ok) {
|
|
const errorData = await response.json()
|
|
throw new Error(errorData.error || 'Error generating title suggestions')
|
|
}
|
|
|
|
const data = await response.json()
|
|
setSuggestions(data.suggestions || [])
|
|
} catch (err: any) {
|
|
if (err.name === 'AbortError') return
|
|
console.error('Title suggestions error:', err)
|
|
setError('Failed to generate title suggestions')
|
|
} finally {
|
|
if (!controller.signal.aborted) {
|
|
setIsAnalyzing(false)
|
|
}
|
|
}
|
|
}
|
|
|
|
generateTitles()
|
|
}, [debouncedContent, enabled, hasAiConsent, requestAiConsent])
|
|
|
|
// Cleanup on unmount
|
|
useEffect(() => {
|
|
return () => { abortRef.current?.abort(); };
|
|
}, []);
|
|
|
|
return {
|
|
suggestions,
|
|
isAnalyzing,
|
|
error,
|
|
clearSuggestions: () => setSuggestions([])
|
|
}
|
|
}
|