The chat showed 'Server offline' until the user opened Settings, and the selected model was forgotten on every restart. useAutoConnect fetches the models once after the persisted settings are restored; activeModelId is now persisted.
24 lines
800 B
TypeScript
24 lines
800 B
TypeScript
import { useEffect } from 'react';
|
|
import { useAppStore } from '../store/useAppStore';
|
|
import { useLMStudio } from './useLMStudio';
|
|
|
|
/**
|
|
* Checks the server once when the app opens, after the saved settings have been
|
|
* restored, so the connection badge and model list are not stale until the user
|
|
* visits Settings.
|
|
*/
|
|
export function useAutoConnect() {
|
|
const { fetchModels } = useLMStudio();
|
|
|
|
useEffect(() => {
|
|
const { persist } = useAppStore;
|
|
if (persist.hasHydrated()) {
|
|
void fetchModels();
|
|
return;
|
|
}
|
|
return persist.onFinishHydration(() => void fetchModels());
|
|
// Mount only: later checks are triggered by the user (model selector, Settings).
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, []);
|
|
}
|