Files
cloudbeaver/common-react/@dbeaver/react-data-grid/src/useGridReactiveValue.ts
T
b8952c87b8 CB-6308 fix: grid auto-size (#3298)
* CB-6308 fix: grid auto-size

* CB-6308 fix: grid auto size

* CB-6308 fix: icons sizing in grid

* CB-6308 fix: grid header

* CB-6308 fix: types

* fix: min-width for columns

---------

Co-authored-by: mr-anton-t <42037741+mr-anton-t@users.noreply.github.com>
Co-authored-by: Daria Marutkina <125263541+dariamarutkina@users.noreply.github.com>
2025-03-28 22:52:46 +08:00

37 lines
1.1 KiB
TypeScript

import { useMemo, useSyncExternalStore } from 'react';
import type { IGridReactiveValue } from './IGridReactiveValue.js';
export function useGridReactiveValue<P extends IGridReactiveValue<any, any[]> | undefined>(
value: P,
...args: P extends IGridReactiveValue<any, infer T> ? T : never[]
): P extends IGridReactiveValue<infer T, any[]> ? T : undefined {
const mappedValue = useMemo(
() => ({
cache: null as any,
cacheInvalidated: true,
subscribe: (onValueChange: () => void) => {
if (value) {
return value.subscribe(
() => {
mappedValue.cacheInvalidated = true;
return onValueChange();
},
...args,
);
}
return () => {};
},
get: () => {
if (mappedValue.cacheInvalidated) {
mappedValue.cache = value?.get(...args);
mappedValue.cacheInvalidated = false;
}
return mappedValue.cache;
},
}),
[value, ...args],
);
return useSyncExternalStore(mappedValue.subscribe, mappedValue.get);
}