Skip to content

Commit 060d8d7

Browse files
authored
Merge pull request #43 from maastrichtlawtech/dev-cluster-toggle
Dev cluster toggle
2 parents fad83ff + d4a62c3 commit 060d8d7

File tree

3 files changed

+61
-10
lines changed

3 files changed

+61
-10
lines changed

components/CaseLawExplorer/cluster_graph.tsx

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Button, Typography, Divider} from '@mui/material'
1+
import {Button, FormControlLabel, Switch, FormGroup} from '@mui/material'
22
import React from 'react'
33
import {ControllerContext, FullGraphContext} from './Contexts'
44
import {Node, Edge, NetworkStats, Graph} from './types'
@@ -27,7 +27,6 @@ export function selectCluster(
2727
nodes: new_nodes,
2828
edges: new_edges
2929
}
30-
ClusterCache.set(activeCluster, result)
3130
return result
3231
}
3332

@@ -60,24 +59,22 @@ export function clusterGraph({networkStatistics, nodes, edges}: Graph): {nodes:
6059
nodes: Array.from(new_nodes).map(make_node),
6160
edges: Array.from(new_edges).map(make_edge)
6261
}
63-
ClusterCache.set(null, result)
6462
return result
6563
}
6664

6765
export function GraphClusterButton({itemId}: {itemId: any}) {
6866
const {fullGraph} = React.useContext(FullGraphContext)
6967
const {controller, activeCluster} = React.useContext(ControllerContext)
70-
const showing_clusters = activeCluster === null
68+
const showing_clusters = activeCluster === null || activeCluster === false
7169

72-
if (showing_clusters && !itemId) return null
70+
if ((showing_clusters && !itemId) || activeCluster === false) return null
7371

7472
return (
7573
<div>
7674
<Button
7775
onClick={() => {
7876
const zoomIn = showing_clusters && itemId
7977
const {nodes, edges} = zoomIn ? selectCluster(fullGraph, itemId) : clusterGraph(fullGraph)
80-
8178
controller.update((draft: any) => {
8279
draft.nodes = nodes
8380
draft.edges = edges
@@ -91,3 +88,34 @@ export function GraphClusterButton({itemId}: {itemId: any}) {
9188
</div>
9289
)
9390
}
91+
92+
export function ClusterToggleSwitch({itemId}: {itemId: any}) {
93+
const {fullGraph} = React.useContext(FullGraphContext)
94+
const {controller, activeCluster} = React.useContext(ControllerContext)
95+
const showing_clusters = activeCluster === null || activeCluster === false
96+
97+
if (!showing_clusters) return null
98+
return (
99+
<div>
100+
<FormGroup>
101+
<FormControlLabel
102+
control={
103+
<Switch
104+
defaultChecked
105+
onChange={(event: React.ChangeEvent<HTMLInputElement>) => {
106+
const showClusters = event.target.checked
107+
const {nodes, edges} = showClusters ? clusterGraph(fullGraph) : fullGraph
108+
controller.update((draft: any) => {
109+
draft.nodes = nodes
110+
draft.edges = edges
111+
draft.activeCluster = showClusters ? null : false
112+
})
113+
}}
114+
/>
115+
}
116+
label="Show clusters?"
117+
/>
118+
</FormGroup>
119+
</div>
120+
)
121+
}

components/CaseLawExplorer/components/DataBar/Header.tsx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {View} from 'colay-ui'
44
import React from 'react'
55
import {useUser} from '../../useUser'
66
import {useGraphEditor} from 'perfect-graph/hooks/useGraphEditor'
7-
import {selectCluster, GraphClusterButton} from '../../cluster_graph'
7+
import {selectCluster, GraphClusterButton, ClusterToggleSwitch} from '../../cluster_graph'
88
import {Graph} from '../../types'
99
import {ControllerContext, FullGraphContext} from '../../Contexts'
1010
import {Collapsible, CollapsibleContainer, CollapsibleTitle} from 'perfect-graph/components/Collapsible'
@@ -73,12 +73,27 @@ export const DataBarHeader = props => {
7373

7474
return (
7575
<View>
76-
<View style={{flexDirection: 'row', justifyContent: 'space-between'}}>
76+
<View
77+
style={{
78+
flexDirection: 'row',
79+
justifyContent: 'space-between',
80+
padding: 2
81+
}}
82+
>
7783
<Typography>{user?.attributes?.email}</Typography>
7884
<Button color="secondary" onClick={() => Auth.signOut()}>
7985
Signout
8086
</Button>
8187
</View>
88+
<View
89+
style={{
90+
justifyContent: 'space-between',
91+
padding: 2,
92+
alignItems: 'center'
93+
}}
94+
>
95+
<ClusterToggleSwitch itemId={selectedItemId} />
96+
</View>
8297
<Divider />
8398
<View
8499
style={{

components/CaseLawExplorer/index.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,16 @@ async function updateLayout(cluster, layout, graphEditor, nodes, edges, cy) {
142142
layoutName,
143143
boundingBox
144144
})
145-
clusterInfo.locations = layoutResult
146-
clusterInfo.lastLayout = JSON.stringify(layout)
145+
// Create a copy of nodes with the data part removed, as it is
146+
// unexpected by the layout calculator GraphQL query
147+
const make_node = ({id: number}) => ({id: number, data: {}})
148+
const result = {
149+
nodes: Array.from(nodes).map(make_node),
150+
edges: edges,
151+
locations: layoutResult,
152+
lastLayout: JSON.stringify(layout)
153+
}
154+
ClusterCache.set(cluster, result)
147155
}
148156

149157
console.log('layout res', layoutResult)

0 commit comments

Comments
 (0)