Skip to content

Commit

Permalink
Enhance Review2024Card component: optimize share URL generation with …
Browse files Browse the repository at this point in the history
…useMemo and ensure safe access to window object.
  • Loading branch information
joao-vasconcelos committed Jan 11, 2025
1 parent 34cae66 commit b4d93c3
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions frontend/components/review-2024/Review2024Card/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { modals } from '@mantine/modals';
import { IconShare2 } from '@tabler/icons-react';
import { useTranslations } from 'next-intl';
import { useQueryState } from 'nuqs';
import { useEffect, useState } from 'react';
import { useEffect, useMemo, useState } from 'react';

import styles from './styles.module.css';

Expand Down Expand Up @@ -50,7 +50,10 @@ export function Review2024Card({ cardData, isFirstChild, isLastChild }: Props) {
'--color-text': cardData.colors.text,
};

const shareUrl = `${window.location.origin}${window.location.pathname}?card=${cardData._id}`;
const shareUrl = useMemo(() => {
if (typeof window === 'undefined') return;
return `${window.location.origin}${window.location.pathname}?card=${cardData._id}`;
}, []);

//
// C. Handle actions
Expand Down Expand Up @@ -82,7 +85,7 @@ export function Review2024Card({ cardData, isFirstChild, isLastChild }: Props) {
<Section withGap>
<p>{t('share.message')}</p>
<p className={styles.urlCopy}>{shareUrl}</p>
<CopyButton timeout={1500} value={shareUrl}>
<CopyButton timeout={1500} value={shareUrl || ''}>
{({ copied, copy }) => (
<Button onClick={copy} w="100%">
{copied ? t('share.copied') : t('share.copy')}
Expand Down

0 comments on commit b4d93c3

Please sign in to comment.