Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

1.8 KB
Raw Blame
import { requireOptionalNativeModule } from 'expo-modules-core'

export interface WidgetCredentials {
    host: string
    projectId: number
    projectName: string
    accessToken: string
    /** Epoch ms, or null when the token does not expire. */
    expiresAt: number | null
    userUuid: string
}

export interface WidgetState {
    installed: boolean
    pinIds: string[]
    usesDefault: boolean
}

interface ChartWidgetNativeModule {
    getWidgetState(): Promise<WidgetState>
    setCredentials(json: string): void
    clearCredentials(): void
    setCatalog(json: string): void
    clearCatalog(): void
    reloadWidgets(): void
}

const native = requireOptionalNativeModule<ChartWidgetNativeModule>('ChartWidget')

/** False on web and in any build without the native module, so callers can skip the sync. */
export function isAvailable(): boolean {
    return native !== null
}

/** Stores a read-only copy of the session for the widget's own reads, then refreshes every placed widget. */
export function setCredentials(credentials: WidgetCredentials): void {
    native?.setCredentials(JSON.stringify({ version: 1, ...credentials }))
}

/** Removes the stored session and every cached snapshot; widgets then ask to sign in. */
export function clearCredentials(): void {
    native?.clearCredentials()
}

/** Replaces the pin catalog the OS pickers list, then refreshes every placed widget. */
export function setCatalog(json: string): void {
    native?.setCatalog(json)
}

export function clearCatalog(): void {
    native?.clearCatalog()
}

export function reloadWidgets(): void {
    native?.reloadWidgets()
}

export async function getWidgetState(): Promise<WidgetState> {
    return (await native?.getWidgetState()) ?? { installed: false, pinIds: [], usesDefault: false }
}