Skip to content

The agent that grows with you

2.8 KB
Raw Blame
import type { DesktopProfileRoute } from '@/global'

import { browserStartupProfileKey, readBrowserStartupProfile, requireProfileRoute } from './browser-startup-profile'

interface BrowserProfileOptions {
  basePath: string
  currentProfile: () => string | null
  openWindow: (target: URL) => Promise<{ ok: true }>
}

// A tab without `?profile=` boots on this origin's saved default, so a named
// profile — Default included — is always written out; only null clears it.
function setUrlProfile(url: URL, profile: string | null): void {
  if (profile) {
    url.searchParams.set('profile', profile)
  } else {
    url.searchParams.delete('profile')
  }
}

export function createBrowserProfileBridge({
  basePath,
  currentProfile,
  openWindow
}: BrowserProfileOptions): Pick<Window['hermesDesktop'], 'openWindow' | 'profile'> {
  // localStorage supplies origin isolation; the base path separates colocated servers.
  const storageKey = browserStartupProfileKey(basePath)
  const listeners = new Set<(route: DesktopProfileRoute | null) => void>()

  const readDefault = (): DesktopProfileRoute | null => readBrowserStartupProfile(basePath)

  const remember = async (profile: string | null) => {
    const selected = profile?.trim() || null
    const url = new URL(window.location.href)
    setUrlProfile(url, selected)
    // Keep react-router's entry state (idx/key/usr): only the URL changes.
    window.history.replaceState(window.history.state, '', `${url.pathname}${url.search}${url.hash}`)

    return { profile: selected }
  }

  return {
    openWindow: async options => {
      const url = new URL(window.location.href)
      setUrlProfile(url, options === undefined ? currentProfile() : requireProfileRoute(options).profile)

      url.searchParams.delete('win')
      url.searchParams.delete('watch')
      url.hash = '/'

      return openWindow(url)
    },
    profile: {
      get: async () => ({ profile: currentProfile() }),
      getDefault: async () => readDefault(),
      setDefault: async value => {
        const route = requireProfileRoute(value)
        window.localStorage.setItem(storageKey, JSON.stringify(route))
        listeners.forEach(listener => listener(route))

        return route
      },
      onDefaultChanged: callback => {
        listeners.add(callback)

        const onStorage = (event: StorageEvent) => {
          if (event.storageArea === window.localStorage && (event.key === storageKey || event.key === null)) {
            callback(readDefault())
          }
        }

        window.addEventListener('storage', onStorage)

        return () => {
          listeners.delete(callback)
          window.removeEventListener('storage', onStorage)
        }
      },
      remember,
      set: async profile => {
        const result = await remember(profile)
        window.location.reload()

        return result
      }
    }
  }
}