"use client" import { createColumnHelper, flexRender, getCoreRowModel, getSortedRowModel, type SortingState, useReactTable, } from "@tanstack/react-table" import { cx } from "class-variance-authority" import { useState } from "react" import { useIntl } from "react-intl" import { MaterialIcon } from "@scandic-hotels/design-system/Icons/MaterialIcon" import Table from "@scandic-hotels/design-system/Table" import { TextLink } from "@scandic-hotels/design-system/TextLink" import { Typography } from "@scandic-hotels/design-system/Typography" import { OfferPrice } from "./OfferPrice" import { formatPoints, hasActiveCampaign, type HotelData, nameToSort, } from "./util" import styles from "./rewardNights.module.css" interface RewardNightsTableProps { hotelData: HotelData[] } export function RewardNightsTable({ hotelData }: RewardNightsTableProps) { const intl = useIntl() const [sorting, setSorting] = useState([ { id: "destination", desc: false }, ]) const columnHelper = createColumnHelper() const columns = [ columnHelper.accessor("name", { header: intl.formatMessage({ id: "rewardNights.table.hotel", defaultMessage: "Hotel", }), sortingFn: (a, b) => nameToSort(a.original.name).localeCompare(nameToSort(b.original.name)), cell: ({ row }) => ( {row.original.name} ), }), columnHelper.accessor((row) => `${row.city}, ${row.country}`, { id: "destination", header: intl.formatMessage({ id: "rewardNights.table.destination", defaultMessage: "Destination", }), sortingFn: (a, b) => a.original.city.localeCompare(b.original.city), cell: ({ row }) => { const hotel = row.original const hasCampaign = hasActiveCampaign(hotel.rewardNight.campaign) return ( <> {/* eslint-disable-next-line formatjs/no-literal-string-in-jsx */} {hotel.city}, {hotel.country} {hasCampaign ? ( ) : null} ) }, }), columnHelper.accessor((row) => row.rewardNight.points, { id: "points", header: intl.formatMessage({ id: "common.points", defaultMessage: "Points", }), sortingFn: (a, b) => a.original.rewardNight.points - b.original.rewardNight.points, cell: ({ row }) => { const hotel = row.original const hasCampaign = hasActiveCampaign(hotel.rewardNight.campaign) return (
{formatPoints(hotel.rewardNight.points)} {hasCampaign ? ( {formatPoints(hotel.rewardNight.campaign.points)} ) : null}
) }, }), ] const table = useReactTable({ data: hotelData, columns, state: { sorting }, onSortingChange: setSorting, getCoreRowModel: getCoreRowModel(), getSortedRowModel: getSortedRowModel(), enableSortingRemoval: false, }) return ( {table.getHeaderGroups().map((headerGroup) => ( {headerGroup.headers.map((header) => ( {flexRender( header.column.columnDef.header, header.getContext() )} ))} ))} {table.getRowModel().rows.map((row) => ( {row.getVisibleCells().map((cell) => ( {flexRender(cell.column.columnDef.cell, cell.getContext())} ))} ))}
) }