mirror of
https://github.com/balzack/databag.git
synced 2025-02-12 03:29:16 +00:00
more cleanup of profile context
This commit is contained in:
parent
b6ce11d766
commit
30ac3d7a3f
@ -336,7 +336,7 @@ export function useConversationContext() {
|
|||||||
contacts.push(card.state.cards.get(item.cardId));
|
contacts.push(card.state.cards.get(item.cardId));
|
||||||
}
|
}
|
||||||
if (item?.detail?.members) {
|
if (item?.detail?.members) {
|
||||||
const profileGuid = profile.state.profile.guid;
|
const profileGuid = profile.state.identity.guid;
|
||||||
item.detail.members.forEach(guid => {
|
item.detail.members.forEach(guid => {
|
||||||
if (profileGuid !== guid) {
|
if (profileGuid !== guid) {
|
||||||
const contact = getCard(guid);
|
const contact = getCard(guid);
|
||||||
|
@ -8,7 +8,7 @@ import { StoreContext } from 'context/StoreContext';
|
|||||||
|
|
||||||
export function useProfileContext() {
|
export function useProfileContext() {
|
||||||
const [state, setState] = useState({
|
const [state, setState] = useState({
|
||||||
profile: {},
|
identity: {},
|
||||||
imageUrl: null,
|
imageUrl: null,
|
||||||
});
|
});
|
||||||
const store = useContext(StoreContext);
|
const store = useContext(StoreContext);
|
||||||
@ -29,10 +29,11 @@ export function useProfileContext() {
|
|||||||
try {
|
try {
|
||||||
const revision = curRevision.current;
|
const revision = curRevision.current;
|
||||||
const { server, appToken, guid } = session.current;
|
const { server, appToken, guid } = session.current;
|
||||||
const profile = await getProfile(server, appToken);
|
const identity = await getProfile(server, appToken);
|
||||||
await store.actions.setProfile(guid, profile);
|
const imageUrl = identity.image ? getProfileImageUrl(server, appToken, revision) : null;
|
||||||
|
await store.actions.setProfile(guid, identity);
|
||||||
await store.actions.setProfileRevision(guid, revision);
|
await store.actions.setProfileRevision(guid, revision);
|
||||||
updateState({ profile, imageUrl: getProfileImageUrl(server, appToken, revision) });
|
updateState({ identity, imageUrl: getProfileImageUrl(server, appToken, revision) });
|
||||||
setRevision.current = revision;
|
setRevision.current = revision;
|
||||||
}
|
}
|
||||||
catch(err) {
|
catch(err) {
|
||||||
@ -49,9 +50,10 @@ export function useProfileContext() {
|
|||||||
const actions = {
|
const actions = {
|
||||||
setSession: async (access) => {
|
setSession: async (access) => {
|
||||||
const { guid, server, appToken } = 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);
|
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;
|
setRevision.current = revision;
|
||||||
curRevision.current = revision;
|
curRevision.current = revision;
|
||||||
session.current = access;
|
session.current = access;
|
||||||
@ -72,17 +74,10 @@ export function useProfileContext() {
|
|||||||
const { server, appToken } = session.current;
|
const { server, appToken } = session.current;
|
||||||
await setProfileImage(server, appToken, image);
|
await setProfileImage(server, appToken, image);
|
||||||
},
|
},
|
||||||
getHandle: async (name) => {
|
getHandleStatus: async (name) => {
|
||||||
const { server, appToken } = session.current;
|
const { server, appToken } = session.current;
|
||||||
return await getHandle(server, appToken, name);
|
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 }
|
return { state, actions }
|
||||||
|
@ -98,7 +98,7 @@ export function useChannels() {
|
|||||||
const guid = item?.summary?.lastTopic?.guid;
|
const guid = item?.summary?.lastTopic?.guid;
|
||||||
if (created && login && login < created) {
|
if (created && login && login < created) {
|
||||||
if (!item.readRevision || item.readRevision < item.revision) {
|
if (!item.readRevision || item.readRevision < item.revision) {
|
||||||
if (profile.state.profile.guid != guid) {
|
if (profile.state.identity.guid != guid) {
|
||||||
updated = true;
|
updated = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -111,7 +111,7 @@ export function useChannels() {
|
|||||||
if (item?.detail?.members) {
|
if (item?.detail?.members) {
|
||||||
|
|
||||||
item.detail.members.forEach(guid => {
|
item.detail.members.forEach(guid => {
|
||||||
const profileGuid = profile.state.profile.guid;
|
const profileGuid = profile.state.identity.guid;
|
||||||
if (profileGuid !== guid) {
|
if (profileGuid !== guid) {
|
||||||
contacts.push(getCard(guid));
|
contacts.push(getCard(guid));
|
||||||
}
|
}
|
||||||
|
@ -44,7 +44,7 @@ export function useTopicItem(item, hosting, remove, sealed, sealKey) {
|
|||||||
const { guid, dataType, data, status, transform } = detail;
|
const { guid, dataType, data, status, transform } = detail;
|
||||||
|
|
||||||
let name, nameSet, known, logo;
|
let name, nameSet, known, logo;
|
||||||
const identity = profile.state?.profile;
|
const identity = profile.state?.identity;
|
||||||
if (guid === identity.guid) {
|
if (guid === identity.guid) {
|
||||||
known = true;
|
known = true;
|
||||||
if (identity.name) {
|
if (identity.name) {
|
||||||
@ -53,7 +53,7 @@ export function useTopicItem(item, hosting, remove, sealed, sealKey) {
|
|||||||
else {
|
else {
|
||||||
name = `${identity.handle}@${identity.node}`;
|
name = `${identity.handle}@${identity.node}`;
|
||||||
}
|
}
|
||||||
const img = profile.actions.getImageUrl();
|
const img = profile.state.imageUrl;
|
||||||
if (img) {
|
if (img) {
|
||||||
logo = img;
|
logo = img;
|
||||||
}
|
}
|
||||||
|
@ -24,8 +24,8 @@ export function useBlockedMessages() {
|
|||||||
|
|
||||||
const setItem = (item) => {
|
const setItem = (item) => {
|
||||||
let name, nameSet
|
let name, nameSet
|
||||||
if (item.detail.guid === profile.state.profile.guid) {
|
if (item.detail.guid === profile.state.identity.guid) {
|
||||||
const identity = profile.state.profile;
|
const identity = profile.state.identity;
|
||||||
if (identity.name) {
|
if (identity.name) {
|
||||||
name = identity.name;
|
name = identity.name;
|
||||||
}
|
}
|
||||||
|
@ -48,7 +48,7 @@ export function useBlockedTopics() {
|
|||||||
contacts.push(card.state.cards.get(item.cardId));
|
contacts.push(card.state.cards.get(item.cardId));
|
||||||
}
|
}
|
||||||
if (item?.detail?.members) {
|
if (item?.detail?.members) {
|
||||||
const profileGuid = profile.state.profile.guid;
|
const profileGuid = profile.state.identity.guid;
|
||||||
item.detail.members.forEach(guid => {
|
item.detail.members.forEach(guid => {
|
||||||
if (profileGuid !== guid) {
|
if (profileGuid !== guid) {
|
||||||
const contact = getCard(guid);
|
const contact = getCard(guid);
|
||||||
|
@ -79,7 +79,7 @@ export function useProfileBody() {
|
|||||||
}, [dimensions]);
|
}, [dimensions]);
|
||||||
|
|
||||||
useEffect(() => {
|
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';
|
const imageSource = image ? profile.state.imageUrl : 'avatar';
|
||||||
updateState({ name, handle, node, location, description, imageSource, editHandle: handle,
|
updateState({ name, handle, node, location, description, imageSource, editHandle: handle,
|
||||||
editName: name, editLocation: location, editDescription: description });
|
editName: name, editLocation: location, editDescription: description });
|
||||||
@ -398,7 +398,7 @@ export function useProfileBody() {
|
|||||||
updateState({ available: true, checked: true });
|
updateState({ available: true, checked: true });
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
const available = await profile.actions.getHandle(editHandle);
|
const available = await profile.actions.getHandleStatus(editHandle);
|
||||||
updateState({ available, checked: true });
|
updateState({ available, checked: true });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -36,7 +36,7 @@ export function useProfile() {
|
|||||||
}, [dimensions]);
|
}, [dimensions]);
|
||||||
|
|
||||||
useEffect(() => {
|
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';
|
const imageSource = image ? profile.state.imageUrl : 'avatar';
|
||||||
updateState({ name, handle, node, location, description, imageSource, editHandle: handle,
|
updateState({ name, handle, node, location, description, imageSource, editHandle: handle,
|
||||||
editName: name, editLocation: location, editDescription: description });
|
editName: name, editLocation: location, editDescription: description });
|
||||||
|
@ -35,7 +35,7 @@ export function useRegistry() {
|
|||||||
}, [dimensions]);
|
}, [dimensions]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const server = profile.state.profile.node;
|
const server = profile.state.identity.node;
|
||||||
updateState({ server });
|
updateState({ server });
|
||||||
getAccounts(server, true);
|
getAccounts(server, true);
|
||||||
}, [profile]);
|
}, [profile]);
|
||||||
@ -58,7 +58,7 @@ export function useRegistry() {
|
|||||||
accounts = await getListing(server);
|
accounts = await getListing(server);
|
||||||
}
|
}
|
||||||
const filtered = accounts.filter(item => {
|
const filtered = accounts.filter(item => {
|
||||||
if (item.guid === profile.state.profile.guid) {
|
if (item.guid === profile.state.identity.guid) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import { useState, useRef } from 'react';
|
import { useState, useRef } from 'react';
|
||||||
|
import { getHandle } from 'api/getHandle';
|
||||||
import { getProfile } from 'api/getProfile';
|
import { getProfile } from 'api/getProfile';
|
||||||
import { setProfileData } from 'api/setProfileData';
|
import { setProfileData } from 'api/setProfileData';
|
||||||
import { setProfileImage } from 'api/setProfileImage';
|
import { setProfileImage } from 'api/setProfileImage';
|
||||||
@ -19,26 +20,24 @@ export function useProfileContext() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const sync = async () => {
|
const sync = async () => {
|
||||||
if (!syncing.current) {
|
if (!syncing.current && setRevision.current !== curRevision.current) {
|
||||||
if (setRevision.current !== curRevision.current) {
|
syncing.current = true;
|
||||||
syncing.current = true;
|
|
||||||
|
try {
|
||||||
const revision = curRevision.current;
|
const revision = curRevision.current;
|
||||||
|
const identity = await getProfile(access.current);
|
||||||
try {
|
const imageUrl = identity.image ? getProfileImageUrl(access.current, revision) : null;
|
||||||
const identity = await getProfile(access.current);
|
updateState({ identity, imageUrl });
|
||||||
const imageUrl = identity.image ? getProfileImageUrl(access.current, revision) : null;
|
setRevision.current = revision;
|
||||||
updateState({ identity, imageUrl });
|
|
||||||
setRevision.current = revision;
|
|
||||||
}
|
|
||||||
catch(err) {
|
|
||||||
console.log(err);
|
|
||||||
syncing.current = false;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
syncing.current = false;
|
|
||||||
sync();
|
|
||||||
}
|
}
|
||||||
|
catch(err) {
|
||||||
|
console.log(err);
|
||||||
|
syncing.current = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
syncing.current = false;
|
||||||
|
sync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,6 +61,9 @@ export function useProfileContext() {
|
|||||||
setProfileImage: async (image) => {
|
setProfileImage: async (image) => {
|
||||||
await setProfileImage(access.current, image);
|
await setProfileImage(access.current, image);
|
||||||
},
|
},
|
||||||
|
getHandleStatus: async (name) => {
|
||||||
|
return await getUsername(name, access.current);
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
return { state, actions }
|
return { state, actions }
|
||||||
|
Loading…
Reference in New Issue
Block a user