All files / src/shared/Table GiveTable.tsx

92.98% Statements 53/57
92.5% Branches 37/40
100% Functions 13/13
92.59% Lines 50/54

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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321                                                                                                                      36x       2939x 2939x         2939x 2939x                                   2939x   36x   36x       2094x   2089x         2089x 2089x         2089x   36x   36x       668x               36x   36x       2089x               2089x                                           36x   36x       2096x     2093x   2089x 2089x 1613x 1395x 631x     2089x                                           36x                                           1241x 1241x   1241x       818x                   1241x                   1241x   1241x   387x 166x 166x   221x 37x     387x 387x       1241x                                 2939x 10x                 2929x             2929x                                      
import React, { isValidElement, useEffect, useRef } from "react";
import { CustomTheme } from "@theme/v2/palette.interface";
import {
  Table,
  TableContainer,
  TableBody,
  Paper,
  TableHeadProps,
  TableRowProps,
} from "@mui/material";
import {
  FixedFooterContent,
  FixedHeaderContent,
  ItemContent,
  TableComponents,
  TableVirtuoso,
  TableVirtuosoHandle,
} from "react-virtuoso";
import GiveAnimatedListItem from "@shared/GiveAnimatedList/GiveAnimatedListItem";
import { TableColumnType } from "componentsV2/Table/Table.types";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import GiveTableLoadingPlaceholder from "./GivaTableLoadingPlaceholder";
import { useAppTheme } from "@theme/v2/Provider";
import { Box } from "@mui/material";
import FetchingRowSkeletons from "@common/Skeleton/FetchingRowSkeletons";
 
interface BaseContext {
  sticky?: boolean;
  top?: number;
  isWideView?: boolean;
  hasPagination?: boolean;
  disablePadding?: boolean;
  tablePadding?: string;
}
 
interface Props<TData, TCustomContext extends BaseContext = BaseContext> {
  sticky?: boolean;
  top?: number;
  rowProps?: (data: TData, idx: number) => TableRowProps;
  data: TData[];
  columns: TableColumnType[];
  itemContent: ItemContent<TData, TCustomContext>;
  fixedHeaderContent?: FixedHeaderContent;
  fixedFooterContent?: FixedFooterContent;
  emptyStateContent?: FixedHeaderContent;
  endReached?: (index: number) => void;
  context?: Omit<TCustomContext, keyof BaseContext>;
  isLoading?: boolean;
  page?: number;
  onScroll?: React.UIEventHandler<HTMLDivElement>;
  hasPagination?: boolean;
  isInfiniteScroll?: boolean;
  disablePadding?: boolean;
  tablePadding?: string;
  customLoadingRowProps?: any;
  isFetchingNextPage?: boolean;
  increaseViewportByTop?: number;
}
 
const TRow = React.forwardRef<
  HTMLTableRowElement,
  TableRowProps & { context?: BaseContext }
>(({ style, children, context, ...otherProps }, ref) => {
  const { palette } = useAppTheme();
  Iif (!isValidElement(children)) {
    console.error("TRow expects a single child element.");
    return null;
  }
 
  const ChildComponent = children.type;
  const childProps = {
    ...children.props,
    ...otherProps,
    sx: {
      ...style,
      cursor: "pointer",
      borderBottom: palette.border?.primary,
      borderBottomWidth: 1,
      borderBottomStyle: "solid",
      "&:hover": {
        backgroundColor: palette.primitive?.transparent["darken-2"],
      },
      ...children.props.sx,
      height: "100%",
    },
    ref,
  };
 
  return <ChildComponent {...childProps} />;
});
TRow.displayName = "TRow";
 
const THead = React.forwardRef<
  HTMLTableSectionElement,
  TableHeadProps & { context?: BaseContext }
>((props, ref) => {
  const { style, context, children, ...otherProps } = props;
 
  Iif (!isValidElement(children)) {
    console.error("THead expects a single child element.");
    return null;
  }
 
  const ChildComponent = children.type;
  const childProps = {
    ...children.props,
    ...otherProps,
  };
 
  return <ChildComponent {...childProps} />;
});
THead.displayName = "THead";
 
const TBody = React.forwardRef<
  HTMLTableSectionElement,
  React.ComponentPropsWithoutRef<typeof TableBody>
>((props, ref) => (
  <TableBody
    {...props}
    ref={ref}
    style={{
      height: "100%",
    }}
  />
));
TBody.displayName = "TableBody";
 
const Scroller = React.forwardRef<
  HTMLDivElement,
  React.ComponentPropsWithoutRef<typeof TableContainer>
>(({ style, ...props }, ref) => (
  <TableContainer
    component={Paper}
    {...props}
    style={{
      ...style,
      boxShadow: "none",
      backgroundColor: "transparent",
    }}
    sx={({ palette }: CustomTheme) => ({
      "&::-webkit-scrollbar": {
        width: 10,
      },
      "&::-webkit-scrollbar-thumb": {
        border: "2px solid #F9FCFF",
        borderRadius: "8px",
        backgroundColor: palette.surface?.tertiary,
      },
      "&::-webkit-scrollbar-thumb:focus": {
        backgroundColor: palette.surface?.tertiary,
      },
      "&::-webkit-scrollbar-thumb:active": {
        backgroundColor: palette.surface?.tertiary,
      },
      "&::-webkit-scrollbar-thumb:hover": {
        backgroundColor: palette.surface?.tertiary,
      },
    })}
    ref={ref}
  />
));
Scroller.displayName = "Scroller";
 
const BaseTableComponents: TableComponents<any, any> = {
  Scroller,
  TableHead: THead,
  Table: (props) => {
    const { isWideView, isMobileView, isLargeView } = useCustomThemeV2();
    const {
      context: { hasPagination, disablePadding, tablePadding },
    } = props;
 
    const getPadding = () => {
      if (disablePadding) return 0;
      if (isMobileView) return "0px 20px";
      if (isLargeView) return tablePadding ?? "0px 64px";
      return "0px 40px";
    };
 
    return (
      <Box
        sx={{
          padding: getPadding(),
          paddingBottom: hasPagination ? "76px !important" : 0,
        }}
      >
        <Table
          {...props}
          style={{
            borderCollapse: "collapse",
            tableLayout: isWideView ? "fixed" : "auto",
            height: "1px",
          }}
        />
      </Box>
    );
  },
  TableRow: TRow,
  TableBody: TBody,
};
 
const GiveTable = <TData, TCustomContext extends BaseContext = BaseContext>({
  sticky,
  top,
  data: allRows,
  itemContent,
  fixedHeaderContent,
  fixedFooterContent,
  emptyStateContent,
  endReached,
  context: customContext,
  isLoading,
  rowProps,
  columns,
  onScroll,
  hasPagination,
  isInfiniteScroll,
  disablePadding,
  tablePadding,
  customLoadingRowProps,
  isFetchingNextPage,
  increaseViewportByTop = 1000,
}: Props<TData, TCustomContext>) => {
  const { isWideView } = useCustomThemeV2();
  const virtuoso = useRef<TableVirtuosoHandle | null>(null);
 
  const Components = {
    ...BaseTableComponents,
    // Add EmptyPlaceholder component to show loading state with header
    EmptyPlaceholder: () =>
      isLoading ? (
        <GiveTableLoadingPlaceholder
          columns={columns}
          customLoadingRowProps={customLoadingRowProps}
        />
      ) : emptyStateContent ? (
        emptyStateContent()
      ) : null,
  } as TableComponents<TData, TCustomContext>;
 
  const context = {
    sticky,
    top,
    isWideView,
    hasPagination,
    disablePadding,
    tablePadding,
    ...customContext,
  } as TCustomContext;
 
  const shouldAnimate = useRef(true);
 
  useEffect(() => {
    let timeout: NodeJS.Timeout;
    if (isLoading) {
      !isInfiniteScroll && virtuoso.current?.scrollToIndex(0);
      shouldAnimate.current = true;
    } else {
      timeout = setTimeout(() => {
        shouldAnimate.current = false;
      }, 800);
    }
    return () => {
      clearTimeout(timeout);
    };
  }, [isLoading]);
 
  return (
    <TableVirtuoso<TData, TCustomContext>
      increaseViewportBy={{ top: increaseViewportByTop, bottom: 0 }}
      context={context}
      style={{
        height: "100%",
        width: "100%",
        scrollbarWidth: "none",
        ...(isLoading && isWideView && { overflowY: "hidden" }),
      }}
      ref={virtuoso}
      data={isLoading ? [] : allRows}
      onScroll={onScroll}
      components={Components}
      fixedHeaderContent={fixedHeaderContent}
      fixedFooterContent={fixedFooterContent}
      itemContent={(index, data, context) => {
        if (isFetchingNextPage && index === allRows.length - 1) {
          return (
            <FetchingRowSkeletons
              columns={columns as any}
              rowHeight={customLoadingRowProps?.height}
              sx={customLoadingRowProps?.sx}
            />
          );
        }
 
        const combinedRowProps = {
          ...(rowProps ? rowProps(data, index) : {}),
          sx: {
            ...(rowProps ? rowProps(data, index)?.sx : {}),
          },
        };
 
        return (
          <GiveAnimatedListItem
            index={index}
            key={index}
            shouldAnimate={
              shouldAnimate.current && index > allRows.length - 1 - 20
            }
            rowProps={combinedRowProps}
          >
            {itemContent(index, data, context)}
          </GiveAnimatedListItem>
        );
      }}
      endReached={endReached}
    />
  );
};
 
export default GiveTable;