Skip to content

Commit

Permalink
chore: Convert EuiCustomLink to TypeScript in /ui
Browse files Browse the repository at this point in the history
This was the last JavaScript component, convert it to TypeScript too.

The `href` prop was never used, so we omit it from the props type
definition, and no longer pass it to the component.

We could also have chosen to drop the `to` prop and use `href` instead,
but `to` is used by React Router's Link
(https://reactrouter.com/6.29.0/components/link), and it makes sense to
use the same name in EuiCustomLink since we pass it to React Router's
`navigate` function; the naming hints that it's tied to React Router and
not just a regular URL path.

Signed-off-by: Harri Lehtola <peruukki@hotmail.com>
  • Loading branch information
peruukki committed Feb 15, 2025
1 parent a13fa9b commit f8d41d1
Show file tree
Hide file tree
Showing 14 changed files with 15 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
// File name: "EuiCustomLink.js".
import React from "react";
import { EuiLink } from "@elastic/eui";
import { useNavigate, useHref } from "react-router-dom";
import { EuiLink, type EuiLinkAnchorProps } from "@elastic/eui";
import { useNavigate, useHref, type To } from "react-router-dom";

const isModifiedEvent = (event) =>
interface EuiCustomLinkProps extends Omit<EuiLinkAnchorProps, 'href'> {
to: To;
}

const isModifiedEvent = (event: React.MouseEvent) =>
!!(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey);

const isLeftClickEvent = (event) => event.button === 0;
const isLeftClickEvent = (event: React.MouseEvent) => event.button === 0;

const isTargetBlank = (event) => {
const target = event.target.getAttribute("target");
const isTargetBlank = (event: React.MouseEvent) => {
const target = (event.target as Element).getAttribute("target");
return target && target !== "_self";
};

export default function EuiCustomLink({ to, ...rest }) {
export default function EuiCustomLink({ to, ...rest }: EuiCustomLinkProps) {
// This is the key!
const navigate = useNavigate();

function onClick(event) {
const onClick: React.MouseEventHandler<HTMLAnchorElement> = (event) => {
if (event.defaultPrevented) {
return;
}
Expand All @@ -41,6 +44,5 @@ export default function EuiCustomLink({ to, ...rest }) {
// Generate the correct link href (with basename accounted for)
const href = useHref(to);

const props = { ...rest, href, onClick };
return <EuiLink {...props} />;
return <EuiLink {...rest} href={href} onClick={onClick} />;
}
1 change: 0 additions & 1 deletion ui/src/components/FeaturesInServiceDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ const FeaturesInServiceList = ({ featureViews }: FeatureViewsListInterace) => {
render: (name: string) => {
return (
<EuiCustomLink
href={`${process.env.PUBLIC_URL || ""}/p/${projectName}/feature-view/${name}`}
to={`${process.env.PUBLIC_URL || ""}/p/${projectName}/feature-view/${name}`}
>
{name}
Expand Down
1 change: 0 additions & 1 deletion ui/src/components/FeaturesListDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const FeaturesList = ({
field: "name",
render: (item: string) => (
<EuiCustomLink
href={`${process.env.PUBLIC_URL || ""}/p/${projectName}/feature-view/${featureViewName}/feature/${item}`}
to={`${process.env.PUBLIC_URL || ""}/p/${projectName}/feature-view/${featureViewName}/feature/${item}`}
>
{item}
Expand Down
1 change: 0 additions & 1 deletion ui/src/pages/data-sources/DataSourcesListingTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const DatasourcesListingTable = ({
render: (name: string) => {
return (
<EuiCustomLink
href={`${process.env.PUBLIC_URL || ""}/p/${projectName}/data-source/${name}`}
to={`${process.env.PUBLIC_URL || ""}/p/${projectName}/data-source/${name}`}
>
{name}
Expand Down
1 change: 0 additions & 1 deletion ui/src/pages/entities/EntitiesListingTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const EntitiesListingTable = ({ entities }: EntitiesListingTableProps) => {
render: (name: string) => {
return (
<EuiCustomLink
href={`${process.env.PUBLIC_URL || ""}/p/${projectName}/entity/${name}`}
to={`${process.env.PUBLIC_URL || ""}/p/${projectName}/entity/${name}`}
>
{name}
Expand Down
1 change: 0 additions & 1 deletion ui/src/pages/entities/FeatureViewEdgesList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ const FeatureViewEdgesList = ({ fvNames }: FeatureViewEdgesListInterace) => {
render: ({ name }: { name: string }) => {
return (
<EuiCustomLink
href={`${process.env.PUBLIC_URL || ""}/p/${projectName}/feature-view/${name}`}
to={`${process.env.PUBLIC_URL || ""}/p/${projectName}/feature-view/${name}`}
>
{name}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ const FeatureServiceListingTable = ({
render: (name: string) => {
return (
<EuiCustomLink
href={`${process.env.PUBLIC_URL || ""}/p/${projectName}/feature-service/${name}`}
to={`${process.env.PUBLIC_URL || ""}/p/${projectName}/feature-service/${name}`}
>
{name}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ const ConsumingFeatureServicesList = ({
render: ({ name }: { name: string }) => {
return (
<EuiCustomLink
href={`${process.env.PUBLIC_URL || ""}/p/${projectName}/feature-service/${name}`}
to={`${process.env.PUBLIC_URL || ""}/p/${projectName}/feature-service/${name}`}
>
{name}
Expand Down
1 change: 0 additions & 1 deletion ui/src/pages/feature-views/FeatureViewListingTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ const FeatureViewListingTable = ({
render: (name: string, item: genericFVType) => {
return (
<EuiCustomLink
href={`${process.env.PUBLIC_URL || ""}/p/${projectName}/feature-view/${name}`}
to={`${process.env.PUBLIC_URL || ""}/p/${projectName}/feature-view/${name}`}
>
{name} {(item.type === "ondemand" && <EuiBadge>ondemand</EuiBadge>) || (item.type === "stream" && <EuiBadge>stream</EuiBadge>)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ const StreamFeatureViewOverviewTab = ({
</EuiText>
<EuiTitle size="s">
<EuiCustomLink
href={`${process.env.PUBLIC_URL || ""}/p/${projectName}/data-source/${inputGroup?.name}`}
to={`${process.env.PUBLIC_URL || ""}/p/${projectName}/data-source/${inputGroup?.name}`}
>
{inputGroup?.name}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ const FeatureViewProjectionDisplayPanel = (featureViewProjection: RequestDataDis
<EuiSpacer size="xs" />
<EuiTitle size="s">
<EuiCustomLink
href={`${process.env.PUBLIC_URL || ""}/p/${projectName}/feature-view/${featureViewProjection.featureViewName}`}
to={`${process.env.PUBLIC_URL || ""}/p/${projectName}/feature-view/${featureViewProjection.featureViewName}`}
>
{featureViewProjection?.featureViewName}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ const RequestDataDisplayPanel = ({
<EuiSpacer size="xs" />
<EuiTitle size="s">
<EuiCustomLink
href={`${process.env.PUBLIC_URL || ""}/p/${projectName}/data-source/${requestDataSource?.name}`}
to={`${process.env.PUBLIC_URL || ""}/p/${projectName}/data-source/${requestDataSource?.name}`}
>
{requestDataSource?.name}
Expand Down
4 changes: 2 additions & 2 deletions ui/src/pages/features/FeatureOverviewTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ const FeatureOverviewTab = () => {
<EuiDescriptionListTitle>FeatureView</EuiDescriptionListTitle>
<EuiDescriptionListDescription>
<EuiCustomLink
href={`${process.env.PUBLIC_URL || ""}/p/${projectName}/feature-view/${FeatureViewName}`}
to={`${process.env.PUBLIC_URL || ""}/p/${projectName}/feature-view/${FeatureViewName}`}>
to={`${process.env.PUBLIC_URL || ""}/p/${projectName}/feature-view/${FeatureViewName}`}
>
{FeatureViewName}
</EuiCustomLink>
</EuiDescriptionListDescription>
Expand Down
1 change: 0 additions & 1 deletion ui/src/pages/saved-data-sets/DatasetsListingTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const DatasetsListingTable = ({ datasets }: DatasetsListingTableProps) => {
render: (name: string) => {
return (
<EuiCustomLink
href={`${process.env.PUBLIC_URL || ""}/p/${projectName}/data-set/${name}`}
to={`${process.env.PUBLIC_URL || ""}/p/${projectName}/data-set/${name}`}
>
{name}
Expand Down

0 comments on commit f8d41d1

Please sign in to comment.