mirror of
https://github.com/NovaOSS/nova-betterchat.git
synced 2024-11-26 00:34:00 +01:00
4fa5c14734
fixes partially #9 and #16
20 lines
516 B
TypeScript
20 lines
516 B
TypeScript
import React, { useEffect, useRef } from 'react';
|
|
import useStore from '@store/store';
|
|
|
|
const useSaveToLocalStorage = () => {
|
|
const chatsRef = useRef(useStore.getState().chats);
|
|
|
|
useEffect(() => {
|
|
const unsubscribe = useStore.subscribe((state) => {
|
|
if (chatsRef && chatsRef.current !== state.chats) {
|
|
chatsRef.current = state.chats;
|
|
localStorage.setItem('chats', JSON.stringify(state.chats));
|
|
}
|
|
});
|
|
|
|
return unsubscribe;
|
|
}, []);
|
|
};
|
|
|
|
export default useSaveToLocalStorage;
|