mirror of
https://github.com/balzack/databag.git
synced 2025-02-14 12:39:17 +00:00
updating channel item reference
This commit is contained in:
parent
9e3074132d
commit
ab2bcc751b
@ -15,7 +15,7 @@ export function ChannelItem({ item }) {
|
|||||||
return (
|
return (
|
||||||
<ChannelItemWrapper onClick={() => onSelect()}>
|
<ChannelItemWrapper onClick={() => onSelect()}>
|
||||||
<ChannelLogo item={item} />
|
<ChannelLogo item={item} />
|
||||||
{state.updated && (
|
{item.updated && (
|
||||||
<Marker />
|
<Marker />
|
||||||
)}
|
)}
|
||||||
<ChannelLabel item={item} />
|
<ChannelLabel item={item} />
|
||||||
|
@ -1,25 +1,11 @@
|
|||||||
import { useContext, useState, useEffect } from 'react';
|
import { useContext, useState, useEffect } from 'react';
|
||||||
import { useNavigate } from 'react-router-dom';
|
import { useNavigate } from 'react-router-dom';
|
||||||
import { StoreContext } from 'context/StoreContext';
|
|
||||||
|
|
||||||
export function useChannelItem(item) {
|
export function useChannelItem(item) {
|
||||||
|
|
||||||
const [state, setState] = useState({
|
const [state, setState] = useState({
|
||||||
updated: false,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const store = useContext(StoreContext);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
let key = `${item.id}::${item.cardId}`;
|
|
||||||
if (store.state[key] == item.revision) {
|
|
||||||
updateState({ updated: false });
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
updateState({ updated: true });
|
|
||||||
}
|
|
||||||
}, [store]);
|
|
||||||
|
|
||||||
const updateState = (value) => {
|
const updateState = (value) => {
|
||||||
setState((s) => ({ ...s, ...value }));
|
setState((s) => ({ ...s, ...value }));
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ import { useNavigate } from 'react-router-dom';
|
|||||||
import { CardContext } from 'context/CardContext';
|
import { CardContext } from 'context/CardContext';
|
||||||
import { ProfileContext } from 'context/ProfileContext';
|
import { ProfileContext } from 'context/ProfileContext';
|
||||||
import { ChannelContext } from 'context/ChannelContext';
|
import { ChannelContext } from 'context/ChannelContext';
|
||||||
|
import { StoreContext } from 'context/StoreContext';
|
||||||
|
|
||||||
export function useChannels() {
|
export function useChannels() {
|
||||||
|
|
||||||
@ -26,6 +27,7 @@ export function useChannels() {
|
|||||||
const card = useContext(CardContext);
|
const card = useContext(CardContext);
|
||||||
const profile = useContext(ProfileContext);
|
const profile = useContext(ProfileContext);
|
||||||
const channel = useContext(ChannelContext);
|
const channel = useContext(ChannelContext);
|
||||||
|
const store = useContext(StoreContext);
|
||||||
|
|
||||||
const actions = {
|
const actions = {
|
||||||
getCardImageUrl: card.actions.getImageUrl,
|
getCardImageUrl: card.actions.getImageUrl,
|
||||||
@ -54,11 +56,27 @@ export function useChannels() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const setUpdated = (chan) => {
|
||||||
|
let key = `${chan.id}::${chan.cardId}`
|
||||||
|
if (store.state[key] && store.state[key] == chan.revision) {
|
||||||
|
chan.updated = false;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
chan.updated = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
let merged = [ ...channels.current, ...cardChannels.current ];
|
||||||
|
merged.forEach(c => { setUpdated(c) });
|
||||||
|
}, [store]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
cardChannels.current = [];
|
cardChannels.current = [];
|
||||||
card.state.cards.forEach((value, key, map) => {
|
card.state.cards.forEach((value, key, map) => {
|
||||||
cardChannels.current.push(...Array.from(value.channels.values()));
|
cardChannels.current.push(...Array.from(value.channels.values()));
|
||||||
});
|
});
|
||||||
|
cardChannels.current.forEach(c => { setUpdated(c) });
|
||||||
let merged = [ ...channels.current, ...cardChannels.current ];
|
let merged = [ ...channels.current, ...cardChannels.current ];
|
||||||
merged.sort((a, b) => {
|
merged.sort((a, b) => {
|
||||||
if (a?.data?.channelSummary?.lastTopic?.created > b?.data?.channelSummary?.lastTopic?.created) {
|
if (a?.data?.channelSummary?.lastTopic?.created > b?.data?.channelSummary?.lastTopic?.created) {
|
||||||
@ -71,6 +89,7 @@ export function useChannels() {
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
channels.current = Array.from(channel.state.channels.values());
|
channels.current = Array.from(channel.state.channels.values());
|
||||||
|
channels.current.forEach(c => { setUpdated(c) });
|
||||||
let merged = [ ...channels.current, ...cardChannels.current ];
|
let merged = [ ...channels.current, ...cardChannels.current ];
|
||||||
merged.sort((a, b) => {
|
merged.sort((a, b) => {
|
||||||
if (a?.data?.channelSummary?.lastTopic?.created > b?.data?.channelSummary?.lastTopic?.created) {
|
if (a?.data?.channelSummary?.lastTopic?.created > b?.data?.channelSummary?.lastTopic?.created) {
|
||||||
|
@ -103,7 +103,7 @@ export function useCardContext() {
|
|||||||
cur.channels = new Map();
|
cur.channels = new Map();
|
||||||
cur.articles = new Map();
|
cur.articles = new Map();
|
||||||
cur.revision = 0;
|
cur.revision = 0;
|
||||||
cards.current.set(card.id, cur);
|
cards.current.set(card.id, { ...cur });
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -112,7 +112,7 @@ export function useCardContext() {
|
|||||||
cur.articles = new Map();
|
cur.articles = new Map();
|
||||||
}
|
}
|
||||||
cur.revision = card.revision;
|
cur.revision = card.revision;
|
||||||
cards.current.set(card.id, cur);
|
cards.current.set(card.id, { ...cur });
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
cards.current.delete(card.id);
|
cards.current.delete(card.id);
|
||||||
@ -149,7 +149,7 @@ export function useCardContext() {
|
|||||||
cur.data.topicRevision = channel.data.topicRevision;
|
cur.data.topicRevision = channel.data.topicRevision;
|
||||||
}
|
}
|
||||||
cur.revision = channel.revision;
|
cur.revision = channel.revision;
|
||||||
channelMap.set(channel.id, cur);
|
channelMap.set(channel.id, { ...cur });
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
channelMap.delete(channel.id);
|
channelMap.delete(channel.id);
|
||||||
|
@ -56,7 +56,7 @@ export function useChannelContext() {
|
|||||||
cur.data.topicRevision = channel.data.topicRevision;
|
cur.data.topicRevision = channel.data.topicRevision;
|
||||||
}
|
}
|
||||||
cur.revision = channel.revision;
|
cur.revision = channel.revision;
|
||||||
channels.current.set(channel.id, cur);
|
channels.current.set(channel.id, { ...cur });
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
channels.current.delete(channel.id);
|
channels.current.delete(channel.id);
|
||||||
|
Loading…
Reference in New Issue
Block a user