|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { Skeleton } from "@pythnetwork/component-library/Skeleton"; |
| 4 | +import { |
| 5 | + GridList, |
| 6 | + GridListItem, |
| 7 | +} from "@pythnetwork/component-library/unstyled/GridList"; |
| 8 | +import clsx from "clsx"; |
| 9 | +import type { ComponentProps, ReactNode } from "react"; |
| 10 | + |
| 11 | +import styles from "./index.module.scss"; |
| 12 | + |
| 13 | +type Props<T extends string> = ComponentProps<typeof GridList<RowConfig<T>>> & { |
| 14 | + headerLoadingSkeleton?: ReactNode | undefined; |
| 15 | + label: string; |
| 16 | + fields: ({ |
| 17 | + id: T; |
| 18 | + name: ReactNode; |
| 19 | + } & ( |
| 20 | + | { loadingSkeleton?: ReactNode | undefined } |
| 21 | + | { loadingSkeletonWidth?: number | undefined } |
| 22 | + ))[]; |
| 23 | +} & ( |
| 24 | + | { |
| 25 | + isLoading: true; |
| 26 | + rows?: RowConfig<T>[] | undefined; |
| 27 | + } |
| 28 | + | { |
| 29 | + isLoading?: false | undefined; |
| 30 | + rows: RowConfig<T>[]; |
| 31 | + } |
| 32 | + ); |
| 33 | + |
| 34 | +type RowConfig<T extends string> = { |
| 35 | + id: string | number; |
| 36 | + data: Record<T, ReactNode>; |
| 37 | + header: ReactNode; |
| 38 | + href?: string; |
| 39 | + textValue: string; |
| 40 | +}; |
| 41 | + |
| 42 | +export const EntityList = <T extends string>({ |
| 43 | + fields, |
| 44 | + isLoading, |
| 45 | + rows, |
| 46 | + headerLoadingSkeleton, |
| 47 | + className, |
| 48 | + label, |
| 49 | + ...props |
| 50 | +}: Props<T>) => ( |
| 51 | + <GridList |
| 52 | + className={clsx(styles.entityList, className)} |
| 53 | + items={isLoading ? [] : rows} |
| 54 | + aria-label={label} |
| 55 | + {...props} |
| 56 | + > |
| 57 | + {isLoading ? ( |
| 58 | + <GridListItem className={styles.entityItem ?? ""}> |
| 59 | + <div className={styles.itemHeader}>{headerLoadingSkeleton}</div> |
| 60 | + <dl className={styles.itemDetails}> |
| 61 | + {fields.map((field) => ( |
| 62 | + <div key={field.id} className={styles.itemDetailsItem}> |
| 63 | + <dt>{field.name}</dt> |
| 64 | + <dd> |
| 65 | + {"loadingSkeleton" in field ? ( |
| 66 | + field.loadingSkeleton |
| 67 | + ) : ( |
| 68 | + <Skeleton |
| 69 | + width={ |
| 70 | + "loadingSkeletonWidth" in field |
| 71 | + ? field.loadingSkeletonWidth |
| 72 | + : 20 |
| 73 | + } |
| 74 | + /> |
| 75 | + )} |
| 76 | + </dd> |
| 77 | + </div> |
| 78 | + ))} |
| 79 | + </dl> |
| 80 | + </GridListItem> |
| 81 | + ) : ( |
| 82 | + ({ data, header, ...props }) => ( |
| 83 | + <GridListItem className={styles.entityItem ?? ""} {...props}> |
| 84 | + <div className={styles.itemHeader}>{header}</div> |
| 85 | + <dl className={styles.itemDetails}> |
| 86 | + {fields.map((field) => ( |
| 87 | + <div key={field.id} className={styles.itemDetailsItem}> |
| 88 | + <dt>{field.name}</dt> |
| 89 | + <dd>{data[field.id]}</dd> |
| 90 | + </div> |
| 91 | + ))} |
| 92 | + </dl> |
| 93 | + </GridListItem> |
| 94 | + ) |
| 95 | + )} |
| 96 | + </GridList> |
| 97 | +); |
0 commit comments