Skip to content

Commit

Permalink
feat: save chat in session storage
Browse files Browse the repository at this point in the history
  • Loading branch information
juancarlosfarah committed Apr 12, 2024
1 parent 7333e99 commit c8c6ed0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
14 changes: 12 additions & 2 deletions src/modules/interaction/ParticipantInteraction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,18 @@ const ParticipantInteraction = (): ReactElement => {
updatedAt: new Date(),
};

const [interaction, setInteraction] =
useState<Interaction>(defaultInteraction);
function load(key: string): Interaction {
const item = window.sessionStorage.getItem(key);
return item != null ? JSON.parse(item) : defaultInteraction;
}

const [interaction, setInteraction] = useState<Interaction>(
load('interaction'),
);

useEffect(() => {
window.sessionStorage.setItem('interaction', JSON.stringify(interaction));
}, [interaction]);

useEffect(() => {
const handleBeforeUnload = (event: BeforeUnloadEvent): string => {
Expand Down
38 changes: 35 additions & 3 deletions src/modules/message/MessagesPane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,43 @@ const MessagesPane = ({
const { mutateAsync: postAppDataAsync } = mutations.usePostAppData();
const { mutateAsync: postChatBot } = mutations.usePostChatBot();

function loadMessages(): Message[] {
const item = window.sessionStorage.getItem('messages');
return item != null ? JSON.parse(item) : [];
}

function loadExchange(): Exchange {
const item = window.sessionStorage.getItem('exchange');
return item != null ? JSON.parse(item) : defaultExchange;
}

function loadSentMessageCount(): number {
const item = window.sessionStorage.getItem('sentMessageCount');
return item != null ? parseInt(item, 10) : 0;
}

const [status, setStatus] = useState<Status>(Status.Idle);
const [exchange, setExchange] = useState<Exchange>(defaultExchange);
const [messages, setMessages] = useState<Message[]>([]);
const [exchange, setExchange] = useState<Exchange>(loadExchange());
const [messages, setMessages] = useState<Message[]>(loadMessages());
const [textAreaValue, setTextAreaValue] = useState('');
const [sentMessageCount, setSentMessageCount] = useState<number>(0);
const [sentMessageCount, setSentMessageCount] = useState<number>(
loadSentMessageCount(),
);

useEffect(() => {
window.sessionStorage.setItem('messages', JSON.stringify(messages));
}, [messages]);

useEffect(() => {
window.sessionStorage.setItem('exchange', JSON.stringify(exchange));
}, [exchange]);

useEffect(() => {
window.sessionStorage.setItem(
'sentMessageCount',
sentMessageCount.toString(),
);
}, [sentMessageCount]);

useEffect(() => {
const defaultMessages: Message[] = [
Expand Down

0 comments on commit c8c6ed0

Please sign in to comment.