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 3 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
25 changes: 18 additions & 7 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,14 @@ const GridTile = ({ widgetType, isDragging, setIsDragging, setWidgetAttribute, w
return null;
}

const widgetLink = (href: string) => {
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 href;
} else {
return `${window.location.origin}${chrome.isBeta() ? '/preview' : ''}${href}`;
Copy link
Contributor

Choose a reason for hiding this comment

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

You don't have to add origin or the preview partial to router links. That is handled via the component.

}
};

const { node, module, scope } = widgetData;

const dropdownItems = useMemo(() => {
Expand Down Expand Up @@ -189,13 +200,13 @@ const GridTile = ({ widgetType, isDragging, setIsDragging, setWidgetAttribute, w
)}
{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>
{headerLink.href && (
<Link to={widgetLink(headerLink.href)}>
Copy link
Contributor

Choose a reason for hiding this comment

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

You can't mix client-side routing with classic HTML routing. You have to pick a link component based on what the target is.

// for origin console.redhat.com

const href = https://www.google.com/

<Link to={href}>...</Link>
// actual link: console.redhat.com/https://www.google.com/

You have to decide which type of a link you are supposed to use.

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 sure I understand. Do I pick a different routing component based on whether its external or not?

Copy link
Contributor

Choose a reason for hiding this comment

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

Exactly. For external links, you use the plain HTML tags.

<Button className="pf-v5-u-font-weight-bold pf-v5-u-font-size-xs pf-v5-u-p-0" variant="link">
{headerLink.title}
</Button>
</Link>
)}
</FlexItem>
)}
</Flex>
Expand Down