Skip to content

Commit

Permalink
fix: local cache
Browse files Browse the repository at this point in the history
  • Loading branch information
GZTimeWalker committed Feb 9, 2024
1 parent 6c013b6 commit 2fb6325
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 28 deletions.
3 changes: 1 addition & 2 deletions src/GZCTF/ClientApp/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { useRoutes } from 'react-router-dom'
import { SWRConfig } from 'swr'
import routes from '~react-pages'
import { ThemeOverride } from '@Utils/ThemeOverride'
import { useBanner, useLocalStorageCache } from '@Utils/useConfig'
import { useBanner, localCacheProvider } from '@Utils/useConfig'
import { fetcher } from '@Api'

export const App: FC = () => {
Expand All @@ -31,7 +31,6 @@ export const App: FC = () => {
useBanner()

const { t } = useTranslation()
const { localCacheProvider } = useLocalStorageCache()

return (
<ColorSchemeProvider colorScheme={colorScheme} toggleColorScheme={toggleColorScheme}>
Expand Down
3 changes: 1 addition & 2 deletions src/GZCTF/ClientApp/src/components/AppHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { useTranslation } from 'react-i18next'
import { Link, useLocation, useNavigate } from 'react-router-dom'
import LogoHeader from '@Components/LogoHeader'
import { useIsMobile } from '@Utils/ThemeOverride'
import { useLocalStorageCache } from '@Utils/useConfig'
import { clearLocalCache } from '@Utils/useConfig'
import { useLoginOut, useUser } from '@Utils/useUser'

const useHeaderStyles = createStyles((theme) => ({
Expand All @@ -34,7 +34,6 @@ const AppHeader: FC = () => {
const navigate = useNavigate()

const { colorScheme, toggleColorScheme } = useMantineColorScheme()
const { clearLocalCache } = useLocalStorageCache()
const { user, error } = useUser()

const logout = useLoginOut()
Expand Down
3 changes: 1 addition & 2 deletions src/GZCTF/ClientApp/src/components/AppNavbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { useTranslation } from 'react-i18next'
import { Link, useLocation, useNavigate } from 'react-router-dom'
import MainIcon from '@Components/icon/MainIcon'
import { LanguageMap, SupportedLanguages, useLanguage } from '@Utils/I18n'
import { useLocalStorageCache } from '@Utils/useConfig'
import { clearLocalCache } from '@Utils/useConfig'
import { useLoginOut, useUser } from '@Utils/useUser'
import { Role } from '@Api'

Expand Down Expand Up @@ -123,7 +123,6 @@ const AppNavbar: FC = () => {
const { colorScheme, toggleColorScheme } = useMantineColorScheme()

const logout = useLoginOut()
const { clearLocalCache } = useLocalStorageCache()
const { user, error } = useUser()
const { t } = useTranslation()
const { setLanguage, supportedLanguages } = useLanguage()
Expand Down
40 changes: 18 additions & 22 deletions src/GZCTF/ClientApp/src/utils/useConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,28 +102,24 @@ export const useBanner = () => {
}, [])
}

export const useLocalStorageCache = () => {
const cacheKey = 'gzctf-cache'

const mapRef = useRef(
new Map(JSON.parse(LZString.decompress(localStorage.getItem(cacheKey) || '') || '[]'))
)

const saveCache = () => {
const appCache = LZString.compress(JSON.stringify(Array.from(mapRef.current.entries())))
localStorage.setItem(cacheKey, appCache)
}

const localCacheProvider = () => {
window.addEventListener('beforeunload', saveCache)
return mapRef.current as Cache
}
const cacheKey = 'gzctf-cache'
const cacheMap = new Map(
JSON.parse(LZString.decompress(localStorage.getItem(cacheKey) || '') || '[]')
)

const saveCache = () => {
const appCache = LZString.compress(JSON.stringify(Array.from(cacheMap.entries())))
localStorage.setItem(cacheKey, appCache)
}

const clearLocalCache = () => {
window.removeEventListener('beforeunload', saveCache)
localStorage.removeItem('gzctf-cache')
window.location.reload()
}
export const localCacheProvider = () => {
window.addEventListener('beforeunload', saveCache, true)
return cacheMap as Cache
}

return { localCacheProvider, clearLocalCache }
export const clearLocalCache = () => {
window.removeEventListener('beforeunload', saveCache, true)
localStorage.removeItem('gzctf-cache')
cacheMap.clear()
window.location.reload()
}

0 comments on commit 2fb6325

Please sign in to comment.