2023-03-05 15:59:31 +01:00
|
|
|
import React, { useEffect, useRef } from 'react';
|
2023-03-03 23:17:11 +01:00
|
|
|
import useStore from '@store/store';
|
|
|
|
|
|
|
|
const useSaveToLocalStorage = () => {
|
2023-03-05 15:59:31 +01:00
|
|
|
const chatsRef = useRef(useStore.getState().chats);
|
2023-03-03 23:17:11 +01:00
|
|
|
|
|
|
|
useEffect(() => {
|
2023-03-05 15:59:31 +01:00
|
|
|
const unsubscribe = useStore.subscribe((state) => {
|
|
|
|
if (chatsRef && chatsRef.current !== state.chats) {
|
|
|
|
chatsRef.current = state.chats;
|
|
|
|
localStorage.setItem('chats', JSON.stringify(state.chats));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return unsubscribe;
|
|
|
|
}, []);
|
2023-03-03 23:17:11 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
export default useSaveToLocalStorage;
|