File size: 1,299 Bytes
3e05655
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { createComputed, on, type Accessor } from "solid-js"
import { createStore, type SetStoreFunction } from "solid-js/store"
import type { PromptHistoryEntry } from "./history"

export type PromptInputTransientState = {
  popover: "at" | "slash" | null
  slashMenu: boolean
  slashMenuQuery: string
  historyIndex: number
  savedPrompt: PromptHistoryEntry | null
  placeholder: number
  draggingType: "image" | "@mention" | null
  mode: "normal" | "shell"
  applyingHistory: boolean
}

function resetPromptInputTransientState(setStore: SetStoreFunction<PromptInputTransientState>) {
  setStore({
    popover: null,
    slashMenu: false,
    slashMenuQuery: "",
    historyIndex: -1,
    savedPrompt: null,
    draggingType: null,
    mode: "normal",
    applyingHistory: false,
  })
}

export function createPromptInputTransientState(identity: Accessor<unknown>, placeholder: number) {
  const [store, setStore] = createStore<PromptInputTransientState>({
    popover: null,
    slashMenu: false,
    slashMenuQuery: "",
    historyIndex: -1,
    savedPrompt: null,
    placeholder,
    draggingType: null,
    mode: "normal",
    applyingHistory: false,
  })

  createComputed(on(identity, () => resetPromptInputTransientState(setStore), { defer: true }))

  return [store, setStore] as const
}