mirror of
https://github.com/cheeaun/phanpy.git
synced 2025-03-30 01:21:36 +01:00
Experimental: caching status + context
Cache all statuses from Status page so that it can load faster when navigating between statuses. Uses sessionStorage
This commit is contained in:
parent
3fede678c7
commit
04323f05ff
1 changed files with 12 additions and 2 deletions
|
@ -13,20 +13,29 @@ import Loader from '../components/loader';
|
||||||
import Status from '../components/status';
|
import Status from '../components/status';
|
||||||
import shortenNumber from '../utils/shorten-number';
|
import shortenNumber from '../utils/shorten-number';
|
||||||
import states from '../utils/states';
|
import states from '../utils/states';
|
||||||
|
import store from '../utils/store';
|
||||||
import useTitle from '../utils/useTitle';
|
import useTitle from '../utils/useTitle';
|
||||||
|
|
||||||
function StatusPage({ id }) {
|
function StatusPage({ id }) {
|
||||||
const snapStates = useSnapshot(states);
|
const snapStates = useSnapshot(states);
|
||||||
const [statuses, setStatuses] = useState([{ id }]);
|
const cachedStatuses = store.session.getJSON('statuses-' + id);
|
||||||
|
const [statuses, setStatuses] = useState(cachedStatuses || [{ id }]);
|
||||||
const [uiState, setUIState] = useState('default');
|
const [uiState, setUIState] = useState('default');
|
||||||
const heroStatusRef = useRef();
|
const heroStatusRef = useRef();
|
||||||
|
|
||||||
useEffect(async () => {
|
useEffect(() => {
|
||||||
const containsStatus = statuses.find((s) => s.id === id);
|
const containsStatus = statuses.find((s) => s.id === id);
|
||||||
if (!containsStatus) {
|
if (!containsStatus) {
|
||||||
setStatuses([{ id }]);
|
setStatuses([{ id }]);
|
||||||
|
} else {
|
||||||
|
const cachedStatuses = store.session.getJSON('statuses-' + id);
|
||||||
|
if (cachedStatuses) {
|
||||||
|
setStatuses(cachedStatuses);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
}, [id]);
|
||||||
|
|
||||||
|
useEffect(async () => {
|
||||||
setUIState('loading');
|
setUIState('loading');
|
||||||
|
|
||||||
const hasStatus = snapStates.statuses.has(id);
|
const hasStatus = snapStates.statuses.has(id);
|
||||||
|
@ -93,6 +102,7 @@ function StatusPage({ id }) {
|
||||||
];
|
];
|
||||||
console.log({ allStatuses });
|
console.log({ allStatuses });
|
||||||
setStatuses(allStatuses);
|
setStatuses(allStatuses);
|
||||||
|
store.session.setJSON('statuses-' + id, allStatuses);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
setUIState('error');
|
setUIState('error');
|
||||||
|
|
Loading…
Add table
Reference in a new issue