added kind 6 to newsfeed and changed useMetadata to useProfileMetadata
This commit is contained in:
23
src/utils/hooks/useNetworkStatus.tsx
Normal file
23
src/utils/hooks/useNetworkStatus.tsx
Normal file
@@ -0,0 +1,23 @@
|
||||
import { useEffect, useState } from 'react';
|
||||
|
||||
const getOnLineStatus = () =>
|
||||
typeof navigator !== 'undefined' && typeof navigator.onLine === 'boolean' ? navigator.onLine : true;
|
||||
|
||||
export const useNetworkStatus = () => {
|
||||
const [status, setStatus] = useState(getOnLineStatus());
|
||||
|
||||
const setOnline = () => setStatus(true);
|
||||
const setOffline = () => setStatus(false);
|
||||
|
||||
useEffect(() => {
|
||||
window.addEventListener('online', setOnline);
|
||||
window.addEventListener('offline', setOffline);
|
||||
|
||||
return () => {
|
||||
window.removeEventListener('online', setOnline);
|
||||
window.removeEventListener('offline', setOffline);
|
||||
};
|
||||
}, []);
|
||||
|
||||
return status;
|
||||
};
|
||||
66
src/utils/hooks/useProfileMetadata.tsx
Normal file
66
src/utils/hooks/useProfileMetadata.tsx
Normal file
@@ -0,0 +1,66 @@
|
||||
import useLocalStorage from '@rehooks/local-storage';
|
||||
import { fetch } from '@tauri-apps/api/http';
|
||||
import { useCallback, useEffect, useMemo, useState } from 'react';
|
||||
|
||||
export const fetchProfileMetadata = async (pubkey: string) => {
|
||||
const result = await fetch(`https://rbr.bio/${pubkey}/metadata.json`, {
|
||||
method: 'GET',
|
||||
timeout: 5,
|
||||
});
|
||||
return await result.data;
|
||||
};
|
||||
|
||||
export const useProfileMetadata = (pubkey) => {
|
||||
const [activeAccount]: any = useLocalStorage('activeAccount', {});
|
||||
const [plebs] = useLocalStorage('activeAccountFollows', []);
|
||||
const [profile, setProfile] = useState(null);
|
||||
|
||||
const cacheProfile = useMemo(() => {
|
||||
let metadata = false;
|
||||
|
||||
if (pubkey === activeAccount.pubkey) {
|
||||
metadata = JSON.parse(activeAccount.metadata);
|
||||
} else {
|
||||
const findInStorage = plebs.find((item) => item.pubkey === pubkey);
|
||||
|
||||
if (findInStorage !== undefined) {
|
||||
metadata = JSON.parse(findInStorage.metadata);
|
||||
}
|
||||
}
|
||||
|
||||
return metadata;
|
||||
}, [plebs, pubkey, activeAccount.pubkey, activeAccount.metadata]);
|
||||
|
||||
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(() => {
|
||||
if (!cacheProfile) {
|
||||
fetchProfileMetadata(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;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user