Browse Source

Init orbit peer replication

develop
Apostolos Fanakis 4 years ago
parent
commit
556b308230
  1. 43
      packages/concordia-app/src/components/TopicList/TopicListRow/index.jsx

43
packages/concordia-app/src/components/TopicList/TopicListRow/index.jsx

@ -1,24 +1,41 @@
import React, { useContext, useMemo } from 'react'; import React, { useContext, useEffect, useState } from 'react';
import { List } from 'semantic-ui-react'; import { List } from 'semantic-ui-react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { useSelector } from 'react-redux';
import AppContext from '../../AppContext'; import AppContext from '../../AppContext';
const TopicListRow = (props) => { const TopicListRow = (props) => {
const { topicData, topicId } = props; const { topicData, topicId } = props;
const { breeze: { orbit } } = useContext(AppContext.Context);
const [topicSubject, setTopicSubject] = useState();
const userAddress = useSelector((state) => state.user.address);
const { useEffect(() => {
breeze: { if (userAddress === topicData.userAddress) {
orbit: { const topicsDb = Object.values(orbit.stores).find((store) => store.dbname === 'topics');
stores,
},
},
} = useContext(AppContext.Context);
const topicSubject = useMemo(() => { setTopicSubject(topicsDb.get(topicId));
const topicsDb = Object.values(stores).find((store) => store.dbname === 'topics'); return;
}
return topicsDb.get(topicId); // TODO: this is not correct, each TopicListRow inside the TopicList overrides the on.replicated callback. As a
}, [stores, topicId]); // result, for the topics of each user, only the callback of the last TopicListRow in the list survives and gets
// executed. Moving these calls up on the TopicList helps with this issue but introduces other problems.
orbit
.determineAddress('topics', 'keyvalue', { accessController: { write: [topicData.userAddress] } })
.then((ipfsMultihash) => {
const peerDbAddress = `/orbitdb/${ipfsMultihash.root}/topics`;
return orbit.keyvalue(peerDbAddress)
.then((keyValueStore) => {
keyValueStore.events.on('replicated', () => {
setTopicSubject(keyValueStore.get(topicId));
});
})
.catch((error) => console.error(`Error opening a peer's db: ${error}`));
})
.catch((error) => console.error(`Error opening a peer's db: ${error}`));
}, [orbit, topicData.userAddress, topicId, userAddress]);
return ( return (
<> <>
@ -40,7 +57,7 @@ const TopicListRow = (props) => {
const TopicData = PropTypes.PropTypes.shape({ const TopicData = PropTypes.PropTypes.shape({
userAddress: PropTypes.string.isRequired, userAddress: PropTypes.string.isRequired,
username: PropTypes.string.isRequired, username: PropTypes.string.isRequired,
timestamp: PropTypes.string.isRequired, timestamp: PropTypes.number.isRequired,
numberOfReplies: PropTypes.number.isRequired, numberOfReplies: PropTypes.number.isRequired,
}); });

Loading…
Cancel
Save