import { $, Dialog } from "mdui" import AppStateContext, { AppState } from "./AppStateContext.ts" import { Chat, User } from "lingchair-client-protocol" import getClient from "../../getClient.ts" import UserOrChatInfoDialog from "./UserOrChatInfoDialog.tsx" import useEffectRef from "../../utils/useEffectRef.ts" import EditMyProfileDialog from "./EditMyProfileDialog.tsx" import AddFavourtieChatDialog from "./AddFavourtieChatDialog.tsx" import * as React from 'react' import { useContextSelector } from "use-context-selector" import MainSharedContext, { Shared } from "../MainSharedContext.ts" import ChatFragmentDialog from "./ChatFragmentDialog.tsx" import useAsyncEffect from "../../utils/useAsyncEffect.ts" import ClientCache from "../../ClientCache.ts" const config = await fetch('/config.json').then((re) => re.json()) export default function DialogContextWrapper({ children, useRef }: { children: React.ReactNode, useRef: React.MutableRefObject }) { const [userOrChatInfoDialogState, setUserOrChatInfoDialogState] = React.useState([]) const lastUserOrChatInfoDialogStateRef = React.useRef() const userOrChatInfoDialogRef = useEffectRef((ref) => { ref.current!.addEventListener('closed', () => { setUserOrChatInfoDialogState([]) }) ref.current!.addEventListener('overlay-click', () => { ref.current!.open = false }) }, []) React.useEffect(() => { userOrChatInfoDialogState.length != 0 && (lastUserOrChatInfoDialogStateRef.current = userOrChatInfoDialogState[userOrChatInfoDialogState.length - 1]) userOrChatInfoDialogRef.current!.open = userOrChatInfoDialogState.length != 0 }, [userOrChatInfoDialogState]) const editMyProfileDialogRef = React.useRef() const addFavouriteChatDialogRef = React.useRef() const setCurrentSelectedChatId = useContextSelector( MainSharedContext, (context: Shared) => context.setCurrentSelectedChatId ) const currentSelectedChatId = useContextSelector( MainSharedContext, (context: Shared) => context.state.currentSelectedChatId ) const chatFragmentDialogRef = React.useRef() useAsyncEffect(async () => { document.title = (currentSelectedChatId && currentSelectedChatId != '' && await ClientCache.getChat(currentSelectedChatId).then((v) => v?.getTitle()) + ' | ') + (config.title || 'LingChair') }, [currentSelectedChatId]) return setCurrentSelectedChatId('')) } else setCurrentSelectedChatId('') } }}> {children} }