-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathConnectionSetupModal.tsx
More file actions
188 lines (172 loc) · 5.98 KB
/
Copy pathConnectionSetupModal.tsx
File metadata and controls
188 lines (172 loc) · 5.98 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
185
186
187
188
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { Button } from './button';
import type { ConnectionField, ConnectionFieldValues } from '../types/ChiaGaming';
interface ConnectionSetupModalProps {
open: boolean;
title?: string;
description?: string;
fields?: Record<string, ConnectionField>;
onConnect: (values: ConnectionFieldValues) => void;
onCancel?: () => void;
connecting: boolean;
error?: string | null;
}
export function ConnectionSetupModal({
open,
title,
description,
fields,
onConnect,
onCancel,
connecting,
error,
}: ConnectionSetupModalProps) {
const panelRef = useRef<HTMLDivElement>(null);
const dragState = useRef<{ startX: number; startY: number; origX: number; origY: number } | null>(
null,
);
const offsetRef = useRef({ x: 0, y: 0 });
const fieldEntries = useMemo(() => (fields ? Object.entries(fields) : []), [fields]);
const [inputs, setInputs] = useState<Record<string, string>>({});
useEffect(() => {
if (!open) return;
const initial: Record<string, string> = {};
for (const [key, field] of Object.entries(fields ?? {})) {
initial[key] = field.type === 'bigint' ? field.default.toString() : field.default;
}
setInputs(initial);
offsetRef.current = { x: 0, y: 0 };
if (panelRef.current) panelRef.current.style.transform = 'translate(-50%, -50%)';
// Re-initialize whenever the modal opens or the field set changes.
}, [open, fields]);
const clampToContainer = useCallback((x: number, y: number) => {
const panel = panelRef.current;
if (!panel) return { x, y };
const container = panel.offsetParent as HTMLElement | null;
if (!container) return { x, y };
const pw = panel.offsetWidth;
const ph = panel.offsetHeight;
const cw = container.clientWidth;
const ch = container.clientHeight;
const minX = pw / 2 - cw / 2;
const maxX = cw / 2 - pw / 2;
const minY = ph / 2 - ch / 2;
const maxY = ch / 2 - ph / 2;
return {
x: minX < maxX ? Math.max(minX, Math.min(maxX, x)) : 0,
y: minY < maxY ? Math.max(minY, Math.min(maxY, y)) : 0,
};
}, []);
useEffect(() => {
const onMove = (e: MouseEvent) => {
if (!dragState.current || !panelRef.current) return;
e.preventDefault();
document.body.style.cursor = 'grabbing';
document.body.style.userSelect = 'none';
const rawX = dragState.current.origX + (e.clientX - dragState.current.startX);
const rawY = dragState.current.origY + (e.clientY - dragState.current.startY);
const { x, y } = clampToContainer(rawX, rawY);
offsetRef.current = { x, y };
panelRef.current.style.transform = `translate(calc(-50% + ${x}px), calc(-50% + ${y}px))`;
};
const onUp = () => {
dragState.current = null;
document.body.style.cursor = '';
document.body.style.userSelect = '';
};
document.addEventListener('mousemove', onMove);
document.addEventListener('mouseup', onUp);
return () => {
document.removeEventListener('mousemove', onMove);
document.removeEventListener('mouseup', onUp);
document.body.style.cursor = '';
document.body.style.userSelect = '';
};
}, [clampToContainer]);
const handleDragStart = useCallback((e: React.MouseEvent) => {
e.preventDefault();
dragState.current = {
startX: e.clientX,
startY: e.clientY,
origX: offsetRef.current.x,
origY: offsetRef.current.y,
};
}, []);
const handleConnect = useCallback(() => {
const values: ConnectionFieldValues = {};
for (const [key, field] of fieldEntries) {
const raw = inputs[key] ?? '';
if (field.type === 'bigint') {
try {
values[key] = BigInt(raw.trim() === '' ? '0' : raw.trim());
} catch {
values[key] = field.default;
}
} else {
values[key] = raw;
}
}
onConnect(values);
}, [fieldEntries, inputs, onConnect]);
if (!open) return null;
return (
<div
ref={panelRef}
style={{
position: 'absolute',
left: '50%',
top: '50%',
transform: 'translate(-50%, -50%)',
zIndex: 10,
width: '22rem',
maxWidth: 'calc(100% - 2rem)',
}}
className="border border-canvas-border bg-canvas-bg shadow-xl rounded-xl p-5 flex flex-col items-stretch gap-4"
>
<div
onMouseDown={handleDragStart}
style={{ cursor: 'grab' }}
className="select-none w-full text-center"
>
<h2 className="text-lg font-semibold text-canvas-text-contrast leading-tight">
{title ?? 'Connect'}
</h2>
{description ? <p className="text-sm text-canvas-text mt-0.5">{description}</p> : null}
</div>
{fieldEntries.length > 0 ? (
<div className="flex flex-col gap-3 w-full">
{fieldEntries.map(([key, field]) => (
<label key={key} className="flex flex-col gap-1 text-sm text-canvas-text">
<span>{field.label}</span>
<input
type="text"
inputMode={field.type === 'bigint' ? 'numeric' : 'text'}
value={inputs[key] ?? ''}
onChange={(e) => setInputs((prev) => ({ ...prev, [key]: e.target.value }))}
disabled={connecting}
className="px-3 py-2 rounded-md bg-canvas-bg-subtle text-canvas-text border border-canvas-border outline-none"
/>
</label>
))}
</div>
) : null}
{error ? <p className="text-sm text-alert-text break-words">{error}</p> : null}
<div className="flex items-center justify-center gap-2">
{onCancel ? (
<Button variant="outline" onClick={onCancel} disabled={connecting}>
Cancel
</Button>
) : null}
<Button
variant="solid"
onClick={handleConnect}
disabled={connecting}
isLoading={connecting}
loadingText="Connecting…"
>
Connect
</Button>
</div>
</div>
);
}