diff --git a/net/web/src/context/useConversationContext.hook.js b/net/web/src/context/useConversationContext.hook.js index 1701f4d2..ddca7a2a 100644 --- a/net/web/src/context/useConversationContext.hook.js +++ b/net/web/src/context/useConversationContext.hook.js @@ -118,7 +118,7 @@ export function useConversationContext() { members.add(conversation.guid); } for (let member of conversation.data.channelDetail.members) { - if (profile.state.profile.guid !== member) { + if (profile.state.identity.guid !== member) { members.add(member); } } diff --git a/net/web/src/context/useProfileContext.hook.js b/net/web/src/context/useProfileContext.hook.js index c98c5156..4a3aa2c2 100644 --- a/net/web/src/context/useProfileContext.hook.js +++ b/net/web/src/context/useProfileContext.hook.js @@ -6,32 +6,39 @@ import { getProfileImageUrl } from 'api/getProfileImageUrl'; export function useProfileContext() { const [state, setState] = useState({ - init: false, - profile: {}, + identity: {}, + imageUrl: null, }); const access = useRef(null); - const revision = useRef(null); - const next = useRef(null); + const curRevision = useRef(null); + const setRevision = useRef(null); + const syncing = useRef(false); const updateState = (value) => { setState((s) => ({ ...s, ...value })) } - const setProfile = async (rev) => { - if (next.current == null) { - if (revision.current !== rev) { - let profile = await getProfile(access.current); - updateState({ init: true, profile }); - revision.current = rev; + const sync = async () => { + if (!syncing.current) { + if (setRevision.current !== curRevision.current) { + syncing.current = true; + const revision = curRevision.current; + + try { + const identity = await getProfile(access.current); + const imageUrl = identity.image ? getProfileImageUrl(access.current, revision) : null; + updateState({ identity, imageUrl }); + setRevision.current = revision; + } + catch(err) { + console.log(err); + syncing.current = false; + return; + } + + syncing.current = false; + sync(); } - if (next.current != null) { - let r = next.current; - next.current = null; - setProfile(r); - } - } - else { - next.current = rev; } } @@ -41,11 +48,13 @@ export function useProfileContext() { }, clearToken: () => { access.current = null; - revision.current = null; - setState({ init: false, profile: {} }); + curRevision.current = null; + setRevision.current = null; + setState({ identity: {}, imageUrl: null }); }, setRevision: (rev) => { - setProfile(rev); + curRevision.current = rev; + sync(); }, setProfileData: async (name, location, description) => { await setProfileData(access.current, name, location, description); @@ -53,14 +62,6 @@ export function useProfileContext() { setProfileImage: async (image) => { await setProfileImage(access.current, image); }, - getProfile: () => { - const { name, handle, image, revision } = state.profile; - if (image == null || image === '') { - return { name, handle }; - } - return { name, handle, imageUrl: getProfileImageUrl(access.current, revision) }; - }, - profileImageUrl: () => getProfileImageUrl(access.current, revision.current), } return { state, actions } diff --git a/net/web/src/session/accountAccess/useAccountAccess.hook.js b/net/web/src/session/accountAccess/useAccountAccess.hook.js index 2335803e..14fa1142 100644 --- a/net/web/src/session/accountAccess/useAccountAccess.hook.js +++ b/net/web/src/session/accountAccess/useAccountAccess.hook.js @@ -40,8 +40,8 @@ export function useAccountAccess() { } useEffect(() => { - if (profile.state.init) { - const { handle } = profile.state.profile; + if (profile.state.identity?.guid) { + const { handle } = profile.state.identity; updateState({ handle, editHandle: handle }); } }, [profile]); diff --git a/net/web/src/session/channels/useChannels.hook.js b/net/web/src/session/channels/useChannels.hook.js index 220fc909..37037205 100644 --- a/net/web/src/session/channels/useChannels.hook.js +++ b/net/web/src/session/channels/useChannels.hook.js @@ -137,11 +137,11 @@ export function useChannels() { const setContacts = (chan) => { let contacts = []; - if (chan.guid != null && profile.state.profile.guid !== chan.guid) { + if (chan.guid != null && profile.state.identity.guid !== chan.guid) { contacts.push(card.actions.getCardByGuid(chan.guid)); } for (let guid of chan.data.channelDetail?.members) { - if (guid !== profile.state.profile.guid) { + if (guid !== profile.state.identity.guid) { contacts.push(card.actions.getCardByGuid(guid)); } } diff --git a/net/web/src/session/conversation/topicItem/useTopicItem.hook.js b/net/web/src/session/conversation/topicItem/useTopicItem.hook.js index 46ea8c63..b17c7cb1 100644 --- a/net/web/src/session/conversation/topicItem/useTopicItem.hook.js +++ b/net/web/src/session/conversation/topicItem/useTopicItem.hook.js @@ -33,9 +33,13 @@ export function useTopicItem(topic, sealed, sealKey) { setState((s) => ({ ...s, ...value })); } + useEffect(() => { + console.log("TOPIC URL:", state.imageUrl); + }, [state.imageUrl]); + useEffect(() => { let owner = false; - if (profile.state.profile.guid === topic?.data?.topicDetail.guid) { + if (profile.state.identity.guid === topic?.data?.topicDetail.guid) { owner = true; } @@ -97,7 +101,7 @@ export function useTopicItem(topic, sealed, sealKey) { } } - if (profile.state.init && card.state.init && conversation.state.init) { + if (profile.state.identity?.guid && card.state.init && conversation.state.init) { const { guid, created } = topic.data.topicDetail; let createdStr; @@ -114,8 +118,9 @@ export function useTopicItem(topic, sealed, sealKey) { createdStr = date.toLocaleDateString("en-US"); } - if (profile.state.profile.guid === guid) { - const { name, handle, imageUrl } = profile.actions.getProfile(); + if (profile.state.identity.guid === guid) { + const { name, handle } = profile.state.identity; + const imageUrl = profile.state.imageUrl; updateState({ sealed, name, handle, imageUrl, status, text, transform, assets, confirmed, error, ready, created: createdStr, owner, textColor, textSize, topicId: topic.id, init: true }); } else { diff --git a/net/web/src/session/identity/useIdentity.hook.js b/net/web/src/session/identity/useIdentity.hook.js index 4a737298..79ca0ee3 100644 --- a/net/web/src/session/identity/useIdentity.hook.js +++ b/net/web/src/session/identity/useIdentity.hook.js @@ -20,9 +20,9 @@ export function useIdentity() { } useEffect(() => { - if (profile.state.init) { - const { name, handle, image } = profile.state.profile; - let url = !image ? null : profile.actions.profileImageUrl(); + if (profile.state.identity?.guid) { + const { name, handle, image } = profile.state.identity; + let url = !image ? null : profile.state.imageUrl; updateState({ init: true, name, handle, url }); } }, [profile]); diff --git a/net/web/src/session/listing/useListing.hook.js b/net/web/src/session/listing/useListing.hook.js index 8fcf577d..60cbea68 100644 --- a/net/web/src/session/listing/useListing.hook.js +++ b/net/web/src/session/listing/useListing.hook.js @@ -43,7 +43,7 @@ export function useListing() { updateState({ busy: true }); try { let contacts = await getListing(state.node, state.username); - let filtered = contacts.filter(contact => (contact.guid !== profile.state.profile.guid)); + let filtered = contacts.filter(contact => (contact.guid !== profile.state.identity.guid)); let sorted = filtered.sort((a, b) => { if (a?.name < b?.name) { return -1; @@ -61,7 +61,7 @@ export function useListing() { }; useEffect(() => { - let node = profile?.state?.profile?.node; + let node = profile?.state?.identity?.node; updateState({ disabled: node == null || node === '', node }); }, [profile]); diff --git a/net/web/src/session/profile/useProfile.hook.js b/net/web/src/session/profile/useProfile.hook.js index e95dc77b..bf031166 100644 --- a/net/web/src/session/profile/useProfile.hook.js +++ b/net/web/src/session/profile/useProfile.hook.js @@ -34,9 +34,9 @@ export function useProfile() { } useEffect(() => { - if (profile.state.init) { - const { node, name, handle, location, description, image } = profile.state.profile; - let url = !image ? null : profile.actions.profileImageUrl(); + if (profile.state.identity.guid) { + const { node, name, handle, location, description, image } = profile.state.identity; + let url = !image ? null : profile.state.imageUrl; let editImage = !image ? avatar : url; updateState({ init: true, name, location, description, node, handle, url, editName: name, editLocation: location, editDescription: description, editHandle: handle, editImage }); diff --git a/net/web/src/session/useSession.hook.js b/net/web/src/session/useSession.hook.js index 45d7cbc1..6d72dc11 100644 --- a/net/web/src/session/useSession.hook.js +++ b/net/web/src/session/useSession.hook.js @@ -37,7 +37,7 @@ export function useSession() { } useEffect(() => { - if (!profile.state.init || !card.state.init || !channel.state.init) { + if (!profile.state.identity?.guid || !card.state.init || !channel.state.init) { updateState({ loading: true }); } else {