All files / app/features/publicTrips PublicTripsOverview.tsx

45.45% Statements 20/44
0% Branches 0/12
0% Functions 0/9
48.78% Lines 20/41

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  5x 5x 5x 5x 5x 5x 5x 5x 5x         5x 5x 5x   5x 5x 5x   5x   5x   5x         5x           5x                                                                                                                                                                                                                                                                    
import { ArrowBack, ArrowForward } from "@mui/icons-material";
import { Box, IconButton, styled } from "@mui/material";
import Alert from "components/Alert";
import CenteredSpinner from "components/CenteredSpinner/CenteredSpinner";
import FadingScrollTrack from "components/FadingScrollTrack";
import { CouchIcon } from "components/Icons";
import StyledLink from "components/StyledLink";
import TextBody from "components/TextBody";
import { useAuthContext } from "features/auth/AuthProvider";
import { SectionTitle } from "features/communities/CommunityPage";
import {
  CARD_GAP,
  CARD_WIDTH,
  DashboardPublicTripCard,
} from "features/dashboard/DashboardPublicTripCard";
import { useTranslation } from "i18n";
import { PUBLIC_TRIPS } from "i18n/namespaces";
import { Community } from "proto/communities_pb";
import { useEffect, useRef, useState } from "react";
import { routeToCommunity } from "routes";
import { theme } from "theme";
 
import { useListPublicTrips } from "./useListPublicTrips";
 
const PREVIEW_COUNT = 3;
 
const StyledSection = styled("div")(() => ({
  display: "grid",
  rowGap: theme.spacing(2),
}));
 
const SectionHeader = styled("div")({
  display: "flex",
  alignItems: "center",
  justifyContent: "space-between",
});
 
const FooterRow = styled("div")(() => ({
  display: "flex",
  justifyContent: "center",
  alignItems: "center",
  gap: theme.spacing(2),
  flexWrap: "wrap",
}));
 
export default function PublicTripsOverview({
  community,
}: {
  community: Community.AsObject;
}) {
  const {
    t,
    i18n: { language: locale },
  } = useTranslation([PUBLIC_TRIPS]);
  const { authState } = useAuthContext();
 
  const { data, error, isLoading } = useListPublicTrips(
    community.communityId,
    "",
  );
 
  const scrollerRef = useRef<HTMLDivElement>(null);
  const [canScrollLeft, setCanScrollLeft] = useState(false);
  const [canScrollRight, setCanScrollRight] = useState(false);
 
  const updateScrollState = () => {
    const el = scrollerRef.current;
    Iif (!el) return;
    setCanScrollLeft(el.scrollLeft > 0);
    setCanScrollRight(
      Math.round(el.scrollLeft) < el.scrollWidth - el.clientWidth,
    );
  };
 
  const allTrips = data?.publicTripsList ?? [];
  const trips = allTrips.slice(0, PREVIEW_COUNT);
 
  useEffect(() => {
    updateScrollState();
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [trips.length, isLoading]);
 
  const scroll = (dir: 1 | -1) => {
    scrollerRef.current?.scrollBy({
      left: dir * (CARD_WIDTH + CARD_GAP),
      behavior: "smooth",
    });
  };
 
  return (
    <StyledSection>
      <SectionHeader>
        <SectionTitle icon={<CouchIcon />} variant="h2">
          {t("publicTrips:label")}
        </SectionTitle>
        {trips.length > 0 && (
          <Box sx={{ display: "flex", alignItems: "center", gap: "4px" }}>
            <IconButton
              size="small"
              onClick={() => scroll(-1)}
              disabled={!canScrollLeft}
              color={canScrollLeft ? "primary" : "default"}
              aria-label={t("publicTrips:prev_page_button_a11y")}
            >
              <ArrowBack fontSize="small" />
            </IconButton>
            <IconButton
              size="small"
              onClick={() => scroll(1)}
              disabled={!canScrollRight}
              color={canScrollRight ? "primary" : "default"}
              aria-label={t("publicTrips:next_page_button_a11y")}
            >
              <ArrowForward fontSize="small" />
            </IconButton>
          </Box>
        )}
      </SectionHeader>
 
      {error && <Alert severity="error">{error.message}</Alert>}
      {isLoading ? (
        <CenteredSpinner />
      ) : trips.length > 0 ? (
        <FadingScrollTrack
          ref={scrollerRef}
          onScroll={updateScrollState}
          $gap={CARD_GAP}
          $snapType="x proximity"
          $canScrollLeft={canScrollLeft}
          $canScrollRight={canScrollRight}
        >
          {trips.map((trip) => (
            <div
              key={trip.tripId}
              style={{
                flex: `0 0 ${CARD_WIDTH}px`,
                scrollSnapAlign: "start",
              }}
            >
              <DashboardPublicTripCard
                trip={trip}
                locale={locale}
                isOwnTrip={trip.user?.userId === authState.userId}
              />
            </div>
          ))}
        </FadingScrollTrack>
      ) : (
        <TextBody>{t("publicTrips:empty_state")}</TextBody>
      )}
 
      <FooterRow>
        {trips.length > 0 && (
          <StyledLink
            href={routeToCommunity(
              community.communityId,
              community.slug,
              "public-trips",
            )}
          >
            {t("publicTrips:see_all")}
          </StyledLink>
        )}
      </FooterRow>
    </StyledSection>
  );
}