-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathTableNode.tsx
117 lines (101 loc) · 4.04 KB
/
TableNode.tsx
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
import { useState, FC, useEffect } from "react";
import { Handle, Position, NodeProps } from "reactflow";
import { KeyIcon } from "../components";
import { markdown } from "../helpers";
import { tableHighlightsPresent, isTableHighlighted, isColumnHighlighted } from "../helpers/tableHighlights";
import "@reactflow/node-resizer/dist/style.css";
export const TableNode: FC<NodeProps> = ({ data }) => {
const [selectedColumn, setSelectedColumn] = useState("");
const [showDescription, setshowDescription] = useState(false);
const [descriptionOnHoverActive, setDescriptionOnHoverActive] = useState(false);
useEffect(() => {
document.addEventListener("keydown", (e: KeyboardEvent) => {
if(e.code === "MetaLeft") {
setDescriptionOnHoverActive(true)
}
}, false);
document.addEventListener("keyup", (e: KeyboardEvent) => {
if(e.code === "MetaLeft") {
setDescriptionOnHoverActive(false)
}
}, false);
}, []);
const tableClass = ({ schema, tableName}: { schema: string, tableName: string }) => {
const classes = ["table"]
if (isTableHighlighted({ schema, tableName })) {
classes.push("table--highlighted")
} else if (tableHighlightsPresent()) {
classes.push("table--dimmed")
}
return classes.join(" ")
}
const columnClass = ({ selectedColumn, columnName }: { selectedColumn: string, columnName: string }) => {
const classes = ["column-name"]
if (selectedColumn === columnName) {
classes.push("column-name--selected")
}
if (isColumnHighlighted({ schema: data.schema, tableName: data.name, columnName })) {
classes.push("column-name--highlighted")
} else if (tableHighlightsPresent()) {
classes.push("column-name--dimmed")
}
return classes.join(" ")
}
return (
<div
className={tableClass({ schema: data.schema, tableName: data.name })}>
<div
style={isTableHighlighted({ schema: data.schema, tableName: data.name }) ? {} : { backgroundColor: data.schemaColor }}
className="table__name"
onMouseEnter={() => {
if(descriptionOnHoverActive) {
setshowDescription(true)
}
}}
onMouseLeave={() => setshowDescription(false)}>
{data.schema ? `${data.schema}.${data.name}` : data.name}
<div
className={showDescription ? "table__description table__description--active" : "table__description"}
dangerouslySetInnerHTML={{__html: markdown(data.description || "No description.") }} />
</div>
<div className="table__columns">
{data.columns.map((column: any, index: any) => (
<div
key={index}
className={columnClass({ selectedColumn, columnName: column.name })}
onMouseEnter={() => {
if(descriptionOnHoverActive) {
setSelectedColumn(column.name)
}
}}
onMouseLeave={() => setSelectedColumn("")}>
{column.handleType && <Handle
type={column.handleType}
position={Position.Right}
id={`${column.name}-right`}
className={column.handleType === "source" ? "right-handle source-handle" : "right-handle target-handle"}
/>}
{column.handleType && <Handle
type={column.handleType}
position={Position.Left}
id={`${column.name}-left`}
className={column.handleType === "source" ? "left-handle source-handle" : "left-handle target-handle"}
/>}
<div className="column-name__inner">
<div className="column-name__name">
{column.key && <KeyIcon />}
{column.name}
</div>
<div className="column-name__type">
{column.type}
</div>
<div
className="column-name__description"
dangerouslySetInnerHTML={{__html: markdown(column.description || "No description.") }} />
</div>
</div>
))}
</div>
</div>
);
};