more webapp channels refactor

This commit is contained in:
Roland Osborne 2023-01-19 09:58:24 -08:00
parent f1f652bf64
commit bc7bd28502
3 changed files with 85 additions and 76 deletions

View File

@ -45,7 +45,7 @@ export function useChannels() {
} }
if (profile?.imageSet) { if (profile?.imageSet) {
img = null; img = null;
logo = card.actions.getCardImageUrl(cardValue.data.profileRevision); logo = card.actions.getCardImageUrl(cardValue.id);
} }
else { else {
img = 'avatar'; img = 'avatar';
@ -87,84 +87,96 @@ export function useChannels() {
item.label = names.join(','); item.label = names.join(',');
} }
// set subject // update title on detailRevision or sealKey changes
const detail = channelValue.data?.channelDetail; const sealKey = account.state.sealKey;
if (detail?.dataType === 'sealed') { const detailRevision = channelValue?.data?.detailRevision;
item.locked = true; if (item.detailRevision !== detailRevision || item.sealKey !== sealKey) {
try { const detail = channelValue.data?.channelDetail;
const { sealKey } = account.state; if (detail?.dataType === 'sealed') {
const seals = getChannelSeals(detail.data); item.locked = true;
if (isUnsealed(seals, sealKey)) { try {
item.unlocked = true; const { sealKey } = account.state;
if (!item.contentKey) { const seals = getChannelSeals(detail.data);
item.contentKey = getContentKey(seals, sealKey); if (isUnsealed(seals, sealKey)) {
item.unlocked = true;
if (!item.contentKey) {
item.contentKey = getContentKey(seals, sealKey);
}
const unsealed = decryptChannelSubject(detail.data, item.contentKey);
item.title = unsealed?.subject;
}
else {
item.unlocked = false;
item.contentKey = null;
item.title = null;
} }
const unsealed = decryptChannelSubject(detail.data, item.contentKey);
item.subject = unsealed?.subject;
} }
else { catch(err) {
console.log(err);
item.unlocked = false; item.unlocked = false;
item.contentKey = null;
item.subject = null;
} }
} }
catch(err) { else if (detail?.dataType === 'superbasic') {
console.log(err); item.locked = false;
item.unlocked = false; item.unlocked = true;
} try {
} const data = JSON.parse(detail.data);
else if (detail?.dataType === 'superbasic') { item.title = data.subject;
item.locked = false; }
item.unlocked = true; catch(err) {
try { console.log(err);
const data = JSON.parse(detail.data); item.title = null;
item.subject = data.subject; }
}
catch(err) {
console.log(err);
item.subject = null;
} }
item.detailRevision = detailRevision;
item.sealKey = sealKey;
} }
if (item.subject == null || typeof item.subject !== 'string') { if (item.title == null || typeof item.title !== 'string') {
item.subject = item.label; item.subject = item.label;
} }
else {
// set updated revision item.subject = item.title;
item.detailRevision = channelValue.data.detailRevision; }
} }
const syncChannelSummary = (item, channelValue) => { const syncChannelSummary = (item, channelValue) => {
const topic = channelValue.data?.channelSummary?.lastTopic; const sealKey = account.state.sealKey;
item.updated = topic?.created; const topicRevision = channelValue?.data?.topicRevision;
if (topic?.dataType === 'superbasictopic') { if (item.topicRevision !== topicRevision || item.sealKey !== sealKey) {
try { const topic = channelValue.data?.channelSummary?.lastTopic;
const data = JSON.parse(topic.data); item.updated = topic?.created;
item.message = data.text; if (topic?.dataType === 'superbasictopic') {
} try {
catch (err) { const data = JSON.parse(topic.data);
console.log(err); item.message = data.text;
}
}
else if (topic?.dataType === 'sealedtopic') {
try {
if (item.contentKey) {
const unsealed = decryptTopicSubject(topic.data, item.contentKey);
item.message = unsealed?.message?.text;
} }
else { catch (err) {
console.log(err);
}
}
else if (topic?.dataType === 'sealedtopic') {
try {
if (item.contentKey) {
const unsealed = decryptTopicSubject(topic.data, item.contentKey);
item.message = unsealed?.message?.text;
}
else {
item.message = null;
}
}
catch(err) {
console.log(err);
item.message = null; item.message = null;
} }
} }
catch(err) {
console.log(err);
item.message = null;
}
}
// set updated revision // set updated revision
item.topicRevision = channelValue.data.topicRevision; item.topicRevision = topicRevision;
item.sealKey = sealKey;
}
}; };
useEffect(() => { useEffect(() => {
@ -179,12 +191,10 @@ export function useChannels() {
if (!item) { if (!item) {
item = { cardId, channelId }; item = { cardId, channelId };
} }
syncChannelDetail(item, cardValue, channelValue); syncChannelDetail(item, cardValue, channelValue);
if (item.topicRevision !== topicRevision || syncChannelSummary(item, channelValue);
item.sealKey !== sealKey) {
syncChannelSummary(item, channelValue);
}
item.sealKey = sealKey;
const revision = store.state[key]; const revision = store.state[key];
if (login && item.updated && item.updated > login && topicRevision !== revision) { if (login && item.updated && item.updated > login && topicRevision !== revision) {
item.updatedFlag = true; item.updatedFlag = true;
@ -203,11 +213,8 @@ export function useChannels() {
item = { channelId }; item = { channelId };
} }
syncChannelDetail(item, null, channelValue); syncChannelDetail(item, null, channelValue);
if (item.topicRevision !== topicRevision || syncChannelSummary(item, channelValue);
item.sealKey !== sealKey) {
syncChannelSummary(item, channelValue);
}
item.sealKey = sealKey;
const revision = store.state[key]; const revision = store.state[key];
if (login && item.updated && item.updated > login && topicRevision !== revision) { if (login && item.updated && item.updated > login && topicRevision !== revision) {
item.updatedFlag = true; item.updatedFlag = true;

View File

@ -11,10 +11,11 @@ import { UnlockOutlined, LockFilled } from '@ant-design/icons';
export function Details({ cardId, channelId, closeDetails, closeConversation, openContact }) { export function Details({ cardId, channelId, closeDetails, closeConversation, openContact }) {
const [modal, modalContext] = Modal.useModal();
const { state, actions } = useDetails(cardId, channelId); const { state, actions } = useDetails(cardId, channelId);
const deleteChannel = async () => { const deleteChannel = async () => {
Modal.confirm({ modal.confirm({
title: 'Are you sure you want to delete the topic?', title: 'Are you sure you want to delete the topic?',
icon: <ExclamationCircleOutlined />, icon: <ExclamationCircleOutlined />,
okText: "Yes, Delete", okText: "Yes, Delete",
@ -32,7 +33,7 @@ export function Details({ cardId, channelId, closeDetails, closeConversation, op
closeConversation(); closeConversation();
} }
catch(err) { catch(err) {
Modal.error({ modal.error({
title: 'Failed to Delete Topic', title: 'Failed to Delete Topic',
content: 'Please try again.', content: 'Please try again.',
}); });
@ -40,7 +41,7 @@ export function Details({ cardId, channelId, closeDetails, closeConversation, op
} }
const leaveChannel = async () => { const leaveChannel = async () => {
Modal.confirm({ modal.confirm({
title: 'Are you sure you want to leave the topic?', title: 'Are you sure you want to leave the topic?',
icon: <ExclamationCircleOutlined />, icon: <ExclamationCircleOutlined />,
okText: "Yes, Leave", okText: "Yes, Leave",
@ -58,7 +59,7 @@ export function Details({ cardId, channelId, closeDetails, closeConversation, op
closeConversation(); closeConversation();
} }
catch(err) { catch(err) {
Modal.error({ modal.error({
title: 'Failed to Leave Topic', title: 'Failed to Leave Topic',
content: 'Please try again.', content: 'Please try again.',
}); });
@ -71,7 +72,7 @@ export function Details({ cardId, channelId, closeDetails, closeConversation, op
actions.clearEditSubject(); actions.clearEditSubject();
} }
catch(err) { catch(err) {
Modal.error({ modal.error({
title: 'Failed to Update Subject', title: 'Failed to Update Subject',
content: 'Please try again.', content: 'Please try again.',
}); });
@ -93,6 +94,7 @@ export function Details({ cardId, channelId, closeDetails, closeConversation, op
return ( return (
<DetailsWrapper> <DetailsWrapper>
{ modalContext }
<div class="header"> <div class="header">
<div class="label">Topic</div> <div class="label">Topic</div>
{ state.display === 'xlarge' && ( { state.display === 'xlarge' && (

View File

@ -111,7 +111,7 @@ export function useSession() {
updateState({ conversation: true, cardId, channelId }); updateState({ conversation: true, cardId, channelId });
}, },
closeConversation: () => { closeConversation: () => {
updateState({ conversation: false }); updateState({ conversation: false, cardId: null, channelId: null });
}, },
openDetails: () => { openDetails: () => {
updateState({ details: true }); updateState({ details: true });