All files / app/features/landing CouchersMission.tsx

58.97% Statements 23/39
84.61% Branches 11/13
60% Functions 6/10
60% Lines 21/35

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                1x 1x 1x 1x 1x           1x 199x 66x                                                                         1x 11x 11x 11x   11x 11x 11x   11x 6x                           11x                 11x         6x                                                                                                                                                                                                                                                                             6x  
import { ChevronLeft, ChevronRight } from "@mui/icons-material";
import {
  Box,
  Grid,
  IconButton,
  styled,
  Typography,
  useMediaQuery,
} from "@mui/material";
import { useTranslation } from "i18n";
import { GLOBAL, LANDING } from "i18n/namespaces";
import { useEffect, useRef, useState } from "react";
import { theme } from "theme";
 
interface StyledBubbleProps extends React.ComponentProps<typeof Box> {
  selected?: boolean;
}
 
const StyledBubble = styled(Box, {
  shouldForwardProp: (prop) => prop !== "selected",
})<StyledBubbleProps>(({ theme, selected }) => ({
  color: selected ? theme.palette.common.white : theme.palette.text.primary,
  backgroundColor: selected
    ? theme.palette.primary.main
    : theme.palette.primary.light,
  padding: theme.spacing(1),
  borderRadius: theme.spacing(1),
  display: "flex",
  alignItems: "center",
  height: theme.spacing(14),
  cursor: "pointer",
  position: "relative",
  width: 170,
  flexShrink: 0,
 
  [theme.breakpoints.down("md")]: {
    height: theme.spacing(12),
  },
 
  "&::after": {
    content: '""',
    position: "absolute",
    bottom: "-15px",
    left: "50%",
    transform: "translateX(-50%)",
    width: 0,
    height: 0,
    borderLeft: "20px solid transparent",
    borderRight: "20px solid transparent",
    borderTop: `20px solid ${selected ? theme.palette.primary.main : theme.palette.primary.light}`,
    display: selected ? "block" : "none",
    [theme.breakpoints.down("md")]: {
      display: "none",
    },
  },
}));
 
const CouchersMission = () => {
  const { t } = useTranslation([LANDING, GLOBAL]);
  const scrollRef = useRef<HTMLDivElement>(null);
  const isMobile = useMediaQuery(theme.breakpoints.down("md"));
 
  const [selectedItem, setSelectedItem] = useState("nonprofit");
  const [canScrollLeft, setCanScrollLeft] = useState(false);
  const [canScrollRight, setCanScrollRight] = useState(false);
 
  useEffect(() => {
    if (!isMobile) return;
 
    const container = scrollRef.current;
 
    Iif (!container) return;
    handleScroll(); // Initial check
    container.addEventListener("scroll", handleScroll);
    window.addEventListener("resize", handleScroll);
    return () => {
      container.removeEventListener("scroll", handleScroll);
      window.removeEventListener("resize", handleScroll);
    };
  }, [isMobile]);
 
  const handleScroll = () => {
    const container = scrollRef.current;
    Iif (!container) return;
    setCanScrollLeft(container.scrollLeft > 0);
    setCanScrollRight(
      container.scrollLeft + container.clientWidth < container.scrollWidth,
    );
  };
 
  const missionBubble = (itemName: string) => {
    return (
      <StyledBubble
        selected={selectedItem === itemName}
        onClick={() => {
          setSelectedItem(itemName);
        }}
      >
        <Box
          sx={{
            display: "flex",
            flexDirection: "column",
            alignItems: "center",
            textAlign: "center",
            width: "100%",
          }}
        >
          <Typography
            gutterBottom
            sx={{
              color: theme.palette.common.black,
              fontSize: "1.2rem",
              fontWeight: 500,
              marginBottom: 0,
            }}
          >
            {t(`landing:${itemName}_title`)}
          </Typography>
        </Box>
      </StyledBubble>
    );
  };
 
  return (
    <>
      <Typography
        sx={{
          fontSize: "3rem",
          fontWeight: "bold",
          marginBottom: 4,
 
          [theme.breakpoints.down("md")]: {
            fontSize: "1.8rem",
          },
        }}
      >
        {t("couchers_mission_title")}
      </Typography>
      <Box
        sx={{
          position: "relative",
          width: "100%",
          marginTop: 2,
          marginBottom: 4,
        }}
      >
        {canScrollLeft && (
          <IconButton
            size="small"
            sx={{
              position: "absolute",
              left: 0,
              top: "50%",
              transform: "translateY(-50%)",
              zIndex: 1,
              display: { xs: "flex", md: "none" },
              color: theme.palette.common.white,
            }}
            onClick={() => {
              scrollRef.current?.scrollBy({
                left: -200,
                behavior: "smooth",
              });
            }}
          >
            <ChevronLeft sx={{ fontSize: "40px" }} />
          </IconButton>
        )}
        <Box
          ref={scrollRef}
          sx={{
            display: "flex",
            gap: 2,
            justifyContent: { xs: "flex-start", md: "center" },
            overflowX: { xs: "auto", md: "visible" },
            flexWrap: { xs: "nowrap", md: "wrap" },
            WebkitOverflowScrolling: "touch",
            width: "100%",
          }}
        >
          {missionBubble("nonprofit")}
          {missionBubble("free_forever")}
          {missionBubble("authentic")}
          {missionBubble("community_led")}
          {missionBubble("open_source")}
          {missionBubble("non_transactional")}
        </Box>
        {canScrollRight && (
          <IconButton
            sx={{
              position: "absolute",
              right: 0,
              top: "50%",
              transform: "translateY(-50%)",
              zIndex: 2,
              display: { xs: "flex", md: "none" },
              color: theme.palette.common.white,
            }}
            onClick={() => {
              scrollRef.current?.scrollBy({
                left: 200,
                behavior: "smooth",
              });
            }}
          >
            <ChevronRight sx={{ fontSize: "40px" }} />
          </IconButton>
        )}
      </Box>
      <Grid
        item
        sx={{
          marginTop: 2,
          backgroundColor: theme.palette.grey[50],
          padding: 5,
          borderRadius: 2,
          width: "100%",
        }}
      >
        <Typography gutterBottom>
          {t(`landing:${selectedItem}_description`)}
        </Typography>
        <Typography sx={{ marginTop: 2 }}>
          <b>{t("landing:why")}</b> {t(`landing:${selectedItem}_why`)}
        </Typography>
      </Grid>
    </>
  );
};
 
export default CouchersMission;