Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
*/
import React from 'react';
import type { ApiModel, IMethod, IType } from '@looker/sdk-codegen';
import { Link } from 'react-router-dom';
import { Link } from '../Link';
import { RunItHeading } from '@looker/run-it';
import { buildPath, highlightHTML } from '../../utils';

Expand Down
70 changes: 68 additions & 2 deletions packages/api-explorer/src/components/Link/Link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,76 @@

*/

import { NavLink } from 'react-router-dom';
import type { FC, MouseEvent } from 'react';
import React from 'react';
import { NavLink, useHistory, useLocation } from 'react-router-dom';
import styled from 'styled-components';
import { tryGetEnvAdaptor } from '@looker/extension-utils';

export const Link = styled(NavLink)`
export interface LinkProps {
to: string;
exact?: boolean;
activeClassName?: string;
className?: string;
}

const LinkComponent: FC<LinkProps> = ({
to,
exact,
activeClassName,
className,
children,
...props
}) => {
const adaptor = tryGetEnvAdaptor();
const history = useHistory();
const location = useLocation();

if (adaptor && adaptor.isExtension()) {
const absoluteUrl = adaptor.resolveRouteToAbsoluteUrl(to);

const handleClick = (e: MouseEvent<HTMLAnchorElement>) => {
const isRegularClick =
e.button === 0 && !e.metaKey && !e.ctrlKey && !e.altKey && !e.shiftKey;

if (isRegularClick) {
e.preventDefault();
history.push(to);
}
};

const isActive = exact
? location.pathname === to
: location.pathname.startsWith(to);
const activeClass = isActive ? activeClassName || 'active' : '';
const combinedClassName = `${className || ''} ${activeClass}`.trim();

return (
<a
href={absoluteUrl}
onClick={handleClick}
className={combinedClassName}
{...props}
>
{children}
</a>
);
}

return (
<NavLink
to={to}
exact={exact}
activeClassName={activeClassName}
className={className}
{...props}
>
{children}
</NavLink>
);
};

export const Link = styled(LinkComponent)`
text-decoration: none;
color: inherit;

Expand Down
6 changes: 6 additions & 0 deletions packages/extension-utils/src/adaptorUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ export interface IEnvironmentAdaptor extends IAuthAdaptor {
openBrowserWindow: (url: string, target?: string) => void;
/** error logger */
logError: (error: Error, componentStack: string) => void;
/** Resolves a path to a fully qualified URL for the environment host */
resolveRouteToAbsoluteUrl(pathname: string, search?: string): string;
}

/**
Expand Down Expand Up @@ -137,6 +139,10 @@ export const getEnvAdaptor = () => {
return extensionAdaptor;
};

export const tryGetEnvAdaptor = () => {
return extensionAdaptor;
};

/**
* Used by some unit tests
* @param adaptor to use for testing
Expand Down
4 changes: 4 additions & 0 deletions packages/extension-utils/src/browserAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,8 @@ export class BrowserAdaptor
logError(_error: Error, _componentStack: string): void {
// noop - error logging for standalone applications TBD
}

resolveRouteToAbsoluteUrl(pathname: string, search = '') {
return `${window.location.origin}${pathname}${search}`;
}
}
9 changes: 9 additions & 0 deletions packages/extension-utils/src/extensionAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,13 @@ export class ExtensionAdaptor
message: componentStack,
} as ErrorEvent);
}

resolveRouteToAbsoluteUrl(pathname: string, search = '') {
const { lookerHostData } = this.extensionSdk;
if (lookerHostData) {
const { hostOrigin, extensionId } = lookerHostData;
return `${hostOrigin}/extensions/${extensionId}${pathname}${search}`;
}
return `${pathname}${search}`;
}
}
Loading