-
Notifications
You must be signed in to change notification settings - Fork 503
AI Chat: refactor redux file #63775
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AI Chat: refactor redux file #63775
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very cool refactor - I think it significantly improves readability! I can't believe aichatRedux
is currently almost 1K in lines - I remember when it was a baby - RIP. 😁
@@ -25,7 +25,9 @@ const ChatEventsList: React.FunctionComponent<ChatEventsListProps> = ({ | |||
}) => { | |||
const [inProgrammaticScroll, setInProgrammaticScroll] = useState(false); | |||
const [showScrollToBottom, setShowScrollToBottom] = useState(true); | |||
const {isWaitingForChatResponse} = useAppSelector(state => state.aichat); | |||
const isWaitingForChatResponse = useAppSelector( | |||
state => !!state.aichat.chatMessagePending |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧹
// This thunk is used when a student fills out a model card and "publishes" their model, | ||
// enabling access to a "presentation view" where they can interact with their model | ||
// and view its details (temperature, system prompt, etc) in a summary view. | ||
export const publishModelCard = createAsyncThunk( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Notice that this used to be called publishModel
but think it makes sense to call it publishModelCard
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep good catch! Renamed for clarity (forgot to include in description)
apps/src/aichat/redux/slice.ts
Outdated
}; | ||
type ModalType = ModalTypes.WARNING | ModalTypes.TEACHER_ONBOARDING | undefined; | ||
|
||
export interface AichatState { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because of the size of this file and its name, I think it may make sense to storing state in a separate file - but not a strong feeling!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense! I may actually pull this and other local type (ModalType, etc) into a separate types
file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated! Actually ended up slimming this down a bit more
- Got rid of the
MessageLocation
type since it can be inferred from the return type ofgetUpdateMessageLocation
- Got rid of
ModalType
since it's equivalent toModalTypes | undefined
- Which left only the
AichatState
type. Moved this into a separate file calledstate.ts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😍 lovely!
This is an attempt to break up our nearly 900-line redux file into smaller, targeted files, grouped by type (selectors, thunks). We still export a single
index.ts
file in theaichat/redux
directory so other components can still just import fromaichat/redux
directly.Details:
redux/thunks
folder, with a separate file per thunk. Some of the thunks are quite large, so this helps organize things (even if others are pretty small)redux/thunks/helpers
subdirectoryredux/selectors/index.ts
file. I opted to keep these in one file since they're individually pretty small.aichatRedux
is just the state schema and slice definition, so I renamed itslice.ts
. It's still not that small, but is focused on just defining the state and actions. I debated pulling outAichatState
into a separate file too but kept it there for now. Happy to reconsider though.redux/index.ts
file.Like #63710 this is mostly copy+paste only, but there were a few minor changes needed to get everything working correctly:
aichat/redux
instead ofaichat/redux/aichatRedux
waitingForResponse: boolean
state value, and instead just compute this based on the presence of achatMessagePending
. This was primarily so I could get rid of theextraReducers
in the slice and avoid a circular dependency between the thunk file and the slice file. We setwaitingForResponse
totrue
only when we're in the process of sending a chat message, which is also only whenchatMessagePending
is defined, so I felt this made sense.Testing story
Tested locally. No functional/behavioral changes.
Follow-up work
I'd like to clean up the
utils.ts
file too, but I'll leave this for a separate change that tackles all the various util files together - there's a bunch of util files spread out across these three files that all kind of get used in different places.