/ src / utils / dashboardSortUtils.ts
dashboardSortUtils.ts
  1  import { InterestRate } from '@aave/contract-helpers';
  2  import { BorrowAssetsItem } from 'src/modules/dashboard/lists/BorrowAssetsList/types';
  3  import { SupplyAssetsItem } from 'src/modules/dashboard/lists/SupplyAssetsList/types';
  4  
  5  // Sorting keys
  6  import {
  7    ComputedReserveData,
  8    ComputedUserReserveData,
  9  } from '../hooks/app-data-provider/useAppDataProvider';
 10  
 11  // Helpers
 12  export const DASHBOARD_LIST_COLUMN_WIDTHS = {
 13    ASSET: 130,
 14    BUTTONS: 160,
 15    CELL: 130,
 16  };
 17  
 18  // Note: Create a single type that works with all four dashboards list and all 8 list item components
 19  // Each list item may need a combination of a few types but not all, i.e. positions vs assets and supplied vs borrowed
 20  type DashboardReserveData = ComputedUserReserveData &
 21    ComputedReserveData &
 22    BorrowAssetsItem &
 23    SupplyAssetsItem;
 24  
 25  export type DashboardReserve = DashboardReserveData & {
 26    // Additions
 27    borrowRateMode: InterestRate; // for the borrow positions list
 28    // Overrides
 29    reserve: ComputedReserveData;
 30  };
 31  
 32  export const handleSortDashboardReserves = (
 33    sortDesc: boolean,
 34    sortName: string,
 35    sortPosition: string,
 36    positions: DashboardReserve[],
 37    isBorrowedPosition?: boolean
 38  ): DashboardReserve[] => {
 39    if (sortDesc) {
 40      return handleSortDesc(sortName, sortPosition, positions, isBorrowedPosition || false);
 41    } else {
 42      return sortAsc(sortName, sortPosition, positions, isBorrowedPosition || false);
 43    }
 44  };
 45  
 46  const handleSortDesc = (
 47    sortName: string,
 48    sortPosition: string,
 49    positions: DashboardReserve[],
 50    isBorrowedPosition: boolean
 51  ) => {
 52    if (sortName === 'symbol') {
 53      return handleSymbolSort(true, sortPosition, positions);
 54    } else if (sortName === 'usageAsCollateralEnabledOnUser' || sortName === 'debt') {
 55      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
 56      // @ts-ignore
 57      return positions.sort((a, b) => Number(a[sortName]) - Number(b[sortName]));
 58    } else {
 59      if (isBorrowedPosition) {
 60        positions.sort(
 61          (a, b) => Number(b.reserve.variableBorrowAPY) - Number(a.reserve.variableBorrowAPY)
 62        );
 63      }
 64      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
 65      // @ts-ignore
 66      return positions.sort((a, b) => a[sortName] - b[sortName]);
 67    }
 68  };
 69  
 70  const sortAsc = (
 71    sortName: string,
 72    sortPosition: string,
 73    positions: DashboardReserve[],
 74    isBorrowedPosition: boolean
 75  ) => {
 76    if (sortName === 'symbol') {
 77      return handleSymbolSort(false, sortPosition, positions);
 78    } else if (sortName === 'usageAsCollateralEnabledOnUser' || sortName === 'debt') {
 79      // NOTE parse to number for sorting
 80      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
 81      // @ts-ignore
 82      return positions.sort((a, b) => Number(b[sortName]) - Number(a[sortName]));
 83    } else {
 84      // Note because borrow positions have extra logic we need to have this
 85      if (isBorrowedPosition) {
 86        positions.sort(
 87          (a, b) => Number(a.reserve.variableBorrowAPY) - Number(b.reserve.variableBorrowAPY)
 88        );
 89      }
 90      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
 91      // @ts-ignore
 92      return positions.sort((a, b) => b[sortName] - a[sortName]);
 93    }
 94  };
 95  
 96  const handleSymbolSort = (
 97    sortDesc: boolean,
 98    sortPosition: string,
 99    positions: DashboardReserve[]
100  ) => {
101    // NOTE because the data structure is different we need to check for positions(supplied|borrowed)
102    // if position then a.reserve.symbol otherwise a.symbol
103    if (sortDesc) {
104      if (sortPosition === 'position') {
105        return positions.sort((a, b) =>
106          a.reserve.symbol.toUpperCase() < b.reserve.symbol.toUpperCase() ? -1 : 1
107        );
108      }
109      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
110      // @ts-ignore
111      return positions.sort((a, b) => (a.symbol.toUpperCase() < b.symbol.toUpperCase() ? -1 : 1));
112    }
113  
114    if (sortPosition === 'position') {
115      return positions.sort((a, b) =>
116        b.reserve.symbol.toUpperCase() < a.reserve.symbol.toUpperCase() ? -1 : 1
117      );
118    }
119    // eslint-disable-next-line @typescript-eslint/ban-ts-comment
120    // @ts-ignore
121    return positions.sort((a, b) => (b.symbol.toUpperCase() < a.symbol.toUpperCase() ? -1 : 1));
122  };