Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

856 B
Raw Blame
import { useCallback } from 'react'
import { navApi } from './api'
import { useLeaveGuard, useRoute } from './context'

/**
 * An editor page with unsaved input: while `dirty` is true, back asks
 * "Discard changes?". The returned function leaves the page WITHOUT that
 * question, for the moment after Save or Delete: it takes the guard of this
 * page away and goes back in the same step, so the page never asks about
 * changes that were just saved, and a page whose record was just deleted
 * does not get to draw itself once more.
 *
 *   const leave = useGuardedLeave(dirty)
 *   const save = () => { store.save(draft); leave() }
 */
export function useGuardedLeave(dirty: boolean): () => void {
  const { key } = useRoute()
  useLeaveGuard(dirty)
  return useCallback(() => {
    navApi.setLeaveGuard(key, null)
    void navApi.back()
  }, [key])
}