| 1 | import { PayloadAction, createSlice } from "@reduxjs/toolkit"; |
| 2 | import { IndexingStatus } from "core"; |
| 3 | |
| 4 | export type IndexingState = { |
| 5 | indexing: { |
| 6 | hiddenChatPeekTypes: Record<IndexingStatus["type"], boolean>; |
| 7 | statuses: Record<string, IndexingStatus>; // status id -> status |
| 8 | }; |
| 9 | }; |
| 10 | |
| 11 | const initialState: IndexingState = { |
| 12 | indexing: { |
| 13 | statuses: {}, |
| 14 | hiddenChatPeekTypes: { |
| 15 | docs: false, |
| 16 | }, |
| 17 | }, |
| 18 | }; |
| 19 | |
| 20 | export const indexingSlice = createSlice({ |
| 21 | name: "indexing", |
| 22 | initialState, |
| 23 | reducers: { |
| 24 | updateIndexingStatus: ( |
| 25 | state, |
| 26 | { payload }: PayloadAction<IndexingStatus>, |
| 27 | ) => { |
| 28 | state.indexing.statuses = { |
| 29 | ...state.indexing.statuses, |
| 30 | [payload.id]: payload, |
| 31 | }; |
| 32 | |
| 33 | // This check is so that if all indexing is stopped for e.g. docs |
| 34 | // The next time docs indexing starts the peek will show again |
| 35 | const indexingThisType = Object.values(state.indexing.statuses).filter( |
| 36 | (status) => |
| 37 | status.type === payload.type && status.status === "indexing", |
| 38 | ); |
| 39 | if (indexingThisType.length === 0) { |
| 40 | state.indexing.hiddenChatPeekTypes = { |
| 41 | ...state.indexing.hiddenChatPeekTypes, |
| 42 | [payload.type]: false, |
| 43 | }; |
| 44 | } |
| 45 | }, |
| 46 | setIndexingChatPeekHidden: ( |
| 47 | state, |
| 48 | { |
| 49 | payload, |
| 50 | }: PayloadAction<{ |
| 51 | type: IndexingStatus["type"]; |
| 52 | hidden: boolean; |
| 53 | }>, |
| 54 | ) => { |
| 55 | state.indexing.hiddenChatPeekTypes = { |
| 56 | ...state.indexing.hiddenChatPeekTypes, |
| 57 | [payload.type]: payload.hidden, |
| 58 | }; |
| 59 | }, |
| 60 | }, |
| 61 | }); |
| 62 | |
| 63 | export const { updateIndexingStatus, setIndexingChatPeekHidden } = |
| 64 | indexingSlice.actions; |
| 65 | |
| 66 | export default indexingSlice.reducer; |