summaryrefslogtreecommitdiff
path: root/app_expo/screens/graph/graph-screen.tsx
blob: 8c35590f6b37aa4de900aceacec4ba4c9795d062 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import React, { useEffect, useState } from 'react'
import { observer } from 'mobx-react-lite'
import { TouchableOpacity, View, ViewStyle } from 'react-native'
import { Screen, Text } from '../../components'
// import { useNavigation } from "@react-navigation/native"
// import { useStores } from "../../models"
import { color } from '../../theme'

import { Graph } from '../../components'
import { Tweaks } from '../../components'

import genRandomTree from '../../data/randomdata'

import AsyncStorage from '@react-native-async-storage/async-storage'

import axios from 'axios'

import rando from '../../data/rando.json'

const ROOT: ViewStyle = {
  backgroundColor: color.palette.black,
  flex: 1,
}

import { LocalButton } from '../../components/'
import { GraphUi } from '../../components/graph-ui/graph-ui'
import { Switch } from 'react-native-elements'
export const GraphScreen = observer(function GraphScreen() {
  // Pull in one of our MST stores
  // const { someStore, anotherStore } = useStores()

  // Pull in navigation via hook
  // const navigation = useNavigation()

  const [physics, setPhysics] = useState({})
  const [graphData, setGraphData] = useState()
  const [nodeIds, setNodeIds] = useState([])
  //  { "nodes": [{ "id": 1 }, { "id": 2 }], "links": [{ "target": 1, "source": 2 }] });
  const physicsInit = {
    charge: -350,
    collision: true,
    linkStrength: 0.1,
    linkIts: 1,
    particles: 2,
    linkOpacity: 0.4,
    linkWidth: 1,
    particleWidth: 4,
    nodeRel: 4,
    labels: true,
    labelScale: 1.5,
    alphaDecay: 0.16,
    alphaTarget: 0,
    velocityDecay: 0.25,
    gravity: 0.5,
    gravityOn: true,
    hover: true,
    colorful: true,
    galaxy: true,
    rootId: 0,
  }

  const getData = async () => {
    try {
      const value: string = await AsyncStorage.getItem('@physics')
      if (value !== null) {
        const valueJson = JSON.parse(value)
        if (Object.keys(valueJson).length === Object.keys(physicsInit).length) {
          valueJson.local = false
          return valueJson
        } else {
          return physicsInit
        }
      } else {
        return physicsInit
      }
    } catch (e) {
      console.log(e)
    }
  }
  const storeData = async (value) => {
    try {
      let jsonVal = JSON.stringify(value)
      console.log(jsonVal + ' ' + value)
      await AsyncStorage.setItem('@physics', jsonVal)
    } catch (e) {
      console.log(e)
    }
  }

  // hook to save the current configuration of the physics tweaks
  // after it is updated
  useEffect(() => {
    if (timer) {
      clearTimeout(timer)
    }
    // set timer so the thing doesn't run every single slider tick
    const timer = setTimeout(() => {
      storeData(physics)
    }, 1000)
    return () => clearTimeout(timer)
  }, [physics])

  //"ComponentOnMount"
  // Get previous settings and the data from the org-roam-server
  const sanitizeGraph = (data) => {
    const cleanLinks = []
    data.links.forEach((link, j) => {
      let target
      let source
      for (let i = 0; i < data.nodes.length; i++) {
        let a = data.nodes[i]
        !a.neighbors && (a.neighbors = [])
        !a.links && (a.links = [])
        // the first time around,
        // index the node as not a part of the local graph
        !j && (a.local = false)
        if (link.target === data.nodes[i].id) {
          a.links.push(link)
          target = [a, i]
          link.target === link.source ? null : cleanLinks.push(link)
        } else if (link.source === data.nodes[i].id) {
          a.links.push(link)
          source = [a, i]
        }
      }
      if (target && source) {
        data.nodes[target[1]].neighbors.push(source[0])
        data.nodes[source[1]].neighbors.push(target[0])
        link.sourceIndex = source[1]
        link.targetIndex = target[1]
        link.index = [j]
        link.local = false
      }
    })
    data.links = cleanLinks
    return data
  }

  const [theme, setTheme] = useState({
    base1: '#1c1f24',
    base2: '#21272d',
    base3: '#23272e',
    base4: '#484854',
    base5: '#62686E',
    base6: '#757B80',
    base7: '#9ca0a4',
    base8: '#DFDFDF',
    bg: '#242730',
    'bg-alt': '#2a2e38',
    blue: '#51afef',
    cyan: '#5cEfFF',
    'dark-blue': '#1f5582',
    'dark-cyan': '#6A8FBF',
    fg: '#bbc2cf',
    'fg-alt': '#5D656B',
    green: '#7bc275',
    grey: '#484854',
    magenta: '#C57BDB',
    orange: '#e69055',
    red: '#ff665c',
    teal: '#4db5bd',
    violet: '#a991f1',
    yellow: '#FCCE7B',
  })

  useEffect(() => {
    getData().then((data) => setPhysics(data))
    axios
      .get('http://localhost:35901/theme')
      .then((theme) => {
        setTheme(theme.data)
        console.log(theme.data)
      })
      .catch((e) => {
        console.log('No theme found')
      })
    axios
      .get('http://localhost:35901/graph')
      .then((dataa) => {
        let cleanData = sanitizeGraph(dataa.data)
        console.log(cleanData)
        setGraphData(cleanData)
      })
      .catch((e) => {
        console.log(e)
        console.log("Couldn't get data.")
        //setGraphData(rando);
      })
  }, [])

  const [threeDim, setThreeDim] = useState(false)
  const [local, setLocal] = useState(false)

  if (!graphData) {
    return null
  } else {
    return (
      <Screen style={ROOT} preset="fixed">
        <View
          style={{
            display: 'flex',
            flexDirection: 'row',
            height: '100%',
            width: '100%',
            position: 'absolute',
            zIndex: 150,
          }}
        >
          <Tweaks physics={physics} setPhysics={setPhysics} />
          <View
            style={{
              marginLeft: 'auto',
              marginRight: '2%',
              marginTop: '2%',
              zIndex: 5,
              position: 'relative',
              height: '15%',
            }}
          >
            <LocalButton local={local} setLocal={setLocal} />
            <View
              style={{
                display: 'flex',
                flexDirection: 'row',
                alignItems: 'center',
                justifyContent: 'center',
                paddingTop: '2%',
              }}
            >
              <Text preset="header" text="3D" style={{ marginLeft: 10 }} />
              <Switch
                color="#a991f1"
                trackColor={{
                  false: '#62686E',
                  true: '#a991f1',
                }}
                style={{
                  height: 25,
                  marginVertical: 10,
                  marginLeft: 20,
                }}
                value={threeDim}
                onValueChange={() => {
                  setThreeDim(!threeDim)
                }}
              />
            </View>
          </View>
          <Graph
            style={{ position: 'absolute' }}
            setPhysics={setPhysics}
            physics={physics}
            gData={graphData}
            nodeIds={nodeIds}
            threeDim={threeDim}
            setThreeDim={setThreeDim}
            local={local}
            setLocal={setLocal}
          />
        </View>
      </Screen>
    )
  }
})