databag/app/mobile/src/context/useCardContext.hook.js

440 lines
17 KiB
JavaScript
Raw Normal View History

2022-09-15 08:03:20 +00:00
import { useState, useRef, useContext } from 'react';
import { StoreContext } from 'context/StoreContext';
2022-09-15 18:26:03 +00:00
import { getCards } from 'api/getCards';
import { getCardProfile } from 'api/getCardProfile';
import { getCardDetail } from 'api/getCardDetail';
2022-09-15 08:03:20 +00:00
2022-09-16 18:28:54 +00:00
import { getContactChannels } from 'api/getContactChannels';
2022-09-16 06:18:46 +00:00
import { getContactChannelTopics } from 'api/getContactChannelTopics';
import { getContactChannelDetail } from 'api/getContactChannelDetail';
import { getContactChannelSummary } from 'api/getContactChannelSummary';
2022-09-20 07:50:53 +00:00
import { getCardImageUrl } from 'api/getCardImageUrl';
2022-09-16 06:18:46 +00:00
2022-09-26 19:14:06 +00:00
import { addCard } from 'api/addCard';
import { removeCard } from 'api/removeCard';
import { setCardConnecting, setCardConnected, setCardConfirmed } from 'api/setCardStatus';
import { getCardOpenMessage } from 'api/getCardOpenMessage';
import { setCardOpenMessage } from 'api/setCardOpenMessage';
import { getCardCloseMessage } from 'api/getCardCloseMessage';
import { setCardCloseMessage } from 'api/setCardCloseMessage';
2022-09-15 08:03:20 +00:00
export function useCardContext() {
const [state, setState] = useState({
2022-09-20 05:39:53 +00:00
cards: new Map(),
2022-09-15 08:03:20 +00:00
});
const store = useContext(StoreContext);
const session = useRef(null);
const curRevision = useRef(null);
const setRevision = useRef(null);
const syncing = useRef(false);
2022-09-20 05:39:53 +00:00
const cards = useRef(new Map());
const cardChannels = useRef(new Map());
2022-09-15 08:03:20 +00:00
const updateState = (value) => {
setState((s) => ({ ...s, ...value }))
}
2022-09-20 07:50:53 +00:00
const setCard = (cardId, card) => {
let updated = cards.current.get(cardId);
if (updated == null) {
updated = { channels: new Map() };
}
cards.current.set(cardId, {
...updated,
cardId: cardId,
revision: card?.revision,
detailRevision: card?.data?.detailRevision,
profileRevision: card?.data?.profileRevision,
detail: card?.data?.cardDetail,
profile: card?.data?.cardProfile,
notifiedView: card?.data?.notifiedView,
notifiedProfile: card?.data?.notifiedProfile,
notifiedArtile: card?.data?.notifiedArticle,
notifiedChannel: card?.data?.notifiedChannel,
});
}
2022-09-20 05:39:53 +00:00
const setCardDetail = (cardId, detail, revision) => {
let card = cards.current.get(cardId);
2022-09-20 07:50:53 +00:00
if (card) {
card.detail = detail;
card.detailRevision = revision;
2022-09-20 05:39:53 +00:00
cards.current.set(cardId, card);
}
}
const setCardProfile = (cardId, profile, revision) => {
let card = cards.current.get(cardId);
2022-09-20 07:50:53 +00:00
if (card) {
card.profile = profile;
card.profileRevision = revision;
2022-09-20 05:39:53 +00:00
cards.current.set(cardId, card);
}
}
const setCardRevision = (cardId, revision) => {
let card = cards.current.get(cardId);
if (card) {
card.revision = revision;
cards.current.set(cardId, card);
}
}
const setCardOffsync = (cardId, offsync) => {
let card = cards.current.get(cardId);
if (card) {
card.offsync = offsync;
cards.current.set(cardId, card);
}
}
2022-09-26 21:39:01 +00:00
const setCardBlocked = (cardId, blocked) => {
let card = cards.current.get(cardId);
if (card) {
card.blocked = blocked;
cards.current.set(cardId, card);
}
}
2022-09-20 05:39:53 +00:00
const clearCardChannels = (cardId) => {
let card = cards.current.get(cardId);
if (card) {
card.channels = new Map();
cards.current.set(cardId, card);
}
}
const setCardChannel = (cardId, channel) => {
2022-09-20 07:50:53 +00:00
setCardChannelItem(cardId, {
cardId: cardId,
channelId: channel?.id,
revision: channel?.revision,
detailRevision: channel?.data?.detailRevision,
topicRevision: channel?.data?.topicRevision,
detail: channel?.data?.channelDetail,
summary: channel?.data?.channelSummary,
});
}
const setCardChannelItem = (cardId, channel) => {
2022-09-20 05:39:53 +00:00
let card = cards.current.get(cardId);
if (card) {
2022-09-20 21:57:58 +00:00
card.channels.set(channel.channelId, channel);
2022-09-20 05:39:53 +00:00
cards.current.set(cardId, card);
}
}
const setCardChannelDetail = (cardId, channelId, detail, revision) => {
let card = cards.current.get(cardId);
if (card) {
let channel = card.channels.get(channelId);
2022-09-20 07:50:53 +00:00
if (channel) {
channel.detail = detail;
channel.detailRevision = revision;
2022-09-20 05:39:53 +00:00
card.channels.set(channelId, channel);
cards.current.set(cardId, card);
}
}
}
const setCardChannelSummary = (cardId, channelId, summary, revision) => {
let card = cards.current.get(cardId);
if (card) {
let channel = card.channels.get(channelId);
2022-09-20 07:50:53 +00:00
if (channel) {
2022-09-20 21:57:58 +00:00
channel.summary = summary;
2022-09-20 07:50:53 +00:00
channel.topicRevision = revision;
2022-09-20 05:39:53 +00:00
card.channels.set(channelId, channel);
cards.current.set(cardId, card);
}
}
}
const setCardChannelRevision = (cardId, channelId, revision) => {
let card = cards.current.get(cardId);
if (card) {
let channel = card.channels.get(channelId);
if (channel) {
channel.revision = revision;
card.channels.set(channelId, channel);
cards.current.set(cardId, card);
}
}
2022-09-20 21:57:58 +00:00
}
const setCardChannelReadRevision = (cardId, channelId, revision) => {
let card = cards.current.get(cardId);
if (card) {
let channel = card.channels.get(channelId);
if (channel) {
channel.readRevision = revision;
card.channels.set(channelId, channel);
cards.current.set(cardId, card);
}
}
}
2022-09-20 05:39:53 +00:00
const clearCardChannel = (cardId, channelId) => {
let card = cards.current.get(cardId);
if (card) {
card.channels.delete(channelId);
cards.current.set(cardId, card);
}
}
2022-09-15 08:03:20 +00:00
const sync = async () => {
if (!syncing.current && setRevision.current !== curRevision.current) {
syncing.current = true;
try {
const revision = curRevision.current;
const { server, appToken, guid } = session.current;
// get and store
2022-09-15 18:26:03 +00:00
const delta = await getCards(server, appToken, setRevision.current);
for (let card of delta) {
if (card.data) {
if (card.data.cardDetail && card.data.cardProfile) {
await store.actions.setCardItem(guid, card);
2022-09-20 07:50:53 +00:00
setCard(card.id, card);
2022-09-15 18:26:03 +00:00
}
else {
const view = await store.actions.getCardItemView(guid, card.id);
2022-09-15 22:12:06 +00:00
if (view == null) {
let assembled = JSON.parse(JSON.stringify(card));
assembled.data.cardDetail = await getCardDetail(server, appToken, card.id);
assembled.data.cardProfile = await getCardProfile(server, appToken, card.id);
await store.actions.setCardItem(guid, assembled);
2022-09-20 07:50:53 +00:00
setCard(assembled.id, assembled);
2022-09-15 18:26:03 +00:00
}
2022-09-15 22:12:06 +00:00
else {
2022-09-20 21:57:58 +00:00
const { detailRevision, profileRevision } = card.data;
2022-09-15 22:12:06 +00:00
if (view.detailRevision != detailRevision) {
const detail = await getCardDetail(server, appToken, card.id);
await store.actions.setCardItemDetail(guid, card.id, detailRevision, detail);
2022-09-20 05:39:53 +00:00
setCardDetail(card.id, detail, detailRevision);
2022-09-15 22:12:06 +00:00
}
if (view.profileRevision != profileRevision) {
const profile = await getCardProfile(server, appToken, card.id);
await store.actions.setCardItemProfile(guid, card.id, profileRevision, profile);
2022-09-20 05:39:53 +00:00
setCardProfile(card.id, profile, profileRevision);
2022-09-15 22:12:06 +00:00
}
2022-09-20 05:39:53 +00:00
await store.actions.setCardItemRevision(guid, card.id, card.revision);
setCardRevision(card.id, card.revision);
2022-09-15 18:26:03 +00:00
}
2022-09-15 22:12:06 +00:00
}
const status = await store.actions.getCardItemStatus(guid, card.id);
2022-09-16 18:28:54 +00:00
const cardServer = status.profile.node;
const cardToken = status.profile.guid + '.' + status.detail.token;
2022-09-15 22:12:06 +00:00
if (status.detail.status === 'connected') {
2022-09-16 06:18:46 +00:00
try {
const { notifiedView, notifiedProfile, notifiedArticle, notifiedChannel } = card.data;
if (status.notifiedView !== notifiedView) {
await store.actions.clearCardChannelItems(guid, card.id);
2022-09-16 18:28:54 +00:00
await updateCardChannelItems(card.id, cardServer, cardToken, notifiedView, null);
2022-09-15 22:12:06 +00:00
await store.actions.setCardItemNotifiedChannel(guid, card.id, notifiedChannel);
2022-09-16 06:18:46 +00:00
await store.actions.setCardItemNotifiedView(guid, card.id, notifiedView);
2022-09-20 05:39:53 +00:00
clearCardChannel(card.id);
2022-09-16 06:18:46 +00:00
}
else {
if (status.notifiedChannel != notifiedChannel) {
2022-09-20 21:57:58 +00:00
await updateCardChannelItems(card.id, cardServer, cardToken, status.notifiedView, status.notifiedChannel)
await store.actions.setCardItemNotifiedChannel(guid, card.id, notifiedChannel);
2022-09-16 06:18:46 +00:00
}
}
if (status.notifiedProflile != notifiedProfile) {
// TODO update contact profile if different
await store.actions.setCardItemNotifiedProfile(guid, card.id, notifiedProfile);
}
if (status.offsync) {
2022-09-16 18:28:54 +00:00
await store.actions.clearCardItemOffsync(guid, card.id);
2022-09-20 05:39:53 +00:00
setCardOffsync(card.id, false);
2022-09-15 18:26:03 +00:00
}
}
2022-09-16 06:18:46 +00:00
catch(err) {
2022-09-20 07:50:53 +00:00
console.log("card1:", err);
2022-09-16 18:28:54 +00:00
await store.actions.setCardItemOffsync(guid, card.id);
2022-09-20 05:39:53 +00:00
setCardOffsync(card.id, true);
2022-09-16 06:18:46 +00:00
}
2022-09-15 18:26:03 +00:00
}
}
else {
2022-09-15 22:12:06 +00:00
//TODO clear card channel topics
await store.actions.clearCardChannelItems(guid, card.id);
2022-09-15 18:26:03 +00:00
await store.actions.clearCardItem(guid, card.id);
2022-09-20 05:39:53 +00:00
cards.current.delete(card.id);
2022-09-15 18:26:03 +00:00
}
}
2022-09-15 08:03:20 +00:00
setRevision.current = revision;
2022-09-15 18:26:03 +00:00
await store.actions.setCardRevision(guid, revision);
2022-09-15 08:03:20 +00:00
}
catch(err) {
2022-09-20 07:50:53 +00:00
console.log("card2:", err);
2022-09-15 08:03:20 +00:00
syncing.current = false;
return;
}
2022-09-20 05:39:53 +00:00
updateState({ cards: cards.current });
2022-09-15 08:03:20 +00:00
syncing.current = false;
sync();
}
};
2022-09-16 06:18:46 +00:00
const updateCardChannelItems = async (cardId, cardServer, cardToken, notifiedView, notifiedChannel) => {
const { guid } = session.current;
const delta = await getContactChannels(cardServer, cardToken, notifiedView, notifiedChannel);
for (let channel of delta) {
if (channel.data) {
if (channel.data.channelDetail && channel.data.channelSummary) {
await store.actions.setCardChannelItem(guid, cardId, channel);
2022-09-20 05:39:53 +00:00
setCardChannel(cardId, channel);
2022-09-16 06:18:46 +00:00
}
else {
const { detailRevision, topicRevision, channelDetail, channelSummary } = channel.data;
const view = await store.actions.getCardChannelItemView(guid, cardId, channel.id);
if (view == null) {
console.log('alert: expected channel not synced');
let assembled = JSON.parse(JSON.stringify(channel));
2022-09-20 21:57:58 +00:00
assembled.data.channelDetail = await getContactChannelDetail(cardServer, cardToken, channel.id);
assembled.data.channelSummary = await getContactChannelSummary(cardServer, cardToken, channel.id);
2022-09-16 06:18:46 +00:00
await store.actions.setCardChannelItem(guid, cardId, assembled);
2022-09-20 05:39:53 +00:00
setCardChannel(cardId, assembled);
2022-09-16 06:18:46 +00:00
}
else {
if (view.detailRevision != detailRevision) {
2022-09-20 21:57:58 +00:00
const detail = await getContactChannelDetail(cardServer, cardToken, channel.id);
2022-09-16 06:18:46 +00:00
await store.actions.setCardChannelItemDetail(guid, cardId, channel.id, detailRevision, detail);
2022-09-20 05:39:53 +00:00
setCardChannelDetail(cardId, channel.id, detail, detailRevision);
2022-09-16 06:18:46 +00:00
}
if (view.topicRevision != topicRevision) {
2022-09-20 21:57:58 +00:00
const summary = await getContactChannelSummary(cardServer, cardToken, channel.id);
2022-09-16 06:18:46 +00:00
await store.actions.setCardChannelItemSummary(guid, cardId, channel.id, topicRevision, summary);
2022-09-20 05:39:53 +00:00
setCardChannelSummary(cardId, channel.id, summary, topicRevision);
2022-09-16 06:18:46 +00:00
}
await store.actions.setCardChannelItemRevision(guid, cardId, channel.revision);
2022-09-20 05:39:53 +00:00
setCardChannelRevision(cardId, channel.id, channel.revision);
2022-09-16 06:18:46 +00:00
}
}
}
else {
await store.actions.clearCardChannelItem(guid, cardId, channel.id);
2022-09-20 05:39:53 +00:00
clearCardChannel(cardId, channel.id);
2022-09-16 06:18:46 +00:00
}
}
}
2022-09-15 08:03:20 +00:00
const actions = {
setSession: async (access) => {
const { guid, server, appToken } = access;
2022-09-20 05:39:53 +00:00
cards.current = new Map();
const cardItems = await store.actions.getCardItems(guid);
for (item of cardItems) {
cards.current.set(item.cardId, { ...item, channels: new Map() });
}
const cardChannelItems = await store.actions.getCardChannelItems(guid);
for (item of cardChannelItems) {
2022-09-20 07:50:53 +00:00
setCardChannelItem(item.cardId, item);
2022-09-20 05:39:53 +00:00
}
2022-09-15 08:03:20 +00:00
const revision = await store.actions.getCardRevision(guid);
2022-09-20 05:39:53 +00:00
updateState({ cards: cards.current });
2022-09-15 08:03:20 +00:00
setRevision.current = revision;
curRevision.current = revision;
session.current = access;
},
clearSession: () => {
session.current = {};
updateState({ account: null });
},
setRevision: (rev) => {
curRevision.current = rev;
sync();
},
2022-09-20 21:57:58 +00:00
setReadRevision: async (cardId, channelId, rev) => {
await store.actions.setCardChannelItemReadRevision(session.current.guid, cardId, channelId, rev);
setCardChannelReadRevision(cardId, channelId, rev);
updateState({ cards: cards.current });
},
2022-09-20 07:50:53 +00:00
getCardLogo: (cardId, revision) => {
const { server, appToken } = session.current;
return getCardImageUrl(server, appToken, cardId, revision);
2022-09-25 05:56:41 +00:00
},
getByGuid: (guid) => {
let card;
cards.current.forEach((value, key, map) => {
if (value?.profile?.guid === guid) {
card = value;
}
});
return card;
},
2022-09-26 19:14:06 +00:00
addCard: async (message) => {
const { server, appToken } = session.current;
return await addCard(server, appToken, message);
},
removeCard: async (cardId) => {
const { server, appToken } = session.current;
return await removeCard(server, appToken, cardId);
},
setCardConnecting: async (cardId) => {
const { server, appToken } = session.current;
return await setCardConnecting(server, appToken, cardId);
},
setCardConnected: async (cardId, token, rev) => {
const { server, appToken } = session.current;
return await setCardConnected(server, appToken, cardId, token,
rev.viewRevision, rev.articleRevision, rev.channelRevision, rev.profileRevision);
},
setCardConfirmed: async (cardId) => {
const { server, appToken } = session.current;
return await setCardConfirmed(server, appToken, cardId);
},
getCardOpenMessage: async (cardId) => {
const { server, appToken } = session.current;
return await getCardOpenMessage(server, appToken, cardId);
},
setCardOpenMessage: async (server, message) => {
return await setCardOpenMessage(server, message);
},
getCardCloseMessage: async (cardId) => {
const { server, appToken } = session.current;
return await getCardCloseMessage(server, appToken, cardId);
},
setCardCloseMessage: async (server, message) => {
return await setCardCloseMessage(server, message);
},
2022-09-26 21:39:01 +00:00
setCardBlocked: async (cardId) => {
const { guid } = session.current;
setCardBlocked(cardId, true);
await store.actions.setCardItemBlocked(guid, cardId);
updateState({ cards: cards.current });
},
clearCardBlocked: async (cardId) => {
const { guid } = session.current;
setCardBlocked(cardId, false);
await store.actions.clearCardItemBlocked(guid, cardId);
updateState({ cards: cards.current });
2022-09-29 06:30:22 +00:00
},
getSyncRevision: async (cardId, channelId) => {
const { guid } = session.current;
return await store.actions.getCardChannelItemSyncRevision(guid, cardId, channelId);
},
setSyncRevision: async (cardId, channelId, revision) => {
const { guid } = session.current;
return await store.actions.setCardChannelItemSyncRevision(guid, cardId, channelId, revision);
},
getTopicItems: async (cardId, channelId) => {
const { guid } = session.current;
return await store.actions.getCardChannelTopicItems(guid, cardId, channelId);
},
getTopicDeltaItems: async (cardId, channelId, revision) => {
const { guid } = session.current;
return await store.actions.getCardChannelTopicDeltaItems(guid, cardId, channelId, revision);
},
setTopicItem: async (cardId, channelId, topicId, channelRevision, topic) => {
const { guid } = session.current;
return await store.actions.setCardChannelTopicItem(guid, cardId, channelId, topicId, channelRevision, topic);
},
clearTopicItem: async (cardId, channelId, topicId) => {
const { guid } = session.current;
return await store.actions.clearCardChannelTopicItem(guid, cardId, channelId, topicId);
},
clearTopicItems: async (cardId, channelId) => {
const { guid } = session.current;
return await store.actions.clearCardChannelTopicItems(guid, cardId, channelId);
},
2022-09-15 08:03:20 +00:00
}
return { state, actions }
}