tweak: Extracted some buildRow functionality to its own function

Note: More difficult than it sounds given the constraint
that I want to preserve functionality.
This commit is contained in:
NunoSempere 2021-12-08 11:30:13 +01:00
parent f82043d24a
commit f134df4b0a

View File

@ -16,7 +16,7 @@ async function findPathsInner({
let paths = [] let paths = []
let minPos = Math.min(sourceElementPosition, targetElementPosition) let minPos = Math.min(sourceElementPosition, targetElementPosition)
let maxPos = Math.max(sourceElementPosition, targetElementPosition) let maxPos = Math.max(sourceElementPosition, targetElementPosition)
let linksInner = links.filter(link => let linksInner = links.filter(link =>
(minPos <= link.sourceElementPosition && link.sourceElementPosition <= maxPos) && (minPos <= link.sourceElementPosition && link.sourceElementPosition <= maxPos) &&
(minPos <= link.targetElementPosition && link.targetElementPosition <= maxPos) (minPos <= link.targetElementPosition && link.targetElementPosition <= maxPos)
) )
@ -29,21 +29,22 @@ async function findPathsInner({
) { ) {
paths.push(pathSoFar.concat(link).flat()) paths.push(pathSoFar.concat(link).flat())
} else if ((link.source == sourceElementId)) { } else if ((link.source == sourceElementId)) {
let newPaths = await findPathsInner({ let newPaths = await findPathsInner({
sourceElementId: link.target, sourceElementPosition: link.sourceElementPosition, sourceElementId: link.target, sourceElementPosition: link.sourceElementPosition,
targetElementId, targetElementPosition, targetElementId, targetElementPosition,
pathSoFar: pathSoFar.concat(link).flat(), pathSoFar: pathSoFar.concat(link).flat(),
links: linksInner, nodes, maxLengthOfPath: (maxLengthOfPath - 1) links: linksInner, nodes, maxLengthOfPath: (maxLengthOfPath - 1)
}) })
if (newPaths.length != 0) { if (newPaths.length != 0) {
paths.push(...newPaths) paths.push(...newPaths)
} }
} else if ((link.target == sourceElementId)) { } else if ((link.target == sourceElementId)) {
let newPaths = await findPathsInner({ let newPaths = await findPathsInner({
sourceElementId: link.source, sourceElementPosition: link.sourceElementPosition, sourceElementId: link.source, sourceElementPosition: link.sourceElementPosition,
targetElementId, targetElementPosition, targetElementId, targetElementPosition,
pathSoFar: pathSoFar.concat(link).flat(), pathSoFar: pathSoFar.concat(link).flat(),
links: linksInner, nodes, maxLengthOfPath: (maxLengthOfPath - 1) }) links: linksInner, nodes, maxLengthOfPath: (maxLengthOfPath - 1)
})
if (newPaths.length != 0) { if (newPaths.length != 0) {
paths.push(...newPaths) paths.push(...newPaths)
} }
@ -155,42 +156,46 @@ function abridgeArrayAndDisplay(array) {
return result return result
} }
export async function buildRows(isListOrdered, orderedList, listOfElements, links){ export async function buildRows({ isListOrdered, orderedList, listOfElements, links, rows, setTableRows }) {
// Not used yet. // Not used yet.
let rows = [] if (isListOrdered && !(orderedList.length < listOfElements.length) && rows.length == 0) {
if (isListOrdered && ! (orderedList.length < listOfElements.length) && rows.length == 0) {
let nodes = [] let nodes = []
let positionDictionary=({}) let positionDictionary = ({})
orderedList.forEach((id, pos) => { orderedList.forEach((id, pos) => {
nodes.push({ ...listOfElements[id], position: pos }) nodes.push({ ...listOfElements[id], position: pos })
positionDictionary[id] = pos positionDictionary[id] = pos
}) })
// let nodes = orderedList.map((id, pos) => ({ ...listOfElements[id], position: pos })) // let nodes = orderedList.map((id, pos) => ({ ...listOfElements[id], position: pos }))
/* Pre-process links to talk in terms of distances */ /* Pre-process links to talk in terms of distances */
links = links.map(link => ({...link, links = links.map(link => ({
sourceElementPosition: positionDictionary[link.source], ...link,
sourceElementPosition: positionDictionary[link.source],
targetElementPosition: positionDictionary[link.target] targetElementPosition: positionDictionary[link.target]
})) }))
let distances = await findDistancesForAllElements({ nodes, links }) let distances = await findDistancesForAllElements({ nodes, links })
rows = nodes.map((element, i) => ({ rows = nodes.map((element, i) => ({
id: numToAlphabeticalString(element.position), id: numToAlphabeticalString(element.position),
position: element.position, position: element.position,
name: element.name, name: element.name,
distances: distances[i] distances: distances[i]
})) }))
console.log("rows@CreateTableWithDistances") console.log("rows@CreateTableWithDistances")
console.log(rows) console.log(rows)
}else{ setTableRows(rows)
rows = [] } else {
// rows = []
// Do nothing
} }
return rows // return rows
} }
export function CreateTableWithDistances({ isListOrdered, orderedList, listOfElements, links }) { export function CreateTableWithDistances({ isListOrdered, orderedList, listOfElements, links }) {
const [rows, setRows] = useState([]) const [tableRows, setTableRows] = useState([])
useEffect(async () => { useEffect(async () => {
await buildRows({ isListOrdered, orderedList, listOfElements, links, rows: tableRows, setTableRows })
/*
// https://stackoverflow.com/questions/57847626/using-async-await-inside-a-react-functional-component // https://stackoverflow.com/questions/57847626/using-async-await-inside-a-react-functional-component
// https://stackoverflow.com/questions/54936559/using-async-await-in-react-component // https://stackoverflow.com/questions/54936559/using-async-await-in-react-component
// https://reactjs.org/docs/hooks-effect.html#tip-optimizing-performance-by-skipping-effects // https://reactjs.org/docs/hooks-effect.html#tip-optimizing-performance-by-skipping-effects
@ -202,7 +207,7 @@ export function CreateTableWithDistances({ isListOrdered, orderedList, listOfEle
positionDictionary[id] = pos positionDictionary[id] = pos
}) })
// let nodes = orderedList.map((id, pos) => ({ ...listOfElements[id], position: pos })) // let nodes = orderedList.map((id, pos) => ({ ...listOfElements[id], position: pos }))
/* Pre-process links to talk in terms of distances */ // Pre-process links to talk in terms of distances
links = links.map(link => ({...link, links = links.map(link => ({...link,
sourceElementPosition: positionDictionary[link.source], sourceElementPosition: positionDictionary[link.source],
targetElementPosition: positionDictionary[link.target] targetElementPosition: positionDictionary[link.target]
@ -218,6 +223,7 @@ export function CreateTableWithDistances({ isListOrdered, orderedList, listOfEle
console.log("rows@CreateTableWithDistances") console.log("rows@CreateTableWithDistances")
console.log(rows) console.log(rows)
} }
*/
}); // this useEffect should ideally only work when isListOrdered changes, but I haven't bothered to program that. }); // this useEffect should ideally only work when isListOrdered changes, but I haven't bothered to program that.
return ( return (
@ -237,7 +243,7 @@ export function CreateTableWithDistances({ isListOrdered, orderedList, listOfEle
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{rows.map(row => <tr key={row.id}> {tableRows.map(row => <tr key={row.id}>
<td className="" >{row.id}</td> <td className="" >{row.id}</td>
<td>&nbsp;&nbsp;&nbsp;</td> <td>&nbsp;&nbsp;&nbsp;</td>
<td className="" >{row.position}</td> <td className="" >{row.position}</td>