Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
3.1 kB
4
Indexable
Never
import { useState, useCallback } from 'react';
import { useFocusEffect } from '@react-navigation/native';
import { useTranslation } from 'react-i18next';
import isEmpty from 'lodash/isEmpty';

import Matriks from '../../../components/Matriks/module';
import { indicatorTypeDisplayI18n } from '../../../features/functions';

type Stat = {
  description?: string;
  key?: string;
  priceChange?: number;
  value?: number;
  last?: number;
  volume?: number;
};

type SymbolData = {
  symbolDesc: string;
  symbolCode: string;
  difference: number;
  value: number;
  showVolume: boolean;
};

const useMatriksStats = (selectedTabIndex: number) => {
  const [data, setData] = useState<SymbolData[]>([]);
  const [loading, setLoading] = useState(true);
  const i18n = useTranslation();

  useFocusEffect(
    useCallback(() => {
      setLoading(true);

      Matriks.subscribeStats(selectedTabIndex, (response: { stats?: Stat[] }) => {
        if (!isEmpty(response?.stats)) {
          const updatedData = response?.stats.map((item: Stat) => {
            const isVolumeTab =
              selectedTabIndex === 2 || selectedTabIndex === 5 || selectedTabIndex === 8 || selectedTabIndex === 11;
            if (selectedTabIndex > 5) {
              const symbolData: SymbolData = isVolumeTab
                ? {
                    symbolDesc: item.description || '',
                    symbolCode: item.key || '',
                    difference: item.priceChange || 0,
                    value: item.volume || 0,
                    showVolume: true,
                  }
                : {
                    symbolDesc: item.description || '',
                    symbolCode: item.key || '',
                    difference: item.priceChange || 0,
                    value: item.last || 0,
                    showVolume: false,
                  };

              return {
                symbol: symbolData,
                indicators: indicatorTypeDisplayI18n(i18n, symbolData, {}, true),
              };
            } else {
              const symbolData: SymbolData = isVolumeTab
                ? {
                    symbolDesc: item.description || '',
                    symbolCode: item.key || '',
                    difference: item.priceChange || 0,
                    value: item.value || 0,
                    showVolume: true,
                  }
                : {
                    symbolDesc: item.description || '',
                    symbolCode: item.key || '',
                    difference: item.value || 0,
                    value: item.last || 0,
                    showVolume: false,
                  };

              return {
                symbol: symbolData,
                indicators: indicatorTypeDisplayI18n(i18n, symbolData, {}, true),
              };
            }
          });
          setData(updatedData);
        }

        setLoading(false);
      });

      return Matriks.unsubscribeStats;
    }, [selectedTabIndex]),
  );

  return { data, loading };
};

export default useMatriksStats;