interface: pending and new message updates work across timeline, thread, and replies

This commit is contained in:
Logan Allen 2021-05-20 19:31:32 -05:00
parent ad5374d300
commit e63c63022d
2 changed files with 67 additions and 60 deletions

View File

@ -1,7 +1,8 @@
import { GraphNode } from '@urbit/api'; import { GraphNode } from '@urbit/api';
import BigIntOrderedMap from '@urbit/api/lib/BigIntOrderedMap'; import BigIntOrderedMap from '@urbit/api/lib/BigIntOrderedMap';
import BigIntArrayOrderedMap, { import BigIntArrayOrderedMap, {
arrToString arrToString,
stringToArr
} from '@urbit/api/lib/BigIntArrayOrderedMap'; } from '@urbit/api/lib/BigIntArrayOrderedMap';
import bigInt, { BigInteger } from 'big-integer'; import bigInt, { BigInteger } from 'big-integer';
import produce from 'immer'; import produce from 'immer';
@ -70,7 +71,7 @@ const addNodesFlat = (json: any, state: GraphState): GraphState => {
indices.forEach((index) => { indices.forEach((index) => {
if (index.split('/').length === 0) { return; } if (index.split('/').length === 0) { return; }
const indexArr = index.split('/').slice(1).map((ind) => bigInt(ind)); const indexArr = stringToArr(index);
if (indexArr.length === 0) { return state; } if (indexArr.length === 0) { return state; }
let node = data.nodes[index]; let node = data.nodes[index];
@ -103,7 +104,7 @@ const addNodesThread = (json: any, state: GraphState): GraphState => {
indices.forEach((index) => { indices.forEach((index) => {
if (index.split('/').length === 0) { return; } if (index.split('/').length === 0) { return; }
const indexArr = index.split('/').slice(1).map((ind) => bigInt(ind)); const indexArr = stringToArr(index);
if (indexArr.length === 0) { return state; } if (indexArr.length === 0) { return state; }
@ -236,32 +237,44 @@ const addNodes = (json, state) => {
return graph; return graph;
}; };
const _killByFuzzyTimestamp = (graph, resource, timestamp, isFlat = false) => { const _removePending = (
graph,
flatGraph,
threadGraphs,
post,
resource
) => {
if (!post.hash) { return [graph, flatGraph, threadGraphs]; }
const timestamp = post['time-sent'];
if (state.graphTimesentMap[resource][timestamp]) { if (state.graphTimesentMap[resource][timestamp]) {
const index = state.graphTimesentMap[resource][timestamp]; const index = state.graphTimesentMap[resource][timestamp];
if (index.split('/').length === 0) { return graph; } if (index.split('/').length === 0) { return graph; }
const indexArr = index.split('/').slice(1).map((ind) => bigInt(ind)); const indexArr = stringToArr(index);
delete state.graphTimesentMap[resource][timestamp]; delete state.graphTimesentMap[resource][timestamp];
if (isFlat) { if (graph) {
return graph.delete(indexArr); graph = _remove(graph, indexArr);
} else { }
return _remove(graph, indexArr);
if (flatGraph) {
flatGraph = flatGraph.delete(indexArr);
}
if (threadGraphs) {
let k = [];
for (let i in indexArr) {
k.push(indexArr[i]);
if (threadGraphs[k]) {
threadGraphs[k] = threadGraphs[k].delete(indexArr);
}
}
} }
} }
return graph;
};
const _removePending = (graph, post, resource, isFlat = false) => {
if (!post.hash) { return graph; }
graph = _killByFuzzyTimestamp(graph, resource, post['time-sent'], isFlat);
graph = _killByFuzzyTimestamp(graph, resource, post['time-sent'] - 1, isFlat);
graph = _killByFuzzyTimestamp(graph, resource, post['time-sent'] + 1, isFlat);
return graph; return [graph, flatGraph, threadGraphs];
}; };
const data = _.get(json, 'add-nodes', false); const data = _.get(json, 'add-nodes', false);
@ -292,29 +305,30 @@ const addNodes = (json, state) => {
indices.forEach((index) => { indices.forEach((index) => {
let node = data.nodes[index]; let node = data.nodes[index];
const old = state.graphs[resource].size; const old = state.graphs[resource].size;
if (index.split('/').length === 0) { return state; }
const indexArr = stringToArr(index);
if (resource in state.flatGraphs) { let [graph, flatGraph, threadGraphs] =
// TODO: will this cause multiple pending replies to render _removePending(
// in the reply view and thread view? probably
state.flatGraphs[resource] = _removePending(
state.flatGraphs[resource],
node.post,
resource,
true
);
} else {
state.graphs[resource] = _removePending(
state.graphs[resource], state.graphs[resource],
state.flatGraphs[resource],
state.threadGraphs[resource],
node.post, node.post,
resource resource
); );
}
const newSize = state.graphs[resource].size;
if (index.split('/').length === 0) { return; } if (graph) {
const indexArr = index.split('/').slice(1).map((ind) => bigInt(ind)); state.graphs[resource] = graph;
if (indexArr.length === 0) { return state; } }
if (flatGraph) {
state.flatGraphs[resource] = flatGraph;
}
if (threadGraphs) {
state.threadGraphs[resource] = threadGraphs;
}
const newSize = state.graphs[resource].size;
if (node.post.pending) { if (node.post.pending) {
state.graphTimesentMap[resource][node.post['time-sent']] = index; state.graphTimesentMap[resource][node.post['time-sent']] = index;
@ -335,28 +349,23 @@ const addNodes = (json, state) => {
})); }));
} }
// TODO: make pending work here too if (indexArr.length > 1 && resource in state.threadGraphs) {
let threadKey = indexArr[0].toString(); let parentKey = arrToString(indexArr.slice(0, indexArr.length - 1));
if (resource in state.threadGraphs &&
threadKey in state.threadGraphs[resource]) {
let thread = state.threadGraphs[resource][threadKey]; if (parentKey in state.threadGraphs[resource]) {
if (indexArr.length > 1) { let thread = state.threadGraphs[resource][parentKey];
let parentKey = indexArr.slice(0, indexArr.length - 1); let isFirstChild = Array.from(thread.keys()).filter((k) => {
if (thread.has(parentKey)) { return stringToArr(parentKey).length < k.length;
let isFirstChild = Array.from(thread.keys()).filter((k) => { }).length === 0;
return arrToString(k).indexOf(parentKey) !== -1;
}).length === 0;
if (isFirstChild) { if (isFirstChild) {
state.threadGraphs[resource][threadKey] = state.threadGraphs[resource][parentKey] =
state.threadGraphs[resource][threadKey].set( state.threadGraphs[resource][parentKey].set(
indexArr, indexArr,
produce(node, draft => { produce(node, draft => {
draft.children = mapifyChildren({}); draft.children = mapifyChildren({});
}) })
); );
}
} }
} }
} }
@ -408,9 +417,7 @@ const removePosts = (json, state: GraphState): GraphState => {
if (index.split('/').length === 0) { if (index.split('/').length === 0) {
return; return;
} }
const indexArr = index.split('/').slice(1).map((ind) => { const indexArr = stringToArr(index);
return bigInt(ind);
});
state.graphs[res] = _remove(state.graphs[res], indexArr); state.graphs[res] = _remove(state.graphs[res], indexArr);
}); });
} }

View File

@ -14,7 +14,7 @@ export const stateSetter = <T extends {}>(
): void => { ): void => {
const old = get(); const old = get();
const [state, patches] = produceWithPatches(old, fn) as readonly [(T & BaseState<T>), any, Patch[]]; const [state, patches] = produceWithPatches(old, fn) as readonly [(T & BaseState<T>), any, Patch[]];
console.log(patches); //console.log(patches);
set(state); set(state);
}; };