import React from "react";
import { css } from "emotion";
import {
Box,
Skeleton,
Tooltip,
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";
export function ItemTradesOfferingPage() {
return (
);
}
export function ItemTradesSeekingPage() {
return (
);
}
function ItemTradesPage({
title,
userHeading,
compareListHeading,
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,
compareListHeading,
tradesQuery,
}) {
const { loading, error, data } = useQuery(tradesQuery, {
variables: { itemId },
});
// HACK: I'm pretty much hiding this for now, because it's not ready. But
/// it's visible at #show-compare-column!
const shouldShowCompareColumn = window.location.href.includes(
"show-compare-column"
);
const minorColumnWidth = {
base: shouldShowCompareColumn ? "23%" : "30%",
md: "18ex",
};
// 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,
getVaguelyRandomizedSortKeyForDate(trade.user.lastTradeActivity)
);
}
return tradeSortKeys.get(trade.id);
};
const trades = [...(data?.item?.trades || [])];
trades.sort((a, b) => getTradeSortKey(b).localeCompare(getTradeSortKey(a)));
if (error) {
return {error.message};
}
return (
{/* A small wording tweak to fit better on the xsmall screens! */}
Last active
Last edit
{userHeading}
{shouldShowCompareColumn && (
Compare
)}
List
{loading && (
<>
>
)}
{!loading &&
trades.length > 0 &&
trades.map((trade) => (
))}
{!loading && trades.length === 0 && (
No trades yet!
)}
);
}
function ItemTradesTableRow({
compareListHeading,
href,
username,
listName,
lastTradeActivity,
shouldShowCompareColumn,
}) {
const history = useHistory();
const onClick = React.useCallback(() => history.push(href), [history, href]);
const focusBackground = useColorModeValue("gray.100", "gray.600");
return (
{formatVagueDate(lastTradeActivity)}
{username}
{shouldShowCompareColumn && (
{compareListHeading}:
Adorable Freckles
Constellation Dress
(WIP: This is placeholder data!)
}
>
2 match
2 matches
)}
{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 getVaguelyRandomizedSortKeyForDate(dateString) {
const date = new Date(dateString);
// "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)
if (isThisWeek(date)) {
return `ZZZthisweekZZZ-${Math.random()}`;
}
return dateString;
}