Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

1.7 KB
Raw Blame
import { useCallback, useEffect, useRef, useState } from 'react'
import {
  DEFAULT_SESSION_VIEW,
  loadDefaultSessionView,
  saveDefaultSessionView,
  type MobileSessionView
} from '../storage/session-view-preferences'

export type MobileDefaultSessionViewPreference = {
  defaultView: MobileSessionView
  setDefaultView: (view: MobileSessionView) => void
}

/** Owns the optimistic Settings value while keeping AsyncStorage writes ordered. */
export function useMobileDefaultSessionViewPreference(): MobileDefaultSessionViewPreference {
  const [defaultView, setDefaultViewState] = useState<MobileSessionView>(DEFAULT_SESSION_VIEW)
  const mountedRef = useRef(false)
  const mutationRevisionRef = useRef(0)

  useEffect(() => {
    mountedRef.current = true
    const loadRevision = mutationRevisionRef.current
    let stale = false
    void loadDefaultSessionView().then((view) => {
      // Why: a fast toggle is authoritative over the older storage read.
      if (!stale && mutationRevisionRef.current === loadRevision) {
        setDefaultViewState(view)
      }
    })
    return () => {
      stale = true
      mountedRef.current = false
    }
  }, [])

  const setDefaultView = useCallback((view: MobileSessionView) => {
    const revision = mutationRevisionRef.current + 1
    mutationRevisionRef.current = revision
    setDefaultViewState(view)
    // Enqueue persistence at event time so writes stay ordered across route unmount/remount.
    void saveDefaultSessionView(view).catch(async () => {
      const persisted = await loadDefaultSessionView()
      if (mountedRef.current && mutationRevisionRef.current === revision) {
        setDefaultViewState(persisted)
      }
    })
  }, [])

  return { defaultView, setDefaultView }
}