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.

feat(web-app): hide the download button when nothing is downloading (#9427)

James Damb590be5fd1ee
diff --git a/web-app/src/containers/DownloadManegement.tsx b/web-app/src/containers/DownloadManegement.tsx
index 433a54915..d3063ef08 100644
--- a/web-app/src/containers/DownloadManegement.tsx
+++ b/web-app/src/containers/DownloadManegement.tsx
@@ -5,7 +5,7 @@ import {
} from '@/components/ui/popover'
import { Progress } from '@/components/ui/progress'
import { useDownloadStore } from '@/hooks/useDownloadStore'
-import { useAppUpdater } from '@/hooks/useAppUpdater'
+import { useAppUpdateDownload } from '@/stores/app-update-download-store'
import { useServiceHub } from '@/hooks/useServiceHub'
import { events, AppEvent } from '@janhq/core'
import { IconPlayerPause, IconPlayerPlay, IconX } from '@tabler/icons-react'
@@ -28,23 +28,8 @@ export function DownloadManagement() {
setPaused,
addLocalDownloadingModel,
} = useDownloadStore()
- const { updateState } = useAppUpdater()
-
- const [appUpdateState, setAppUpdateState] = useState({
- isDownloading: false,
- downloadProgress: 0,
- downloadedBytes: 0,
- totalBytes: 0,
- })
-
- useEffect(() => {
- setAppUpdateState({
- isDownloading: updateState.isDownloading,
- downloadProgress: updateState.downloadProgress,
- downloadedBytes: updateState.downloadedBytes,
- totalBytes: updateState.totalBytes,
- })
- }, [updateState])
+ const appUpdateState = useAppUpdateDownload()
+ const setAppUpdateState = appUpdateState.update
const onAppUpdateDownloadUpdate = useCallback(
(data: {
@@ -52,37 +37,32 @@ export function DownloadManagement() {
downloadedBytes?: number
totalBytes?: number
}) => {
- setAppUpdateState((prev) => ({
- ...prev,
+ setAppUpdateState({
isDownloading: true,
downloadProgress: data.progress || 0,
downloadedBytes: data.downloadedBytes || 0,
totalBytes: data.totalBytes || 0,
- }))
+ })
},
- []
+ [setAppUpdateState]
)
const onAppUpdateDownloadSuccess = useCallback(() => {
- setAppUpdateState((prev) => ({
- ...prev,
+ setAppUpdateState({
isDownloading: false,
downloadProgress: 1,
- }))
+ })
toast.success(t('common:toast.appUpdateDownloaded.title'), {
description: t('common:toast.appUpdateDownloaded.description'),
})
- }, [t])
+ }, [t, setAppUpdateState])
const onAppUpdateDownloadError = useCallback(() => {
- setAppUpdateState((prev) => ({
- ...prev,
- isDownloading: false,
- }))
+ setAppUpdateState({ isDownloading: false })
toast.error(t('common:toast.appUpdateDownloadFailed.title'), {
description: t('common:toast.appUpdateDownloadFailed.description'),
})
- }, [t])
+ }, [t, setAppUpdateState])
const downloadProcesses = useMemo(() => {
// Get downloads with progress data
@@ -230,11 +210,16 @@ export function DownloadManagement() {
onAppUpdateDownloadError,
])
+ // Stay mounted so the app-update listeners above keep firing; only the
+ // button hides. Keep it while the popover is open so the last download
+ // finishing doesn't yank the anchor out from under it.
+ if (downloadCount === 0 && !isPopoverOpen) return null
+
return (
<>
<Popover open={isPopoverOpen} onOpenChange={setIsPopoverOpen}>
<PopoverTrigger asChild>
- <Button variant="ghost" size="icon" className="text-muted-foreground z-50 rounded-full hover:bg-sidebar-foreground/8! -mt-0.5 size-7 relative">
+ <Button variant="ghost" size="icon" className="text-muted-foreground z-50 rounded-full hover:bg-sidebar-foreground/8! -mt-0.5 size-7 relative animate-in fade-in-0 zoom-in-95">
<DownloadIcon className='text-muted-foreground size-4' />
{downloadCount > 0 && (
<svg className="absolute inset-0 size-7 -rotate-90" viewBox="0 0 36 36">
diff --git a/web-app/src/containers/HeaderPage.tsx b/web-app/src/containers/HeaderPage.tsx
index afa2b8d8c..a7dbbcaec 100644
--- a/web-app/src/containers/HeaderPage.tsx
+++ b/web-app/src/containers/HeaderPage.tsx
@@ -42,7 +42,6 @@ const HeaderPage = memo(function HeaderPage({ children }: HeaderPageProps) {
>
{!open && (
<>
- <DownloadManagement />
<Button
variant="ghost"
size="icon-sm"
@@ -54,6 +53,8 @@ const HeaderPage = memo(function HeaderPage({ children }: HeaderPageProps) {
className="text-muted-foreground relative size-4.5"
/>
</Button>
+ {/* After the toggle so the toggle stays put when this appears */}
+ <DownloadManagement />
</>
)}
<div
diff --git a/web-app/src/containers/__tests__/DownloadManegement.test.tsx b/web-app/src/containers/__tests__/DownloadManegement.test.tsx
index 21cad4a82..5ce6592fd 100644
--- a/web-app/src/containers/__tests__/DownloadManegement.test.tsx
+++ b/web-app/src/containers/__tests__/DownloadManegement.test.tsx
@@ -1,5 +1,5 @@
import { describe, it, expect, vi, beforeEach } from 'vitest'
-import { render, screen, fireEvent, waitFor } from '@testing-library/react'
+import { render, screen, fireEvent, waitFor, act } from '@testing-library/react'
import '@testing-library/jest-dom'
// ---- Module mocks ----------------------------------------------------------
@@ -15,12 +15,6 @@ const hoisted = vi.hoisted(() => ({
setPaused: vi.fn(),
addLocalDownloadingModel: vi.fn(),
},
- updateState: {
- isDownloading: false,
- downloadProgress: 0,
- downloadedBytes: 0,
- totalBytes: 0,
- },
navigateMock: vi.fn(),
toastMock: {
success: vi.fn(),
@@ -44,10 +38,6 @@ vi.mock('@/hooks/useDownloadStore', () => {
return { useDownloadStore }
})
-vi.mock('@/hooks/useAppUpdater', () => ({
- useAppUpdater: () => ({ updateState: hoisted.updateState }),
-}))
-
vi.mock('@/hooks/useServiceHub', () => ({
useServiceHub: () => ({
models: () => ({
@@ -120,6 +110,7 @@ window.core = {
}
import { DownloadManagement } from '../DownloadManegement'
+import { useAppUpdateDownload } from '@/stores/app-update-download-store'
describe('DownloadManagement', () => {
beforeEach(() => {
@@ -127,18 +118,18 @@ describe('DownloadManagement', () => {
hoisted.downloadStore.downloads = {}
hoisted.downloadStore.resumeParams = {}
hoisted.downloadStore.localDownloadingModels = new Set()
- hoisted.updateState = {
+ useAppUpdateDownload.setState({
isDownloading: false,
downloadProgress: 0,
downloadedBytes: 0,
totalBytes: 0,
- }
+ })
hoisted.eventHandlers = {}
})
- it('renders empty state when no downloads', () => {
- render(<DownloadManagement />)
- expect(screen.getByText(/Your download progress/)).toBeInTheDocument()
+ it('renders nothing when no downloads are in progress', () => {
+ const { container } = render(<DownloadManagement />)
+ expect(container).toBeEmptyDOMElement()
})
it('renders a download item with progress', () => {
@@ -170,13 +161,13 @@ describe('DownloadManagement', () => {
expect(screen.getByText('Downloading...')).toBeInTheDocument()
})
- it('renders App Update progress when updater is downloading', () => {
- hoisted.updateState = {
+ it('renders App Update progress when an update is downloading', () => {
+ useAppUpdateDownload.setState({
isDownloading: true,
downloadProgress: 0.5,
downloadedBytes: 1024,
totalBytes: 2048,
- }
+ })
render(<DownloadManagement />)
expect(screen.getByText('App Update')).toBeInTheDocument()
expect(screen.getByText('50%')).toBeInTheDocument()
@@ -219,6 +210,37 @@ describe('DownloadManagement', () => {
+ it('appears when an app update starts downloading', () => {
+ const { container } = render(<DownloadManagement />)
+ expect(container).toBeEmptyDOMElement()
+ act(() => {
+ hoisted.eventHandlers['audu']({ progress: 0.1, downloadedBytes: 10, totalBytes: 100 })
+ })
+ expect(screen.getByText('App Update')).toBeInTheDocument()
+ })
+
+ it('keeps app update progress across a remount (sidebar toggle)', () => {
+ const { unmount } = render(<DownloadManagement />)
+ act(() => {
+ hoisted.eventHandlers['audu']({ progress: 0.1, downloadedBytes: 10, totalBytes: 100 })
+ })
+ unmount()
+ // No further progress events: the new mount must not wait for one
+ render(<DownloadManagement />)
+ expect(screen.getByText('App Update')).toBeInTheDocument()
+ })
+
+ it('hides again once the app update download finishes', () => {
+ const { container } = render(<DownloadManagement />)
+ act(() => {
+ hoisted.eventHandlers['audu']({ progress: 0.5, downloadedBytes: 50, totalBytes: 100 })
+ })
+ act(() => {
+ hoisted.eventHandlers['auds']()
+ })
+ expect(container).toBeEmptyDOMElement()
+ })
+
it('on app update download success, shows success toast', () => {
render(<DownloadManagement />)
hoisted.eventHandlers['auds']()
diff --git a/web-app/src/stores/app-update-download-store.ts b/web-app/src/stores/app-update-download-store.ts
new file mode 100644
index 000000000..53c46d2a4
--- /dev/null
+++ b/web-app/src/stores/app-update-download-store.ts
@@ -0,0 +1,23 @@
+import { create } from 'zustand'
+
+type AppUpdateDownload = {
+ isDownloading: boolean
+ downloadProgress: number
+ downloadedBytes: number
+ totalBytes: number
+}
+
+type AppUpdateDownloadState = AppUpdateDownload & {
+ update: (partial: Partial<AppUpdateDownload>) => void
+}
+
+// App-update download progress, fed by the AppEvent download events.
+// DownloadManagement is mounted in the sidebar or the header depending on
+// whether the sidebar is open, so per-instance state was lost on every toggle.
+export const useAppUpdateDownload = create<AppUpdateDownloadState>()((set) => ({
+ isDownloading: false,
+ downloadProgress: 0,
+ downloadedBytes: 0,
+ totalBytes: 0,
+ update: (partial) => set(partial),
+}))