All files / src/components/Chatbot/Result StatisticResult.tsx

100% Statements 87/87
100% Branches 37/37
100% Functions 1/1
100% Lines 87/87

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124  1x         1x     1x     1x 1x   1x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x                                     26x 26x 26x 23x 16x 16x 16x 16x 16x 14x 14x 14x 14x 2x 16x 15x 15x 15x     15x 15x 15x 15x 15x 15x 15x   15x 15x 8x 7x 4x 15x 1x 1x   14x 14x 14x 14x 14x 6x 8x 2x 6x 14x 14x 4x 10x 14x 15x 15x 16x 16x 16x 16x 16x 23x 26x   26x 26x 26x 26x 2x 2x 2x   24x   26x   26x  
// Libs
import { useEffect } from 'react';
 
// Types
import { ArgsType } from '@/hooks/chat/useChatbotActions';
import { FilterType } from '@/providers/statistic';
import { ChartKey, ChartMode, CHATBOT_STATUS } from '@/types';
 
// Constants
import { CHATBOT_MESSAGES } from '@shared/constants';
 
// Components
import { InterruptedMessage } from '../InterruptedMessage';
import { ProcessingIndicator } from '../ProcessingIndicator';
 
export const StatisticResult = ({
  args,
  response,
  filter,
  onChangeFilter,
  setIsProcessing,
  time,
  status,
  isExecuting,
  onSaveExecuting,
  onStopGeneration,
  actionId,
  requestId,
  saveActionId,
  savePrevActionId,
}: {
  args: ArgsType;
  filter: FilterType;
  result?: string;
  time: string;
  isExecuting: boolean;
  isNew?: boolean;
  isChatLoading?: boolean;
  isProcessing?: boolean;
  response?: string;
  onSaveExecuting?: (value: boolean) => void;
  onChangeFilter?: (filter: FilterType) => void;
  setIsProcessing: (value: boolean) => void;
  onStopGeneration?: () => void;
  status: string;
  saveActionId?: (value: string) => void;
  savePrevActionId?: (value: string) => void;
  actionId?: string;
  requestId?: string;
}) => {
  useEffect(() => {
    if (status === CHATBOT_STATUS.COMPLETE) {
      if (isExecuting && actionId && actionId === requestId) {
        const { mode, subject, nestedMode } = args;
        const trimTime = time?.replace(/[;-]/g, ',');
        const normalizedMode = mode?.toLowerCase() as ChartMode;
        const normalizedTime =
          normalizedMode === ChartMode.Week
            ? trimTime
                .split(',')
                .filter((_, index) => index % 2 === 0)
                .join(',')
            : trimTime;
        if (mode || time || subject || nestedMode) {
          const normalizedTotalMode = nestedMode?.toLowerCase() as ChartMode;
          const normalizedSubject = (
            subject === 'SLOTS' ? ChartKey.SLOT : subject?.toLowerCase()
          ) as ChartKey;
 
          const hasModeChange = mode && filter?.mode !== normalizedMode;
          const hasTotalModeChange =
            nestedMode && filter?.totalMode !== normalizedTotalMode;
          const hasTimeChange =
            trimTime && filter?.timeRange !== normalizedTime;
          const hasSubjectChange =
            subject && filter?.comparisonKey !== normalizedSubject;
 
          if (
            !hasModeChange &&
            !hasTotalModeChange &&
            !hasTimeChange &&
            !hasSubjectChange
          ) {
            return;
          }
 
          onChangeFilter?.({
            ...filter,
            shouldSyncChangeMode: false,
            mode: hasModeChange ? normalizedMode : filter.mode,
            totalMode: hasTotalModeChange
              ? normalizedTotalMode
              : hasModeChange
                ? normalizedMode
                : filter.totalMode,
            timeRange: hasTimeChange ? normalizedTime : filter.timeRange,
            comparisonKey: hasSubjectChange
              ? normalizedSubject
              : filter.comparisonKey,
          } as FilterType);
        }
        onSaveExecuting?.(false);
        setIsProcessing(false);
        onStopGeneration?.();
        saveActionId?.('');
        savePrevActionId?.(requestId);
      }
    }
  }, [status, isExecuting, actionId, requestId]);
 
  return (
    <>
      {status !== CHATBOT_STATUS.COMPLETE && <ProcessingIndicator />}
      {status === CHATBOT_STATUS.COMPLETE && response ? (
        <InterruptedMessage
          message={CHATBOT_MESSAGES.FETCH_STATISTIC_SUCCESSFULLY}
        />
      ) : (
        <></>
      )}
    </>
  );
};