Skip to content

Commit

Permalink
update(console): table list content same with viewport to avoid scrol…
Browse files Browse the repository at this point in the history
…l be blocking, add useCached hook (#2494)
  • Loading branch information
waynelwz authored Jul 11, 2023
1 parent 0ef3a60 commit db0884c
Show file tree
Hide file tree
Showing 8 changed files with 294 additions and 245 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,7 @@ import { RecordListSchemaT, RecordSchemaT } from '../types'
import { SwType } from '../model'
import _ from 'lodash'
import { DataTypes } from '../constants'

class LRUCache<Key, Value> {
private maxSize: number

private cache: Map<Key, Value>

private accessedKeys: Set<Key>

constructor(maxSize: number) {
this.maxSize = maxSize
this.cache = new Map<Key, Value>()
this.accessedKeys = new Set<Key>()
}

has(key: Key): boolean {
return this.cache.has(key)
}

get(key: Key): Value | undefined {
if (this.cache.has(key)) {
// Move the key to the front of the accessedKeys set
this.accessedKeys.delete(key)
this.accessedKeys.add(key)
return this.cache.get(key)
}
return undefined
}

put(key: Key, value: Value) {
if (this.cache.has(key)) {
// Move the key to the front of the accessedKeys set
this.accessedKeys.delete(key)
this.accessedKeys.add(key)
this.cache.set(key, value)
} else {
if (this.cache.size >= this.maxSize) {
// Remove the least recently used key from the cache and accessedKeys set
const lruKey = this.accessedKeys.values().next().value
this.accessedKeys.delete(lruKey)
this.cache.delete(lruKey)
}
// Add the new key to the front of the accessedKeys set
this.accessedKeys.add(key)
this.cache.set(key, value)
}
}
}
import { LRUCache } from '../utils'

export function useDatastoreWithSchema(records: RecordListSchemaT, columnTypes: ColumnSchemaDesc[]) {
const getSchema = React.useCallback(
Expand Down
47 changes: 47 additions & 0 deletions console/packages/starwhale-core/src/datastore/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,50 @@ export const isSearchColumns = (v: string) => {

return true
}

export class LRUCache<Key, Value> {
private maxSize: number

private cache: Map<Key, Value>

private accessedKeys: Set<Key>

constructor(maxSize: number) {
this.maxSize = maxSize
this.cache = new Map<Key, Value>()
this.accessedKeys = new Set<Key>()
}

has(key: Key): boolean {
return this.cache.has(key)
}

get(key: Key): Value | undefined {
if (this.cache.has(key)) {
// Move the key to the front of the accessedKeys set
this.accessedKeys.delete(key)
this.accessedKeys.add(key)
return this.cache.get(key)
}
return undefined
}

put(key: Key, value: Value) {
if (this.cache.has(key)) {
// Move the key to the front of the accessedKeys set
this.accessedKeys.delete(key)
this.accessedKeys.add(key)
this.cache.set(key, value)
} else {
if (this.cache.size >= this.maxSize) {
// Remove the least recently used key from the cache and accessedKeys set
const lruKey = this.accessedKeys.values().next().value
this.accessedKeys.delete(lruKey)
this.cache.delete(lruKey)
}
// Add the new key to the front of the accessedKeys set
this.accessedKeys.add(key)
this.cache.set(key, value)
}
}
}
51 changes: 51 additions & 0 deletions console/packages/starwhale-core/src/utils/useCachedMap.tsx
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
16 changes: 10 additions & 6 deletions console/src/components/Card/index.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,21 @@
font-variant: tabular-nums;
line-height: 1.5715;
list-style: none;
-webkit-font-feature-settings: "tnum","tnum";
font-feature-settings: "tnum","tnum";
-webkit-font-feature-settings: 'tnum', 'tnum';
font-feature-settings: 'tnum', 'tnum';
position: relative;
border-radius: 2px;
-webkit-transition: transform 270ms cubic-bezier(0.1, 0.9, 0.2, 1),opacity 300ms cubic-bezier(0.1, 0.9, 0.2, 1) 0.05s,-webkit-transform 300ms cubic-bezier(0.1, 0.9, 0.2, 1);
transition: transform 270ms cubic-bezier(0.1, 0.9, 0.2, 1),opacity 300ms cubic-bezier(0.1, 0.9, 0.2, 1) 0.05s,-webkit-transform 300ms cubic-bezier(0.1, 0.9, 0.2, 1);
transform: translate3d(0,48px,0);
-webkit-transition: transform 270ms cubic-bezier(0.1, 0.9, 0.2, 1),
opacity 300ms cubic-bezier(0.1, 0.9, 0.2, 1) 0.05s, -webkit-transform 300ms cubic-bezier(0.1, 0.9, 0.2, 1);
transition: transform 270ms cubic-bezier(0.1, 0.9, 0.2, 1), opacity 300ms cubic-bezier(0.1, 0.9, 0.2, 1) 0.05s,
-webkit-transform 300ms cubic-bezier(0.1, 0.9, 0.2, 1);
transform: translate3d(0, 48px, 0);
transition-delay: 0.4s;
margin-bottom: 10px;
display: flex;
border-radius: 8px;
flex-direction: column;
overflow: hidden;

.card {
box-shadow: none;
Expand All @@ -29,7 +32,7 @@
.card:nth-child(#{$i}) {
transition-delay: 0.05s * ($i - 1);
}
}
}

.cardHeadWrapper {
// padding: 0 18px;
Expand Down Expand Up @@ -62,6 +65,7 @@
flex: 1;
display: flex;
flex-direction: column;
overflow: auto;
}

.cardHeadTail {
Expand Down
2 changes: 1 addition & 1 deletion console/src/components/Table/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export default function Table({ isLoading, columns, data, overrides, paginationP
style={{
display: 'flex',
alignItems: 'center',
marginTop: 20,
marginTop: 5,
}}
>
<div
Expand Down
1 change: 1 addition & 0 deletions console/src/pages/BaseLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const useMainStyles = createUseStyles({
display: 'flex',
flexDirection: 'column',
flexGrow: 1,
overflow: 'auto',
},
})

Expand Down
6 changes: 5 additions & 1 deletion console/src/pages/Evaluation/EvaluationListCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,11 @@ export default function EvaluationListCard() {
return CustomColumn<RecordAttr, any>({
...column,
renderCell: ({ value }) => {
return <span title={value.toString()}>{formatTimestampDateTime(value.value)}</span>
return (
<span className='line-clamp line-clamp-2' title={value.toString()}>
{formatTimestampDateTime(value.value)}
</span>
)
},
})
}
Expand Down
Loading

0 comments on commit db0884c

Please sign in to comment.