|
| 1 | +import clsx from 'clsx'; |
| 2 | +import KeepAlive, { useKeepAliveRef } from 'keepalive-for-react'; |
| 3 | + |
| 4 | +import { getReloadFlag } from '@/store/slice/app'; |
| 5 | +import { getRemoveCacheKey, selectCacheRoutes } from '@/store/slice/route'; |
| 6 | +import { getThemeSettings } from '@/store/slice/theme'; |
| 7 | + |
| 8 | +interface Props { |
| 9 | + /** Show padding for content */ |
| 10 | + closePadding?: boolean; |
| 11 | +} |
| 12 | + |
| 13 | +const useGetCacheKey = () => { |
| 14 | + const location = useLocation(); |
| 15 | + |
| 16 | + const cacheKey = (location.pathname + location.search).slice(1).split('/').join('_'); |
| 17 | + |
| 18 | + return cacheKey; |
| 19 | +}; |
| 20 | + |
| 21 | +const GlobalContent: FC<Props> = memo(({ closePadding }) => { |
| 22 | + const currentOutlet = useOutlet(); |
| 23 | + |
| 24 | + const aliveRef = useKeepAliveRef(); |
| 25 | + |
| 26 | + const removeCacheKey = useAppSelector(getRemoveCacheKey); |
| 27 | + |
| 28 | + const cacheKeys = useAppSelector(selectCacheRoutes); |
| 29 | + |
| 30 | + const reload = useAppSelector(getReloadFlag); |
| 31 | + |
| 32 | + const cacheKey = useGetCacheKey(); |
| 33 | + |
| 34 | + // const themeSetting = useAppSelector(getThemeSettings); |
| 35 | + |
| 36 | + // const transitionName = themeSetting.page.animate ? themeSetting.page.animateMode : ''; |
| 37 | + |
| 38 | + useUpdateEffect(() => { |
| 39 | + if (!aliveRef.current || !removeCacheKey) return; |
| 40 | + |
| 41 | + aliveRef.current.destroy(removeCacheKey); |
| 42 | + }, [removeCacheKey]); |
| 43 | + |
| 44 | + useUpdateEffect(() => { |
| 45 | + aliveRef.current?.refresh(); |
| 46 | + }, [reload]); |
| 47 | + |
| 48 | + return ( |
| 49 | + <div className={clsx('h-full flex-grow bg-layout', { 'p-16px': !closePadding })}> |
| 50 | + <KeepAlive |
| 51 | + transition |
| 52 | + activeCacheKey={cacheKey} |
| 53 | + aliveRef={aliveRef} |
| 54 | + duration={300} |
| 55 | + include={cacheKeys} |
| 56 | + > |
| 57 | + {currentOutlet} |
| 58 | + </KeepAlive> |
| 59 | + </div> |
| 60 | + ); |
| 61 | +}); |
| 62 | + |
| 63 | +export default GlobalContent; |
0 commit comments