Skip to content

Commit

Permalink
fix: feature card onclick (#1752)
Browse files Browse the repository at this point in the history
  • Loading branch information
dennyscode authored Feb 12, 2025
1 parent cffc892 commit dfec539
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 15 deletions.
39 changes: 28 additions & 11 deletions src/components/FeatureCards/FeatureCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { useEffect, useMemo, useRef, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { trackSpindl } from 'src/hooks/feature-cards/spindl/trackSpindl';
import { isSpindlTrackData, type SpindlCardAttributes } from 'src/types/spindl';
import { openInNewTab } from 'src/utils/openInNewTab';
import {
FCard as Card,
FeatureCardActions,
Expand Down Expand Up @@ -48,18 +49,20 @@ export const FeatureCard = ({ data }: FeatureCardProps) => {
setDisabledFeatureCard,
]);

const mode = data.attributes?.DisplayConditions.mode;

const typographyColor = useMemo(() => {
if (data.attributes?.DisplayConditions.mode) {
if (data.attributes?.DisplayConditions.mode === 'dark') {
if (mode) {
if (mode === 'dark') {
return theme.palette.white.main;
} else if (data.attributes?.DisplayConditions.mode === 'light') {
} else if (mode === 'light') {
return theme.palette.black.main;
}
} else {
return theme.palette.text.primary;
}
}, [
data.attributes?.DisplayConditions.mode,
mode,
theme.palette.black.main,
theme.palette.text.primary,
theme.palette.white.main,
Expand Down Expand Up @@ -87,18 +90,29 @@ export const FeatureCard = ({ data }: FeatureCardProps) => {
trackEvent,
]);

const mode = data.attributes?.DisplayConditions.mode || theme.palette.mode;

const imageUrl =
mode === 'dark'
const imageUrl = useMemo(() => {
const imageMode = mode || theme.palette.mode;
if (
!data.attributes?.BackgroundImageDark?.data?.attributes?.url ||
!data.attributes?.BackgroundImageLight?.data?.attributes?.url
) {
return null;
}
return imageMode === 'dark'
? new URL(
data.attributes?.BackgroundImageDark.data.attributes?.url,
data.attributes?.BackgroundImageDark?.data?.attributes?.url,
process.env.NEXT_PUBLIC_STRAPI_URL,
)
: new URL(
data.attributes?.BackgroundImageLight.data.attributes?.url,
data.attributes?.BackgroundImageLight?.data?.attributes?.url,
process.env.NEXT_PUBLIC_STRAPI_URL,
);
}, [
data.attributes?.BackgroundImageDark?.data?.attributes?.url,
data.attributes?.BackgroundImageLight?.data?.attributes?.url,
mode,
theme.palette.mode,
]);

const handleSpindl = (
attributes: SpindlCardAttributes | FeatureCardAttributes,
Expand Down Expand Up @@ -142,6 +156,9 @@ export const FeatureCard = ({ data }: FeatureCardProps) => {
label: string,
) => {
event.stopPropagation();
if (data?.attributes?.URL) {
openInNewTab(data?.attributes?.URL);
}

// Mark feature card as disabled if needed
if (
Expand Down Expand Up @@ -177,7 +194,7 @@ export const FeatureCard = ({ data }: FeatureCardProps) => {
<Card
backgroundImageUrl={imageUrl?.href}
onClick={(e) => handleClick(e, 'click_card')}
isDarkCard={data.attributes?.DisplayConditions.mode === 'dark'}
isDarkCard={mode === 'dark'}
>
<FeatureCardContent>
<FeatureCardCloseButton
Expand Down
4 changes: 3 additions & 1 deletion src/hooks/feature-cards/useFeatureCards.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ export const useFeatureCards = () => {
)
.slice(0, 1);
}
}, [cards, disabledFeatureCards, excludedFeatureCardsFilter]);
// prevent re-loading on card-click
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [cards]);

if (slicedFeatureCards?.length === 0 || widgetExpanded) {
return null;
Expand Down
5 changes: 4 additions & 1 deletion src/hooks/feature-cards/usePersonalizedFeatureCards.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import { usePersonalizedFeatureOnLevel } from 'src/hooks/feature-cards/usePerson
import { useLoyaltyPass } from 'src/hooks/useLoyaltyPass';
import { useSettingsStore } from 'src/stores/settings';
import { shallow } from 'zustand/shallow';
import { useWidgetExpanded } from '../useWidgetExpanded';

export const usePersonalizedFeatureCards = () => {
const { account } = useAccount();
const { points } = useLoyaltyPass(account?.address);
const widgetExpanded = useWidgetExpanded();

const { excludedFeatureCardsFilter } = useFeatureCardsFilter();
const disabledFeatureCards = useSettingsStore(
(state) => state.disabledFeatureCards,
Expand Down Expand Up @@ -45,7 +48,7 @@ export const usePersonalizedFeatureCards = () => {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [featureCardsToDisplay, featureCardsLevel]);

if (slicedPersonalizedFeatureCards?.length === 0) {
if (slicedPersonalizedFeatureCards?.length === 0 || widgetExpanded) {
return null;
}

Expand Down
4 changes: 2 additions & 2 deletions src/types/strapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ export interface FeatureCardAttributes {
publishedAt?: string;
locale: string;
uid: string;
BackgroundImageLight: ImageData<MediaData>;
BackgroundImageDark: ImageData<MediaData>;
BackgroundImageLight?: ImageData<MediaData>;
BackgroundImageDark?: ImageData<MediaData>;
featureCardsExclusions?: FeatureCardExclusions;

localizations: {
Expand Down

0 comments on commit dfec539

Please sign in to comment.