Skip to content

Commit

Permalink
Merge pull request #2385 from daostack/cw-2124-open-internal-link-in-…
Browse files Browse the repository at this point in the history
…the-app

open internal links in the app #2124
  • Loading branch information
roienatan authored Dec 19, 2023
2 parents 7bbfc13 + 7a324d7 commit a0a7da6
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
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

0 comments on commit a0a7da6

Please sign in to comment.