import { Order } from "@medusajs/medusa" import moment from "moment" import { useMemo, useRef } from "react" import { Column } from "react-table" import { useTranslation } from "react-i18next" import { useObserveWidth } from "../../../hooks/use-observe-width" import { stringDisplayPrice } from "../../../utils/prices" import Tooltip from "../../atoms/tooltip" import ImagePlaceholder from "../../fundamentals/image-placeholder" import StatusIndicator from "../../fundamentals/status-indicator" import { TFunction } from "i18next" const decidePaymentStatus = (status: string, t: TFunction) => { switch (status) { case "captured": return ( ) case "awaiting": return ( ) case "requires": return ( ) default: return ( ) } } const decideFulfillmentStatus = (status: string, t: TFunction) => { switch (status) { case "fulfilled": return ( ) case "shipped": return ( ) case "not_fulfilled": return ( ) case "partially_fulfilled": return ( ) case "partially_shipped": return ( ) case "requires": return ( ) default: return ( ) } } export const useCustomerOrdersColumns = (): Column[] => { const { t } = useTranslation() const columns = useMemo(() => { return [ { Header: t("customer-orders-table-order", "Order"), accessor: "display_id", Cell: ({ value }) => { return #{value} }, }, { accessor: "items", Cell: ({ value }) => { const containerRef = useRef(null) const width = useObserveWidth(containerRef) const { visibleItems, remainder } = useMemo(() => { if (!value || value.length === 0) { return { visibleItems: [], remainder: 0 } } const columns = Math.max(Math.floor(width / 20) - 1, 1) const visibleItems = value.slice(0, columns) const remainder = value.length - columns return { visibleItems, remainder } }, [value, width]) if (!value) { return null } return (
{visibleItems.map((item) => { return (
{item.thumbnail ? ( {item.title} ) : ( )}
) })}
{remainder > 0 && ( {t( "customer-orders-table-remainder-more", "+ {{remainder}} more", { remainder } )} )}
) }, }, { Header: t("customer-orders-table-date", "Date"), accessor: "created_at", Cell: ({ value }) => { return moment(value).format("DD MMM YYYY hh:mm") }, }, { Header: t("customer-orders-table-fulfillment", "Fulfillment"), accessor: "fulfillment_status", Cell: ({ value }) => { return decideFulfillmentStatus(value, t) }, }, { Header: t("customer-orders-table-status", "Status"), accessor: "payment_status", Cell: ({ value }) => { return decidePaymentStatus(value, t) }, }, { Header: () => (
{t("customer-orders-table-total", "Total")}
), accessor: "total", Cell: ({ value, row: { original: { currency_code }, }, }) => { return (
{stringDisplayPrice({ amount: value, currencyCode: currency_code, })}
) }, }, ] as Column[] }, []) return columns }