@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
97 lines (96 loc) • 3.73 kB
JavaScript
"use client";
import { useCallback, useContext, useMemo, useReducer, useRef } from 'react';
import pointer from "../../utils/json-pointer/index.js";
import { createReferenceKey, useSharedState } from "../../../../shared/helpers/useSharedState.js";
import useMountEffect from "../../../../shared/helpers/useMountEffect.js";
import DataContext from "../../DataContext/Context.js";
import { structuredClone } from "../../../../shared/helpers/structuredClone.js";
export default function useData(id = undefined, initialData = undefined) {
const sharedDataRef = useRef(null);
const sharedAttachmentsRef = useRef(null);
const [, forceUpdate] = useReducer(() => ({}), {});
sharedDataRef.current = useSharedState(id, initialData, forceUpdate);
sharedAttachmentsRef.current = useSharedState(createReferenceKey(id, 'attachments'), {
rerenderUseDataHook: forceUpdate
});
const dataContext = useContext(DataContext);
if (!id) {
if (!dataContext.hasContext) {
throw new Error('useData needs to run inside DataContext (Form.Handler) or have a valid id');
}
sharedDataRef.current.data = dataContext.data;
sharedAttachmentsRef.current.data.filterDataHandler = dataContext.filterDataHandler;
}
const updateDataValue = dataContext?.updateDataValue;
const setData = dataContext?.setData;
const getExistingData = useCallback(() => {
return structuredClone(sharedAttachmentsRef.current?.data?.internalDataRef?.current || sharedDataRef.current.data || {});
}, []);
const set = useCallback(newData => {
if (id) {
sharedDataRef.current.update(newData);
} else {
setData?.(newData);
}
}, [id, setData]);
const update = useCallback((path, value = undefined) => {
const existingData = getExistingData();
const existingValue = pointer.has(existingData, path) ? pointer.get(existingData, path) : undefined;
const newValue = typeof value === 'function' ? value(existingValue) : value;
if (newValue !== existingValue) {
pointer.set(existingData, path, newValue);
if (id) {
sharedDataRef.current.extend(existingData);
} else {
updateDataValue?.(path, newValue);
}
}
}, [getExistingData, id, updateDataValue]);
const remove = useCallback(path => {
const existingData = getExistingData();
if (pointer.has(existingData, path)) {
pointer.remove(existingData, path);
if (id) {
sharedDataRef.current.set(existingData);
} else {
setData?.(existingData);
}
}
}, [getExistingData, id, setData]);
const reduceToVisibleFields = useCallback((data, options = {}) => {
if (id) {
return sharedAttachmentsRef.current.data?.visibleDataHandler?.(data, options);
}
return dataContext?.visibleDataHandler?.(data, options);
}, [dataContext, id]);
const filterData = useCallback((filter, data = sharedDataRef.current.data) => {
if (id) {
return sharedAttachmentsRef.current.data?.filterDataHandler?.(data, filter);
}
return dataContext?.filterDataHandler?.(data, filter);
}, [dataContext, id]);
const getValue = useCallback(path => {
if (pointer.has(sharedDataRef.current.data, path)) {
return pointer.get(sharedDataRef.current.data, path);
}
return undefined;
}, []);
useMountEffect(() => {
if (id && !sharedDataRef.current.hadInitialData && initialData) {
sharedDataRef.current.extend(initialData);
}
});
const {
data
} = sharedDataRef.current;
return useMemo(() => ({
data,
remove,
update,
set,
getValue,
reduceToVisibleFields,
filterData
}), [data, remove, update, set, getValue, reduceToVisibleFields, filterData]);
}
//# sourceMappingURL=useData.js.map