-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathDetailsDrawer.tsx
More file actions
184 lines (178 loc) · 4.66 KB
/
DetailsDrawer.tsx
File metadata and controls
184 lines (178 loc) · 4.66 KB
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
import {
Drawer,
DrawerContent,
DrawerDescription,
DrawerFooter,
DrawerHeader,
DrawerTitle,
} from '../ui/drawer'
import { Button } from '../ui/button'
import { useCallback, type PropsWithChildren } from 'react'
import {
applyNodeChanges,
useNodes,
useNodeId,
useReactFlow,
applyEdgeChanges,
useEdges,
} from 'reactflow'
import type { NodeBaseData } from './nodes/NodeBase'
import type { nodeTypes } from '@/lib/utils/generate-architecture-data'
export interface DetailsDrawerProps extends PropsWithChildren {
title: string
description?: string
open: boolean
testHref?: string
footerChildren?: React.ReactNode
// children that are rendered after the services reference
trailingChildren?: React.ReactNode
nodeType: keyof typeof nodeTypes
icon: NodeBaseData['icon']
address?: string
services?: string[]
type?: 'node' | 'edge'
edgeId?: string
}
export const DetailsDrawer = ({
title,
description,
children,
footerChildren,
trailingChildren,
open,
testHref,
icon: Icon,
nodeType,
address,
services,
type = 'node',
edgeId,
}: DetailsDrawerProps) => {
const nodeId = useNodeId()
const { setNodes, setEdges } = useReactFlow()
const nodes = useNodes()
const edges = useEdges()
const selectServiceNode = useCallback(
(serviceNodeId: string) => {
setNodes(
applyNodeChanges(
[
{
id: nodeId || '',
type: 'select',
selected: false,
},
{
id: serviceNodeId,
type: 'select',
selected: true,
},
],
nodes,
),
)
},
[nodes, nodeId],
)
const close = () => {
if (type === 'edge') {
setEdges(
applyEdgeChanges(
[
{
id: edgeId || '',
type: 'select',
selected: false,
},
],
edges,
),
)
return
}
setNodes(
applyNodeChanges(
[
{
id: nodeId || '',
type: 'select',
selected: false,
},
],
nodes,
),
)
}
return (
<Drawer modal={false} direction="right" open={open}>
<DrawerContent className="fixed inset-auto bottom-0 right-0 mt-24 flex h-full w-[380px] flex-col rounded-l-[10px] rounded-r-none bg-background border border-border">
<div className="mx-auto w-full max-w-sm p-4">
<DrawerHeader
className={`flex items-center react-flow__node-${nodeType}`}
>
<Icon className="resource-icon mr-2 size-8" />
<div>
<DrawerTitle>
<span className="flex items-center">{title}</span>
</DrawerTitle>
{description && (
<DrawerDescription>{description}</DrawerDescription>
)}
</div>
</DrawerHeader>
<div className="space-y-2 py-4">
{children}
{address && (
<div className="flex flex-col">
<span className="font-bold text-foreground">Address:</span>
{address.startsWith('http') ? (
<a
target="_blank"
className="hover:underline text-foreground"
href={address}
rel="noreferrer"
>
{address}
</a>
) : (
address
)}
</div>
)}
{services?.length ? (
<div className="flex flex-col">
<span className="font-bold text-foreground">Requested by:</span>
<div className="flex flex-col items-start gap-y-1">
{services.map((s) => (
<button
key={s}
type="button"
className="hover:underline text-foreground"
onClick={() => selectServiceNode(s)}
>
{s}
</button>
))}
</div>
</div>
) : null}
{trailingChildren}
</div>
<DrawerFooter className="px-0">
{footerChildren}
{testHref && (
<Button asChild>
<a href={testHref} target="_blank" rel="noreferrer">
Test
</a>
</Button>
)}
<Button onClick={close} variant="outline">
Close
</Button>
</DrawerFooter>
</div>
</DrawerContent>
</Drawer>
)
}