-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update(console): table list content same with viewport to avoid scrol…
…l be blocking, add useCached hook (#2494)
- Loading branch information
Showing
8 changed files
with
294 additions
and
245 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
console/packages/starwhale-core/src/utils/useCachedMap.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import React from 'react' | ||
import { useLocalStorage } from 'react-use' | ||
|
||
const defaultMap = {} | ||
const defaultKeys: any[] = [] | ||
|
||
function useCachedMap(storeKey = 'cache', maxCacheSize = 100) { | ||
const [cachedMap, setCachedMap] = useLocalStorage<Record<string, DOMRect>>(`cache-${storeKey}`, defaultMap) | ||
const [cachedKeys, setCachedKeys] = useLocalStorage<string[]>(`cache-keys-${storeKey}`, defaultKeys) | ||
|
||
const has = React.useCallback( | ||
(key: string) => { | ||
return cachedMap?.[key] !== undefined | ||
}, | ||
[cachedMap] | ||
) | ||
const get = React.useCallback( | ||
(key: string) => { | ||
return cachedMap?.[key] | ||
}, | ||
[cachedMap] | ||
) | ||
const put = React.useCallback( | ||
(key: string, value: DOMRect) => { | ||
if (!cachedMap || !cachedKeys) return | ||
|
||
cachedMap[key] = value | ||
if (cachedKeys.length > maxCacheSize) { | ||
const first = cachedKeys.shift() | ||
if (first) delete cachedMap[first] | ||
} else { | ||
cachedKeys.push(key) | ||
} | ||
|
||
setCachedMap({ ...cachedMap }) | ||
setCachedKeys([...cachedKeys]) | ||
}, | ||
[cachedMap, cachedKeys, maxCacheSize, setCachedMap, setCachedKeys] | ||
) | ||
|
||
return React.useMemo(() => { | ||
return { | ||
has, | ||
get, | ||
put, | ||
} | ||
}, [has, get, put]) | ||
} | ||
|
||
export { useCachedMap } | ||
export default useCachedMap |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.