Skip to content

Commit

Permalink
🐛 fix(Insights/index.tsx): add useEffect hook to scroll to anchor on …
Browse files Browse the repository at this point in the history
…page load

✨ feat(Insights/index.tsx): change videoPart to video in InsightCard and HighlightCard components
🐛 fix(Insights/index.tsx): handle case when insightsLightbox is null or undefined
  • Loading branch information
marcbon committed Jul 31, 2023
1 parent de660bf commit 89f9bc5
Showing 1 changed file with 45 additions and 53 deletions.
98 changes: 45 additions & 53 deletions src/pages/Campaign/useWidgets/Experience/widgets/Insights/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { useTranslation } from 'react-i18next';
import { Campaign } from 'src/features/api';
import { SectionTitle } from 'src/pages/Campaign/SectionTitle';
import { WidgetSection } from 'src/pages/Campaign/WidgetSection';
import { useState } from 'react';
import { useEffect, useState } from 'react';
import { Divider } from 'src/common/components/divider';
import { appTheme } from 'src/app/theme';
import { useCampaignInsights } from './useCampaignInsights';
Expand All @@ -27,40 +27,26 @@ export const Insights = ({
}) => {
const { t } = useTranslation();
const { data, isLoading } = useCampaignInsights({
campaignId: campaign.id ?? 0,
campaignId: campaign.id ? campaign.id.toString() : '',
});
const [insightsLightbox, setInsightsLightbox] = useState(
data?.findings.reduce(
(
acc: {
[key: number]: {
isOpen: boolean;
currentIndex: number;
};
},
insight
) => {
acc[insight.id] = {
isOpen: false,
currentIndex: 0,
};
return acc;
},
{}
) ?? {}
);

if (!data || !data.findings) return null;
const [insightsLightbox, setInsightsLightbox] = useState<{
[key: number]: {
isOpen: boolean;
currentIndex: number;
};
}>({});

// Check if url has an anchor and scroll to it
const url = window.location.href;
const urlAnchor = url.split('#')[1];
if (urlAnchor) {
const anchor = document.getElementById(urlAnchor);
if (anchor) {
anchor.scrollIntoView();
useEffect(() => {
const url = window.location.href;
const urlAnchor = url.split('#')[1];
if (urlAnchor) {
const anchor = document.getElementById(urlAnchor);
if (anchor) {
anchor.scrollIntoView();
}
}
}
}, []);

const onSlideChange = (insightId: number, index: number) => {
setInsightsLightbox({
Expand All @@ -82,6 +68,8 @@ export const Insights = ({
});
};

if (!data || !data.findings) return null;

return (
<WidgetSection {...(id && { id })}>
<Grid>
Expand Down Expand Up @@ -128,34 +116,38 @@ export const Insights = ({
<Col xs={12} lg={6}>
<InsightCard insight={insight} />

Check failure on line 117 in src/pages/Campaign/useWidgets/Experience/widgets/Insights/index.tsx

View workflow job for this annotation

GitHub Actions / validate

Type '{ id: number; title: string; description: string; severity: { id: number; name: string; }; cluster: string; video: { id: number; start: number; end: number; mediaId: number; url: string; streamUrl: string; description: string; }[]; } | { ...; }' is not assignable to type '{ id: number; title: string; description?: string | undefined; severity: { id: number; name?: string | undefined; }; cluster: { id?: number | undefined; name?: string | undefined; }[] | "all"; video?: { ...; }[] | undefined; }'.
</Col>
{insight.videoPart.map((videoPart, index) => (
{insight.video?.map((videoPart, index) => (
<Col xs={12} lg={6}>
<HighlightCard
onClick={() => openLightbox(insight.id, index)}
{...{ videoPart, index, insight }}
video={videoPart}
index={index}
insight={insight}

Check failure on line 125 in src/pages/Campaign/useWidgets/Experience/widgets/Insights/index.tsx

View workflow job for this annotation

GitHub Actions / validate

Type '{ id: number; title: string; description: string; severity: { id: number; name: string; }; cluster: string; video: { id: number; start: number; end: number; mediaId: number; url: string; streamUrl: string; description: string; }[]; } | { ...; }' is not assignable to type '{ id: number; title: string; description?: string | undefined; severity: { id: number; name?: string | undefined; }; cluster: { id?: number | undefined; name?: string | undefined; }[] | "all"; video?: { ...; }[] | undefined; }'.
/>
</Col>
))}
{insightsLightbox[insight.id].isOpen && (
<InsightLightbox
currentIndex={
insightsLightbox[insight.id].currentIndex
}
items={insight.videoPart}
onClose={() =>
setInsightsLightbox({
...insightsLightbox,
[insight.id]: {
isOpen: false,
currentIndex: 0,
},
})
}
onSlideChange={(index) =>
onSlideChange(insight.id, index)
}
/>
)}
{insightsLightbox &&
insightsLightbox[insight.id] &&
insightsLightbox[insight.id].isOpen && (
<InsightLightbox
currentIndex={
insightsLightbox[insight.id].currentIndex
}
items={insight?.video}
onClose={() =>
setInsightsLightbox({
...insightsLightbox,
[insight.id]: {
isOpen: false,
currentIndex: 0,
},
})
}
onSlideChange={(index) =>
onSlideChange(insight.id, index)
}
/>
)}
</Row>
))}
</Grid>
Expand Down

0 comments on commit 89f9bc5

Please sign in to comment.