Skip to content

Commit d7531f9

Browse files
author
pengyu
committed
finish toast part remove let chatbar handle polling
1 parent dfea588 commit d7531f9

File tree

4 files changed

+51
-13
lines changed

4 files changed

+51
-13
lines changed

frontend/src/components/chat/code-engine/code-engine.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,15 @@ export function CodeEngine({
4646
const [isCompleting, setIsCompleting] = useState(false);
4747
// 添加一个ref来持久跟踪项目状态,避免重新渲染时丢失
4848
const isProjectLoadedRef = useRef(false);
49+
const context = useContext(ProjectContext);
50+
if (!context) throw new Error('Must be used inside ProjectProvider');
51+
const { setRecentlyCompletedProjectId } = context;
4952

53+
useEffect(() => {
54+
if (projectCompleted) {
55+
setRecentlyCompletedProjectId(curProject?.id || localProject?.id);
56+
}
57+
}, [projectCompleted]);
5058
// 在组件挂载时从localStorage检查项目是否已完成
5159
useEffect(() => {
5260
try {

frontend/src/components/chat/code-engine/project-context.tsx

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ export interface ProjectContextType {
5050
takeProjectScreenshot: (projectId: string, url: string) => Promise<void>;
5151
refreshProjects: () => Promise<void>;
5252
editorRef?: React.MutableRefObject<any>;
53+
recentlyCompletedProjectId: string | null;
54+
setRecentlyCompletedProjectId: (id: string | null) => void;
55+
chatId: string | null;
56+
setChatId: (chatId: string | null) => void;
5357
}
5458

5559
export const ProjectContext = createContext<ProjectContextType | undefined>(
@@ -105,7 +109,10 @@ export function ProjectProvider({ children }: { children: ReactNode }) {
105109
const [filePath, setFilePath] = useState<string | null>(null);
106110
const [isLoading, setIsLoading] = useState<boolean>(false);
107111
const editorRef = useRef<any>(null);
108-
112+
const [recentlyCompletedProjectId, setRecentlyCompletedProjectId] = useState<
113+
string | null
114+
>(null);
115+
const [chatId, setChatId] = useState<string | null>(null);
109116
interface ChatProjectCacheEntry {
110117
project: Project | null;
111118
timestamp: number;
@@ -142,6 +149,24 @@ export function ProjectProvider({ children }: { children: ReactNode }) {
142149
pendingOperations.current.clear();
143150
};
144151
}, []);
152+
// Poll project data every 5 seconds
153+
useEffect(() => {
154+
if (!chatId) return;
155+
let stopped = false;
156+
157+
const interval = setInterval(async () => {
158+
const project = await pollChatProject(chatId);
159+
if (project?.projectPath) {
160+
setCurProject(project);
161+
clearInterval(interval);
162+
stopped = true;
163+
}
164+
}, 5000);
165+
166+
return () => {
167+
if (!stopped) clearInterval(interval);
168+
};
169+
}, [chatId]);
145170

146171
// Function to clean expired cache entries
147172
const cleanCache = useCallback(() => {
@@ -914,6 +939,8 @@ export function ProjectProvider({ children }: { children: ReactNode }) {
914939
takeProjectScreenshot,
915940
refreshProjects,
916941
editorRef,
942+
chatId,
943+
setChatId,
917944
}),
918945
[
919946
projects,
@@ -930,11 +957,19 @@ export function ProjectProvider({ children }: { children: ReactNode }) {
930957
takeProjectScreenshot,
931958
refreshProjects,
932959
editorRef,
960+
chatId,
961+
setChatId,
933962
]
934963
);
935964

936965
return (
937-
<ProjectContext.Provider value={contextValue}>
966+
<ProjectContext.Provider
967+
value={{
968+
...contextValue,
969+
recentlyCompletedProjectId,
970+
setRecentlyCompletedProjectId,
971+
}}
972+
>
938973
{children}
939974
</ProjectContext.Provider>
940975
);

frontend/src/components/sidebar.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,20 +96,17 @@ function ChatSideBarComponent({
9696
}: SidebarProps) {
9797
const router = useRouter();
9898
const [currentChatid, setCurrentChatid] = useState('');
99-
const { setCurProject, pollChatProject } = useContext(ProjectContext);
99+
const { setChatId } = useContext(ProjectContext);
100100

101101
const handleChatSelect = useCallback(
102102
(chatId: string) => {
103103
setCurrentChatid(chatId);
104104
router.push(`/chat?id=${chatId}`);
105-
setCurProject(null);
106-
pollChatProject(chatId).then((p) => {
107-
setCurProject(p);
108-
});
105+
setChatId(chatId);
109106
const event = new Event(EventEnum.CHAT);
110107
window.dispatchEvent(event);
111108
},
112-
[router, setCurProject, pollChatProject]
109+
[router]
113110
);
114111

115112
if (loading) return <SidebarSkeleton />;

frontend/src/providers/BaseProvider.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@ import dynamic from 'next/dynamic';
44
import { ThemeProvider } from 'next-themes';
55
import { Toaster } from 'sonner';
66
import { AuthProvider } from './AuthProvider';
7-
import {
8-
ProjectContext,
9-
ProjectProvider,
10-
} from '@/components/chat/code-engine/project-context';
11-
7+
import { ProjectProvider } from '@/components/chat/code-engine/project-context';
8+
import GlobalToastListener from '@/components/global-toast-listener';
129
const DynamicApolloProvider = dynamic(() => import('./DynamicApolloProvider'), {
1310
ssr: false, // disables SSR for the ApolloProvider
1411
});
@@ -23,6 +20,7 @@ export function BaseProviders({ children }: ProvidersProps) {
2320
<DynamicApolloProvider>
2421
<AuthProvider>
2522
<ProjectProvider>
23+
<GlobalToastListener />
2624
{children}
2725
<Toaster position="bottom-right" />
2826
</ProjectProvider>

0 commit comments

Comments
 (0)