Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | import { ChevronLeft, ChevronRight } from "@mui/icons-material";
import { Chip, Skeleton, styled, Typography } from "@mui/material";
import Alert from "components/Alert";
import Button from "components/Button";
import HeaderButton from "components/HeaderButton";
import HtmlMeta from "components/HtmlMeta";
import { BackIcon } from "components/Icons";
import { useAuthContext } from "features/auth/AuthProvider";
import { useTranslation } from "i18n";
import { COMMUNITIES, PUBLIC_TRIPS } from "i18n/namespaces";
import { useRouter } from "next/router";
import { PublicTripStatus } from "proto/public_trips_pb";
import { useEffect, useMemo, useState } from "react";
import dayjs from "utils/dayjs";
import BetaFlag from "../../components/BetaFlag";
import PublicTripCard from "./PublicTripCard";
import { useListPublicTripsByUser } from "./useListPublicTrips";
type TripFilter = "all" | "active" | "past" | "closed";
const PageWrapper = styled("div")(({ theme }) => ({
padding: theme.spacing(3),
maxWidth: theme.breakpoints.values.md,
width: "100%",
marginInline: "auto",
flex: 1,
display: "flex",
flexDirection: "column",
[theme.breakpoints.down("sm")]: {
paddingInline: theme.spacing(1.5),
},
}));
const EmptyState = styled("div")(({ theme }) => ({
padding: theme.spacing(4),
textAlign: "center",
color: "var(--mui-palette-text-secondary)",
}));
const TripsList = styled("div")(({ theme }) => ({
display: "flex",
flexDirection: "column",
gap: theme.spacing(2),
}));
const PaginationRow = styled("div")(({ theme }) => ({
display: "flex",
justifyContent: "space-between",
alignItems: "center",
marginTop: theme.spacing(2),
}));
const TitleRow = styled("div")(({ theme }) => ({
display: "flex",
alignItems: "center",
gap: theme.spacing(2),
marginBottom: theme.spacing(2),
}));
const FilterRow = styled("div")(({ theme }) => ({
display: "flex",
gap: theme.spacing(1),
flexWrap: "wrap",
marginBottom: theme.spacing(2),
}));
const StyledBackButton = styled(HeaderButton)(({ theme }) => ({
width: "3.125rem",
height: "3.125rem",
[theme.breakpoints.down("sm")]: {
width: "2.25rem",
height: "2.25rem",
},
}));
// @TODO(NA): Add tests for edit flow.
// @TODO(NA): Add ability to reopen a closed trip if it's not in the past
export default function MyPublicTripsPage() {
const { t } = useTranslation([PUBLIC_TRIPS, COMMUNITIES]);
const { authState } = useAuthContext();
const userId = authState.userId;
const router = useRouter();
// Stack of page tokens visited so far. First entry is "" (the initial page).
const [tokens, setTokens] = useState<string[]>([""]);
const [filter, setFilter] = useState<TripFilter>("all");
const pageIndex = tokens.length - 1;
const currentToken = tokens[pageIndex];
const { data, error, isLoading } = useListPublicTripsByUser({
userId: userId ?? 0,
pageToken: currentToken,
ascending: true,
});
const filteredTrips = useMemo(() => {
const trips = data?.publicTripsList ?? [];
const startOfToday = dayjs().startOf("day");
return trips.filter((trip) => {
const isClosed =
trip.status === PublicTripStatus.PUBLIC_TRIP_STATUS_CLOSED;
const isPast = dayjs(trip.toDate).isBefore(startOfToday);
switch (filter) {
case "active":
return !isClosed && !isPast;
case "past":
return isPast;
case "closed":
return isClosed;
case "all":
default:
return !isPast;
}
});
}, [data?.publicTripsList, filter]);
const goNext = () => {
Iif (data?.nextPageToken) {
setTokens([...tokens, data.nextPageToken]);
}
};
const goPrev = () => {
Iif (pageIndex > 0) {
setTokens(tokens.slice(0, -1));
}
};
const trips = data?.publicTripsList ?? [];
const hasResults = trips.length > 0;
useEffect(() => {
Iif (isLoading) return;
const hash = window.location.hash.slice(1);
Iif (!hash) return;
const el = document.getElementById(hash);
Iif (el) el.scrollIntoView({ behavior: "smooth", block: "center" });
}, [isLoading]);
const filters: { value: TripFilter; label: string }[] = [
{ value: "all", label: t("publicTrips:filter_all") },
{ value: "active", label: t("publicTrips:filter_active") },
{ value: "past", label: t("publicTrips:filter_past") },
{ value: "closed", label: t("publicTrips:filter_closed") },
];
return (
<PageWrapper>
<HtmlMeta title={t("publicTrips:my_title")} />
<TitleRow>
<StyledBackButton
onClick={() => router.back()}
aria-label={t("communities:previous_page")}
>
<BackIcon />
</StyledBackButton>
<Typography variant="h1">{t("publicTrips:my_title")}</Typography>
<BetaFlag />
</TitleRow>
{error && <Alert severity="error">{error.message}</Alert>}
{isLoading ? (
<TripsList>
{[0, 1, 2].map((i) => (
<Skeleton
key={i}
variant="rounded"
height={180}
sx={{ borderRadius: 1 }}
/>
))}
</TripsList>
) : (
<>
{hasResults && (
<FilterRow>
{filters.map((f) => (
<Chip
key={f.value}
label={f.label}
onClick={() => setFilter(f.value)}
color={filter === f.value ? "primary" : "default"}
variant={filter === f.value ? "filled" : "outlined"}
/>
))}
</FilterRow>
)}
<TripsList>
{!hasResults ? (
<EmptyState>
<Typography variant="body1">
{t("publicTrips:my_empty_state")}
</Typography>
</EmptyState>
) : filteredTrips.length === 0 ? (
<EmptyState>
<Typography variant="body1">
{t("publicTrips:my_filter_empty_state")}
</Typography>
</EmptyState>
) : (
filteredTrips.map((trip) => (
<PublicTripCard
key={trip.tripId}
id={`public-trip-${trip.tripId}`}
trip={trip}
ownerView
/>
))
)}
</TripsList>
{hasResults && (
<PaginationRow>
<Button
onClick={goPrev}
disabled={pageIndex === 0}
startIcon={<ChevronLeft />}
>
{t("publicTrips:previous")}
</Button>
<Typography variant="body2">
{t("publicTrips:page_indicator", {
current: pageIndex + 1,
})}
</Typography>
<Button
onClick={goNext}
disabled={!data?.nextPageToken}
endIcon={<ChevronRight />}
>
{t("publicTrips:next")}
</Button>
</PaginationRow>
)}
</>
)}
</PageWrapper>
);
}
|