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

refactor: hyperlink extract common logic [TOL-1637] #1568

Merged
merged 1 commit into from
Jan 4, 2024
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
@@ -1,17 +1,15 @@
import * as React from 'react';

import { FieldAppSDK, Link } from '@contentful/app-sdk';
import { Link } from '@contentful/app-sdk';
import { Text } from '@contentful/f36-components';

import { useContentfulEditor } from '../../../ContentfulEditorProvider';
import { findNodePath, isChildPath } from '../../../internal/queries';
import { Element, RenderElementProps } from '../../../internal/types';
import { useSdkContext } from '../../../SdkProvider';
import { useLinkTracking } from '../../links-tracking';
import { useEntityInfo } from '../useEntityInfo';
import { handleEditLink, handleRemoveLink } from './linkHandlers';
import { LinkPopover } from './LinkPopover';
import { styles } from './styles';
import { useHyperlinkCommon } from './useHyperlinkCommon';

export type HyperlinkElementProps = {
element: Element & {
Expand All @@ -32,13 +30,9 @@ export type HyperlinkElementProps = {
};

export function EntityHyperlink(props: HyperlinkElementProps) {
const editor = useContentfulEditor();
const sdk: FieldAppSDK = useSdkContext();
const focus = editor.selection?.focus;
const { target } = props.element.data;
const { editor, sdk, isLinkFocused, pathToElement } = useHyperlinkCommon(props.element);
const { onEntityFetchComplete } = useLinkTracking();
const pathToElement = findNodePath(editor, props.element);
const isLinkFocused = pathToElement && focus && isChildPath(focus.path, pathToElement);
const { target } = props.element.data;

const tooltipContent = useEntityInfo({
target,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import * as React from 'react';

import { FieldAppSDK, Link } from '@contentful/app-sdk';
import { Link } from '@contentful/app-sdk';
import { Text } from '@contentful/f36-components';

import { useContentfulEditor } from '../../../ContentfulEditorProvider';
import { findNodePath, isChildPath } from '../../../internal/queries';
import { Element, RenderElementProps } from '../../../internal/types';
import { useSdkContext } from '../../../SdkProvider';
import { useLinkTracking } from '../../links-tracking';
import { useResourceEntityInfo } from '../useResourceEntityInfo';
import { handleEditLink, handleRemoveLink } from './linkHandlers';
import { LinkPopover } from './LinkPopover';
import { styles } from './styles';
import { useHyperlinkCommon } from './useHyperlinkCommon';

export type ResourceHyperlinkProps = {
element: Element & {
Expand All @@ -31,13 +29,9 @@ export type ResourceHyperlinkProps = {
};

export function ResourceHyperlink(props: ResourceHyperlinkProps) {
const editor = useContentfulEditor();
const sdk: FieldAppSDK = useSdkContext();
const focus = editor.selection?.focus;
const { target } = props.element.data;
const { editor, sdk, isLinkFocused, pathToElement } = useHyperlinkCommon(props.element);
const { onEntityFetchComplete } = useLinkTracking();
const pathToElement = findNodePath(editor, props.element);
const isLinkFocused = pathToElement && focus && isChildPath(focus.path, pathToElement);
const { target } = props.element.data;

const tooltipContent = useResourceEntityInfo({ target, onEntityFetchComplete });

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import * as React from 'react';

import { FieldAppSDK, Link } from '@contentful/app-sdk';
import { Link } from '@contentful/app-sdk';
import { TextLink } from '@contentful/f36-components';

import { useContentfulEditor } from '../../../ContentfulEditorProvider';
import { findNodePath, isChildPath } from '../../../internal/queries';
import { Element, RenderElementProps } from '../../../internal/types';
import { useSdkContext } from '../../../SdkProvider';
import { handleCopyLink, handleEditLink, handleRemoveLink } from './linkHandlers';
import { LinkPopover } from './LinkPopover';
import { styles } from './styles';
import { useHyperlinkCommon } from './useHyperlinkCommon';

type HyperlinkElementProps = {
type UrlHyperlinkProps = {
element: Element & {
data: {
uri?: string;
Expand All @@ -29,13 +27,9 @@ type HyperlinkElementProps = {
children: Pick<RenderElementProps, 'children'>;
};

export function UrlHyperlink(props: HyperlinkElementProps) {
const editor = useContentfulEditor();
const sdk: FieldAppSDK = useSdkContext();
const focus = editor.selection?.focus;
export function UrlHyperlink(props: UrlHyperlinkProps) {
const { editor, sdk, isLinkFocused, pathToElement } = useHyperlinkCommon(props.element);
const uri = props.element.data?.uri;
const pathToElement = findNodePath(editor, props.element);
const isLinkFocused = pathToElement && focus && isChildPath(focus.path, pathToElement);

const popoverText = (
<TextLink className={styles.openLink} href={uri} rel="noopener noreferrer" target="_blank">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { FieldAppSDK } from '@contentful/app-sdk';

import { useContentfulEditor } from '../../../ContentfulEditorProvider';
import { findNodePath, isChildPath } from '../../../internal/queries';
import { useSdkContext } from '../../../SdkProvider';

export function useHyperlinkCommon(element) {
const editor = useContentfulEditor();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I'm not sure I see the benefits for editor and sdk feels like adding a layer of complexity rather than removing. isLinkFocused and pathToElement make sense

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please feel free to ignore though just my opinion

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm yes not sure, I think we need editor to calculate pathToElement

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right okay then

const sdk: FieldAppSDK = useSdkContext();
const focus = editor.selection?.focus;
const pathToElement = findNodePath(editor, element);
const isLinkFocused = pathToElement && focus && isChildPath(focus.path, pathToElement);

return { editor, sdk, isLinkFocused, pathToElement };
}
Loading