Skip to content

Commit af089f7

Browse files
authored
feat(graph): cid resolving (#1204)
2 parents 7add72d + ec5be0a commit af089f7

File tree

8 files changed

+163
-46
lines changed

8 files changed

+163
-46
lines changed

src/components/ContentItem/contentItem.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ import { getRankGrade } from '../../utils/search/utils';
1313
import ContentIpfs from '../contentIpfs/contentIpfs';
1414

1515
type ContentItemProps = {
16-
item: $TsFixMe;
16+
item?: $TsFixMe;
1717
cid: string;
1818
grade?: $TsFixMe;
1919
className?: string;
2020
parent?: string;
21-
linkType: LinksType;
21+
linkType?: LinksType;
2222
setType?: (type: IpfsContentType) => void;
2323
};
2424

@@ -35,7 +35,7 @@ function ContentItem({
3535

3636
useEffect(() => {
3737
details?.type && setType && setType(details?.type);
38-
}, [details]); //TODO: REFACT - setType rise infinite loop
38+
}, [details]); // TODO: REFACT - setType rise infinite loop
3939

4040
if (hidden) {
4141
return <div />;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// https://www.sketch.com/s/b13841a7-cfd2-47e8-a114-efb8e29285af/a/JAOznjr
2+
3+
@mixin glass {
4+
backdrop-filter: blur(15px);
5+
box-shadow: 0px 0px 10px rgba(75, 0, 130, 0.09);
6+
background-color: rgba(75, 0, 130, 0.09);
7+
}
8+
9+
@mixin glassBackground {
10+
position: relative;
11+
12+
&:before {
13+
content: '';
14+
position: absolute;
15+
top: 0;
16+
left: 0;
17+
right: 0;
18+
bottom: 0;
19+
z-index: -1;
20+
21+
@include glass;
22+
}
23+
}

src/features/cyberlinks/CyberlinksGraph/CyberlinksGraph.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { useEffect, useState, useRef, useCallback } from 'react';
22
import { ForceGraph3D } from 'react-force-graph';
3+
import GraphHoverInfo from './GraphHoverInfo/GraphHoverInfo';
4+
35
import styles from './CyberlinksGraph.module.scss';
46

57
type Props = {
@@ -17,6 +19,7 @@ const CAMERA_ZOOM_IN_EFFECT_DELAY = 500;
1719
function CyberlinksGraph({ data, size }: Props) {
1820
const [isRendering, setRendering] = useState(true);
1921
const [touched, setTouched] = useState(false);
22+
const [hoverNode, setHoverNode] = useState(null);
2023

2124
const fgRef = useRef();
2225

@@ -157,6 +160,7 @@ function CyberlinksGraph({ data, size }: Props) {
157160
<div
158161
style={{
159162
minHeight: size,
163+
position: 'relative',
160164
}}
161165
>
162166
{isRendering && (
@@ -175,18 +179,19 @@ function CyberlinksGraph({ data, size }: Props) {
175179
enableNodeDrag={false}
176180
enablePointerInteraction
177181
enableNavigationControls
178-
nodeLabel="id"
182+
// nodeLabel="id"
179183
nodeColor={() => 'rgba(0,100,235,1)'}
180184
nodeOpacity={1.0}
181185
nodeRelSize={8}
186+
onNodeHover={setHoverNode}
182187
linkColor={
183188
// not working
184189
(link) =>
185190
// link.subject && link.subject === currentAddress
186191
// ? 'red'
187192
'rgba(9,255,13,1)'
188193
}
189-
linkLabel={''}
194+
linkLabel=""
190195
linkWidth={4}
191196
linkCurvature={0.2}
192197
linkOpacity={0.7}
@@ -204,6 +209,12 @@ function CyberlinksGraph({ data, size }: Props) {
204209
onLinkRightClick={handleLinkClick}
205210
onEngineStop={handleEngineStop}
206211
/>
212+
213+
<GraphHoverInfo
214+
node={hoverNode}
215+
camera={fgRef.current?.camera()}
216+
size={size || window.innerWidth}
217+
/>
207218
</div>
208219
);
209220
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
@import '../../../../components/atoms/glass/mixins';
2+
3+
.hoverInfo {
4+
@include glassBackground;
5+
6+
position: absolute;
7+
z-index: 0;
8+
max-width: 480px;
9+
border-radius: 5px;
10+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import * as THREE from 'three';
2+
import ContentItem from 'src/components/ContentItem/contentItem';
3+
import { useMemo } from 'react';
4+
import styles from './GraphHoverInfo.module.scss';
5+
6+
// fix
7+
type Props = {
8+
node: any;
9+
camera: any;
10+
size: number;
11+
};
12+
13+
function HoverInfo({ node, camera, size }: Props) {
14+
const calc = useMemo(() => {
15+
if (!node || !camera) {
16+
return null;
17+
}
18+
19+
const { x, y, z } = node;
20+
const vector = new THREE.Vector3(x, y, z);
21+
vector.project(camera);
22+
23+
const widthHalf = window.innerWidth / 2;
24+
const heightHalf = window.innerHeight / 2;
25+
26+
const posX = vector.x * widthHalf + widthHalf;
27+
const posY = -(vector.y * heightHalf) + heightHalf;
28+
29+
return {
30+
posX,
31+
posY,
32+
};
33+
}, [camera, node]);
34+
35+
if (!calc) {
36+
return null;
37+
}
38+
39+
const { posX, posY } = calc;
40+
41+
const isCid = node.id.startsWith('Qm');
42+
43+
if (!isCid) {
44+
// debug, delete
45+
debugger;
46+
}
47+
48+
return (
49+
<div
50+
className={styles.hoverInfo}
51+
style={{
52+
top: posY,
53+
left: posX,
54+
}}
55+
>
56+
<ContentItem cid={node.id} />
57+
</div>
58+
);
59+
}
60+
61+
export default HoverInfo;

src/features/cyberlinks/CyberlinksGraph/useCyberlinks.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,20 +86,18 @@ function useCyberlinks(
8686
const to = cyberlinks.map((a) => a.to);
8787

8888
const object = Array.from(new Set(from.concat(to))).map((value) => ({
89-
id: valueByKeyOrSelf(value, particlesPreview),
89+
// id: valueByKeyOrSelf(value, particlesPreview),
90+
id: value,
9091
}));
9192

92-
// console.log(
93-
// '---obj',
94-
// object.filter((o) => o.id.indexOf('Qm') === 0).map((o) => o.id)
95-
// );
96-
9793
const links = [];
9894

9995
for (let i = 0; i < cyberlinks.length; i++) {
10096
links[i] = {
101-
source: valueByKeyOrSelf(cyberlinks[i].from, particlesPreview),
102-
target: valueByKeyOrSelf(cyberlinks[i].to, particlesPreview),
97+
// source: valueByKeyOrSelf(cyberlinks[i].from, particlesPreview),
98+
// target: valueByKeyOrSelf(cyberlinks[i].to, particlesPreview),
99+
source: cyberlinks[i].from,
100+
target: cyberlinks[i].to,
103101
name: cyberlinks[i].transaction_hash,
104102
subject: cyberlinks[i].neuron,
105103
// curvative: getRandomInt(20, 500) / 1000,
@@ -110,7 +108,7 @@ function useCyberlinks(
110108
nodes: object,
111109
links,
112110
};
113-
}, [cyberlinks, isLoading, particlesPreview]);
111+
}, [cyberlinks]);
114112

115113
return {
116114
data,

src/pages/robot/Brain/Brain.tsx

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { useEffect, useState } from 'react';
2-
import { useAdviser } from 'src/features/adviser/context';
31
import { Tabs } from 'src/components';
2+
import { Route, Routes, useParams } from 'react-router-dom';
3+
import { useMemo } from 'react';
4+
import useAdviserTexts from 'src/features/adviser/useAdviserTexts';
45
import { useRobotContext } from '../robot.context';
56
import TreedView from './ui/TreedView';
67
import styles from './Brain.module.scss';
7-
import { LIMIT_GRAPH } from './utils';
88
import GraphView from './ui/GraphView';
9+
import { LIMIT_GRAPH } from './utils';
910

1011
enum TabsKey {
1112
list = 'list',
@@ -14,41 +15,54 @@ enum TabsKey {
1415

1516
function Brain() {
1617
const { address } = useRobotContext();
17-
const [selected, setSelected] = useState(TabsKey.list);
18-
const { setAdviser } = useAdviser();
19-
20-
useEffect(() => {
21-
setAdviser(
22-
<>
23-
neurons public knowledge cybergraph <br />
24-
{selected === TabsKey.graph && (
25-
<> that is how last {LIMIT_GRAPH} cyberlinks looks like </>
26-
)}
27-
</>
28-
);
29-
}, [setAdviser, selected]);
30-
31-
let content;
32-
33-
if (selected === TabsKey.list) {
34-
content = <TreedView address={address} />;
35-
}
36-
37-
if (selected === TabsKey.graph) {
38-
content = <GraphView address={address} />;
39-
}
18+
19+
const params = useParams();
20+
21+
const selected = params['*'] || TabsKey.graph;
22+
23+
useAdviserTexts({
24+
defaultText: useMemo(
25+
() => (
26+
<>
27+
neurons public knowledge cybergraph <br />
28+
{selected === TabsKey.graph && (
29+
<> that is how last {LIMIT_GRAPH} cyberlinks looks like </>
30+
)}
31+
</>
32+
),
33+
[selected]
34+
),
35+
});
4036

4137
return (
4238
<div className={styles.wrapper}>
4339
<div className={styles.tabs}>
4440
<Tabs
45-
options={[TabsKey.list, TabsKey.graph].map((item) => {
46-
return { key: item, onClick: () => setSelected(item) };
47-
})}
41+
options={[
42+
{
43+
key: TabsKey.graph,
44+
to: './graph',
45+
},
46+
{
47+
key: TabsKey.list,
48+
to: './list',
49+
},
50+
]}
4851
selected={selected}
4952
/>
5053
</div>
51-
{content}
54+
55+
<Routes>
56+
{['/', 'graph'].map((path) => (
57+
<Route
58+
key={path}
59+
path={path}
60+
element={<GraphView address={address} />}
61+
/>
62+
))}
63+
64+
<Route path="list" element={<TreedView address={address} />} />
65+
</Routes>
5266
</div>
5367
);
5468
}

src/pages/robot/Robot.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import TxsTable from 'src/pages/robot/_refactor/account/component/txsTable';
33
import Sigma from 'src/containers/sigma';
44
import RoutedEnergy from 'src/containers/energy';
55
import TableDiscipline from 'src/containers/gol/table';
6+
import useAdviserTexts from 'src/features/adviser/useAdviserTexts';
67
import Layout from './Layout/Layout';
78
import RobotContextProvider, { useRobotContext } from './robot.context';
89
import Brain from './Brain/Brain';
@@ -15,7 +16,6 @@ import Heroes from './_refactor/account/tabs/heroes';
1516
import Karma from './Karma/Karma';
1617
import Follows from './_refactor/account/tabs/Follows/Follows';
1718
import Soul from './Soul/Soul';
18-
import useAdviserTexts from 'src/features/adviser/useAdviserTexts';
1919

2020
function RobotRoutes() {
2121
const { isLoading, address } = useRobotContext();
@@ -54,7 +54,7 @@ function RobotRoutes() {
5454
<Route key={path} path={path} element={<SensePage />} />
5555
))}
5656

57-
<Route path="brain" element={<Brain />} />
57+
<Route path="brain/*" element={<Brain />} />
5858

5959
<Route path="*" element={<p>Page should not exist</p>} />
6060
</Route>

0 commit comments

Comments
 (0)