change to use refs

This commit is contained in:
Nate Kelley 2025-02-04 19:55:15 -07:00
parent 535b6658a9
commit 420fe8f90f
No known key found for this signature in database
GPG Key ID: FD90372AB8D98B4F
5 changed files with 55 additions and 26 deletions

View File

@ -1,4 +1,4 @@
import React, { PropsWithChildren, useCallback, useState } from 'react'; import React, { PropsWithChildren, useCallback, useRef, useState, useTransition } from 'react';
import { import {
createContext, createContext,
ContextSelector, ContextSelector,
@ -9,27 +9,23 @@ import { useBusterWebSocket } from '../BusterWebSocket';
import type { BusterMetricData } from '../Metrics'; import type { BusterMetricData } from '../Metrics';
import { MetricEvent_fetchingData } from '@/api/buster_socket/metrics/eventsInterfaces'; import { MetricEvent_fetchingData } from '@/api/buster_socket/metrics/eventsInterfaces';
import { MOCK_DATA } from './MOCK_DATA'; import { MOCK_DATA } from './MOCK_DATA';
import { DEFAULT_MESSAGE_DATA } from './config';
const DEFAULT_MESSAGE_DATA: BusterMetricData = {
fetched: false,
fetching: false,
fetchedAt: 0,
data_metadata: null,
code: null,
error: null
};
const useMetricData = () => { const useMetricData = () => {
const busterSocket = useBusterWebSocket(); const busterSocket = useBusterWebSocket();
const [isPending, startTransition] = useTransition();
const [metricData, setMetricData] = useState<Record<string, BusterMetricData>>({}); const metricDataRef = useRef<Record<string, BusterMetricData>>({});
const _setMetricData = useMemoizedFn( const _setMetricData = useMemoizedFn(
(metricId: string, newMetricData: Partial<BusterMetricData>) => { (metricId: string, newMetricData: Partial<BusterMetricData>) => {
setMetricData((prev) => ({ metricDataRef.current = {
...prev, ...metricDataRef.current,
[metricId]: { ...prev[metricId], ...newMetricData } [metricId]: { ...metricDataRef.current[metricId], ...newMetricData }
})); };
startTransition(() => {
//trigger re-render
});
} }
); );
@ -90,6 +86,7 @@ const useMetricData = () => {
const fetchDataByMetricId = useMemoizedFn(async ({ metricId }: { metricId: string }) => { const fetchDataByMetricId = useMemoizedFn(async ({ metricId }: { metricId: string }) => {
const selectedMetricData = getMetricData(metricId); const selectedMetricData = getMetricData(metricId);
if (selectedMetricData?.fetching || selectedMetricData?.fetched) { if (selectedMetricData?.fetching || selectedMetricData?.fetched) {
return; return;
} }
@ -121,17 +118,17 @@ const useMetricData = () => {
const getMetricData = useCallback( const getMetricData = useCallback(
(metricId: string | undefined) => { (metricId: string | undefined) => {
if (metricId && metricData[metricId]) { if (metricId && metricDataRef.current[metricId]) {
return metricData[metricId]; return metricDataRef.current[metricId];
} }
return DEFAULT_MESSAGE_DATA; return DEFAULT_MESSAGE_DATA;
}, },
[metricData] [isPending]
); );
const getAllMetricDataMemoized = useMemoizedFn( const getAllMetricDataMemoized = useMemoizedFn(
(metricId: string): BusterMetricData | undefined => { (metricId: string): BusterMetricData | undefined => {
return metricData[metricId]; return metricDataRef.current[metricId];
} }
); );
@ -143,7 +140,7 @@ const useMetricData = () => {
}); });
return { return {
metricData, metricData: metricDataRef.current,
onSetMetricDataCode, onSetMetricDataCode,
onSetMetricData, onSetMetricData,
fetchDataByMetricId, fetchDataByMetricId,

View File

@ -0,0 +1,10 @@
import type { BusterMetricData } from '../Metrics';
export const DEFAULT_MESSAGE_DATA: BusterMetricData = {
fetched: false,
fetching: false,
fetchedAt: 0,
data_metadata: null,
code: null,
error: null
};

View File

@ -292,6 +292,21 @@ export const useBusterMetrics = () => {
onUpdateMetric(upgradedMetric); onUpdateMetric(upgradedMetric);
}); });
const _setLoadingMetric = useMemoizedFn((metricId: string) => {
const metrics = metricsRef.current || {};
metrics[metricId] = resolveEmptyMetric(
{
...metrics[metricId],
fetching: true
},
metricId
);
setMetrics(metrics);
return metrics[metricId];
});
const bulkUpdateMetrics = useMemoizedFn((newMetrics: Record<string, IBusterMetric>) => { const bulkUpdateMetrics = useMemoizedFn((newMetrics: Record<string, IBusterMetric>) => {
metricsRef.current = { metricsRef.current = {
...metricsRef.current, ...metricsRef.current,
@ -307,7 +322,6 @@ export const useBusterMetrics = () => {
...currentMetric, ...currentMetric,
...newMetricPartial ...newMetricPartial
}; };
setMetrics({ setMetrics({
[metricId]: newMetric [metricId]: newMetric
}); });
@ -359,8 +373,7 @@ export const useBusterMetrics = () => {
...currentMetric.chart_config, ...currentMetric.chart_config,
...chartConfig ...chartConfig
}; };
onUpdateMetric({
return onUpdateMetric({
id: metricId, id: metricId,
chart_config: newChartConfig chart_config: newChartConfig
}); });
@ -495,10 +508,12 @@ export const useBusterMetrics = () => {
const { password } = getAssetPassword(metricId); const { password } = getAssetPassword(metricId);
const foundMetric: undefined | IBusterMetric = metricsRef.current[metricId]; const foundMetric: undefined | IBusterMetric = metricsRef.current[metricId];
if (foundMetric && (foundMetric.fetching || foundMetric.fetched)) { if (foundMetric && (foundMetric?.fetching || foundMetric?.fetched)) {
return foundMetric; return foundMetric;
} }
_setLoadingMetric(metricId);
//TODO: remove this //TODO: remove this
setTimeout(() => { setTimeout(() => {
_onGetMetricState({ _onGetMetricState({

View File

@ -16,7 +16,14 @@ const MOCK_CHART_CONFIG: IBusterMetricChartConfig = {
x: ['date'], x: ['date'],
y: ['sales'], y: ['sales'],
category: [] category: []
// category: ['product'] },
pieChartAxis: {
x: ['product'],
y: ['sales']
},
scatterAxis: {
x: ['date'],
y: ['sales']
} }
}; };

View File

@ -37,7 +37,7 @@ export const resolveEmptyMetric = (
metricId: string metricId: string
): IBusterMetric => { ): IBusterMetric => {
if (!metric || !metric?.id) { if (!metric || !metric?.id) {
return { ...defaultIBusterMetric, id: metricId }; return { ...defaultIBusterMetric, ...metric, id: metricId };
} }
return metric; return metric;
}; };