Skip to content

Commit

Permalink
Fix a serious bug / infinite loop when setting an API key introduced in
Browse files Browse the repository at this point in the history
bac7ad3

Part of #5.
  • Loading branch information
jkomoros committed Jan 6, 2024
1 parent 5037f48 commit 3ce0d37
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
6 changes: 5 additions & 1 deletion app/actions/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {

import {
selectAIProvider,
selectAPIKeys,
selectAttachedImage,
selectChangesMade,
selectCurrentSprout,
Expand Down Expand Up @@ -114,7 +115,10 @@ export const selectSprout = (sprout : SproutLocation, skipCanonicalize = false)
if (!skipCanonicalize) dispatch(canonicalizePath());
};

export const setAPIKey = (provider: ModelProvider, key : string) : ThunkSomeAction => (dispatch) => {
export const setAPIKey = (provider: ModelProvider, key : string) : ThunkSomeAction => (dispatch, getState) => {
const state = getState();
const apiKeys = selectAPIKeys(state);
if (apiKeys[provider] === key) return;
dispatch({
type: SET_API_KEY,
provider,
Expand Down
3 changes: 3 additions & 0 deletions app/components/sprout-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,9 @@ class SproutView extends connect(store)(PageViewElement) {
this._focusTextArea();
}
if (changedProps.has('_apiKeys')) {
//This will have a few extra sets as multiple are set, but it will
//settle because the action creator checks for equality. Previously
//in bac7ad3 before the equality check this led to an infinite loop.
for (const [provider, key] of TypedObject.entries(this._apiKeys)) {
if (!key) continue;
store.dispatch(setAPIKey(provider, key));
Expand Down

0 comments on commit 3ce0d37

Please sign in to comment.