@@ -3,7 +3,7 @@ import { useEffect, useRef, useState } from "react";
33import { lock , getLockInfo } from "../local/lockfile" ;
44import { join } from "node:path" ;
55import chalk from "chalk" ;
6- import { getDevhookID } from "../cli/lib/devhook " ;
6+ import { getHost } from "../cli/lib/auth " ;
77import type { Logger } from "./use-logger" ;
88
99export interface UseDevhookOptions {
@@ -25,6 +25,36 @@ export default function useDevhook(options: UseDevhookOptions) {
2525 const [ status , setStatus ] = useState < "connected" | "disconnected" | "error" > (
2626 "disconnected"
2727 ) ;
28+ const [ publicUrl , setPublicUrl ] = useState < string | undefined > ( undefined ) ;
29+
30+ useEffect ( ( ) => {
31+ if ( ! options . id ) {
32+ setPublicUrl ( undefined ) ;
33+ return ;
34+ }
35+ const host = getHost ( ) ;
36+ if ( ! host ) {
37+ // Skip URL lookup if not logged in
38+ setPublicUrl ( undefined ) ;
39+ return ;
40+ }
41+ let cancelled = false ;
42+ setPublicUrl ( undefined ) ;
43+ const client = new Client ( { baseURL : host } ) ;
44+ void client . devhook
45+ . getUrl ( options . id )
46+ . then ( ( url ) => {
47+ if ( ! cancelled ) {
48+ setPublicUrl ( url ) ;
49+ }
50+ } )
51+ . catch ( ( ) => {
52+ // Ignore lookup errors; listener will retry on connect.
53+ } ) ;
54+ return ( ) => {
55+ cancelled = true ;
56+ } ;
57+ } , [ options . id ] ) ;
2858
2959 useEffect ( ( ) => {
3060 // Don't connect if disabled or no devhook ID exists
@@ -95,6 +125,16 @@ export default function useDevhook(options: UseDevhookOptions) {
95125 ) ;
96126 }
97127
128+ // Check if user is logged in before connecting
129+ const host = getHost ( ) ;
130+ if ( ! host ) {
131+ options . logger . log (
132+ "system" ,
133+ `Run ${ chalk . bold ( "blink login" ) } to send webhooks to your agent from anywhere`
134+ ) ;
135+ return ;
136+ }
137+
98138 // Lock acquired, now connect
99139 const connect = ( ) => {
100140 if ( disposed || isConnecting ) return ;
@@ -112,19 +152,19 @@ export default function useDevhook(options: UseDevhookOptions) {
112152 }
113153
114154 // No authentication needed for devhooks.
115- const client = new Client ( {
116- // TODO: This shouldn't be hardcoded but our @blink.so/api package
117- // currently uses `BLINK_API_URL` which this does too 🤦🤦🤦.
118- baseURL : "https://blink.coder.com" ,
119- } ) ;
155+ const client = new Client ( { baseURL : host } ) ;
120156 currentListener = client . devhook . listen ( {
121157 id : options . id ! ,
122158 onRequest : async ( request ) => {
123159 return onRequestRef . current ( request ) ;
124160 } ,
125161 onConnect : ( ) => {
126- isConnecting = false ;
127- setStatus ( "connected" ) ;
162+ void ( async ( ) => {
163+ const url = await client . devhook . getUrl ( options . id ! ) ;
164+ isConnecting = false ;
165+ setStatus ( "connected" ) ;
166+ setPublicUrl ( url ) ;
167+ } ) ( ) ;
128168 } ,
129169 onDisconnect : ( ) => {
130170 isConnecting = false ;
@@ -181,11 +221,11 @@ export default function useDevhook(options: UseDevhookOptions) {
181221 }
182222 }
183223 } ;
184- } , [ options . disabled , options . directory ] ) ;
224+ } , [ options . disabled , options . directory , options . id ] ) ;
185225
186226 return {
187227 id : options . id ,
188- url : options . id ? `https:// ${ options . id } .blink.host` : undefined ,
228+ url : publicUrl ,
189229 status,
190230 } ;
191231}
0 commit comments