Repository navigation
Expand file tree
/
Copy pathchatInputSlice.ts
More file actions
44 lines (40 loc) · 1.5 KB
/
Copy pathchatInputSlice.ts
File metadata and controls
44 lines (40 loc) · 1.5 KB
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
import { createSlice, PayloadAction } from '@reduxjs/toolkit'
// Cross-component channel for the chat input, plus persisted draft text.
//
// `pendingPrefill` is a one-shot payload that Chat consumes via useEffect and
// clears immediately. Used by the Playbook modal (and any future feature
// that needs to drop text into the composer from elsewhere in the app).
//
// `drafts` persists each conversation's composer text, keyed by
// `livingUIId ?? 'main'` (Chat.tsx is shared between the main Chat page and
// every Living UI project's chat panel), so it survives the route unmount
// that happens when navigating to another tab (Settings, Tasks & Actions)
// and back.
interface ChatInputState {
pendingPrefill: string | null
drafts: Record<string, string>
}
const initialState: ChatInputState = {
pendingPrefill: null,
drafts: {},
}
const chatInputSlice = createSlice({
name: 'chatInput',
initialState,
reducers: {
setPendingPrefill(state, action: PayloadAction<string>) {
state.pendingPrefill = action.payload
},
clearPendingPrefill(state) {
state.pendingPrefill = null
},
setDraftText(state, action: PayloadAction<{ key: string; text: string }>) {
state.drafts[action.payload.key] = action.payload.text
},
clearDraftText(state, action: PayloadAction<string>) {
delete state.drafts[action.payload]
},
},
})
export const { setPendingPrefill, clearPendingPrefill, setDraftText, clearDraftText } = chatInputSlice.actions
export default chatInputSlice.reducer