Merge pull request #551 from kinode-dao/bp/appstoreuistore

app_store UI: don't persist local UI store
This commit is contained in:
bitful-pannul 2024-09-20 00:21:09 +03:00 committed by GitHub
commit e0f7a716ef
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 368 additions and 360 deletions

View File

@ -21,16 +21,24 @@ const MirrorSelector: React.FC<MirrorSelectorProps> = ({ packageId, onMirrorSele
if (!appData) return; if (!appData) return;
const mirrors = [appData.package_id.publisher_node, ...(appData.metadata?.properties?.mirrors || [])]; const mirrors = [appData.package_id.publisher_node, ...(appData.metadata?.properties?.mirrors || [])];
setAvailableMirrors(mirrors); setAvailableMirrors(mirrors);
setSelectedMirror(appData.package_id.publisher_node);
mirrors.forEach(mirror => { // Start with the publisher node
setSelectedMirror(appData.package_id.publisher_node);
setMirrorStatuses(prev => ({ ...prev, [appData.package_id.publisher_node]: null }));
for (const mirror of mirrors) {
if (mirror.startsWith('http')) { if (mirror.startsWith('http')) {
setMirrorStatuses(prev => ({ ...prev, [mirror]: 'http' })); setMirrorStatuses(prev => ({ ...prev, [mirror]: 'http' }));
setSelectedMirror(mirror);
break;
} else { } else {
setMirrorStatuses(prev => ({ ...prev, [mirror]: null })); const status = await checkMirrorStatus(mirror);
checkMirrorStatus(mirror); if (status) {
setSelectedMirror(mirror);
break;
}
}
} }
});
}, [packageId, fetchListing, checkMirror]); }, [packageId, fetchListing, checkMirror]);
useEffect(() => { useEffect(() => {
@ -41,8 +49,10 @@ const MirrorSelector: React.FC<MirrorSelectorProps> = ({ packageId, onMirrorSele
try { try {
const status = await checkMirror(mirror); const status = await checkMirror(mirror);
setMirrorStatuses(prev => ({ ...prev, [mirror]: status?.is_online ?? false })); setMirrorStatuses(prev => ({ ...prev, [mirror]: status?.is_online ?? false }));
return status?.is_online ?? false;
} catch { } catch {
setMirrorStatuses(prev => ({ ...prev, [mirror]: false })); setMirrorStatuses(prev => ({ ...prev, [mirror]: false }));
return false;
} }
}, [checkMirror]); }, [checkMirror]);
@ -62,7 +72,6 @@ const MirrorSelector: React.FC<MirrorSelectorProps> = ({ packageId, onMirrorSele
setIsCustomMirrorSelected(false); setIsCustomMirrorSelected(false);
setCustomMirror(""); setCustomMirror("");
if (!value.startsWith('http')) { if (!value.startsWith('http')) {
// Recheck the status when a non-HTTP mirror is selected
setMirrorStatuses(prev => ({ ...prev, [value]: null })); setMirrorStatuses(prev => ({ ...prev, [value]: null }));
await checkMirrorStatus(value); await checkMirrorStatus(value);
} }
@ -92,7 +101,6 @@ const MirrorSelector: React.FC<MirrorSelectorProps> = ({ packageId, onMirrorSele
return ( return (
<div className="mirror-selector"> <div className="mirror-selector">
<select value={selectedMirror || ""} onChange={handleMirrorChange}> <select value={selectedMirror || ""} onChange={handleMirrorChange}>
<option value="">Select a mirror</option>
{availableMirrors.map((mirror, index) => ( {availableMirrors.map((mirror, index) => (
<option key={`${mirror}-${index}`} value={mirror}> <option key={`${mirror}-${index}`} value={mirror}>
{mirror} {getMirrorStatus(mirror, mirrorStatuses[mirror])} {mirror} {getMirrorStatus(mirror, mirrorStatuses[mirror])}

View File

@ -45,9 +45,7 @@ interface AppsStore {
} }
const useAppsStore = create<AppsStore>()( const useAppsStore = create<AppsStore>()((set, get) => ({
persist(
(set, get): AppsStore => ({
listings: {}, listings: {},
installed: {}, installed: {},
downloads: {}, downloads: {},
@ -220,6 +218,13 @@ const useAppsStore = create<AppsStore>()(
}); });
if (res.status === HTTP_STATUS.CREATED) { if (res.status === HTTP_STATUS.CREATED) {
await get().fetchInstalled(); await get().fetchInstalled();
// hacky: a small delay (500ms) before fetching homepage apps
// to give the app time to add itself to the homepage
// might make sense to add more state and do retry logic instead.
await new Promise(resolve => setTimeout(resolve, 500));
await get().fetchHomepageApps();
} }
} catch (error) { } catch (error) {
console.error("Error installing app:", error); console.error("Error installing app:", error);
@ -395,11 +400,6 @@ const useAppsStore = create<AppsStore>()(
console.error('WebSocket error:', error); console.error('WebSocket error:', error);
}, },
}), }),
}), }))
{
name: 'app_store',
}
)
)
export default useAppsStore export default useAppsStore