refactor of profile context in webapp

This commit is contained in:
Roland Osborne 2023-01-01 22:28:12 -08:00
parent 2456a1f436
commit 583d28d001
9 changed files with 53 additions and 47 deletions

View File

@ -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);
}
}

View File

@ -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 }

View File

@ -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]);

View File

@ -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));
}
}

View File

@ -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 {

View File

@ -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]);

View File

@ -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]);

View File

@ -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 });

View File

@ -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 {