Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

fix(ui): save a Trusted Hosts entry on Enter and when the popover closes (#9470) (#9575)

Thinh Le225aeb3842d9
diff --git a/web-app/src/containers/TrustedHostsInput.tsx b/web-app/src/containers/TrustedHostsInput.tsx
index ce835e0a0..3d67095fe 100644
--- a/web-app/src/containers/TrustedHostsInput.tsx
+++ b/web-app/src/containers/TrustedHostsInput.tsx
@@ -1,9 +1,19 @@
import { Input } from '@/components/ui/input'
import { useLocalApiServer } from '@/hooks/useLocalApiServer'
-import { useState, useEffect } from 'react'
+import { useState, useEffect, useRef } from 'react'
import { useTranslation } from '@/i18n/react-i18next-compat'
import { cn } from '@/lib/utils'
+// Split by comma, clean up each host and drop duplicates.
+const parseHosts = (raw: string) => [
+ ...new Set(
+ raw
+ .split(',')
+ .map((host) => host.trim())
+ .filter((host) => host.length > 0)
+ ),
+]
+
export function TrustedHostsInput({
isServerRunning,
}: {
@@ -23,26 +33,35 @@ export function TrustedHostsInput({
setInputValue(value)
}
- const handleBlur = () => {
- // Split by comma and clean up each host
- const hosts = inputValue
- .split(',')
- .map((host) => host.trim())
- .filter((host) => host.length > 0)
-
- // Remove duplicates
- const uniqueHosts = [...new Set(hosts)]
-
+ const commit = () => {
+ const uniqueHosts = parseHosts(inputValue)
setTrustedHosts(uniqueHosts)
setInputValue(uniqueHosts.join(', '))
}
+ // Closing the settings popover removes the field without a blur event, so
+ // an entry that was typed but not yet committed would be lost. Save it then.
+ const typed = useRef(inputValue)
+ typed.current = inputValue
+ useEffect(
+ () => () => {
+ const { trustedHosts: saved, setTrustedHosts: save } =
+ useLocalApiServer.getState()
+ const hosts = parseHosts(typed.current)
+ if (hosts.join(',') !== saved.join(',')) save(hosts)
+ },
+ []
+ )
+
return (
<Input
type="text"
value={inputValue}
onChange={handleChange}
- onBlur={handleBlur}
+ onBlur={commit}
+ onKeyDown={(e) => {
+ if (e.key === 'Enter') commit()
+ }}
placeholder={t('common:enterTrustedHosts')}
className={cn(
'h-8 text-sm',
diff --git a/web-app/src/containers/__tests__/TrustedHostsInput.test.tsx b/web-app/src/containers/__tests__/TrustedHostsInput.test.tsx
new file mode 100644
index 000000000..e0fb00d85
--- /dev/null
+++ b/web-app/src/containers/__tests__/TrustedHostsInput.test.tsx
@@ -0,0 +1,62 @@
+import { describe, it, expect, vi, beforeEach } from 'vitest'
+import { render, screen, cleanup } from '@testing-library/react'
+import userEvent from '@testing-library/user-event'
+import { TrustedHostsInput } from '../TrustedHostsInput'
+import { useLocalApiServer } from '@/hooks/useLocalApiServer'
+
+vi.mock('@/i18n/react-i18next-compat', () => ({
+ useTranslation: () => ({ t: (k: string) => k }),
+}))
+
+// The settings store persists to localStorage; keep these tests in memory.
+vi.mock('zustand/middleware', () => ({
+ persist: (fn: unknown) => fn,
+ createJSONStorage: () => ({
+ getItem: vi.fn(),
+ setItem: vi.fn(),
+ removeItem: vi.fn(),
+ }),
+}))
+
+const savedHosts = () => useLocalApiServer.getState().trustedHosts
+const field = () => screen.getByPlaceholderText('common:enterTrustedHosts')
+
+describe('TrustedHostsInput', () => {
+ beforeEach(() => {
+ cleanup()
+ useLocalApiServer.setState({ trustedHosts: [] })
+ })
+
+ it('saves a typed entry when the field loses focus, trimmed and de-duplicated', async () => {
+ const user = userEvent.setup()
+ render(<TrustedHostsInput />)
+ await user.type(field(), ' a.example , b.example,a.example ')
+ await user.tab()
+ expect(savedHosts()).toEqual(['a.example', 'b.example'])
+ expect(field()).toHaveValue('a.example, b.example')
+ })
+
+ it('saves a typed entry on Enter', async () => {
+ const user = userEvent.setup()
+ render(<TrustedHostsInput />)
+ await user.type(field(), 'a.example{Enter}')
+ expect(savedHosts()).toEqual(['a.example'])
+ })
+
+ // Closing the settings popover unmounts the field without a blur event.
+ it('keeps a typed entry when the field is removed without losing focus', async () => {
+ const user = userEvent.setup()
+ const { unmount } = render(<TrustedHostsInput />)
+ await user.type(field(), 'a.example, b.example')
+ unmount()
+ expect(savedHosts()).toEqual(['a.example', 'b.example'])
+ })
+
+ it('does not rewrite the saved hosts when nothing was typed', () => {
+ const hosts = ['a.example']
+ useLocalApiServer.setState({ trustedHosts: hosts })
+ const { unmount } = render(<TrustedHostsInput />)
+ unmount()
+ expect(savedHosts()).toBe(hosts)
+ })
+})