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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 31x 31x 31x 20x 10x 10x 10x 10x 10x 10x 12x 9x 31x 31x 31x 31x 31x 31x 31x 9x 31x 2x | import { Collapse, styled } from "@mui/material";
import Alert from "components/Alert";
import CenteredSpinner from "components/CenteredSpinner/CenteredSpinner";
import HtmlMeta from "components/HtmlMeta";
import Snackbar from "components/Snackbar";
import { createForegroundTracker } from "features/analytics/foregroundTracker";
import { useLogEvent } from "features/analytics/hooks";
import { readSearchReferrer, referrerToProperties } from "features/analytics/searchAttribution";
import { ProfileUserProvider } from "features/profile/hooks/useProfileUser";
import NewHostRequest from "features/profile/view/NewHostRequest";
import NewMessage from "features/profile/view/NewMessage";
import Overview from "features/profile/view/Overview";
import useUserByUsername from "features/userQueries/useUserByUsername";
import { useTranslation } from "i18n";
import { GLOBAL, PROFILE } from "i18n/namespaces";
import { useRouter } from "next/router";
import { useEffect, useLayoutEffect, useMemo, useState } from "react";
import { routeToUser, UserTab } from "routes";
import UserCard from "./UserCard";
const REQUEST_ID = "request";
/**
* Logs `profile.tab_viewed` when the viewed tab changes or the page unmounts,
* reporting how long the tab was open (`total_ms`) and visible (`foreground_ms`),
* plus any search referrer that led the user here.
*/
function useProfileTabViewTracking(userId: number | undefined, tab: UserTab) {
const logEvent = useLogEvent();
const referrerProps = useMemo(() => referrerToProperties(userId ? readSearchReferrer(userId) : null), [userId]);
useEffect(() => {
if (!userId) return;
const tracker = createForegroundTracker();
document.addEventListener("visibilitychange", tracker.onVisibilityChange);
return () => {
document.removeEventListener("visibilitychange", tracker.onVisibilityChange);
const { foregroundMs, totalMs } = tracker.finalize();
logEvent("profile.tab_viewed", {
user_id: userId,
tab,
foreground_ms: foregroundMs,
total_ms: totalMs,
...referrerProps,
});
};
}, [tab, userId, logEvent, referrerProps]);
}
export const StyledProfileRoot = styled("div")(({ theme }) => ({
padding: theme.spacing(1),
[theme.breakpoints.up("sm")]: {
display: "grid",
gridTemplateColumns: "2fr 3fr",
gap: theme.spacing(3),
margin: theme.spacing(0, 3),
padding: 0,
paddingTop: theme.spacing(3),
paddingBottom: theme.spacing(3),
},
[theme.breakpoints.up("md")]: {
gridTemplateColumns: "2fr 4fr",
maxWidth: "61.5rem",
margin: "0 auto",
},
}));
export default function UserPage({ username, tab = "about" }: { username: string; tab?: UserTab }) {
const { t } = useTranslation([PROFILE, GLOBAL]);
const router = useRouter();
const { data: user, isLoading, error } = useUserByUsername(username, true);
const [isRequesting, setIsRequesting] = useState(false);
const [isSuccessRequest, setIsSuccessRequest] = useState(false);
const [isMessaging, setIsMessaging] = useState(false);
useLayoutEffect(() => {
Iif (isRequesting || isMessaging) {
const requestEl = document.getElementById(REQUEST_ID);
requestEl?.scrollIntoView();
}
}, [isRequesting, isMessaging]);
useProfileTabViewTracking(user?.userId, tab);
return (
<>
<HtmlMeta title={user?.name} />
{isSuccessRequest && <Snackbar severity="success">{t("request_form.success")}</Snackbar>}
{error && <Alert severity="error">{error}</Alert>}
{isLoading ? (
<CenteredSpinner />
) : user ? (
<ProfileUserProvider user={user}>
<StyledProfileRoot>
<Overview setIsRequesting={setIsRequesting} setIsMessaging={setIsMessaging} tab={tab} />
<UserCard
tab={tab}
onTabChange={(newTab) => {
router.push(routeToUser(user.username, newTab), undefined, {
scroll: false,
});
}}
top={
<>
<Collapse in={isRequesting} mountOnEnter unmountOnExit>
<NewHostRequest setIsRequesting={setIsRequesting} setIsRequestSuccess={setIsSuccessRequest} />
</Collapse>
<Collapse in={isMessaging}>
<NewMessage setIsMessaging={setIsMessaging} />
</Collapse>
</>
}
/>
</StyledProfileRoot>
</ProfileUserProvider>
) : null}
</>
);
}
|