import React from "react"; import { css } from "emotion"; import { Box, Skeleton, useColorModeValue, useToken } from "@chakra-ui/core"; import gql from "graphql-tag"; import { useQuery } from "@apollo/client"; import { Link, useHistory, useParams } from "react-router-dom"; import { Heading2, usePageTitle } from "./util"; import ItemPageLayout from "./ItemPageLayout"; import useCurrentUser from "./components/useCurrentUser"; export function ItemTradesOfferingPage() { return ( ); } export function ItemTradesSeekingPage() { return ( ); } function ItemTradesPage({ title, userHeading, compareColumnLabel, tradesQuery, }) { const { itemId } = useParams(); const { error, data } = useQuery( gql` query ItemTradesPage($itemId: ID!) { item(id: $itemId) { id name isNc isPb thumbnailUrl description createdAt } } `, { variables: { itemId }, returnPartialData: true } ); usePageTitle(`${data?.item?.name} | ${title}`, { skip: !data?.item?.name }); if (error) { return {error.message}; } return ( {title} ); } function ItemTradesTable({ itemId, userHeading, compareColumnLabel, tradesQuery, }) { const { isLoggedIn } = useCurrentUser(); const { loading, error, data } = useQuery(tradesQuery, { variables: { itemId }, }); const shouldShowCompareColumn = isLoggedIn; // We partially randomize trade sorting, but we want it to stay stable across // re-renders. To do this, we can use `getTradeSortKey`, which will either // build a new sort key for the trade, or return the cached one from the // `tradeSortKeys` map. const tradeSortKeys = React.useMemo(() => new Map(), []); const getTradeSortKey = (trade) => { if (!tradeSortKeys.has(trade.id)) { tradeSortKeys.set( trade.id, getVaguelyRandomizedTradeSortKey( trade.user.lastTradeActivity, trade.user.matchingItems.length ) ); } return tradeSortKeys.get(trade.id); }; const trades = [...(data?.item?.trades || [])]; trades.sort((a, b) => getTradeSortKey(b).localeCompare(getTradeSortKey(a))); if (error) { return {error.message}; } const minorColumnWidth = { base: shouldShowCompareColumn ? "23%" : "30%", md: "20ex", }; return ( {/* A small wording tweak to fit better on the xsmall screens! */} Last active Last edit {shouldShowCompareColumn && ( {compareColumnLabel} Matches )} {userHeading} List {loading && ( <> )} {!loading && trades.length > 0 && trades.map((trade) => ( ))} {!loading && trades.length === 0 && ( No trades yet! )} ); } function ItemTradesTableRow({ href, username, listName, lastTradeActivity, matchingItems, shouldShowCompareColumn, }) { const history = useHistory(); const onClick = React.useCallback(() => history.push(href), [history, href]); const focusBackground = useColorModeValue("gray.100", "gray.600"); const sortedMatchingItems = [...matchingItems].sort((a, b) => a.name.localeCompare(b.name) ); return ( {formatVagueDate(lastTradeActivity)} {shouldShowCompareColumn && ( {matchingItems.length > 0 ? ( {sortedMatchingItems.slice(0, 4).map((item) => ( {item.name} ))} {matchingItems.length > 4 && ( + {matchingItems.length - 4} more )} ) : ( <> No matches None )} )} {username} {listName} ); } function ItemTradesTableRowSkeleton({ shouldShowCompareColumn }) { return ( X X X {shouldShowCompareColumn && ( X )} ); } function ItemTradesTableCell({ children, as = "td", ...props }) { const borderColor = useColorModeValue("gray.300", "gray.400"); const borderColorCss = useToken("colors", borderColor); const borderRadiusCss = useToken("radii", "md"); return ( {children} ); } function isThisWeek(date) { const startOfThisWeek = new Date(); startOfThisWeek.setDate(startOfThisWeek.getDate() - 7); return date > startOfThisWeek; } const shortMonthYearFormatter = new Intl.DateTimeFormat("en", { month: "short", year: "numeric", }); function formatVagueDate(dateString) { const date = new Date(dateString); if (isThisWeek(date)) { return "This week"; } return shortMonthYearFormatter.format(date); } function getVaguelyRandomizedTradeSortKey(dateString, numMatchingItems) { const date = new Date(dateString); const hasMatchingItems = numMatchingItems >= 1; // "This week" sorts after all other dates, but with a random factor! I don't // want people worrying about gaming themselves up to the very top, just be // active and trust the system 😅 (I figure that, if you care enough to "game" // the system by faking activity every week, you probably also care enough to // be... making real trades every week lmao) // // We also prioritize having matches, but we don't bother to sort _how many_ // matches, to decrease the power of gaming with large honeypot lists, and // because it's hard to judge how good matches are anyway. if (isThisWeek(date)) { const matchingItemsKey = hasMatchingItems ? "ZZmatchingZZ" : "AAnotmatchingAA"; return `ZZZthisweekZZZ-${matchingItemsKey}-${Math.random()}`; } return dateString; }