mirror of
https://github.com/Qortal/qapp-core.git
synced 2025-06-14 17:41:20 +00:00
fix previous list state
This commit is contained in:
parent
b5d3bbdc3f
commit
6105a36ea2
@ -137,10 +137,12 @@ export const MemorizedComponent = ({
|
|||||||
const [isLoading, setIsLoading] = useState(list?.length > 0 ? false : true);
|
const [isLoading, setIsLoading] = useState(list?.length > 0 ? false : true);
|
||||||
|
|
||||||
const isListExpired = useCacheStore().isListExpired(listName)
|
const isListExpired = useCacheStore().isListExpired(listName)
|
||||||
const isListExpiredRef = useRef<boolean>(true)
|
const isListExpiredRef = useRef<boolean | string>(true)
|
||||||
|
const memoizedParamsRef = useRef<string>('')
|
||||||
useEffect(()=> {
|
useEffect(()=> {
|
||||||
isListExpiredRef.current = isListExpired
|
isListExpiredRef.current = isListExpired
|
||||||
}, [isListExpired])
|
memoizedParamsRef.current = memoizedParams
|
||||||
|
}, [isListExpired, memoizedParams])
|
||||||
|
|
||||||
const filterOutDeletedResources = useCacheStore((s) => s.filterOutDeletedResources);
|
const filterOutDeletedResources = useCacheStore((s) => s.filterOutDeletedResources);
|
||||||
const deletedResources = useCacheStore((s) => s.deletedResources);
|
const deletedResources = useCacheStore((s) => s.deletedResources);
|
||||||
@ -264,11 +266,18 @@ const addItems = useListStore((s) => s.addItems);
|
|||||||
}, [resetSearch])
|
}, [resetSearch])
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if(!generatedIdentifier) return
|
if(!generatedIdentifier) return
|
||||||
|
|
||||||
if(!isListExpiredRef.current && !initialized.current) {
|
if(typeof isListExpiredRef.current === 'string' && typeof memoizedParamsRef.current === 'string') {
|
||||||
setIsLoading(false)
|
const parsedParams = {...(JSON.parse(memoizedParamsRef.current))};
|
||||||
|
parsedParams.identifier = generatedIdentifier
|
||||||
|
const stringedParams = JSON.stringify(parsedParams)
|
||||||
|
|
||||||
|
if(stringedParams === isListExpiredRef.current && !initialized.current){
|
||||||
|
setIsLoading(false)
|
||||||
initialized.current = true
|
initialized.current = true
|
||||||
return
|
return
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sessionStorage.removeItem(`scroll-position-${listName}`);
|
sessionStorage.removeItem(`scroll-position-${listName}`);
|
||||||
|
@ -240,7 +240,7 @@ export const useResources = (retryAttempts: number = 2) => {
|
|||||||
if (!lastCreated) break;
|
if (!lastCreated) break;
|
||||||
}
|
}
|
||||||
|
|
||||||
setSearchCache(listName, cacheKey, filteredResults);
|
setSearchCache(listName, cacheKey, filteredResults, JSON.stringify(params));
|
||||||
fetchDataFromResults(filteredResults, returnType);
|
fetchDataFromResults(filteredResults, returnType);
|
||||||
|
|
||||||
return filteredResults;
|
return filteredResults;
|
||||||
|
@ -4,6 +4,7 @@ export { useResourceStatus } from './hooks/useResourceStatus';
|
|||||||
export { Spacer } from './common/Spacer';
|
export { Spacer } from './common/Spacer';
|
||||||
export { useModal } from './hooks/useModal';
|
export { useModal } from './hooks/useModal';
|
||||||
import './index.css'
|
import './index.css'
|
||||||
|
export { formatBytes, formatDuration } from './utils/numbers';
|
||||||
export { createQortalLink } from './utils/qortal';
|
export { createQortalLink } from './utils/qortal';
|
||||||
export { IndexCategory } from './state/indexes';
|
export { IndexCategory } from './state/indexes';
|
||||||
export { hashWordWithoutPublicSalt } from './utils/encryption';
|
export { hashWordWithoutPublicSalt } from './utils/encryption';
|
||||||
|
@ -10,6 +10,7 @@ interface SearchCache {
|
|||||||
};
|
};
|
||||||
temporaryNewResources: QortalMetadata[],
|
temporaryNewResources: QortalMetadata[],
|
||||||
expiry: number; // Expiry timestamp for the whole list
|
expiry: number; // Expiry timestamp for the whole list
|
||||||
|
searchParamsStringified: string;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -55,7 +56,7 @@ interface CacheState {
|
|||||||
// Search cache actions
|
// Search cache actions
|
||||||
setResourceCache: (id: string, data: ListItem | false | null, customExpiry?: number) => void;
|
setResourceCache: (id: string, data: ListItem | false | null, customExpiry?: number) => void;
|
||||||
|
|
||||||
setSearchCache: (listName: string, searchTerm: string, data: QortalMetadata[], customExpiry?: number) => void;
|
setSearchCache: (listName: string, searchTerm: string, data: QortalMetadata[], searchParamsStringified: string, customExpiry?: number) => void;
|
||||||
getSearchCache: (listName: string, searchTerm: string) => QortalMetadata[] | null;
|
getSearchCache: (listName: string, searchTerm: string) => QortalMetadata[] | null;
|
||||||
clearExpiredCache: () => void;
|
clearExpiredCache: () => void;
|
||||||
getResourceCache: (id: string, ignoreExpire?: boolean) => ListItem | false | null;
|
getResourceCache: (id: string, ignoreExpire?: boolean) => ListItem | false | null;
|
||||||
@ -64,7 +65,7 @@ interface CacheState {
|
|||||||
deletedResources: DeletedResources;
|
deletedResources: DeletedResources;
|
||||||
markResourceAsDeleted: (item: QortalMetadata) => void;
|
markResourceAsDeleted: (item: QortalMetadata) => void;
|
||||||
filterOutDeletedResources: (items: QortalMetadata[]) => QortalMetadata[];
|
filterOutDeletedResources: (items: QortalMetadata[]) => QortalMetadata[];
|
||||||
isListExpired: (listName: string)=> boolean
|
isListExpired: (listName: string)=> boolean | string;
|
||||||
searchCacheExpiryDuration: number;
|
searchCacheExpiryDuration: number;
|
||||||
resourceCacheExpiryDuration: number;
|
resourceCacheExpiryDuration: number;
|
||||||
setSearchCacheExpiryDuration: (duration: number) => void;
|
setSearchCacheExpiryDuration: (duration: number) => void;
|
||||||
@ -107,7 +108,7 @@ export const useCacheStore = create<CacheState>
|
|||||||
};
|
};
|
||||||
}),
|
}),
|
||||||
|
|
||||||
setSearchCache: (listName, searchTerm, data, customExpiry) =>
|
setSearchCache: (listName, searchTerm, data, searchParamsStringified, customExpiry) =>
|
||||||
set((state) => {
|
set((state) => {
|
||||||
const expiry = Date.now() + (customExpiry || get().searchCacheExpiryDuration);
|
const expiry = Date.now() + (customExpiry || get().searchCacheExpiryDuration);
|
||||||
|
|
||||||
@ -121,6 +122,7 @@ export const useCacheStore = create<CacheState>
|
|||||||
},
|
},
|
||||||
temporaryNewResources: state.searchCache[listName]?.temporaryNewResources || [],
|
temporaryNewResources: state.searchCache[listName]?.temporaryNewResources || [],
|
||||||
expiry,
|
expiry,
|
||||||
|
searchParamsStringified
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
@ -207,9 +209,10 @@ export const useCacheStore = create<CacheState>
|
|||||||
(item) => !deletedResources[`${item.service}-${item.name}-${item.identifier}`]
|
(item) => !deletedResources[`${item.service}-${item.name}-${item.identifier}`]
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
isListExpired: (listName: string): boolean => {
|
isListExpired: (listName: string): boolean | string => {
|
||||||
const cache = get().searchCache[listName];
|
const cache = get().searchCache[listName];
|
||||||
return cache ? cache.expiry <= Date.now() : true; // ✅ Expired if expiry timestamp is in the past
|
const isExpired = cache ? cache.expiry <= Date.now() : true; // ✅ Expired if expiry timestamp is in the past
|
||||||
|
return isExpired === true ? true : cache.searchParamsStringified
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
||||||
|
30
src/utils/numbers.ts
Normal file
30
src/utils/numbers.ts
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
type ByteFormat = "Decimal" | "Binary";
|
||||||
|
export function formatBytes(
|
||||||
|
bytes: number,
|
||||||
|
decimals = 2,
|
||||||
|
format: ByteFormat = "Binary"
|
||||||
|
) {
|
||||||
|
if (bytes === 0) return "0 Bytes";
|
||||||
|
|
||||||
|
const k = format === "Binary" ? 1024 : 1000;
|
||||||
|
const dm = decimals < 0 ? 0 : decimals;
|
||||||
|
const sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
|
||||||
|
|
||||||
|
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
||||||
|
|
||||||
|
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + " " + sizes[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
export function formatDuration(seconds: number): string {
|
||||||
|
const hrs = Math.floor(seconds / 3600);
|
||||||
|
const mins = Math.floor((seconds % 3600) / 60);
|
||||||
|
const secs = Math.floor(seconds % 60);
|
||||||
|
|
||||||
|
const paddedMins = mins.toString().padStart(hrs > 0 ? 2 : 1, '0');
|
||||||
|
const paddedSecs = secs.toString().padStart(2, '0');
|
||||||
|
|
||||||
|
return hrs > 0
|
||||||
|
? `${hrs}:${paddedMins}:${paddedSecs}`
|
||||||
|
: `${paddedMins}:${paddedSecs}`;
|
||||||
|
}
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user