diff --git a/src/gameUnload.ts b/src/gameUnload.ts new file mode 100644 index 00000000..d5622411 --- /dev/null +++ b/src/gameUnload.ts @@ -0,0 +1,17 @@ +import { subscribe } from 'valtio' +import { miscUiState } from './globalState' + +let toCleanup = [] as Array<() => void> + +export const watchUnloadForCleanup = (func: () => void) => { + toCleanup.push(func) +} + +subscribe(miscUiState, () => { + if (!miscUiState.gameLoaded) { + for (const func of toCleanup) { + func() + } + toCleanup = [] + } +}) diff --git a/src/react/Chat.stories.tsx b/src/react/Chat.stories.tsx index 7790f3dc..93585dd8 100644 --- a/src/react/Chat.stories.tsx +++ b/src/react/Chat.stories.tsx @@ -95,6 +95,7 @@ type Story = StoryObj export const Primary: Story = { args: { + usingTouch: false, messages: [{ parts: [ { diff --git a/src/react/ChatContainer.tsx b/src/react/ChatContainer.tsx index ccb8e9a2..5d9d0585 100644 --- a/src/react/ChatContainer.tsx +++ b/src/react/ChatContainer.tsx @@ -29,6 +29,7 @@ const MessageLine = ({ message }: { message: Message }) => { type Props = { messages: Message[] + usingTouch: boolean opacity?: number opened?: boolean onClose?: () => void @@ -52,9 +53,7 @@ export const fadeMessage = (message: Message, initialTimeout: boolean, requestUp }, initialTimeout ? 5000 : 0) } -export default ({ messages, opacity = 1, fetchCompletionItems, opened, sendMessage, onClose }: Props) => { - const usingTouch = useSnapshot(miscUiState).currentTouch - +export default ({ messages, opacity = 1, fetchCompletionItems, opened, sendMessage, onClose, usingTouch }: Props) => { const sendHistoryRef = useRef(JSON.parse(window.sessionStorage.chatHistory || '[]')) const [completePadText, setCompletePadText] = useState('') diff --git a/src/react/ChatProvider.tsx b/src/react/ChatProvider.tsx index 9275b14a..46f81fb8 100644 --- a/src/react/ChatProvider.tsx +++ b/src/react/ChatProvider.tsx @@ -1,7 +1,8 @@ import { useEffect, useRef, useState } from 'react' +import { useSnapshot } from 'valtio' import { formatMessage } from '../botUtils' import { getBuiltinCommandsList, tryHandleBuiltinCommand } from '../builtinCommands' -import { hideCurrentModal } from '../globalState' +import { hideCurrentModal, miscUiState } from '../globalState' import { options } from '../optionsStorage' import ChatContainer, { Message, fadeMessage } from './ChatContainer' import { useIsModalActive } from './utils' @@ -11,6 +12,7 @@ export default () => { const isChatActive = useIsModalActive('chat') const { messagesLimit, chatOpacity, chatOpacityOpened } = options const lastMessageId = useRef(0) + const usingTouch = useSnapshot(miscUiState).currentTouch useEffect(() => { bot.addListener('message', (jsonMsg, position) => { @@ -33,6 +35,7 @@ export default () => { }, []) return + +const Button2 = ({ title, icon }) => { + //@ts-expect-error + return