Something went wrong. Try again.
An AI-powered SaaS development platform for building, customizing, and deploying modern applications directly from natural language at scal… zentro.arjunkrishna.dev
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687import type { ProjectStatus } from '~/types/chat'
export const useProjectStatus = (chatId: ComputedRef<string>) => { const { apiClient, API_ENDPOINTS } = useApi()
const isProjectReady = ref(false) const codeServerUrl = ref('') const previewUrl = ref('') const workerHost = ref('') const statusPollInterval = ref<ReturnType<typeof setInterval> | null>(null)
const pollStatus = async (): Promise<{ shouldConnect: boolean; workerHost: string | null }> => { if (!chatId.value) { return { shouldConnect: false, workerHost: null } }
try { const response = await apiClient.get<ProjectStatus>(API_ENDPOINTS.projects.status(chatId.value)) const data = response.data
if (data.status === 'ready' && data.codeServerHost) { isProjectReady.value = true codeServerUrl.value = data.codeServerHost workerHost.value = data.workerHost || '' previewUrl.value = data.devServerHost || ''
// Stop polling once ready if (statusPollInterval.value) { clearInterval(statusPollInterval.value) statusPollInterval.value = null }
return { shouldConnect: true, workerHost: workerHost.value } } } catch (err) { console.error('Error polling project status:', err) }
return { shouldConnect: false, workerHost: null } }
const startPolling = (onReady?: (workerHost: string) => void) => { if (!chatId.value) return
// Initial poll pollStatus().then(({ shouldConnect, workerHost: host }) => { if (shouldConnect && host && onReady) { onReady(host) } })
// Poll every 5 seconds statusPollInterval.value = setInterval(async () => { const { shouldConnect, workerHost: host } = await pollStatus() if (shouldConnect && host && onReady) { onReady(host) } }, 5000) }
const stopPolling = () => { if (statusPollInterval.value) { clearInterval(statusPollInterval.value) statusPollInterval.value = null } }
const reset = () => { isProjectReady.value = false codeServerUrl.value = '' previewUrl.value = '' workerHost.value = '' stopPolling() }
return { isProjectReady: readonly(isProjectReady), codeServerUrl: readonly(codeServerUrl), previewUrl: readonly(previewUrl), workerHost: readonly(workerHost), startPolling, stopPolling, reset, }}