Skip to content

Commit

Permalink
Merge pull request #4481 from janhq/chore/engines-data-cache
Browse files Browse the repository at this point in the history
chore: add caching for engines to improve load time
  • Loading branch information
louis-jan authored Jan 18, 2025
2 parents 0547cac + 9cf63f8 commit 0dd2519
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ const TableActiveModel = () => {
<div className="w-1/2">
<div className="overflow-hidden border-b border-[hsla(var(--app-border))]">
<table className="w-full px-8">
{activeModel &&
engines &&
isLocalEngine(engines, activeModel.engine) ? (
{activeModel && isLocalEngine(engines, activeModel.engine) ? (
<tbody>
<tr>
<td
Expand Down
4 changes: 2 additions & 2 deletions web/containers/ModelDropdown/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ const ModelDropdown = ({
const model = downloadedModels.find((model) => model.id === modelId)
if (model) {
if (
engines?.[model.engine]?.[0].type === 'local' ||
(engines?.[model.engine]?.[0].api_key?.length ?? 0) > 0
engines?.[model.engine]?.[0]?.type === 'local' ||
(engines?.[model.engine]?.[0]?.api_key?.length ?? 0) > 0
)
setSelectedModel(model)
} else {
Expand Down
30 changes: 30 additions & 0 deletions web/containers/Providers/SWRConfigProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use client'

import * as React from 'react'

import { SWRConfig } from 'swr'

function SWRConfigProvider({ children }: { children: React.ReactNode }) {
// https://swr.vercel.app/docs/advanced/cache#localstorage-based-persistent-cache
// When initializing, we restore the data from `localStorage` into a map.

const map = React.useMemo(() => new Map<string, object>(), [])
React.useEffect(() => {
const savedCache = JSON.parse(
window.localStorage.getItem('app-cache') || '[]'
)
savedCache.forEach(([key, value]: [string, object]) => {
map.set(key, value)
})

// Before unloading the app, we write back all the data into `localStorage`.
window.addEventListener('beforeunload', () => {
const appCache = JSON.stringify(Array.from(map.entries()))
window.localStorage.setItem('app-cache', appCache)
})
}, [map])

return <SWRConfig value={{ provider: () => map }}>{children}</SWRConfig>
}

export default SWRConfigProvider
39 changes: 22 additions & 17 deletions web/containers/Providers/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { PropsWithChildren } from 'react'

import { Toaster } from 'react-hot-toast'

import { SWRConfig } from 'swr'

Check warning on line 7 in web/containers/Providers/index.tsx

View workflow job for this annotation

GitHub Actions / test-on-macos

'SWRConfig' is defined but never used

Check warning on line 7 in web/containers/Providers/index.tsx

View workflow job for this annotation

GitHub Actions / test-on-ubuntu

'SWRConfig' is defined but never used

Check warning on line 7 in web/containers/Providers/index.tsx

View workflow job for this annotation

GitHub Actions / test-on-windows (mcafee)

'SWRConfig' is defined but never used

Check warning on line 7 in web/containers/Providers/index.tsx

View workflow job for this annotation

GitHub Actions / test-on-windows (default-windows-security)

'SWRConfig' is defined but never used

Check warning on line 7 in web/containers/Providers/index.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

'SWRConfig' is defined but never used

Check warning on line 7 in web/containers/Providers/index.tsx

View workflow job for this annotation

GitHub Actions / test-on-windows (bit-defender)

'SWRConfig' is defined but never used

Check warning on line 7 in web/containers/Providers/index.tsx

View workflow job for this annotation

GitHub Actions / test-on-macos

'SWRConfig' is defined but never used

Check warning on line 7 in web/containers/Providers/index.tsx

View workflow job for this annotation

GitHub Actions / test-on-ubuntu

'SWRConfig' is defined but never used

Check warning on line 7 in web/containers/Providers/index.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

'SWRConfig' is defined but never used

Check warning on line 7 in web/containers/Providers/index.tsx

View workflow job for this annotation

GitHub Actions / test-on-windows-pr

'SWRConfig' is defined but never used

import EventListener from '@/containers/Providers/EventListener'
import JotaiWrapper from '@/containers/Providers/Jotai'

Expand All @@ -18,27 +20,30 @@ import DeepLinkListener from './DeepLinkListener'
import KeyListener from './KeyListener'
import Responsive from './Responsive'

import SWRConfigProvider from './SWRConfigProvider'
import SettingsHandler from './SettingsHandler'

const Providers = ({ children }: PropsWithChildren) => {
return (
<ThemeWrapper>
<JotaiWrapper>
<Umami />
<CoreConfigurator>
<>
<Responsive />
<KeyListener />
<EventListener />
<DataLoader />
<SettingsHandler />
<DeepLinkListener />
<Toaster />
{children}
</>
</CoreConfigurator>
</JotaiWrapper>
</ThemeWrapper>
<SWRConfigProvider>
<ThemeWrapper>
<JotaiWrapper>
<Umami />
<CoreConfigurator>
<>
<Responsive />
<KeyListener />
<EventListener />
<DataLoader />
<SettingsHandler />
<DeepLinkListener />
<Toaster />
{children}
</>
</CoreConfigurator>
</JotaiWrapper>
</ThemeWrapper>
</SWRConfigProvider>
)
}

Expand Down
2 changes: 1 addition & 1 deletion web/screens/Settings/MyModels/MyModelList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { memo, useState } from 'react'

import { Model } from '@janhq/core'
import { Badge, Button, Tooltip, useClickOutside } from '@janhq/joi'
import { useAtom, useAtomValue } from 'jotai'
import { useAtom } from 'jotai'
import {
MoreVerticalIcon,
PlayIcon,
Expand Down

0 comments on commit 0dd2519

Please sign in to comment.