import { Fragment } from 'react' import { switchWorkspace } from '@/service/common' import { Menu, Transition } from '@headlessui/react' import { ChevronRightIcon, CheckIcon } from '@heroicons/react/24/outline' import cn from 'classnames' import s from './index.module.css' import { useContext } from 'use-context-selector' import { ToastContext } from '@/app/components/base/toast' import { useTranslation } from 'react-i18next' import { useRouter } from 'next/navigation' import { useWorkspacesContext } from '@/context/workspace-context' const itemClassName = ` flex items-center px-3 py-2 h-10 cursor-pointer ` const itemIconClassName = ` shrink-0 mr-2 w-6 h-6 bg-[#EFF4FF] rounded-md ` const itemNameClassName = ` grow mr-2 text-sm text-gray-700 text-left ` const itemCheckClassName = ` shrink-0 w-4 h-4 text-primary-600 ` const WorkplaceSelector = () => { const { t } = useTranslation() const router = useRouter() const { notify } = useContext(ToastContext) const { workspaces } = useWorkspacesContext() const currentWrokspace = workspaces.filter(item => item.current)?.[0] const handleSwitchWorkspace = async (tenant_id: string) => { try { await switchWorkspace({ url: `/workspaces/switch`, body: { tenant_id } }) notify({ type: 'success', message: t('common.actionMsg.modifiedSuccessfully') }) router.replace('/apps') } catch (e) { notify({ type: 'error', message: t('common.provider.saveFailed') }) } finally { } } return ( { ({ open }) => ( <>
{currentWrokspace?.name}
{ workspaces.map(workspace => (
handleSwitchWorkspace(workspace.id)}>
{workspace.name}
{workspace.current && }
)) }
) }
) } export default WorkplaceSelector