From bd6f1d509017e624e349454d9bec4a9589f72e6f Mon Sep 17 00:00:00 2001 From: Jeremy Kahn Date: Tue, 25 Oct 2022 20:17:58 -0500 Subject: [PATCH] feat: [closes #46] limit chat transcripts to 10,000 messages --- src/components/Room/useRoom.ts | 10 ++++++++-- src/config/messaging.ts | 1 + 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/components/Room/useRoom.ts b/src/components/Room/useRoom.ts index ab6a0a5..d2ae979 100644 --- a/src/components/Room/useRoom.ts +++ b/src/components/Room/useRoom.ts @@ -6,13 +6,15 @@ import { v4 as uuid } from 'uuid' import { ShellContext } from 'contexts/ShellContext' import { SettingsContext } from 'contexts/SettingsContext' import { PeerActions } from 'models/network' -import { ReceivedMessage, UnsentMessage } from 'models/chat' +import { ReceivedMessage, UnsentMessage, Message } from 'models/chat' import { funAnimalName } from 'fun-animal-names' import { getPeerName } from 'components/PeerNameDisplay' import { NotificationService } from 'services/Notification' import { Audio } from 'services/Audio' import { PeerRoom } from 'services/PeerRoom' +import { messageTranscriptSizeLimit } from 'config/messaging' + import { usePeerRoomAction } from './usePeerRoomAction' interface UseRoomConfig { @@ -30,13 +32,17 @@ export function useRoom( const shellContext = useContext(ShellContext) const settingsContext = useContext(SettingsContext) const [isMessageSending, setIsMessageSending] = useState(false) - const [messageLog, setMessageLog] = useState< + const [messageLog, _setMessageLog] = useState< Array >([]) const [newMessageAudio] = useState( () => new Audio(process.env.PUBLIC_URL + '/sounds/new-message.aac') ) + const setMessageLog = (messages: Message[]) => { + _setMessageLog(messages.slice(-messageTranscriptSizeLimit)) + } + useEffect(() => { return () => { peerRoom.leaveRoom() diff --git a/src/config/messaging.ts b/src/config/messaging.ts index 3a7b72a..eb42d02 100644 --- a/src/config/messaging.ts +++ b/src/config/messaging.ts @@ -1 +1,2 @@ export const messageCharacterSizeLimit = 10_000 +export const messageTranscriptSizeLimit = 10_000