2023-03-31 17:40:14 +00:00
|
|
|
import React from "react";
|
2022-02-04 00:15:50 +00:00
|
|
|
import { Link } from "react-router";
|
|
|
|
|
2023-03-31 17:40:14 +00:00
|
|
|
import { buildQueryStringFromParams, QueryParams } from "utilities/url";
|
|
|
|
import { pick } from "lodash";
|
2022-02-04 00:15:50 +00:00
|
|
|
|
|
|
|
interface ILinkWithContextProps {
|
|
|
|
className: string;
|
|
|
|
children: React.ReactChild | React.ReactChild[];
|
2023-03-31 17:40:14 +00:00
|
|
|
currentQueryParams: QueryParams;
|
2022-02-04 00:15:50 +00:00
|
|
|
to: string;
|
2023-03-31 17:40:14 +00:00
|
|
|
withParams: {
|
|
|
|
type: "query";
|
|
|
|
names: string[];
|
|
|
|
};
|
2022-02-04 00:15:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const LinkWithContext = ({
|
|
|
|
className,
|
|
|
|
children,
|
2023-03-31 17:40:14 +00:00
|
|
|
currentQueryParams,
|
2022-02-04 00:15:50 +00:00
|
|
|
to,
|
2023-03-31 17:40:14 +00:00
|
|
|
withParams,
|
2022-02-04 00:15:50 +00:00
|
|
|
}: ILinkWithContextProps): JSX.Element => {
|
2023-03-31 17:40:14 +00:00
|
|
|
let queryString = "";
|
|
|
|
if (withParams.type === "query") {
|
|
|
|
const newParams = pick(currentQueryParams, withParams.names);
|
|
|
|
queryString = buildQueryStringFromParams(newParams);
|
|
|
|
}
|
2022-02-04 00:15:50 +00:00
|
|
|
return (
|
2023-03-31 17:40:14 +00:00
|
|
|
<Link
|
|
|
|
className={className}
|
|
|
|
to={queryString.length ? `${to}?${queryString}` : to}
|
|
|
|
>
|
2022-02-04 00:15:50 +00:00
|
|
|
{children}
|
|
|
|
</Link>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
export default LinkWithContext;
|