From 30ac3d7a3f8ec147f3fb9c44fbae9f99c178b85d Mon Sep 17 00:00:00 2001 From: balzack Date: Mon, 2 Jan 2023 14:59:27 -0800 Subject: [PATCH] more cleanup of profile context --- .../context/useConversationContext.hook.js | 2 +- .../src/context/useProfileContext.hook.js | 23 +++++------ .../src/session/channels/useChannels.hook.js | 4 +- .../topicItem/useTopicItem.hook.js | 4 +- .../useBlockedMessages.hook.js | 4 +- .../blockedTopics/useBlockedTopics.hook.js | 2 +- .../profileBody/useProfileBody.hook.js | 4 +- .../src/session/profile/useProfile.hook.js | 2 +- .../src/session/registry/useRegistry.hook.js | 4 +- net/web/src/context/useProfileContext.hook.js | 38 ++++++++++--------- 10 files changed, 42 insertions(+), 45 deletions(-) diff --git a/app/mobile/src/context/useConversationContext.hook.js b/app/mobile/src/context/useConversationContext.hook.js index c5970b39..b67f3ce7 100644 --- a/app/mobile/src/context/useConversationContext.hook.js +++ b/app/mobile/src/context/useConversationContext.hook.js @@ -336,7 +336,7 @@ export function useConversationContext() { contacts.push(card.state.cards.get(item.cardId)); } if (item?.detail?.members) { - const profileGuid = profile.state.profile.guid; + const profileGuid = profile.state.identity.guid; item.detail.members.forEach(guid => { if (profileGuid !== guid) { const contact = getCard(guid); diff --git a/app/mobile/src/context/useProfileContext.hook.js b/app/mobile/src/context/useProfileContext.hook.js index 9a8dbc95..65a1f2ba 100644 --- a/app/mobile/src/context/useProfileContext.hook.js +++ b/app/mobile/src/context/useProfileContext.hook.js @@ -8,7 +8,7 @@ import { StoreContext } from 'context/StoreContext'; export function useProfileContext() { const [state, setState] = useState({ - profile: {}, + identity: {}, imageUrl: null, }); const store = useContext(StoreContext); @@ -29,10 +29,11 @@ export function useProfileContext() { try { const revision = curRevision.current; const { server, appToken, guid } = session.current; - const profile = await getProfile(server, appToken); - await store.actions.setProfile(guid, profile); + const identity = await getProfile(server, appToken); + const imageUrl = identity.image ? getProfileImageUrl(server, appToken, revision) : null; + await store.actions.setProfile(guid, identity); await store.actions.setProfileRevision(guid, revision); - updateState({ profile, imageUrl: getProfileImageUrl(server, appToken, revision) }); + updateState({ identity, imageUrl: getProfileImageUrl(server, appToken, revision) }); setRevision.current = revision; } catch(err) { @@ -49,9 +50,10 @@ export function useProfileContext() { const actions = { setSession: async (access) => { const { guid, server, appToken } = access; - const profile = await store.actions.getProfile(guid); + const identity = await store.actions.getProfile(guid); const revision = await store.actions.getProfileRevision(guid); - updateState({ profile, imageUrl: getProfileImageUrl(server, appToken, revision) }); + const imageUrl = identity.image ? getProfileImageUrl(server, appToken, revision) : null; + updateState({ identity, imageUrl }); setRevision.current = revision; curRevision.current = revision; session.current = access; @@ -72,17 +74,10 @@ export function useProfileContext() { const { server, appToken } = session.current; await setProfileImage(server, appToken, image); }, - getHandle: async (name) => { + getHandleStatus: async (name) => { const { server, appToken } = session.current; return await getHandle(server, appToken, name); }, - getImageUrl: () => { - const { server, appToken } = session.current; - if (!state.profile.image) { - return null; - } - return getProfileImageUrl(server, appToken, state.profile.revision); - }, } return { state, actions } diff --git a/app/mobile/src/session/channels/useChannels.hook.js b/app/mobile/src/session/channels/useChannels.hook.js index b96d50f0..f55025e8 100644 --- a/app/mobile/src/session/channels/useChannels.hook.js +++ b/app/mobile/src/session/channels/useChannels.hook.js @@ -98,7 +98,7 @@ export function useChannels() { const guid = item?.summary?.lastTopic?.guid; if (created && login && login < created) { if (!item.readRevision || item.readRevision < item.revision) { - if (profile.state.profile.guid != guid) { + if (profile.state.identity.guid != guid) { updated = true; } } @@ -111,7 +111,7 @@ export function useChannels() { if (item?.detail?.members) { item.detail.members.forEach(guid => { - const profileGuid = profile.state.profile.guid; + const profileGuid = profile.state.identity.guid; if (profileGuid !== guid) { contacts.push(getCard(guid)); } diff --git a/app/mobile/src/session/conversation/topicItem/useTopicItem.hook.js b/app/mobile/src/session/conversation/topicItem/useTopicItem.hook.js index 64e2cc27..f3056cd6 100644 --- a/app/mobile/src/session/conversation/topicItem/useTopicItem.hook.js +++ b/app/mobile/src/session/conversation/topicItem/useTopicItem.hook.js @@ -44,7 +44,7 @@ export function useTopicItem(item, hosting, remove, sealed, sealKey) { const { guid, dataType, data, status, transform } = detail; let name, nameSet, known, logo; - const identity = profile.state?.profile; + const identity = profile.state?.identity; if (guid === identity.guid) { known = true; if (identity.name) { @@ -53,7 +53,7 @@ export function useTopicItem(item, hosting, remove, sealed, sealKey) { else { name = `${identity.handle}@${identity.node}`; } - const img = profile.actions.getImageUrl(); + const img = profile.state.imageUrl; if (img) { logo = img; } diff --git a/app/mobile/src/session/profile/profileBody/blockedMessages/useBlockedMessages.hook.js b/app/mobile/src/session/profile/profileBody/blockedMessages/useBlockedMessages.hook.js index 4254ed5c..f0c0e0f8 100644 --- a/app/mobile/src/session/profile/profileBody/blockedMessages/useBlockedMessages.hook.js +++ b/app/mobile/src/session/profile/profileBody/blockedMessages/useBlockedMessages.hook.js @@ -24,8 +24,8 @@ export function useBlockedMessages() { const setItem = (item) => { let name, nameSet - if (item.detail.guid === profile.state.profile.guid) { - const identity = profile.state.profile; + if (item.detail.guid === profile.state.identity.guid) { + const identity = profile.state.identity; if (identity.name) { name = identity.name; } diff --git a/app/mobile/src/session/profile/profileBody/blockedTopics/useBlockedTopics.hook.js b/app/mobile/src/session/profile/profileBody/blockedTopics/useBlockedTopics.hook.js index ff817e48..5ee87bef 100644 --- a/app/mobile/src/session/profile/profileBody/blockedTopics/useBlockedTopics.hook.js +++ b/app/mobile/src/session/profile/profileBody/blockedTopics/useBlockedTopics.hook.js @@ -48,7 +48,7 @@ export function useBlockedTopics() { contacts.push(card.state.cards.get(item.cardId)); } if (item?.detail?.members) { - const profileGuid = profile.state.profile.guid; + const profileGuid = profile.state.identity.guid; item.detail.members.forEach(guid => { if (profileGuid !== guid) { const contact = getCard(guid); diff --git a/app/mobile/src/session/profile/profileBody/useProfileBody.hook.js b/app/mobile/src/session/profile/profileBody/useProfileBody.hook.js index 4144e72e..afa7b0c5 100644 --- a/app/mobile/src/session/profile/profileBody/useProfileBody.hook.js +++ b/app/mobile/src/session/profile/profileBody/useProfileBody.hook.js @@ -79,7 +79,7 @@ export function useProfileBody() { }, [dimensions]); useEffect(() => { - const { name, handle, node, location, description, image } = profile.state.profile; + const { name, handle, node, location, description, image } = profile.state.identity; const imageSource = image ? profile.state.imageUrl : 'avatar'; updateState({ name, handle, node, location, description, imageSource, editHandle: handle, editName: name, editLocation: location, editDescription: description }); @@ -398,7 +398,7 @@ export function useProfileBody() { updateState({ available: true, checked: true }); } else { - const available = await profile.actions.getHandle(editHandle); + const available = await profile.actions.getHandleStatus(editHandle); updateState({ available, checked: true }); } } diff --git a/app/mobile/src/session/profile/useProfile.hook.js b/app/mobile/src/session/profile/useProfile.hook.js index 041e40a5..c58f9d35 100644 --- a/app/mobile/src/session/profile/useProfile.hook.js +++ b/app/mobile/src/session/profile/useProfile.hook.js @@ -36,7 +36,7 @@ export function useProfile() { }, [dimensions]); useEffect(() => { - const { name, handle, node, location, description, image } = profile.state.profile; + const { name, handle, node, location, description, image } = profile.state.identity; const imageSource = image ? profile.state.imageUrl : 'avatar'; updateState({ name, handle, node, location, description, imageSource, editHandle: handle, editName: name, editLocation: location, editDescription: description }); diff --git a/app/mobile/src/session/registry/useRegistry.hook.js b/app/mobile/src/session/registry/useRegistry.hook.js index 0ff38b50..0a326fec 100644 --- a/app/mobile/src/session/registry/useRegistry.hook.js +++ b/app/mobile/src/session/registry/useRegistry.hook.js @@ -35,7 +35,7 @@ export function useRegistry() { }, [dimensions]); useEffect(() => { - const server = profile.state.profile.node; + const server = profile.state.identity.node; updateState({ server }); getAccounts(server, true); }, [profile]); @@ -58,7 +58,7 @@ export function useRegistry() { accounts = await getListing(server); } const filtered = accounts.filter(item => { - if (item.guid === profile.state.profile.guid) { + if (item.guid === profile.state.identity.guid) { return false; } return true; diff --git a/net/web/src/context/useProfileContext.hook.js b/net/web/src/context/useProfileContext.hook.js index 4a3aa2c2..7cea0199 100644 --- a/net/web/src/context/useProfileContext.hook.js +++ b/net/web/src/context/useProfileContext.hook.js @@ -1,4 +1,5 @@ import { useState, useRef } from 'react'; +import { getHandle } from 'api/getHandle'; import { getProfile } from 'api/getProfile'; import { setProfileData } from 'api/setProfileData'; import { setProfileImage } from 'api/setProfileImage'; @@ -19,26 +20,24 @@ export function useProfileContext() { } const sync = async () => { - if (!syncing.current) { - if (setRevision.current !== curRevision.current) { - syncing.current = true; + if (!syncing.current && setRevision.current !== curRevision.current) { + syncing.current = true; + + try { 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(); + 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(); } } @@ -62,6 +61,9 @@ export function useProfileContext() { setProfileImage: async (image) => { await setProfileImage(access.current, image); }, + getHandleStatus: async (name) => { + return await getUsername(name, access.current); + }, } return { state, actions }