import { FC, useEffect } from 'react'
import { useWorkspaces } from '@/pages/home/hooks/useHomeData'
import { workspaceItem } from '@/pages/home/types/HomeTypes'

const CheckWorkSpace: FC = () => {

    const getWorkspaces = useWorkspaces()

    useEffect(() => {
        if (getWorkspaces.data?.data?.workspaces) {
            const workspaceId = localStorage.getItem(import.meta.env.VITE_WORKSPACE_ID)
            const workspace = getWorkspaces.data?.data?.workspaces.find((item: workspaceItem) => item.id === workspaceId)
            if (!workspace) {
                localStorage.setItem(import.meta.env.VITE_WORKSPACE_ID, getWorkspaces.data?.data?.workspaces[0].id)
                window.location.reload()
            }
        }
    }, [getWorkspaces])


    return null
}

export default CheckWorkSpace