import { SalesChannel } from "@medusajs/medusa" import clsx from "clsx" import React, { useMemo } from "react" import { TableInstance } from "react-table" import Button from "../../../fundamentals/button" import PlusIcon from "../../../fundamentals/icons/plus-icon" import IndeterminateCheckbox from "../../../molecules/indeterminate-checkbox" import { useLayeredModal } from "../../../molecules/modal/layered-modal" import Table from "../../../molecules/table" import TableContainer from "../../../organisms/table-container" import { useAddChannelsModalScreen } from "./add-screen" type SalesChannelsTableProps = { count: number limit: number offset: number setOffset: (offset: number) => void setQuery: (query: string) => void setSelectedRowIds?: (selectedRowIds: string[]) => void tableAction?: React.ReactNode tableState: TableInstance isLoading?: boolean } type SalesChannelTableActionProps = { numberOfSelectedRows: number onDeselect: () => void onRemove: () => void } export const useSalesChannelsTableColumns = () => { const columns = useMemo( () => [ { width: 30, id: "selection", Header: ({ getToggleAllPageRowsSelectedProps }) => ( ), Cell: ({ row }) => { return ( e.stopPropagation()} className="flex w-[30px] justify-center" > ) }, }, { Header: "Title", accessor: "name", }, { Header: "Description", accessor: "description", }, ], [] ) return [columns] as const } const SalesChannelTable = ({ count, limit, offset, setOffset, setQuery, tableState, setSelectedRowIds, tableAction, isLoading, }: SalesChannelsTableProps) => { const { getTableProps, getTableBodyProps, headerGroups, rows, prepareRow, canPreviousPage, canNextPage, pageCount, nextPage, previousPage, // Get the state from the instance state: { pageIndex, ...state }, } = tableState React.useEffect(() => { if (setSelectedRowIds) { setSelectedRowIds(Object.keys(state.selectedRowIds)) } }, [Object.keys(state.selectedRowIds).length]) const handleNext = () => { if (canNextPage) { setOffset(offset + limit) nextPage() } } const handlePrev = () => { if (canPreviousPage) { setOffset(Math.max(offset - limit, 0)) previousPage() } } return ( {headerGroups?.map((headerGroup) => ( {headerGroup.headers.map((col) => ( {col.render("Header")} ))} ))} {rows.map((row) => { prepareRow(row) return ( {row.cells.map((cell) => { return ( {cell.render("Cell")} ) })} ) })}
) } export const SalesChannelTableActions = ({ numberOfSelectedRows, onDeselect, onRemove, }: SalesChannelTableActionProps) => { const addChannelModalScreen = useAddChannelsModalScreen() const showAddChannels = !!numberOfSelectedRows const classes = { "translate-y-[-42px]": !showAddChannels, "translate-y-[0px]": showAddChannels, } const { push } = useLayeredModal() return (
{numberOfSelectedRows} selected
) } export default SalesChannelTable