import React, { FC } from 'react'; /** * IShortcut holds the data for one shortcut link. */ interface IShortcut { /** The title text on the card. */ name: string; /** The description text on the card. */ description: string; /** The link target. */ url: string; } /** * Props for AUShortCutComponent. */ type Props = { /** The shortcut data to show. */ shortcut: IShortcut; /** * Set this value to mark the link as internal or external. * If you do not set a value, the component compares the URL with the current page. * A URL that points at a different origin is external. All other URLs are internal. */ isLocal?: boolean; /** Extra class names to add to the link. */ classNames?: string[]; /** A function that renders custom content instead of the default card. */ render?: (shortcut: IShortcut, isLocal: boolean) => React.ReactNode; }; /** * Tells if a URL stays on the current site. * * Relative URLs and absolute URLs that point at the current origin are internal. * Schemes other than http and https, and URLs that do not parse, are internal too. */ const isLocalUrl = (url: string): boolean => { // No window during server-side rendering, so fall back to the raw-prefix test. if (typeof window === 'undefined') { return !/^https?:\/\//i.test(url); } try { const target = new URL(url, window.location.href); if (target.protocol !== 'http:' && target.protocol !== 'https:') { return true; } return target.origin === window.location.origin; } catch { return true; } }; /** * AUShortCutComponent shows a link card for one shortcut. * * The card shows an icon. The icon marks the link as internal or external. * Internal links open in the same tab. * External links open in a new tab. * * Pass a `render` function to replace the default card with custom content. */ const AUShortCutComponent: FC = ({ shortcut, isLocal, classNames = [], render, }) => { const resolvedIsLocal = isLocal ?? isLocalUrl(shortcut.url); if (render) { return <>{render(shortcut, resolvedIsLocal)}; } return (

{shortcut.name}

{shortcut.description}

); }; AUShortCutComponent.displayName = 'AUShortCutComponent'; export default AUShortCutComponent; export type { Props, IShortcut, };