|
1 | 1 | import { memo, useCallback, useEffect, useState } from 'react'; |
| 2 | +import { ClickhouseClient } from '@hyperdx/common-utils/dist/clickhouse/browser'; |
2 | 3 | import { |
3 | 4 | MetricsDataType, |
4 | 5 | MetricTable, |
@@ -245,6 +246,47 @@ function OnboardingModalComponent({ |
245 | 246 | const [hasAutodetected, setHasAutodetected] = useState(false); |
246 | 247 | const [autoDetectedSources, setAutoDetectedSources] = useState<TSource[]>([]); |
247 | 248 |
|
| 249 | + // Auto-connect for clickstack build: test default credentials against origin |
| 250 | + const [hasAttemptedAutoConnect, setHasAttemptedAutoConnect] = useState(false); |
| 251 | + useEffect(() => { |
| 252 | + if (!IS_CLICKHOUSE_BUILD) return; |
| 253 | + if (step !== 'connection') return; |
| 254 | + if (connections?.length !== 0) return; |
| 255 | + if (hasAttemptedAutoConnect) return; |
| 256 | + // lets try to auto connect to the origin clickhouse server |
| 257 | + setHasAttemptedAutoConnect(true); |
| 258 | + const host = window.location.origin; |
| 259 | + const client = new ClickhouseClient({ |
| 260 | + host, |
| 261 | + username: 'default', |
| 262 | + password: '', |
| 263 | + }); |
| 264 | + client |
| 265 | + .query({ query: 'SELECT 1', shouldSkipApplySettings: true }) |
| 266 | + .then(result => { |
| 267 | + result.json().then(() => { |
| 268 | + createConnectionMutation.mutate( |
| 269 | + { |
| 270 | + connection: { |
| 271 | + name: 'Default', |
| 272 | + host, |
| 273 | + username: 'default', |
| 274 | + password: '', |
| 275 | + }, |
| 276 | + }, |
| 277 | + { |
| 278 | + onSuccess: () => { |
| 279 | + setStep('auto-detect'); |
| 280 | + }, |
| 281 | + }, |
| 282 | + ); |
| 283 | + }); |
| 284 | + }) |
| 285 | + .catch(() => { |
| 286 | + // Auto-connect failed, user will use the form manually |
| 287 | + }); |
| 288 | + }, [step, connections, hasAttemptedAutoConnect, createConnectionMutation]); |
| 289 | + |
248 | 290 | const handleAutoDetectSources = useCallback( |
249 | 291 | async (connectionId: string) => { |
250 | 292 | try { |
@@ -693,7 +735,9 @@ function OnboardingModalComponent({ |
693 | 735 | connection={{ |
694 | 736 | id: '', |
695 | 737 | name: 'Default', |
696 | | - host: 'http://localhost:8123', |
| 738 | + host: IS_CLICKHOUSE_BUILD |
| 739 | + ? window.location.origin |
| 740 | + : 'http://localhost:8123', |
697 | 741 | username: 'default', |
698 | 742 | password: '', |
699 | 743 | }} |
|
0 commit comments