All files / src/components/layouts Articles.tsx

100% Statements 24/24
100% Branches 10/10
100% Functions 8/8
100% Lines 23/23

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 1831x 1x                                       1x 1x   1x 1x 1x     1x 6x         1x 4x 4x 3x 3x   3x                   6x 6x 6x         1x                                                                                       10x                                                                           3x                       12x                     6x                                      
import { Inventory as InventoryIcon, SellOutlined as SellIcon, ThumbUp as ThumbUpIcon } from '@mui/icons-material';
import {
  Badge,
  BadgeProps,
  Box,
  Card,
  CardActionArea,
  CardActions,
  CardContent,
  CardMedia,
  Chip,
  Container,
  Grid,
  Skeleton,
  Stack,
  styled,
  Theme,
  Typography,
} from '@mui/material';
import { SxProps } from '@mui/system';
import React from 'react';
import { useTranslation } from 'react-i18next';
import { useInView } from 'react-intersection-observer';
 
import { ChipList, ChipListItem, SectionFadeIn, SectionTitle } from '@/components/elements';
import { sections, styles, values } from '@/constants';
import { useArticles } from '@/hooks';
import { LocaleCodes } from '@/utils/localization';
 
const OverlayBadge = styled(Badge)<BadgeProps>(({ theme }) =>
  theme.unstable_sx({
    '& .MuiBadge-badge': { top: 5, right: 28 },
  })
);
 
export const Articles = (props: { sx?: SxProps<Theme> }) => {
  const { ref, inView } = useInView(styles.intersectionOptions);
  const [_, i18n] = useTranslation();
  const articles = useArticles(inView);
  const section = sections.articles;
 
  return (
    <Box component="section" id={section} sx={{ py: 8, ...props.sx }}>
      <Container>
        <SectionTitle data-testid="Articles__SectionTitle">{section}</SectionTitle>
 
        <Box ref={ref}>
          {articles ? (
            <SectionFadeIn in={inView}>
              <Grid container spacing={4} sx={{ mt: 2 }}>
                {Object.values(articles).map((article, i) => {
                  const title = article[`title_${i18n.language as LocaleCodes}` as keyof typeof article] as string;
                  const body = article[`body_${i18n.language as LocaleCodes}` as keyof typeof article] as string;
                  return (
                    <Grid item key={`Articles__Card${i}`} xs={12} sm={6} md={4}>
                      <Card sx={{ ...styles.hoverOptions }}>
                        <CardActionArea
                          sx={{ p: 1 }}
                          onClick={() => window.open(article.url, '_blank')}
                          data-testid={`Articles__Card${i}__CardActionArea`}
                        >
                          <OverlayBadge
                            invisible={
                              !(article.likesCount && values.likesCountThreshold.visible <= article.likesCount)
                            }
                            badgeContent={
                              <Stack direction="row" gap={0.5} sx={{ alignItems: 'center', color: 'common.white' }}>
                                <ThumbUpIcon fontSize="small" />
                                {/* eslint-disable @typescript-eslint/no-non-null-assertion */}
                                <Typography sx={{ fontWeight: 'bold' }}>{`${article
                                  .likesCount!.toString()
                                  .slice(0, 1)}${'0'.repeat(article.likesCount!.toString().length - 1)}+`}</Typography>
                                {/* eslint-enable @typescript-eslint/no-non-null-assertion */}
                              </Stack>
                            }
                            color={
                              article.likesCount && values.likesCountThreshold.popular <= article.likesCount
                                ? 'error'
                                : 'secondary'
                            }
                          >
                            <CardMedia
                              component="img"
                              src={article.image}
                              alt={title}
                              loading="lazy"
                              decoding="async"
                            />
                          </OverlayBadge>
                          <CardContent>
                            <Typography
                              sx={{ fontSize: 14, color: 'text.secondary' }}
                              data-testid={`Articles__Card${i}__Body`}
                            >
                              {body}
                            </Typography>
                          </CardContent>
                          <CardActions
                            sx={{ display: 'flex', justifyContent: 'space-between', alignItems: 'end', pt: 0 }}
                          >
                            <ChipList>
                              {article.tags?.map((tag, j) => (
                                <ChipListItem
                                  key={`Articles__Card${i}__Tag${j}`}
                                  data-testid={`Articles__Card${i}__Tag${j}`}
                                >
                                  <Chip
                                    icon={<SellIcon color="disabled" />}
                                    label={tag}
                                    size="small"
                                    sx={{ borderRadius: 1, mt: 0.5, height: 'auto', color: 'grey.300' }}
                                  />
                                </ChipListItem>
                              ))}
                            </ChipList>
                            <Stack direction="row" gap={1} sx={{ ml: 1 }}>
                              <Stack direction="row" gap={0.5} sx={{ alignItems: 'center', color: 'grey.300' }}>
                                <ThumbUpIcon sx={{ fontSize: 18 }} />
                                <Typography data-testid={`Articles__Card${i}__LikesCount`}>
                                  {article.likesCount}
                                </Typography>
                              </Stack>
                              <Stack direction="row" gap={0.5} sx={{ alignItems: 'center', color: 'grey.300' }}>
                                <InventoryIcon sx={{ fontSize: 18 }} />
                                <Typography data-testid={`Articles__Card${i}__StocksCount`}>
                                  {article.stocksCount}
                                </Typography>
                              </Stack>
                            </Stack>
                          </CardActions>
                        </CardActionArea>
                      </Card>
                    </Grid>
                  );
                })}
              </Grid>
            </SectionFadeIn>
          ) : (
            <Grid container spacing={4} sx={{ mt: 2 }}>
              {[...Array(values.skeltonCount.articles).keys()].map((i) => (
                <Grid
                  item
                  key={`Articles__Card${i}--Loading`}
                  data-testid={`Articles__Card${i}--Loading`}
                  xs={12}
                  sm={6}
                  md={4}
                >
                  <Card sx={{ p: 1 }}>
                    <Skeleton animation="wave" variant="rectangular" sx={{ height: '180px' }} />
                    <CardContent>
                      {[...Array(4).keys()].reverse().map((n) => (
                        <Skeleton
                          key={`Articles__Card${i}__SkeletonBody${n}`}
                          data-testid={`Articles__Card${i}__SkeletonBody${n}`}
                          animation="wave"
                          variant="text"
                          sx={{ height: 10, mt: 1.5, width: n ? '100%' : '80%' }}
                        />
                      ))}
                    </CardContent>
                    <CardActions>
                      {[...Array(2)].map((_, j) => (
                        <Skeleton
                          key={`Articles__Card${i}__SkeletonTag${j}`}
                          data-testid={`Articles__Card${i}__SkeletonTag${j}`}
                          animation="wave"
                          variant="rectangular"
                          sx={{ height: 20, width: '15%' }}
                        />
                      ))}
                    </CardActions>
                  </Card>
                </Grid>
              ))}
            </Grid>
          )}
        </Box>
      </Container>
    </Box>
  );
};