Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

fix(settings): der getippte Modellordner geht beim Weggehen nicht mehr verloren

purple95f65791b88b
diff --git a/src/components/settings/SettingsPage.tsx b/src/components/settings/SettingsPage.tsx
index 0a2adee6..51281d5a 100644
--- a/src/components/settings/SettingsPage.tsx
+++ b/src/components/settings/SettingsPage.tsx
@@ -270,9 +270,18 @@ function WorkflowSection() {
// Models tab stayed empty beside a folder full of models. It is read now
// (lib/custom-model-dir.ts), and the copy says what it does.
+/**
+ * How long typing has to stop before the folder is stored anyway (A14-2a).
+ *
+ * Long enough that it does not fire between two keystrokes of a typed path,
+ * short enough that walking away right after typing has already saved. The
+ * write itself is a settings update plus one model-list refresh, so a stray
+ * extra one costs nothing.
+ */
+export const MODEL_DIR_SAVE_DEBOUNCE_MS = 600
+
export function HfDownloadPathSetting() {
const override = useSettingsStore(s => s.settings.hfDownloadPathOverride)
- const updateSettings = useSettingsStore(s => s.updateSettings)
const [draft, setDraft] = useState(override)
const [comfy, setComfy] = useState<CustomModelDirResult | null>(null)
const [scan, setScan] = useState<ScannedDir | null>(null)
@@ -321,14 +330,64 @@ export function HfDownloadPathSetting() {
// to ComfyUI is unaffected by that, so both lines belong on screen.
const scanHidesHandoff = scan?.status === 'unreachable' || scan?.status === 'unusable'
- function apply(next: string) {
- setDraft(next)
- updateSettings({ hfDownloadPathOverride: next })
+ // A16 (A14-2a), Windows counter-check 02.09.: the folder was stored on Enter
+ // or on blur and on nothing else. Type a path, walk away to another tab or
+ // close Settings, and the value stood in the box while
+ // `hfDownloadPathOverride` stayed empty and Installed never changed. The
+ // counter-check's first attempt concluded from that the folder was being
+ // ignored, which is the reading anyone would reach.
+ //
+ // Two things are added, and neither replaces the blur. A short debounce, so
+ // a value that has stopped changing is stored without any further gesture
+ // and a refresh of the list sees the folder. And a commit when the field
+ // goes away, so leaving the panel saves rather than discards.
+ const saveTimer = useRef<ReturnType<typeof setTimeout> | null>(null)
+ /** Typed but not stored yet. Null means there is nothing owed. */
+ const pendingPath = useRef<string | null>(null)
+
+ /** Store it. Everything that saves goes through here, so cancelling the
+ * debounce and clearing the debt cannot be forgotten on one path. */
+ function commit(next: string) {
+ if (saveTimer.current) { clearTimeout(saveTimer.current); saveTimer.current = null }
+ pendingPath.current = null
+ const store = useSettingsStore.getState()
+ if (store.settings.hfDownloadPathOverride === next) return
+ store.updateSettings({ hfDownloadPathOverride: next })
// The Models tab is the surface this setting is about: refresh it now
// instead of after the next navigation.
window.dispatchEvent(new CustomEvent('lu-models-refresh'))
}
+ function apply(next: string) {
+ setDraft(next)
+ commit(next)
+ }
+
+ /** One keystroke. The value is owed from here until something commits it. */
+ function typePath(next: string) {
+ setDraft(next)
+ pendingPath.current = next.trim()
+ if (saveTimer.current) clearTimeout(saveTimer.current)
+ saveTimer.current = setTimeout(() => {
+ saveTimer.current = null
+ const owed = pendingPath.current
+ if (owed !== null) commit(owed)
+ }, MODEL_DIR_SAVE_DEBOUNCE_MS)
+ }
+
+ // Closing Settings or switching tabs unmounts this field. Whatever was owed
+ // at that moment is stored, not thrown away.
+ useEffect(() => () => {
+ if (saveTimer.current) clearTimeout(saveTimer.current)
+ const owed = pendingPath.current
+ pendingPath.current = null
+ if (owed === null) return
+ const store = useSettingsStore.getState()
+ if (store.settings.hfDownloadPathOverride === owed) return
+ store.updateSettings({ hfDownloadPathOverride: owed })
+ window.dispatchEvent(new CustomEvent('lu-models-refresh'))
+ }, [])
+
async function pickFolder() {
try {
const chosen = await backendCall<string | null>('pick_folder')
@@ -346,7 +405,7 @@ export function HfDownloadPathSetting() {
<input
type="text"
value={draft}
- onChange={(e) => setDraft(e.target.value)}
+ onChange={(e) => typePath(e.target.value)}
onBlur={() => apply(draft.trim())}
placeholder={luEngineFolderPlaceholder(autoDir)}
aria-label="LU Engine folder"
diff --git a/src/components/settings/__tests__/model-folder-survives-walking-away.test.ts b/src/components/settings/__tests__/model-folder-survives-walking-away.test.ts
new file mode 100644
index 00000000..cd663811
--- /dev/null
+++ b/src/components/settings/__tests__/model-folder-survives-walking-away.test.ts
@@ -0,0 +1,138 @@
+/**
+ * @vitest-environment jsdom
+ *
+ * A16 (A14-2a), Windows counter-check 02.09.: the LU Engine folder was stored
+ * on Enter or on blur and on nothing else. Type a path and navigate away, and
+ * the value stood in the box while `hfDownloadPathOverride` stayed empty,
+ * Installed did not change, and a refresh did not help either. The
+ * counter-check's first run concluded from that the folder was simply being
+ * ignored. It is the reading anyone would reach, and a field that keeps
+ * showing a value it never saved has earned it.
+ *
+ * Two ways to save were added and the blur was kept: a short debounce after
+ * typing stops, and a commit when the field is unmounted.
+ *
+ * Run: npx vitest run src/components/settings/__tests__/model-folder-survives-walking-away.test.ts
+ */
+import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
+import { createElement } from 'react'
+import { render, screen, cleanup, act, fireEvent } from '@testing-library/react'
+
+vi.mock('../../../api/backend', () => ({
+ backendCall: vi.fn(async () => null),
+ isTauri: () => true,
+ isMacOS: () => false,
+ openExternal: vi.fn(),
+ secretGet: vi.fn().mockRejectedValue(new Error('no keychain here')),
+ secretSet: vi.fn(), secretDelete: vi.fn(),
+ setComfyPort: vi.fn(), setComfyHost: vi.fn(),
+}))
+vi.mock('../../../api/engine', async () => {
+ const actual = await vi.importActual<typeof import('../../../api/engine')>('../../../api/engine')
+ return {
+ ...actual,
+ listBundledModels: vi.fn(async () => []),
+ lastCustomScanDir: () => null,
+ lastScanDirs: () => [],
+ syncCustomModelDir: vi.fn(async () => null),
+ }
+})
+
+const { HfDownloadPathSetting, MODEL_DIR_SAVE_DEBOUNCE_MS } = await import('../SettingsPage')
+const { useSettingsStore } = await import('../../../stores/settingsStore')
+
+const TYPED = 'C:\\lu-e2e-models'
+
+function storedFolder() {
+ return useSettingsStore.getState().settings.hfDownloadPathOverride
+}
+
+async function fieldWithTypedPath() {
+ render(createElement(HfDownloadPathSetting))
+ await act(async () => { await Promise.resolve(); await Promise.resolve() })
+ const box = screen.getByLabelText('LU Engine folder')
+ fireEvent.change(box, { target: { value: TYPED } })
+ return box
+}
+
+beforeEach(() => {
+ vi.useFakeTimers()
+ useSettingsStore.getState().updateSettings({ hfDownloadPathOverride: '' })
+})
+afterEach(() => {
+ cleanup()
+ vi.useRealTimers()
+ useSettingsStore.getState().updateSettings({ hfDownloadPathOverride: '' })
+})
+
+describe('a folder typed into the LU Engine field', () => {
+ it('is stored when the field goes away with the panel', async () => {
+ await fieldWithTypedPath()
+ expect(storedFolder(), 'nothing should be stored yet').toBe('')
+
+ // Closing Settings, or switching to another tab: the field unmounts with
+ // the typed value never having been blurred.
+ cleanup()
+
+ expect(storedFolder(), 'the typed folder was thrown away on the way out').toBe(TYPED)
+ })
+
+ it('is stored on its own shortly after typing stops', async () => {
+ await fieldWithTypedPath()
+ await act(async () => { await vi.advanceTimersByTimeAsync(MODEL_DIR_SAVE_DEBOUNCE_MS + 50) })
+ expect(storedFolder()).toBe(TYPED)
+ })
+
+ it('and the model list is told, so a refresh sees the folder', async () => {
+ const refreshes: Event[] = []
+ const onRefresh = (e: Event) => refreshes.push(e)
+ window.addEventListener('lu-models-refresh', onRefresh)
+ await fieldWithTypedPath()
+ await act(async () => { await vi.advanceTimersByTimeAsync(MODEL_DIR_SAVE_DEBOUNCE_MS + 50) })
+ window.removeEventListener('lu-models-refresh', onRefresh)
+ expect(refreshes.length).toBeGreaterThan(0)
+ })
+
+ // NEGATIVE CONTROL: it is a debounce, not a write per keystroke. A path
+ // being typed must not be stored half finished, or the scan runs against
+ // "C:\lu" while the user is still on the second syllable.
+ it('is not stored mid word', async () => {
+ render(createElement(HfDownloadPathSetting))
+ await act(async () => { await Promise.resolve() })
+ const box = screen.getByLabelText('LU Engine folder')
+ fireEvent.change(box, { target: { value: 'C:\\lu' } })
+ await act(async () => { await vi.advanceTimersByTimeAsync(MODEL_DIR_SAVE_DEBOUNCE_MS - 200) })
+ fireEvent.change(box, { target: { value: TYPED } })
+ await act(async () => { await vi.advanceTimersByTimeAsync(MODEL_DIR_SAVE_DEBOUNCE_MS - 200) })
+ expect(storedFolder(), 'a half typed path reached the store').toBe('')
+ await act(async () => { await vi.advanceTimersByTimeAsync(300) })
+ expect(storedFolder()).toBe(TYPED)
+ })
+
+ // NEGATIVE CONTROL: the blur path, which worked before and has to keep
+ // working, and it trims the way it always did.
+ it('still saves on blur, trimmed', async () => {
+ render(createElement(HfDownloadPathSetting))
+ await act(async () => { await Promise.resolve() })
+ const box = screen.getByLabelText('LU Engine folder')
+ fireEvent.change(box, { target: { value: ` ${TYPED} ` } })
+ fireEvent.blur(box)
+ expect(storedFolder()).toBe(TYPED)
+ })
+
+ // NEGATIVE CONTROL: an unmount that owes nothing must not write. Otherwise
+ // opening and closing Settings would rewrite the setting, and Reset would be
+ // undone by the very act of leaving.
+ it('writes nothing when the field is left untouched', async () => {
+ useSettingsStore.getState().updateSettings({ hfDownloadPathOverride: 'D:\\models' })
+ const writes: Event[] = []
+ const onRefresh = (e: Event) => writes.push(e)
+ window.addEventListener('lu-models-refresh', onRefresh)
+ render(createElement(HfDownloadPathSetting))
+ await act(async () => { await Promise.resolve() })
+ cleanup()
+ window.removeEventListener('lu-models-refresh', onRefresh)
+ expect(writes, 'leaving the panel wrote the setting again').toHaveLength(0)
+ expect(storedFolder()).toBe('D:\\models')
+ })
+})