mirror of
https://github.com/SigNoz/signoz.git
synced 2026-09-21 10:50:41 +01:00
* fix: log details filters use data types from log data response as primary data type * chore: added test cases * test: add comprehensive unit tests for chooseAutocompleteFromCustomValue function * fix: added datatypes to util and test cases * fix: added new tests * fix: performance optimizations in log details view json field rendering * fix: fixed import failing tests * fix: added default html rendering field in body field in log details * fix: fixed eslint errors * chore: moved hook to a new file and renamed a state
103 lines
2.6 KiB
TypeScript
103 lines
2.6 KiB
TypeScript
/* eslint-disable sonarjs/no-duplicate-string */
|
|
import { isEmpty } from 'lodash-es';
|
|
import { useEffect, useRef, useState } from 'react';
|
|
|
|
import { jsonToDataNodes, recursiveParseJSON } from '../utils';
|
|
|
|
// Hook for async JSON processing
|
|
const useAsyncJSONProcessing = (
|
|
value: string,
|
|
shouldProcess: boolean,
|
|
): {
|
|
isLoading: boolean;
|
|
treeData: any[] | null;
|
|
error: string | null;
|
|
} => {
|
|
const [jsonState, setJsonState] = useState<{
|
|
isLoading: boolean;
|
|
treeData: any[] | null;
|
|
error: string | null;
|
|
}>({
|
|
isLoading: false,
|
|
treeData: null,
|
|
error: null,
|
|
});
|
|
|
|
const processingRef = useRef<boolean>(false);
|
|
|
|
// eslint-disable-next-line sonarjs/cognitive-complexity
|
|
useEffect((): (() => void) => {
|
|
if (!shouldProcess || processingRef.current) {
|
|
return (): void => {};
|
|
}
|
|
|
|
processingRef.current = true;
|
|
setJsonState({ isLoading: true, treeData: null, error: null });
|
|
|
|
// Option 1: Using setTimeout for non-blocking processing
|
|
const processAsync = (): void => {
|
|
setTimeout(() => {
|
|
try {
|
|
const parsedBody = recursiveParseJSON(value);
|
|
if (!isEmpty(parsedBody)) {
|
|
const treeData = jsonToDataNodes(parsedBody);
|
|
setJsonState({ isLoading: false, treeData, error: null });
|
|
} else {
|
|
setJsonState({ isLoading: false, treeData: null, error: null });
|
|
}
|
|
} catch (error) {
|
|
setJsonState({
|
|
isLoading: false,
|
|
treeData: null,
|
|
error: error instanceof Error ? error.message : 'Parsing failed',
|
|
});
|
|
} finally {
|
|
processingRef.current = false;
|
|
}
|
|
}, 0);
|
|
};
|
|
|
|
// Option 2: Using requestIdleCallback for better performance
|
|
const processWithIdleCallback = (): void => {
|
|
if ('requestIdleCallback' in window) {
|
|
requestIdleCallback(
|
|
// eslint-disable-next-line sonarjs/no-identical-functions
|
|
(): void => {
|
|
try {
|
|
const parsedBody = recursiveParseJSON(value);
|
|
if (!isEmpty(parsedBody)) {
|
|
const treeData = jsonToDataNodes(parsedBody);
|
|
setJsonState({ isLoading: false, treeData, error: null });
|
|
} else {
|
|
setJsonState({ isLoading: false, treeData: null, error: null });
|
|
}
|
|
} catch (error) {
|
|
setJsonState({
|
|
isLoading: false,
|
|
treeData: null,
|
|
error: error instanceof Error ? error.message : 'Parsing failed',
|
|
});
|
|
} finally {
|
|
processingRef.current = false;
|
|
}
|
|
},
|
|
{ timeout: 1000 },
|
|
);
|
|
} else {
|
|
processAsync();
|
|
}
|
|
};
|
|
|
|
processWithIdleCallback();
|
|
|
|
// Cleanup function
|
|
return (): void => {
|
|
processingRef.current = false;
|
|
};
|
|
}, [value, shouldProcess]);
|
|
|
|
return jsonState;
|
|
};
|
|
|
|
export default useAsyncJSONProcessing;
|