Skip to content
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

open internal links in the app #2124 #2385

Merged
merged 10 commits into from
Dec 19, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface ParseMessageLinkData {
params: Record<string, string>;
}

export const parseMessageLink = (url: string): ParseMessageLinkData | null => {
export const parseMessageLink = (url?: string): ParseMessageLinkData | null => {
if (!url) {
return null;
}
Expand Down
36 changes: 36 additions & 0 deletions src/shared/hooks/useInternalLink.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { useCallback } from "react";
import { useHistory } from "react-router-dom";
import { InternalLinkData } from "../components";
import { QueryParamKey, ROUTE_PATHS } from "../constants";
import { useRoutesContext } from "../contexts";
import { getParamsFromOneOfRoutes } from "../utils";

const useInternalLink = () => {
const { getCommonPagePath } = useRoutesContext();
const history = useHistory();

const onInternalLinkClick = useCallback((data: InternalLinkData) => {
const feedPageParams = getParamsFromOneOfRoutes<{ id: string }>(
data.pathname,
[ROUTE_PATHS.COMMON, ROUTE_PATHS.V04_COMMON],
);

if (!feedPageParams) {
return;
}

const itemId = data.params[QueryParamKey.Item];
const messageId = data.params[QueryParamKey.Message];

history.push(
getCommonPagePath(feedPageParams.id, {
item: itemId,
message: messageId,
}),
);
}, []);

return { onInternalLinkClick };
};

export default useInternalLink;
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

.link {
color: var(--primary-fill);
cursor: pointer;
text-decoration: underline;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import React, { FC } from "react";
import classNames from "classnames";
import { parseMessageLink } from "@/shared/components/Chat/ChatMessage/components/ChatMessageLinkify/utils";
import useInternalLink from "@/shared/hooks/useInternalLink";
import { LinkElement } from "../../../../types";
import { ElementAttributes } from "../../types";
import { InlineChromiumBugfix } from "../InlineChromiumBugfix";
Expand All @@ -12,6 +14,22 @@ interface LinkProps {

const Link: FC<LinkProps> = (props) => {
const { attributes, element, children } = props;
const { onInternalLinkClick } = useInternalLink();
const internalLink = parseMessageLink(element.url);

if (internalLink) {
return (
<a
{...attributes}
className={classNames(styles.link, attributes.className)}
onClick={() => onInternalLinkClick(internalLink)}
>
<InlineChromiumBugfix />
{children}
<InlineChromiumBugfix />
</a>
);
}

return (
<a
Expand Down
Loading