databag/app/mobile/src/session/useSession.hook.js

63 lines
1.5 KiB
JavaScript
Raw Normal View History

2022-10-24 06:37:14 +00:00
import { useRef, useState, useEffect, useContext } from 'react';
2022-09-16 20:06:52 +00:00
import { useWindowDimensions } from 'react-native';
import { useNavigate } from 'react-router-dom';
2022-09-17 07:22:12 +00:00
import config from 'constants/Config';
2022-10-24 20:11:28 +00:00
import { StoreContext } from 'context/StoreContext';
2022-09-16 20:06:52 +00:00
export function useSession() {
const [state, setState] = useState({
2022-09-17 07:22:12 +00:00
tabbled: null,
2022-10-31 00:22:05 +00:00
subWidth: '50%',
baseWidth: '50%',
2022-09-19 05:42:27 +00:00
cardId: null,
converstaionId: null,
2022-10-24 20:11:28 +00:00
firstRun: null,
2022-09-16 20:06:52 +00:00
});
2022-10-24 06:37:14 +00:00
2022-10-24 20:11:28 +00:00
const store = useContext(StoreContext);
2022-09-17 07:22:12 +00:00
const dimensions = useWindowDimensions();
2022-09-16 20:06:52 +00:00
const navigate = useNavigate();
2022-10-24 06:37:14 +00:00
const tabbed = useRef(null);
2022-09-16 20:06:52 +00:00
const updateState = (value) => {
setState((s) => ({ ...s, ...value }));
}
2022-10-24 20:11:28 +00:00
useEffect(() => {
checkFirstRun();
}, []);
const checkFirstRun = async () => {
const firstRun = await store.actions.getFirstRun();
updateState({ firstRun });
}
2022-09-17 07:22:12 +00:00
useEffect(() => {
2022-10-24 06:37:14 +00:00
if (tabbed.current !== true) {
if (dimensions.width > config.tabbedWidth) {
const width = Math.floor((dimensions.width * 33) / 100);
tabbed.current = false;
2022-10-31 00:22:05 +00:00
updateState({ tabbed: false, baseWidth: width + 50, subWidth: width });
2022-09-19 00:04:17 +00:00
}
else {
2022-10-24 06:37:14 +00:00
tabbed.current = true;
updateState({ tabbed: true });
2022-09-19 00:04:17 +00:00
}
}
2022-09-17 07:22:12 +00:00
}, [dimensions]);
2022-09-16 20:06:52 +00:00
const actions = {
setCardId: (cardId) => {
updateState({ cardId });
2022-10-24 20:11:28 +00:00
},
clearFirstRun: () => {
updateState({ firstRun: false });
store.actions.setFirstRun();
},
2022-09-16 20:06:52 +00:00
};
return { state, actions };
}