Skip to content

Commit

Permalink
Fix default bot
Browse files Browse the repository at this point in the history
  • Loading branch information
crspeller committed Jun 25, 2024
1 parent caaa915 commit 3834232
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions webapp/src/bots.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ const defaultBotLocalStorageKey = 'defaultBot';

export const useBotlist = () => {
const bots = useSelector<GlobalState, LLMBot[] | null>((state: any) => state['plugins-' + manifest.id].bots);
const defaultActiveBotName = localStorage.getItem(defaultBotLocalStorageKey);
const defaultActiveBot = bots?.find((bot: LLMBot) => bot.username === defaultActiveBotName) || null;
const [activeBot, setActiveBotState] = useState<LLMBot | null>(defaultActiveBot);
const [activeBot, setActiveBot] = useState<LLMBot | null>(null);
const currentUserId = useSelector<GlobalState, string>((state) => state.entities.users.currentUserId);
const dispatch = useDispatch();

Expand All @@ -45,10 +43,17 @@ export const useBotlist = () => {
}
}, [currentUserId, bots, dispatch]);

const setActiveBot = (bot: LLMBot) => {
setActiveBotState(bot);
localStorage.setItem(defaultBotLocalStorageKey, bot.username);
};
useEffect(() => {
const defaultActiveBotName = localStorage.getItem(defaultBotLocalStorageKey);
setActiveBot(bots?.find((bot: LLMBot) => bot.username === defaultActiveBotName) || bots?.[0] || null);
}, [bots]);

useEffect(() => {
if (!activeBot) {
return;
}
localStorage.setItem(defaultBotLocalStorageKey, activeBot.username);
}, [activeBot]);

return {bots, activeBot, setActiveBot};
};

0 comments on commit 3834232

Please sign in to comment.