integrating refactored context back into screens

This commit is contained in:
Roland Osborne 2023-01-12 11:30:01 -08:00
parent dd05284e23
commit 0cf94193a2
12 changed files with 56 additions and 59 deletions

View File

@ -1,5 +1,6 @@
import { useContext, useState, useEffect } from 'react'; import { useContext, useState, useEffect } from 'react';
import { AppContext } from 'context/AppContext'; import { AppContext } from 'context/AppContext';
import { getAvailable } from 'api/getAvailable';
import { useNavigate, useLocation } from "react-router-dom"; import { useNavigate, useLocation } from "react-router-dom";
export function useLogin() { export function useLogin() {
@ -56,43 +57,38 @@ export function useLogin() {
}; };
useEffect(() => { useEffect(() => {
if (app) { if (app.state.status) {
if (app.state) { navigate('/session')
if (app.state.access) { }
navigate('/session') else {
} let params = new URLSearchParams(search);
else { let token = params.get("access");
let params = new URLSearchParams(search); if (token) {
let token = params.get("access"); const access = async () => {
if (token) { updateState({ busy: true })
const access = async () => {
updateState({ busy: true })
try {
await app.actions.access(token)
}
catch (err) {
console.log(err);
}
updateState({ busy: false })
}
access();
}
}
}
if (app.actions && app.actions.available) {
const count = async () => {
try { try {
const available = await app.actions.available() await app.actions.access(token)
updateState({ available: available !== 0 })
} }
catch(err) { catch (err) {
console.log(err); console.log(err);
} }
updateState({ busy: false })
} }
count(); access();
} }
} }
}, [app, navigate, search]) const count = async () => {
try {
const available = await getAvailable()
updateState({ available: available !== 0 })
}
catch(err) {
console.log(err);
}
}
count();
// eslint-disable-next-line
}, [app.state, navigate, search])
return { state, actions }; return { state, actions };
} }

View File

@ -3,7 +3,6 @@ const TIMEOUT = 15000;
//await new Promise(r => setTimeout(r, 2000)); //await new Promise(r => setTimeout(r, 2000));
export function createWebsocket(url) { export function createWebsocket(url) {
console.log("HERE");
return new WebSocket(url); return new WebSocket(url);
} }

View File

@ -1,6 +1,6 @@
export function getCardByGuid(cards, guid) { export function getCardByGuid(cards, guid) {
let card = null; let card = null;
cards.current.forEach((value, key, map) => { cards.forEach((value, key, map) => {
if(value?.data?.cardProfile?.guid === guid) { if(value?.data?.cardProfile?.guid === guid) {
card = value; card = value;
} }
@ -8,13 +8,12 @@ export function getCardByGuid(cards, guid) {
return card; return card;
} }
export getProfileByGuid: (cards, guid) => { export function getProfileByGuid(cards, guid) {
const card = getCardByGuid(guid); const card = getCardByGuid(cards, guid);
if (card?.data?.cardProfile) { if (card?.data?.cardProfile) {
const { name, handle, imageSet } = card.data.cardProfile; const { name, handle, imageSet } = card.data.cardProfile;
const revision = card.data.profileRevision;
const cardId = card.id; const cardId = card.id;
return { cardId, revision, name, handle, imageSet, revision } return { cardId, name, handle, imageSet }
} }
return {}; return {};
} }

View File

@ -13,7 +13,7 @@ import { createWebsocket } from 'api/fetchUtil';
export function useAppContext(websocket) { export function useAppContext(websocket) {
const [state, setState] = useState({ const [state, setState] = useState({
status: 'disconnected', status: null,
}); });
const [appRevision, setAppRevision] = useState(); const [appRevision, setAppRevision] = useState();
@ -36,6 +36,7 @@ export function useAppContext(websocket) {
const cardContext = useContext(CardContext); const cardContext = useContext(CardContext);
const setSession = (token) => { const setSession = (token) => {
console.log("SET SESSION", token);
try { try {
accountContext.actions.setToken(token); accountContext.actions.setToken(token);
profileContext.actions.setToken(token); profileContext.actions.setToken(token);
@ -185,7 +186,7 @@ export function useAppContext(websocket) {
ws.current.onclose = () => {} ws.current.onclose = () => {}
ws.current.close() ws.current.close()
ws.current = null ws.current = null
updateState({ status: 'disconnected' }); updateState({ status: null });
} }
useEffect(() => { useEffect(() => {

View File

@ -8,15 +8,16 @@ export function Root() {
const app = useContext(AppContext); const app = useContext(AppContext);
useEffect(() => { useEffect(() => {
console.log(app.state);
if (app.state) { if (app.state) {
if (app.state.access) { if (app.state.status) {
navigate('/session'); navigate('/session');
} }
else { else {
navigate('/login'); navigate('/login');
} }
} }
}, [app, navigate]); }, [app.state, navigate]);
return <></> return <></>
} }

View File

@ -23,10 +23,8 @@ export function Session() {
const navigate = useNavigate(); const navigate = useNavigate();
useEffect(() => { useEffect(() => {
if (app.state) { if (!app.state.status) {
if (!app.state.access) { navigate('/');
navigate('/');
}
} }
}, [app, navigate]); }, [app, navigate]);

View File

@ -28,7 +28,7 @@ export function useSelectItem(item, selected, markup) {
}, [selected, item]); }, [selected, item]);
useEffect(() => { useEffect(() => {
updateState({ logo: card.actions.getImageUrl(item.id) }); updateState({ logo: card.actions.getCardImageUrl(item.id) });
}, [card, item]); }, [card, item]);
const actions = { const actions = {

View File

@ -5,6 +5,7 @@ import { CardContext } from 'context/CardContext';
import { AccountContext } from 'context/AccountContext'; import { AccountContext } from 'context/AccountContext';
import { ProfileContext } from 'context/ProfileContext'; import { ProfileContext } from 'context/ProfileContext';
import { ViewportContext } from 'context/ViewportContext'; import { ViewportContext } from 'context/ViewportContext';
import { getCardByGuid } from 'context/cardUtil';
export function useChannels() { export function useChannels() {
@ -138,16 +139,18 @@ export function useChannels() {
const setContacts = (chan) => { const setContacts = (chan) => {
let contacts = []; let contacts = [];
if (chan.guid != null && profile.state.identity.guid !== chan.guid) { if (chan.guid != null && profile.state.identity.guid !== chan.guid) {
contacts.push(card.actions.getCardByGuid(chan.guid)); const contact = getCardByGuid(card.state.cards, chan.guid);
contacts.push(contact);
} }
for (let guid of chan.data.channelDetail?.members) { for (let guid of chan.data.channelDetail?.members) {
if (guid !== profile.state.identity.guid) { if (guid !== profile.state.identity.guid) {
contacts.push(card.actions.getCardByGuid(guid)); const contact = getCardByGuid(card.state.cards, guid);
contacts.push(contact);
} }
} }
chan.contacts = contacts; chan.contacts = contacts;
if (contacts.length === 1 && contacts[0]) { if (contacts.length === 1 && contacts[0]) {
chan.logo = card.actions.getImageUrl(contacts[0].id); chan.logo = card.actions.getCardImageUrl(contacts[0].id);
} }
} }

View File

@ -2,6 +2,7 @@ import { useContext, useState, useEffect, useRef } from 'react';
import { ConversationContext } from 'context/ConversationContext'; import { ConversationContext } from 'context/ConversationContext';
import { ProfileContext } from 'context/ProfileContext'; import { ProfileContext } from 'context/ProfileContext';
import { CardContext } from 'context/CardContext'; import { CardContext } from 'context/CardContext';
import { getProfileByGuid } from 'context/cardUtil';
export function useTopicItem(topic, sealed, sealKey) { export function useTopicItem(topic, sealed, sealKey) {
@ -33,10 +34,6 @@ export function useTopicItem(topic, sealed, sealKey) {
setState((s) => ({ ...s, ...value })); setState((s) => ({ ...s, ...value }));
} }
useEffect(() => {
console.log("TOPIC URL:", state.imageUrl);
}, [state.imageUrl]);
useEffect(() => { useEffect(() => {
let owner = false; let owner = false;
if (profile.state.identity.guid === topic?.data?.topicDetail.guid) { if (profile.state.identity.guid === topic?.data?.topicDetail.guid) {
@ -94,14 +91,16 @@ export function useTopicItem(topic, sealed, sealKey) {
sealed = false; sealed = false;
} }
else { else {
conversation.actions.unsealTopic(topic.id, sealKey); if (sealKey) {
conversation.actions.unsealTopic(topic.id, sealKey);
}
sealed = true; sealed = true;
} }
ready = true; ready = true;
} }
} }
if (profile.state.identity?.guid && card.state.init && conversation.state.init) { if (profile.state.identity?.guid) {
const { guid, created } = topic.data.topicDetail; const { guid, created } = topic.data.topicDetail;
let createdStr; let createdStr;
@ -124,7 +123,8 @@ export function useTopicItem(topic, sealed, sealKey) {
updateState({ sealed, name, handle, imageUrl, status, text, transform, assets, confirmed, error, ready, created: createdStr, owner, textColor, textSize, topicId: topic.id, init: true }); updateState({ sealed, name, handle, imageUrl, status, text, transform, assets, confirmed, error, ready, created: createdStr, owner, textColor, textSize, topicId: topic.id, init: true });
} }
else { else {
const { name, handle, imageUrl } = card.actions.getCardProfileByGuid(guid); const { cardId, name, handle, imageSet } = getProfileByGuid(card.state.cards, guid);
const imageUrl = imageSet ? card.actions.getCardImageUrl(cardId) : null;
updateState({ sealed, name, handle, imageUrl, status, text, transform, assets, confirmed, error, ready, created: createdStr, owner, textColor, textSize, topicId: topic.id, init: true }); updateState({ sealed, name, handle, imageUrl, status, text, transform, assets, confirmed, error, ready, created: createdStr, owner, textColor, textSize, topicId: topic.id, init: true });
} }
} }

View File

@ -90,7 +90,7 @@ export function useConversation(cardId, channelId) {
setTimeout(() => { setTimeout(() => {
updateState({ delayed: true }); updateState({ delayed: true });
}, 250); }, 250);
conversation.actions.setConversationId(cardId, channelId); conversation.actions.setChannel(cardId, channelId);
// eslint-disable-next-line // eslint-disable-next-line
}, [cardId, channelId]); }, [cardId, channelId]);
@ -122,7 +122,7 @@ export function useConversation(cardId, channelId) {
const actions = { const actions = {
more: () => { more: () => {
conversation.actions.addHistory(); conversation.actions.loadMore();
}, },
resync: () => { resync: () => {
conversation.actions.resync(); conversation.actions.resync();

View File

@ -70,7 +70,7 @@ export function useDetails(cardId, channelId) {
try { try {
const seals = JSON.parse(chan.data.channelDetail.data).seals; const seals = JSON.parse(chan.data.channelDetail.data).seals;
seals.forEach(seal => { seals.forEach(seal => {
if (account.state.sealKey.public === seal.publicKey) { if (account.state.sealKey?.public === seal.publicKey) {
editable = true; editable = true;
} }
}); });

View File

@ -37,7 +37,7 @@ export function useSession() {
} }
useEffect(() => { useEffect(() => {
if (!profile.state.identity?.guid || !card.state.init || !channel.state.init) { if (!profile.state.identity?.guid) {
updateState({ loading: true }); updateState({ loading: true });
} }
else { else {