-
Notifications
You must be signed in to change notification settings - Fork 12
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
base: master
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
|
@@ -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; | ||
|
||
|
@@ -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)); | ||
|
@@ -62,6 +65,14 @@ const GridTile = ({ widgetType, isDragging, setIsDragging, setWidgetAttribute, w | |
return null; | ||
} | ||
|
||
const widgetLink = (href: string) => { | ||
if (href.includes('https://')) { | ||
return href; | ||
} else { | ||
return `${window.location.origin}${chrome.isBeta() ? '/preview' : ''}${href}`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(() => { | ||
|
@@ -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)}> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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 downThere was a problem hiding this comment.
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.