refactor useMetadata hook and minor fixes
This commit is contained in:
@@ -125,7 +125,9 @@ export default function EventCollector() {
|
|||||||
if (unsubscribe.current) {
|
if (unsubscribe.current) {
|
||||||
unsubscribe.current();
|
unsubscribe.current();
|
||||||
}
|
}
|
||||||
unlisten.current();
|
if (unlisten.current) {
|
||||||
|
unlisten.current;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}, [setHasNewerNote, subscribe, listenWindowClose]);
|
}, [setHasNewerNote, subscribe, listenWindowClose]);
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ import {
|
|||||||
useContext,
|
useContext,
|
||||||
useEffect,
|
useEffect,
|
||||||
useRef,
|
useRef,
|
||||||
useState,
|
|
||||||
} from 'react';
|
} from 'react';
|
||||||
|
|
||||||
export default function Page() {
|
export default function Page() {
|
||||||
@@ -133,9 +132,10 @@ export default function Page() {
|
|||||||
},
|
},
|
||||||
undefined,
|
undefined,
|
||||||
() => {
|
() => {
|
||||||
eose.current += 1;
|
if (eose.current > relays.length - 7) {
|
||||||
if (eose.current > relays.length / 2) {
|
|
||||||
router.replace('/newsfeed/following');
|
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 { 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) => {
|
export const fetchMetadata = async (pubkey: string) => {
|
||||||
const result = await fetch(`https://rbr.bio/${pubkey}/metadata.json`, {
|
const result = await fetch(`https://rbr.bio/${pubkey}/metadata.json`, {
|
||||||
@@ -10,32 +11,50 @@ export const fetchMetadata = async (pubkey: string) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const useMetadata = (pubkey) => {
|
export const useMetadata = (pubkey) => {
|
||||||
|
const [activeAccount]: any = useLocalStorage('activeAccount', {});
|
||||||
|
const [plebs] = useLocalStorage('activeAccountFollows', []);
|
||||||
const [profile, setProfile] = useState(null);
|
const [profile, setProfile] = useState(null);
|
||||||
|
|
||||||
const getCachedMetadata = useCallback(async () => {
|
const cacheProfile = useMemo(() => {
|
||||||
const { getPlebByPubkey } = await import('@utils/bindings');
|
const findInStorage = plebs.find((item) => item.pubkey === pubkey);
|
||||||
getPlebByPubkey({ pubkey: pubkey })
|
|
||||||
.then((res) => {
|
if (findInStorage !== undefined) {
|
||||||
if (res && res.metadata.length > 0) {
|
return JSON.parse(findInStorage.metadata);
|
||||||
const metadata = JSON.parse(res.metadata);
|
|
||||||
// update state
|
|
||||||
setProfile(metadata);
|
|
||||||
} else {
|
} else {
|
||||||
fetchMetadata(pubkey).then((res: any) => {
|
return false;
|
||||||
if (res) {
|
|
||||||
const metadata = JSON.parse(res.content);
|
|
||||||
// update state
|
|
||||||
setProfile(metadata);
|
|
||||||
}
|
}
|
||||||
});
|
}, [plebs, pubkey]);
|
||||||
}
|
|
||||||
})
|
const insertPlebToDB = useCallback(
|
||||||
.catch(console.error);
|
async (pubkey: string, metadata: string) => {
|
||||||
}, [pubkey]);
|
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(() => {
|
useEffect(() => {
|
||||||
getCachedMetadata().catch(console.error);
|
if (!cacheProfile) {
|
||||||
}, [getCachedMetadata]);
|
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;
|
return profile;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user