interface: ported in Liam's graph-update reducer and threaded through some display parameters into LinkItem

This commit is contained in:
Logan Allen 2020-09-24 14:59:15 -05:00
parent 1eef5ac05f
commit 19b5d8cbfc
6 changed files with 50 additions and 19 deletions

View File

@ -0,0 +1,22 @@
export class OrderedMap<V> extends Map<number, V>
implements Iterable<[number, V]> {
[Symbol.iterator](): IterableIterator<[number, V]> {
const sorted = Array.from(super[Symbol.iterator]()).sort(
([a], [b]) => b - a
);
let index = 0;
return {
[Symbol.iterator]: this[Symbol.iterator],
next: (): IteratorResult<[number, V]> => {
if (index < sorted.length) {
return { value: sorted[index++], done: false };
} else {
return { done: true, value: null };
}
},
};
}
}

View File

@ -1,4 +1,5 @@
import _ from 'lodash'; import _ from 'lodash';
import { OrderedMap } from "~/logic/lib/OrderedMap";
export const GraphReducer = (json, state) => { export const GraphReducer = (json, state) => {
@ -17,11 +18,6 @@ const keys = (json, state) => {
if (data) { if (data) {
state.graphKeys = new Set(data.map((res) => { state.graphKeys = new Set(data.map((res) => {
let resource = res.ship + '/' + res.name; let resource = res.ship + '/' + res.name;
if (!(resource in state.graphs)) {
state.graphs[resource] = new Map();
}
return resource; return resource;
})); }));
} }
@ -32,12 +28,12 @@ const addGraph = (json, state) => {
const _processNode = (node) => { const _processNode = (node) => {
// is empty // is empty
if (!node.children) { if (!node.children) {
node.children = new Map(); node.children = new OrderedMap();
return node; return node;
} }
// is graph // is graph
let converted = new Map(); let converted = new OrderedMap();
for (let i in node.children) { for (let i in node.children) {
let item = node.children[i]; let item = node.children[i];
let index = item[0].split('/').slice(1).map((ind) => { let index = item[0].split('/').slice(1).map((ind) => {
@ -62,7 +58,7 @@ const addGraph = (json, state) => {
} }
let resource = data.resource.ship + '/' + data.resource.name; let resource = data.resource.ship + '/' + data.resource.name;
state.graphs[resource] = new Map(); state.graphs[resource] = new OrderedMap();
for (let i in data.graph) { for (let i in data.graph) {
let item = data.graph[i]; let item = data.graph[i];
@ -86,12 +82,19 @@ const removeGraph = (json, state) => {
if (!('graphs' in state)) { if (!('graphs' in state)) {
state.graphs = {}; state.graphs = {};
} }
let resource = data.ship + '/' + data.name; let resource = data.resource.ship + '/' + data.resource.name;
delete state.graphs[resource]; delete state.graphs[resource];
state.graphKeys.delete(resource);
} }
}; };
const mapifyChildren = (children) => {
return new OrderedMap(
children.map(([idx, node]) => {
const nd = {...node, children: mapifyChildren(node.children || []) };
return [parseInt(idx.slice(1), 10), nd];
}));
};
const addNodes = (json, state) => { const addNodes = (json, state) => {
const _addNode = (graph, index, node) => { const _addNode = (graph, index, node) => {
// set child of graph // set child of graph
@ -128,8 +131,8 @@ const addNodes = (json, state) => {
if (index.length === 0) { return; } if (index.length === 0) { return; }
// TODO: support adding nodes with children item[1].children = mapifyChildren(item[1].children || []);
item[1].children = new Map();
state.graphs[resource] = _addNode( state.graphs[resource] = _addNode(
state.graphs[resource], state.graphs[resource],
@ -167,4 +170,3 @@ const removeNodes = (json, state) => {
}); });
} }
}; };

View File

@ -43,7 +43,6 @@ export default class GlobalStore extends BaseStore<StoreState> {
this.localReducer.dehydrate(this.state); this.localReducer.dehydrate(this.state);
} }
initialState(): StoreState { initialState(): StoreState {
return { return {
pendingMessages: new Map(), pendingMessages: new Map(),

View File

@ -185,6 +185,7 @@ export default class LinksApp extends Component {
associations.graph[metPath] ? associations.graph[metPath] ?
associations.graph[metPath] : { metadata: {} }; associations.graph[metPath] : { metadata: {} };
const popout = props.match.url.includes('/popout/'); const popout = props.match.url.includes('/popout/');
const contactDetails = contacts[resource['group-path']] || {}; const contactDetails = contacts[resource['group-path']] || {};
const indexArr = props.match.params.index.split('-'); const indexArr = props.match.params.index.split('-');

View File

@ -8,6 +8,8 @@ export const LinkItem = (props) => {
const { const {
node, node,
nickname, nickname,
color,
avatar,
resource, resource,
hideAvatars, hideAvatars,
hideNicknames hideNicknames
@ -18,14 +20,14 @@ export const LinkItem = (props) => {
const size = node.children ? node.children.size : 0; const size = node.children ? node.children.size : 0;
const contents = node.post.contents; const contents = node.post.contents;
const showAvatar = props.avatar && !hideAvatars; const showAvatar = avatar && !hideAvatars;
const showNickname = nickname && !hideNicknames; const showNickname = nickname && !hideNicknames;
const mono = showNickname ? 'inter white-d' : 'mono white-d'; const mono = showNickname ? 'inter white-d' : 'mono white-d';
const img = showAvatar const img = showAvatar
? <img src={props.avatar} height={38} width={38} className="dib" /> ? <img src={avatar} height={38} width={38} className="dib" />
: <Sigil ship={`~${author}`} size={38} color={'#' + props.color} />; : <Sigil ship={`~${author}`} size={38} color={'#' + color} />;
return ( return (
<div className="w-100 pv3 flex bg-white bg-gray0-d lh-solid"> <div className="w-100 pv3 flex bg-white bg-gray0-d lh-solid">

View File

@ -68,12 +68,17 @@ export const LinkList = (props) => {
ship={props.ship} ship={props.ship}
api={props.api} /> api={props.api} />
</div> </div>
{ Array.from(props.graph.values()).reverse().map((node) => { { Array.from(props.graph.values()).map((node) => {
const { nickname, color, avatar } =
getContactDetails(props.contacts[ship]);
return ( return (
<LinkItem <LinkItem
resource={resource} resource={resource}
node={node} node={node}
nickname={props.metadata.nickname} nickname={nickname}
color={color}
avatar={avatar}
hideAvatars={props.hideAvatars} hideAvatars={props.hideAvatars}
hideNicknames={props.hideNicknames} hideNicknames={props.hideNicknames}
/> />