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 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 18x 1x 3x 6x 6x 3x 6x 6x 3x 6x 6x 2x 6x 2x 6x | import {
ArrowBack,
ArrowForward,
Luggage,
MeetingRoom,
} from "@mui/icons-material";
import { Box, IconButton, styled, Typography } from "@mui/material";
import { useInfiniteQuery } from "@tanstack/react-query";
import Alert from "components/Alert";
import FadingScrollTrack from "components/FadingScrollTrack";
import { hostRequestsListKey } from "features/queryKeys";
import { RpcError } from "grpc-web";
import { useTranslation } from "i18n";
import { DASHBOARD } from "i18n/namespaces";
import Link from "next/link";
import { HostRequestStatus } from "proto/conversations_pb";
import {
HostRequest,
HostRequestSortBy,
ListHostRequestsRes,
} from "proto/requests_pb";
import { useEffect, useRef, useState } from "react";
import { routeToEditProfile, searchRoute } from "routes";
import { service } from "service";
import { theme } from "theme";
import UpcomingStayCard, { UpcomingStayCardSkeleton } from "./UpcomingStayCard";
interface UpcomingStaysWidgetProps {
icon: React.ReactNode;
title: string;
requests: HostRequest.AsObject[];
isLoading: boolean;
emptyMessage: string;
emptyCtaLabel: string;
emptyCtaHref: string;
}
const CARD_WIDTH = 220;
const CARD_GAP = 12;
const SectionHeader = styled("div")({
display: "flex",
alignItems: "center",
justifyContent: "space-between",
marginBottom: theme.spacing(1.5),
minHeight: 28,
});
const EmptyStateRow = styled("div")(({ theme }) => ({
display: "flex",
alignItems: "center",
gap: theme.spacing(2),
padding: theme.spacing(2),
border: "1px dashed var(--mui-palette-divider)",
borderRadius: 10,
background: "var(--mui-palette-grey-50)",
}));
function UpcomingStaysWidget({
icon,
title,
requests,
isLoading,
emptyMessage,
emptyCtaLabel,
emptyCtaHref,
}: UpcomingStaysWidgetProps) {
const { t } = useTranslation([DASHBOARD]);
const scrollerRef = useRef<HTMLDivElement>(null);
const [canScrollLeft, setCanScrollLeft] = useState(false);
const [canScrollRight, setCanScrollRight] = useState(false);
const updateScrollState = () => {
const el = scrollerRef.current;
Eif (!el) return;
// scrollLeft: pixels scrolled; clientWidth: visible width; scrollWidth: total content width.
// scrollLeft is a float on high-DPI screens, so Math.round prevents the right arrow staying lit at the end.
setCanScrollLeft(el.scrollLeft > 0);
setCanScrollRight(
Math.round(el.scrollLeft) < el.scrollWidth - el.clientWidth,
);
};
useEffect(() => {
updateScrollState();
}, [requests.length, isLoading]);
const scroll = (dir: 1 | -1) => {
scrollerRef.current?.scrollBy({
left: dir * (CARD_WIDTH + CARD_GAP) * 2,
behavior: "smooth",
});
};
return (
<section>
<SectionHeader>
<Typography
variant="h2"
sx={{ display: "inline-flex", alignItems: "center", gap: 1 }}
>
{icon}
{title}
{!isLoading && requests.length > 0 && (
<Box
component="span"
sx={{
fontSize: 12,
fontWeight: 700,
color: "var(--mui-palette-text-secondary)",
background: "var(--mui-palette-grey-50)",
borderRadius: 999,
px: 1.125,
lineHeight: "20px",
display: "inline-block",
}}
>
{requests.length}
</Box>
)}
</Typography>
<div>
<IconButton
size="small"
onClick={() => scroll(-1)}
disabled={!canScrollLeft}
color={canScrollLeft ? "primary" : "default"}
aria-label={t("dashboard:prev_page_button_a11y")}
>
<ArrowBack fontSize="small" />
</IconButton>
<IconButton
size="small"
onClick={() => scroll(1)}
disabled={!canScrollRight}
color={canScrollRight ? "primary" : "default"}
aria-label={t("dashboard:next_page_button_a11y")}
>
<ArrowForward fontSize="small" />
</IconButton>
</div>
</SectionHeader>
{isLoading ? (
<FadingScrollTrack $gap={CARD_GAP} $snapType="x proximity">
{[0, 1, 2].map((i) => (
<Box
key={i}
sx={{ flex: `0 0 ${CARD_WIDTH}px`, scrollSnapAlign: "start" }}
>
<UpcomingStayCardSkeleton />
</Box>
))}
</FadingScrollTrack>
) : requests.length === 0 ? (
<EmptyStateRow>
<Typography
variant="body2"
sx={{ flex: 1, color: "var(--mui-palette-text-secondary)" }}
>
{emptyMessage}
</Typography>
<Link
href={emptyCtaHref}
style={{ textDecoration: "none", whiteSpace: "nowrap" }}
>
<Typography
component="span"
variant="body2"
sx={{
color: "var(--mui-palette-primary-main)",
fontWeight: 600,
"&:hover": { textDecoration: "underline" },
}}
>
{emptyCtaLabel} →
</Typography>
</Link>
</EmptyStateRow>
) : (
<FadingScrollTrack
ref={scrollerRef}
onScroll={updateScrollState}
$gap={CARD_GAP}
$snapType="x proximity"
$canScrollLeft={canScrollLeft}
$canScrollRight={canScrollRight}
>
{requests.map((r) => (
<Box
key={r.hostRequestId}
sx={{
flex: `0 0 ${CARD_WIDTH}px`,
minWidth: 0,
scrollSnapAlign: "start",
}}
>
<UpcomingStayCard hostRequest={r} />
</Box>
))}
</FadingScrollTrack>
)}
</section>
);
}
const UPCOMING_STATUSES = [
HostRequestStatus.HOST_REQUEST_STATUS_ACCEPTED,
HostRequestStatus.HOST_REQUEST_STATUS_CONFIRMED,
];
export default function UpcomingStays() {
const { t } = useTranslation([DASHBOARD]);
const {
data: tripsData,
isLoading: tripsLoading,
error: tripsError,
} = useInfiniteQuery<ListHostRequestsRes.AsObject, RpcError>({
queryKey: hostRequestsListKey({ type: "surfing", onlyActive: true }),
queryFn: ({ pageParam }) =>
service.requests.listHostRequests({
pageToken: pageParam as string | undefined,
type: "surfing",
onlyActive: true,
statusIn: UPCOMING_STATUSES,
sortBy: HostRequestSortBy.HOST_REQUEST_SORT_BY_FROM_DATE,
}),
initialPageParam: undefined,
getNextPageParam: (lastPage) =>
lastPage.noMore ? undefined : lastPage.nextPageToken,
});
const {
data: guestsData,
isLoading: guestsLoading,
error: guestsError,
} = useInfiniteQuery<ListHostRequestsRes.AsObject, RpcError>({
queryKey: hostRequestsListKey({ type: "hosting", onlyActive: true }),
queryFn: ({ pageParam }) =>
service.requests.listHostRequests({
pageToken: pageParam as string | undefined,
type: "hosting",
onlyActive: true,
statusIn: UPCOMING_STATUSES,
sortBy: HostRequestSortBy.HOST_REQUEST_SORT_BY_FROM_DATE,
}),
initialPageParam: undefined,
getNextPageParam: (lastPage) =>
lastPage.noMore ? undefined : lastPage.nextPageToken,
});
const upcomingTrips = (tripsData?.pages ?? []).flatMap(
(page) => page.hostRequestsList,
);
const upcomingGuests = (guestsData?.pages ?? []).flatMap(
(page) => page.hostRequestsList,
);
const error = tripsError ?? guestsError;
return (
<div>
{error && <Alert severity="error">{error.message}</Alert>}
<UpcomingStaysWidget
icon={
<MeetingRoom
sx={{ fontSize: 20, color: "var(--mui-palette-primary-main)" }}
/>
}
title={t("dashboard:stays.upcoming_guests_header")}
requests={upcomingGuests}
isLoading={guestsLoading}
emptyMessage={t("dashboard:stays.no_upcoming_guests")}
emptyCtaLabel={t("dashboard:become_a_host")}
emptyCtaHref={`${routeToEditProfile("about")}#preferences`}
/>
<Box sx={{ height: theme.spacing(3) }} />
<UpcomingStaysWidget
icon={
<Luggage
sx={{ fontSize: 20, color: "var(--mui-palette-primary-main)" }}
/>
}
title={t("dashboard:stays.upcoming_trips_header")}
requests={upcomingTrips}
isLoading={tripsLoading}
emptyMessage={t("dashboard:stays.no_upcoming_trips")}
emptyCtaLabel={t("dashboard:find_a_host")}
emptyCtaHref={searchRoute}
/>
</div>
);
}
|