Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix widget link issues #91

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions src/Components/DnDLayout/GridTile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
import { CompressIcon, EllipsisVIcon, ExpandIcon, GripVerticalIcon, LockIcon, MinusCircleIcon, UnlockIcon } from '@patternfly/react-icons';
import React, { useMemo, useState } from 'react';
import clsx from 'clsx';
import { Link } from 'react-router-dom';

import './GridTile.scss';
import { Layout } from 'react-grid-layout';
Expand All @@ -30,6 +31,7 @@ import { getWidget } from '../Widgets/widgetDefaults';
import { useAtomValue } from 'jotai';
import classNames from 'classnames';
import HeaderIcon from '../Icons/HeaderIcon';
import useChrome from '@redhat-cloud-services/frontend-components/useChrome';

export type SetWidgetAttribute = <T extends string | number | boolean>(id: string, attributeName: keyof ExtendedLayoutItem, value: T) => void;

Expand All @@ -53,6 +55,7 @@ const GridTile = ({ widgetType, isDragging, setIsDragging, setWidgetAttribute, w
const widgetMapping = useAtomValue(widgetMappingAtom);
const { headerLink } = widgetConfig.config || {};
const hasHeader = headerLink && headerLink.href && headerLink.title;
const chrome = useChrome();

const widgetData = useMemo(() => {
return getWidget(widgetMapping, widgetType, () => setIsLoaded(true));
Expand All @@ -62,6 +65,24 @@ const GridTile = ({ widgetType, isDragging, setIsDragging, setWidgetAttribute, w
return null;
}

const widgetLink = (href: string, linkTitle: string | undefined) => {
if (href.includes('https://')) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a better way to check if the href is an external link?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not reliable. We use a isExternal prop in our links to determine if a link is external. Configuration is responsible for marking external links.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not seeing an isExternal prop being passed down

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think exists yet in the widgets. But its a pattern all across the platform.

return (
<Button component="a" href={href} className="pf-v5-u-font-weight-bold pf-v5-u-font-size-xs pf-v5-u-p-0" variant="link">
{linkTitle}
</Button>
);
} else {
return (
<Link to={href}>
<Button className="pf-v5-u-font-weight-bold pf-v5-u-font-size-xs pf-v5-u-p-0" variant="link">
{linkTitle}
</Button>
</Link>
);
}
};

const { node, module, scope } = widgetData;

const dropdownItems = useMemo(() => {
Expand Down Expand Up @@ -187,17 +208,7 @@ const GridTile = ({ widgetType, isDragging, setIsDragging, setWidgetAttribute, w
) : (
<Skeleton width="50%" />
)}
{hasHeader && isLoaded && (
<FlexItem>
<Button
className="pf-v5-u-font-weight-bold pf-v5-u-font-size-xs pf-v5-u-p-0"
variant="link"
onClick={() => window.open(headerLink.href, '_blank')}
>
{headerLink.title}
</Button>
</FlexItem>
)}
{hasHeader && isLoaded && <FlexItem>{headerLink.href && widgetLink(headerLink.href, headerLink.title)}</FlexItem>}
</Flex>
</Flex>
</Flex>
Expand Down