fix follow sgsts glitch when no connection

This commit is contained in:
Martti Malmi 2023-08-18 20:30:04 +03:00
parent 85513e5970
commit 56c0ccc16c
2 changed files with 16 additions and 7 deletions

View File

@ -1,10 +1,15 @@
import { useEffect, useState } from 'preact/hooks';
import { useEffect, useMemo, useState } from 'preact/hooks';
const useCachedFetch = (url, storageKey, dataProcessor = (data) => data) => {
const cachedData = localStorage.getItem(storageKey);
const initialData = cachedData ? JSON.parse(cachedData).data : [];
const cachedData = useMemo(() => {
const cached = localStorage.getItem(storageKey);
return cached ? JSON.parse(cached) : null;
}, [storageKey]);
const initialData = cachedData ? cachedData.data : [];
const [data, setData] = useState(initialData);
const [hasError, setHasError] = useState(false);
useEffect(() => {
const fetchData = () => {
@ -19,15 +24,17 @@ const useCachedFetch = (url, storageKey, dataProcessor = (data) => data) => {
);
})
.catch(() => {
if (cachedData) {
const { data } = JSON.parse(cachedData);
setData(data);
if (!hasError) {
if (cachedData) {
setData(cachedData);
}
setHasError(true);
}
});
};
if (cachedData) {
const { timestamp } = JSON.parse(cachedData);
const { timestamp } = cachedData;
const age = (new Date().getTime() - timestamp) / 1000 / 60;
if (age >= 15) {

View File

@ -115,6 +115,8 @@ const PubSub = {
filter.authors.forEach((a) => this.subscribedAuthors.add(a));
}
// TODO if filter.ids & found in EventDB, don't ask others
callback && EventDB.find(filter, callback);
if (dev.indexedDbLoad !== false) {