mirror of
https://github.com/luminous-devs/lume.git
synced 2024-09-19 11:43:30 +00:00
refactor useMetadata hook and minor fixes
This commit is contained in:
parent
2fced53a60
commit
cb35c70351
@ -125,7 +125,9 @@ export default function EventCollector() {
|
||||
if (unsubscribe.current) {
|
||||
unsubscribe.current();
|
||||
}
|
||||
unlisten.current();
|
||||
if (unlisten.current) {
|
||||
unlisten.current;
|
||||
}
|
||||
};
|
||||
}, [setHasNewerNote, subscribe, listenWindowClose]);
|
||||
|
||||
|
@ -18,7 +18,6 @@ import {
|
||||
useContext,
|
||||
useEffect,
|
||||
useRef,
|
||||
useState,
|
||||
} from 'react';
|
||||
|
||||
export default function Page() {
|
||||
@ -133,9 +132,10 @@ export default function Page() {
|
||||
},
|
||||
undefined,
|
||||
() => {
|
||||
eose.current += 1;
|
||||
if (eose.current > relays.length / 2) {
|
||||
if (eose.current > relays.length - 7) {
|
||||
router.replace('/newsfeed/following');
|
||||
} else {
|
||||
eose.current += 1;
|
||||
}
|
||||
}
|
||||
);
|
||||
|
@ -1,5 +1,6 @@
|
||||
import useLocalStorage from '@rehooks/local-storage';
|
||||
import { fetch } from '@tauri-apps/api/http';
|
||||
import { useCallback, useEffect, useState } from 'react';
|
||||
import { useCallback, useEffect, useMemo, useState } from 'react';
|
||||
|
||||
export const fetchMetadata = async (pubkey: string) => {
|
||||
const result = await fetch(`https://rbr.bio/${pubkey}/metadata.json`, {
|
||||
@ -10,32 +11,50 @@ export const fetchMetadata = async (pubkey: string) => {
|
||||
};
|
||||
|
||||
export const useMetadata = (pubkey) => {
|
||||
const [activeAccount]: any = useLocalStorage('activeAccount', {});
|
||||
const [plebs] = useLocalStorage('activeAccountFollows', []);
|
||||
const [profile, setProfile] = useState(null);
|
||||
|
||||
const getCachedMetadata = useCallback(async () => {
|
||||
const { getPlebByPubkey } = await import('@utils/bindings');
|
||||
getPlebByPubkey({ pubkey: pubkey })
|
||||
.then((res) => {
|
||||
if (res && res.metadata.length > 0) {
|
||||
const metadata = JSON.parse(res.metadata);
|
||||
// update state
|
||||
setProfile(metadata);
|
||||
const cacheProfile = useMemo(() => {
|
||||
const findInStorage = plebs.find((item) => item.pubkey === pubkey);
|
||||
|
||||
if (findInStorage !== undefined) {
|
||||
return JSON.parse(findInStorage.metadata);
|
||||
} else {
|
||||
fetchMetadata(pubkey).then((res: any) => {
|
||||
if (res) {
|
||||
const metadata = JSON.parse(res.content);
|
||||
// update state
|
||||
setProfile(metadata);
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
})
|
||||
.catch(console.error);
|
||||
}, [pubkey]);
|
||||
}, [plebs, pubkey]);
|
||||
|
||||
const insertPlebToDB = useCallback(
|
||||
async (pubkey: string, metadata: string) => {
|
||||
const { createPleb } = await import('@utils/bindings');
|
||||
return await createPleb({
|
||||
pleb_id: pubkey + '-lume' + activeAccount.id,
|
||||
pubkey: pubkey,
|
||||
kind: 1,
|
||||
metadata: metadata,
|
||||
account_id: activeAccount.id,
|
||||
}).catch(console.error);
|
||||
},
|
||||
[activeAccount.id]
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
getCachedMetadata().catch(console.error);
|
||||
}, [getCachedMetadata]);
|
||||
if (!cacheProfile) {
|
||||
fetchMetadata(pubkey)
|
||||
.then((res: any) => {
|
||||
// update state
|
||||
setProfile(JSON.parse(res.content));
|
||||
// save to db
|
||||
insertPlebToDB(pubkey, res.content);
|
||||
})
|
||||
.catch(console.error);
|
||||
}
|
||||
}, [cacheProfile, insertPlebToDB, pubkey]);
|
||||
|
||||
if (cacheProfile) {
|
||||
return cacheProfile;
|
||||
} else {
|
||||
return profile;
|
||||
}
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user