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 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 13x 2x 13x 13x 6x 13x 13x 13x 13x 6x 13x 6x 20x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 12x 13x 30x 13x 13x 13x 13x 13x 13x | import { ArchiveOutlined, UnarchiveOutlined } from "@mui/icons-material";
import { Skeleton, styled, useMediaQuery } from "@mui/material";
import { InfiniteData, useInfiniteQuery, useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import Alert from "components/Alert";
import HeaderButton from "components/HeaderButton";
import { BackIcon, OverflowMenuIcon } from "components/Icons";
import Menu, { MenuItem } from "components/Menu";
import { BOTTOM_NAV_BASE_HEIGHT } from "components/Navigation/constants";
import PageTitle from "components/PageTitle";
import { useAuthContext } from "features/auth/AuthProvider";
import { GROUP_CHAT_REFETCH_INTERVAL } from "features/messages/groupchats/constants";
import HostRequestSendField from "features/messages/requests/HostRequestSendField";
import useMarkLastSeen from "features/messages/useMarkLastSeen";
import {
hostRequestKey,
hostRequestMessagesKey,
hostRequestsListKey,
listNotificationsQueryKey,
messageThreadsListKey,
pingQueryKey,
} from "features/queryKeys";
import { useLiteUser } from "features/userQueries/useLiteUsers";
import { RpcError } from "grpc-web";
import { useTranslation } from "i18n";
import { MESSAGES } from "i18n/namespaces";
import { useRouter } from "next/router";
import { HostRequestStatus } from "proto/messages_pb";
import { GetHostRequestMessagesRes, RespondHostRequestReq } from "proto/requests_pb";
import { useEffect, useRef, useState } from "react";
import { messagesRoute } from "routes";
import { service } from "service";
import { Temporal } from "temporal-polyfill";
import { theme } from "theme";
import { firstName } from "utils/names";
import { sendNativeBack, useIsNativeEmbed } from "utils/nativeLink";
import { requestStatusToTransKey } from "../constants";
import ChatContent from "../groupchats/ChatContent";
import HostRequestFeedbackCard from "./HostRequestFeedbackCard";
import HostRequestReferenceCard from "./HostRequestReferenceCard";
import HostRequestRespondButtons from "./HostRequestRespondButtons";
import HostRequestStatusBanner from "./HostRequestStatusBanner";
import HostRequestUserSummarySection from "./HostRequestUserSummarySection";
const StyledHeader = styled("div")(({ theme }) => ({
padding: theme.spacing(1, 2),
borderBottom: `1px solid ${theme.palette.divider}`,
alignItems: "center",
display: "flex",
flexShrink: 0,
"& > * + *": {
marginInlineStart: theme.spacing(2),
},
[theme.breakpoints.down("sm")]: {
padding: theme.spacing(0, 1.5),
},
}));
const StyledPageTitle = styled(PageTitle)({
flexGrow: 1,
width: "100%",
display: "flex",
alignItems: "center",
marginInlineEnd: theme.spacing(2),
marginInlineStart: theme.spacing(2),
"& > *": { marginInlineEnd: theme.spacing(2) },
[theme.breakpoints.down("sm")]: {
fontSize: "0.9rem",
},
});
const StyledPageWrapper = styled("div")<{ isNativeEmbed: boolean }>(({ theme, isNativeEmbed }) => ({
display: "flex",
flexDirection: "column",
// Use dvh (dynamic viewport height) which adjusts for mobile keyboard
// Use CSS custom property set by Navigation component for actual height
height: isNativeEmbed
? "calc(100dvh - var(--nav-height, 3.5rem) - var(--cookie-banner-height, 0px))"
: `calc(100dvh - var(--nav-height, 3.5rem) - ${BOTTOM_NAV_BASE_HEIGHT}px - env(safe-area-inset-bottom, 0px) - var(--cookie-banner-height, 0px))`,
[theme.breakpoints.up("md")]: {
// On desktop, only subtract top nav (no bottom nav)
height: "calc(100dvh - var(--nav-height, 4rem) - var(--cookie-banner-height, 0px))",
},
}));
// Footer is fixed at bottom - never scrolls away
const StyledFooter = styled("div")(({ theme }) => ({
background: "var(--mui-palette-background-default)",
flexShrink: 0,
paddingBottom: theme.spacing(2),
paddingLeft: theme.spacing(2),
paddingRight: theme.spacing(2),
[theme.breakpoints.down("md")]: {
paddingLeft: theme.spacing(1),
paddingRight: theme.spacing(1),
paddingBottom: `calc(${theme.spacing(2)} + env(safe-area-inset-bottom, 0px))`,
},
}));
export default function HostRequestView({ hostRequestId }: { hostRequestId: number }) {
const { t } = useTranslation([MESSAGES]);
const isNativeEmbed = useIsNativeEmbed();
const isMobile = useMediaQuery(theme.breakpoints.down("md"));
const { data: hostRequest, error: hostRequestError } = useQuery({
queryKey: hostRequestKey(hostRequestId),
queryFn: () => service.requests.getHostRequest(hostRequestId),
enabled: !!hostRequestId,
});
const {
data: messagesRes,
isLoading: isMessagesLoading,
error: messagesError,
fetchNextPage,
isFetchingNextPage,
hasNextPage,
} = useInfiniteQuery<
GetHostRequestMessagesRes.AsObject,
RpcError,
InfiniteData<GetHostRequestMessagesRes.AsObject>,
(string | number | undefined)[],
number
>({
queryKey: hostRequestMessagesKey(hostRequestId),
queryFn: ({ pageParam: lastMessageId }) => service.requests.getHostRequestMessages(hostRequestId, lastMessageId),
enabled: !!hostRequestId,
initialPageParam: 0,
getNextPageParam: (lastPage) => (lastPage.noMore ? undefined : lastPage.lastMessageId),
refetchInterval: GROUP_CHAT_REFETCH_INTERVAL,
});
const { data: surfer } = useLiteUser(hostRequest?.surferUserId);
const { data: host } = useLiteUser(hostRequest?.hostUserId);
const currentUserId = useAuthContext().authState.userId;
const isOffer = !!hostRequest?.publicTripId;
// Whether the viewer is the host of the *stay* (the one offering a couch).
// surferUserId/hostUserId are generated stay-role columns, so the backend has already reversed
// them for public-trip offers: hostUserId is the host either way. Reversing again here would
// invert the role on offers.
const isHost = hostRequest?.hostUserId === currentUserId;
// The other party in the conversation, independent of stay-role.
const otherUser = hostRequest?.surferUserId === currentUserId ? host : surfer;
const isRequestPast =
hostRequest &&
Temporal.PlainDate.compare(Temporal.PlainDate.from(hostRequest.toDate), Temporal.Now.plainDateISO()) < 0;
const titleStatus =
hostRequest && t(requestStatusToTransKey[hostRequest.status as keyof typeof requestStatusToTransKey]);
const titleTransKey = isOffer
? isHost
? "host_request_view.invitation_title_for_host"
: "host_request_view.invitation_title_for_surfer"
: isHost
? "host_request_view.title_for_host"
: "host_request_view.title_for_surfer";
let title =
otherUser && hostRequest
? t(titleTransKey, {
user: firstName(otherUser.name),
status: titleStatus,
})
: undefined;
Iif (isRequestPast) {
title = title + ` (${t("host_request_status.past")})`;
}
const queryClient = useQueryClient();
const sendMutation = useMutation<string | undefined, RpcError, string>({
mutationFn: (text: string) => service.requests.sendHostRequestMessage(hostRequestId, text),
onSuccess: () => {
queryClient.invalidateQueries({
queryKey: hostRequestMessagesKey(hostRequestId),
});
queryClient.invalidateQueries({ queryKey: hostRequestsListKey() });
queryClient.invalidateQueries({ queryKey: messageThreadsListKey() });
},
});
const respondMutation = useMutation<void, RpcError, Required<RespondHostRequestReq.AsObject>>({
mutationFn: (req) => service.requests.respondHostRequest(req.hostRequestId, req.status, req.text),
onSuccess: () => {
queryClient.invalidateQueries({
queryKey: hostRequestKey(hostRequest?.hostRequestId),
});
queryClient.invalidateQueries({
queryKey: hostRequestMessagesKey(hostRequestId),
});
queryClient.invalidateQueries({ queryKey: hostRequestsListKey() });
queryClient.invalidateQueries({ queryKey: messageThreadsListKey() });
},
});
const { mutate: markLastRequestSeen } = useMutation({
mutationFn: (messageId: number) => service.requests.markLastRequestSeen(hostRequestId, messageId),
onSuccess: () => {
queryClient.invalidateQueries({
queryKey: hostRequestKey(hostRequestId),
});
queryClient.invalidateQueries({ queryKey: messageThreadsListKey() });
queryClient.invalidateQueries({ queryKey: [pingQueryKey] });
// The backend also marks the related notifications seen, so refresh the feed.
queryClient.invalidateQueries({ queryKey: [listNotificationsQueryKey] });
},
});
const { markLastSeen } = useMarkLastSeen(markLastRequestSeen, hostRequest?.lastSeenMessageId);
// Mark the thread seen as soon as it's opened (while still mounted), so the
// read state and unread badges update reliably even if the user navigates back
// immediately — the per-message onVisible debounce alone can be lost on unmount.
const latestMessageId = messagesRes?.pages?.[0]?.messagesList?.[0]?.messageId;
useEffect(() => {
Iif (hostRequest && latestMessageId && latestMessageId > hostRequest.lastSeenMessageId) {
markLastRequestSeen(latestMessageId);
}
}, [hostRequest, latestMessageId, markLastRequestSeen]);
const archiveMutation = useMutation<void, RpcError>({
mutationFn: async () => {
await service.requests.setHostRequestArchiveStatus(hostRequestId, !hostRequest?.isArchived);
},
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: hostRequestsListKey() });
queryClient.invalidateQueries({ queryKey: messageThreadsListKey() });
queryClient.invalidateQueries({
queryKey: hostRequestKey(hostRequestId),
});
router.push(messagesRoute);
},
});
const handleBannerRespond = (status: HostRequestStatus) => () => {
respondMutation.mutate({ hostRequestId, status, text: "" });
};
const router = useRouter();
const [isMenuOpen, setIsMenuOpen] = useState(false);
const menuAnchor = useRef<HTMLButtonElement>(null);
// Go back to wherever the user came from (preserving the messages tab they were
// on). On the native embed, pop the native nav stack; on web go back if there's
// in-app history, otherwise fall back to the messages list.
const handleBack = () => {
if (isNativeEmbed) {
sendNativeBack();
return;
}
if (window.history.length > 1) {
router.back();
} else {
router.push(messagesRoute);
}
};
const hasError = respondMutation.error || sendMutation.error || hostRequestError;
Iif (!hostRequestId) {
return <Alert severity="error">{t("host_request_view.error_message")}</Alert>;
}
return (
<StyledPageWrapper isNativeEmbed={isNativeEmbed}>
<StyledHeader>
<HeaderButton
onClick={handleBack}
aria-label={t("host_request_view.back_button_a11y_label")}
{...(isMobile ? { size: "small" } : {})}
>
<BackIcon sx={{ fontSize: isMobile ? "small" : "medium" }} />
</HeaderButton>
<StyledPageTitle>{!title || hostRequestError ? <Skeleton width="100" /> : title}</StyledPageTitle>
<HeaderButton
onClick={() => setIsMenuOpen(true)}
aria-label={t("show_more_actions_button_a11y")}
aria-haspopup="true"
aria-controls="request-menu"
ref={menuAnchor}
{...(isMobile ? { size: "small" } : {})}
>
<OverflowMenuIcon sx={{ fontSize: isMobile ? "small" : "medium" }} />
</HeaderButton>
<Menu
id="request-menu"
anchorEl={menuAnchor.current}
keepMounted
open={isMenuOpen}
onClose={() => setIsMenuOpen(false)}
>
{hostRequest && (
<MenuItem
onClick={() => {
archiveMutation.mutate();
setIsMenuOpen(false);
}}
disabled={archiveMutation.isPending}
>
{hostRequest.isArchived ? (
<>
<UnarchiveOutlined fontSize="small" sx={{ mr: 1 }} />
{t("archive.unarchive_button")}
</>
) : (
<>
<ArchiveOutlined fontSize="small" sx={{ mr: 1 }} />
{t("archive.archive_button")}
</>
)}
</MenuItem>
)}
</Menu>
</StyledHeader>
<HostRequestUserSummarySection hostRequest={hostRequest} otherUser={otherUser} isOffer={isOffer} />
{hostRequest && !isRequestPast && (
<HostRequestStatusBanner
isHost={isHost}
status={hostRequest.status}
isOffer={isOffer}
isLoading={respondMutation.isPending}
onAccept={handleBannerRespond(HostRequestStatus.HOST_REQUEST_STATUS_ACCEPTED)}
onDecline={handleBannerRespond(HostRequestStatus.HOST_REQUEST_STATUS_REJECTED)}
onCancel={handleBannerRespond(HostRequestStatus.HOST_REQUEST_STATUS_CANCELLED)}
hostName={otherUser ? firstName(otherUser.name) : undefined}
/>
)}
{hasError && (
<Alert severity={"error"}>
{respondMutation.error?.message || sendMutation.error?.message || hostRequestError?.message || ""}
</Alert>
)}
{messagesError && <Alert severity="error">{messagesError.message}</Alert>}
<ChatContent
isHostRequest
isLoading={isMessagesLoading}
messages={messagesRes}
hostRequest={hostRequest}
fetchNextPage={fetchNextPage}
isFetchingNextPage={isFetchingNextPage}
hasNextPage={!!hasNextPage}
markLastSeen={markLastSeen}
isError={!!messagesError}
footer={
hostRequest ? (
<>
{!isRequestPast && (
<HostRequestRespondButtons
isHost={isHost}
status={hostRequest.status}
isOffer={isOffer}
isLoading={respondMutation.isPending}
handleStatus={handleBannerRespond}
name={otherUser ? firstName(otherUser.name) : undefined}
/>
)}
{isHost && hostRequest.needHostRequestFeedback && (
<HostRequestFeedbackCard hostRequestId={hostRequestId} />
)}
<HostRequestReferenceCard hostRequest={hostRequest} />
</>
) : undefined
}
/>
<StyledFooter>
{hostRequest && <HostRequestSendField hostRequest={hostRequest} sendMutation={sendMutation} />}
</StyledFooter>
</StyledPageWrapper>
);
}
|