import React, { useEffect, useState } from 'react'; import { useStore } from '@/store/useStore'; import { useTranslation } from 'react-i18next'; import { Bell, Folder, Upload, X } from 'lucide-react'; import { invoke } from '@tauri-apps/api/core'; import { listen } from '@tauri-apps/api/event'; export const FileWatcher: React.FC = () => { const { t } = useTranslation(); const [isTauri, setIsTauri] = useState(false); const [watchedPath, setWatchedPath] = useState(null); const [isWatching, setIsWatching] = useState(false); const [notifications, setNotifications] = useState([]); const { addChatMessage } = useStore(); useEffect(() => { // Check if running in Tauri if ((window as any).__TAURI_INTERNALS__) { setIsTauri(true); } }, []); useEffect(() => { if (!isTauri) return; // Listen for file-change events from Tauri const unlisten = listen('file-changed', async (event: any) => { const { path, filename, action } = event.payload; if (action === 'create' || action === 'modify') { const newNotif = { id: Date.now(), filename, path, status: 'detected', timestamp: new Date().toLocaleTimeString() }; setNotifications(prev => [newNotif, ...prev].slice(0, 5)); // Auto-upload and parse logic (mocked for now) // In a real app, we would read the file content via Tauri fs and upload to server addChatMessage({ id: `file-${Date.now()}`, role: 'system', content: `Detected file: ${filename}. Automatically parsing...`, timestamp: Date.now() }); } }); return () => { unlisten.then(f => f()); }; }, [isTauri, addChatMessage]); const startWatching = async () => { try { // In a real Tauri app, we'd use a dialog to pick a folder // For this demo, we'll use a mocked path or the downloads folder const path = await invoke('start_watching', { path: '/Users/by/Downloads/codex_watch' }); setWatchedPath(path as string); setIsWatching(true); } catch (err) { console.error('Failed to start watching:', err); } }; const stopWatching = async () => { try { await invoke('stop_watching'); setIsWatching(false); } catch (err) { console.error('Failed to stop watching:', err); } }; if (!isTauri) return null; return (
{/* Notifications */} {notifications.map(n => (
{n.filename} {n.timestamp}
))} {/* Control Panel */}
{isWatching ? (
Watching...
) : ( )}
); };