|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useContext, useEffect, useRef } from 'react'; |
| 4 | +import { toast } from 'sonner'; |
| 5 | +import { useRouter } from 'next/navigation'; |
| 6 | +import { ProjectContext } from './chat/code-engine/project-context'; |
| 7 | +import { logger } from '@/app/log/logger'; |
| 8 | +import { ProjectReadyToast } from './project-ready-toast'; |
| 9 | + |
| 10 | +const COMPLETED_CACHE_KEY = 'completedChatIds'; |
| 11 | + |
| 12 | +const getCompletedFromLocalStorage = (): Set<string> => { |
| 13 | + try { |
| 14 | + const raw = localStorage.getItem(COMPLETED_CACHE_KEY); |
| 15 | + if (raw) { |
| 16 | + return new Set(JSON.parse(raw)); |
| 17 | + } |
| 18 | + } catch (e) { |
| 19 | + logger.warn('Failed to read completedChatIds from localStorage'); |
| 20 | + } |
| 21 | + return new Set(); |
| 22 | +}; |
| 23 | + |
| 24 | +const saveCompletedToLocalStorage = (set: Set<string>) => { |
| 25 | + try { |
| 26 | + localStorage.setItem(COMPLETED_CACHE_KEY, JSON.stringify(Array.from(set))); |
| 27 | + } catch (e) { |
| 28 | + logger.warn('Failed to save completedChatIds to localStorage'); |
| 29 | + } |
| 30 | +}; |
| 31 | + |
| 32 | +const GlobalToastListener = () => { |
| 33 | + const { |
| 34 | + recentlyCompletedProjectId, |
| 35 | + setRecentlyCompletedProjectId, |
| 36 | + pollChatProject, |
| 37 | + setChatId, |
| 38 | + refreshProjects, |
| 39 | + refetchPublicProjects, |
| 40 | + setTempLoadingProjectId, |
| 41 | + } = useContext(ProjectContext); |
| 42 | + const router = useRouter(); |
| 43 | + const intervalRef = useRef<NodeJS.Timeout | null>(null); |
| 44 | + const completedIdsRef = useRef<Set<string>>(getCompletedFromLocalStorage()); |
| 45 | + |
| 46 | + const setCurrentChatid = (id: string) => {}; |
| 47 | + |
| 48 | + useEffect(() => { |
| 49 | + const chatId = recentlyCompletedProjectId; |
| 50 | + if (!chatId || completedIdsRef.current.has(chatId)) return; |
| 51 | + |
| 52 | + intervalRef.current = setInterval(async () => { |
| 53 | + try { |
| 54 | + const project = await pollChatProject(chatId); |
| 55 | + if (project?.projectPath) { |
| 56 | + await refreshProjects(); |
| 57 | + await refetchPublicProjects(); |
| 58 | + setTempLoadingProjectId(null); |
| 59 | + toast.custom( |
| 60 | + (t) => ( |
| 61 | + <ProjectReadyToast |
| 62 | + chatId={chatId} |
| 63 | + close={() => toast.dismiss(t)} |
| 64 | + router={router} |
| 65 | + setCurrentChatid={setCurrentChatid} |
| 66 | + setChatId={setChatId} |
| 67 | + /> |
| 68 | + ), |
| 69 | + { duration: 10000 } |
| 70 | + ); |
| 71 | + |
| 72 | + completedIdsRef.current.add(chatId); |
| 73 | + saveCompletedToLocalStorage(completedIdsRef.current); |
| 74 | + setRecentlyCompletedProjectId(null); |
| 75 | + |
| 76 | + if (intervalRef.current) { |
| 77 | + clearInterval(intervalRef.current); |
| 78 | + intervalRef.current = null; |
| 79 | + } |
| 80 | + } else { |
| 81 | + logger.debug(`Chat ${chatId} not ready yet...`); |
| 82 | + } |
| 83 | + } catch (e) { |
| 84 | + logger.error('pollChatProject error:', e); |
| 85 | + } |
| 86 | + }, 6000); |
| 87 | + |
| 88 | + return () => { |
| 89 | + if (intervalRef.current) clearInterval(intervalRef.current); |
| 90 | + }; |
| 91 | + }, [recentlyCompletedProjectId]); |
| 92 | + |
| 93 | + return null; |
| 94 | +}; |
| 95 | + |
| 96 | +export default GlobalToastListener; |
0 commit comments