Skip to content

Commit

Permalink
refactor: extract components
Browse files Browse the repository at this point in the history
  • Loading branch information
jesperhodge committed Dec 13, 2024
1 parent 8e38065 commit 08850e4
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 50 deletions.
51 changes: 51 additions & 0 deletions src/optimizer-page/SectionCollapsible.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { useState, FC } from 'react';
import {
Collapsible,
Icon,
} from '@openedx/paragon';
import {
ArrowRight,
ArrowDropDown,
} from '@openedx/paragon/icons';

interface Props {
title: string;
children: React.ReactNode;
redItalics: string;
className: string;
}

const SectionCollapsible: FC<Props> = ({
title, children, redItalics, className,
}) => {
const [isOpen, setIsOpen] = useState(false);
const styling = 'card-lg';
const collapsibleTitle = (
<div className={className}>
<Icon src={isOpen ? ArrowDropDown : ArrowRight} className="open-arrow" />
<strong>{title}</strong>
<span className="red-italics">{redItalics}</span>
</div>
);

return (
<div className={`section ${isOpen ? 'is-open' : ''}`}>
<Collapsible
styling={styling}
title={(
<p>
<strong>{collapsibleTitle}</strong>
</p>
)}
iconWhenClosed=""
iconWhenOpen=""
open={isOpen}
onToggle={() => setIsOpen(!isOpen)}
>
<Collapsible.Body>{children}</Collapsible.Body>
</Collapsible>
</div>
);
};

export default SectionCollapsible;
118 changes: 69 additions & 49 deletions src/optimizer-page/scan-results/ScanResults.jsx
Original file line number Diff line number Diff line change
@@ -1,68 +1,21 @@
import { useState, useCallback } from 'react';
import {
Container,
Layout,
Button,
Card,
Collapsible,
Icon,
Table,
CheckBox,
OverlayTrigger,
Tooltip,
} from '@openedx/paragon';
import {
ArrowRight,
ArrowDropDown,
OpenInNew,
Question,
Lock,
LinkOff,
} from '@openedx/paragon/icons';
import { useIntl } from '@edx/frontend-platform/i18n';
import messages from './messages';

const SectionCollapsible = ({
title, children, redItalics, className,
}) => {
const [isOpen, setIsOpen] = useState(false);
const styling = 'card-lg';
const collapsibleTitle = (
<div className={className}>
<Icon src={isOpen ? ArrowDropDown : ArrowRight} className="open-arrow" />
<strong>{title}</strong>
<span className="red-italics">{redItalics}</span>
</div>
);

return (
<div className={`section ${isOpen ? 'is-open' : ''}`}>
<Collapsible
styling={styling}
title={(
<p>
<strong>{collapsibleTitle}</strong>
</p>
)}
iconWhenClosed=""
iconWhenOpen=""
open={isOpen}
onToggle={() => setIsOpen(!isOpen)}
>
<Collapsible.Body>{children}</Collapsible.Body>
</Collapsible>
</div>
);
};

function getBaseUrl(url) {
try {
const parsedUrl = new URL(url);
return `${parsedUrl.origin}`;
} catch (error) {
return null;
}
}
import SectionCollapsible from '../SectionCollapsible';

const BrokenLinkHref = ({ href }) => (
<div className="broken-link-container">
Expand Down Expand Up @@ -110,6 +63,73 @@ const InfoCard = ({ text }) => (
</Card>
);

const BrokenLinkTable = ({ unit }) => (
<>
<p className="block-header">{unit.displayName}</p>
<Table
data={unit.blocks.reduce((acc, block) => {
const blockBrokenLinks = block.brokenLinks.map(
(link) => ({
blockLink: <GoToBlock block={block} />,
brokenLink: <BrokenLinkHref href={link} />,
status: (
<span className="link-status-text">
<Icon
src={LinkOff}
className="broken-link-icon"
/>
{intl.formatMessage(messages.brokenLinkStatus)}
</span>
),
}),
);
acc.push(...blockBrokenLinks);
if (!showLockedLinks) {
return acc;
}

const blockLockedLinks = block.lockedLinks.map(
(link) => ({
blockLink: <GoToBlock block={block} />,
brokenLink: <BrokenLinkHref href={link} />,
status: (
<span className="link-status-text">
<Icon src={Lock} className="lock-icon" />
{intl.formatMessage(messages.lockedLinkStatus)} <LockedInfoIcon />
</span>
),
}),
);
acc.push(...blockLockedLinks);
return acc;
}, [])}
columns={[
{
key: 'blockLink',
columnSortable: true,
onSort: () => {},
width: 'col-3',
hideHeader: true,
},
{
key: 'brokenLink',
columnSortable: false,
onSort: () => {},
width: 'col-6',
hideHeader: true,
},
{
key: 'status',
columnSortable: false,
onSort: () => {},
width: 'col-6',
hideHeader: true,
},
]}
/>
</>
);

const ScanResults = ({ data }) => {
const intl = useIntl();
const [showLockedLinks, setShowLockedLinks] = useState(true);
Expand Down Expand Up @@ -179,7 +199,7 @@ const ScanResults = ({ data }) => {
</h2>
{subsection.units.map((unit) => (
<div className="unit">
<p className="block-header">{unit.displayName}</p>
<p className="unit-header">{unit.displayName}</p>
<Table
data={unit.blocks.reduce((acc, block) => {
const blockBrokenLinks = block.brokenLinks.map(
Expand Down
2 changes: 1 addition & 1 deletion src/optimizer-page/scan-results/ScanResults.scss
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
}

/* Unit Header */
.block-header {
.unit-header {
font-size: 14px;
font-weight: 700;
margin-bottom: 5px;
Expand Down

0 comments on commit 08850e4

Please sign in to comment.