useLazyKVMap.js 1018 B

1234567891011121314151617181920212223242526272829
  1. import * as React from 'react';
  2. const useLazyKVMap = (data, childrenColumnName, getRowKey) => {
  3. const mapCacheRef = React.useRef({});
  4. function getRecordByKey(key) {
  5. var _a;
  6. if (!mapCacheRef.current || mapCacheRef.current.data !== data || mapCacheRef.current.childrenColumnName !== childrenColumnName || mapCacheRef.current.getRowKey !== getRowKey) {
  7. const kvMap = new Map();
  8. function dig(records) {
  9. records.forEach((record, index) => {
  10. const rowKey = getRowKey(record, index);
  11. kvMap.set(rowKey, record);
  12. if (record && typeof record === 'object' && childrenColumnName in record) {
  13. dig(record[childrenColumnName] || []);
  14. }
  15. });
  16. }
  17. dig(data);
  18. mapCacheRef.current = {
  19. data,
  20. childrenColumnName,
  21. kvMap,
  22. getRowKey
  23. };
  24. }
  25. return (_a = mapCacheRef.current.kvMap) === null || _a === void 0 ? void 0 : _a.get(key);
  26. }
  27. return [getRecordByKey];
  28. };
  29. export default useLazyKVMap;