From 58d92742b4ff6da73e97022984e9d9a1adb32517 Mon Sep 17 00:00:00 2001 From: M-ZubairAhmed Date: Mon, 26 Aug 2024 08:41:52 +0000 Subject: [PATCH] [MM-60261] Replace "useTooltip" of "components/common/hooks/useTooltip" to WithTooltip (#28037) --- .../components/common/hooks/useTooltip.tsx | 148 --- .../file_attachment.test.tsx.snap | 1048 +++++++++-------- .../file_attachment/archived_tooltip.tsx | 1 + .../file_attachment/file_attachment.tsx | 47 +- .../post_priority_picker_overlay.tsx | 24 +- 5 files changed, 575 insertions(+), 693 deletions(-) delete mode 100644 webapp/channels/src/components/common/hooks/useTooltip.tsx diff --git a/webapp/channels/src/components/common/hooks/useTooltip.tsx b/webapp/channels/src/components/common/hooks/useTooltip.tsx deleted file mode 100644 index a90ede3197..0000000000 --- a/webapp/channels/src/components/common/hooks/useTooltip.tsx +++ /dev/null @@ -1,148 +0,0 @@ -// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. -// See LICENSE.txt for license information. - -import {useHover, useInteractions, useFloating, arrow, offset, autoPlacement} from '@floating-ui/react'; -import type {Strategy, Placement, ReferenceType} from '@floating-ui/react'; -import classNames from 'classnames'; -import type {ReactNode, HTMLProps} from 'react'; -import React, {useState, useRef} from 'react'; -import ReactDOM from 'react-dom'; - -import {Constants} from 'utils/constants'; - -interface TooltipOptions { - message: ReactNode; - strategy?: Strategy; - placement: Placement; - allowedPlacements?: Placement[]; - hoverDelay?: Exclude[1], undefined>['delay']; - zIndex?: number; - mountPoint?: string | Element; -} - -const defaultOptions: Required> = { - strategy: 'fixed', - hoverDelay: { - open: Constants.OVERLAY_TIME_DELAY, - close: 0, - }, - zIndex: 1, - mountPoint: 'root', -}; - -const transitionTime = 150; - -interface TooltipReturn { - setReference: (node: ReferenceType | null) => void; - getReferenceProps: (userProps?: HTMLProps) => Record; - tooltip: ReactNode; -} - -export default function useTooltip(options: TooltipOptions): TooltipReturn { - const [open, setOpen] = useState(false); - const [visible, setVisible] = useState(false); - const transition = useRef(null); - const arrowRef = useRef(null); - const effectiveStrategy = options.strategy || defaultOptions.strategy; - const effectiveMountpoint = options.mountPoint || defaultOptions.mountPoint; - const effectiveAllowedPlacements = options.allowedPlacements ?? [options.placement]; - const { - x, - y, - strategy, - placement, - refs: { - setReference, - setFloating, - }, - middlewareData: { - arrow: { - x: arrowX, - y: arrowY, - } = {}, - }, - context, - } = useFloating({ - open, - onOpenChange: (nowOpen) => { - if (transition.current) { - clearTimeout(transition.current); - } - if (nowOpen) { - setOpen(nowOpen); - setVisible(true); - } else { - setVisible(false); - setTimeout(() => { - setOpen(nowOpen); - }, transitionTime); - } - }, - middleware: [ - autoPlacement({ - allowedPlacements: effectiveAllowedPlacements, - autoAlignment: false, - }), - offset(10), - arrow({ - element: arrowRef, - padding: 4, - }), - ], - placement: options.placement, - strategy: effectiveStrategy, - }); - - const {getReferenceProps, getFloatingProps} = useInteractions([ - useHover( - context, - { - delay: options.hoverDelay || defaultOptions.hoverDelay, - }, - ), - ]); - - const content = ( -
- {options.message} -
-
- ); - - let tooltip: ReactNode = false; - - if (open) { - if (effectiveStrategy === 'fixed') { - tooltip = ReactDOM.createPortal( - content, - typeof effectiveMountpoint === 'string' ? document.getElementById(effectiveMountpoint) as Element : effectiveMountpoint, - ); - } - } - - return { - setReference, - getReferenceProps, - tooltip, - }; -} diff --git a/webapp/channels/src/components/file_attachment/__snapshots__/file_attachment.test.tsx.snap b/webapp/channels/src/components/file_attachment/__snapshots__/file_attachment.test.tsx.snap index 39301daad6..2109ae56ed 100644 --- a/webapp/channels/src/components/file_attachment/__snapshots__/file_attachment.test.tsx.snap +++ b/webapp/channels/src/components/file_attachment/__snapshots__/file_attachment.test.tsx.snap @@ -1,585 +1,641 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`FileAttachment should match snapshot, after change from file to image 1`] = ` -
} > - - -
-
-
- - test.png - - - PNG - - - 100B - -
-
- - - + +
+
+
+ + test.png + + + PNG + + + 100B + +
+
+ + + +
-
+ `; exports[`FileAttachment should match snapshot, regular file 1`] = ` -
} > - - -
-
-
- - test.pdf - - - PDF - - - 100B - -
-
- - - + +
+
+
+ + test.pdf + + + PDF + + + 100B + +
+
+ + + +
-
+ `; exports[`FileAttachment should match snapshot, regular image 1`] = ` -
} > - -
-
-
- - test.png - - - PNG - - - 100B - -
-
- - - + +
+
+
+ + test.png + + + PNG + + + 100B + +
+
+ + + +
-
+ `; exports[`FileAttachment should match snapshot, small image 1`] = ` -
} > - -
-
-
- - test.png - - - PNG - - - 100B - -
-
- - - + +
+
+
+ + test.png + + + PNG + + + 100B + +
+
+ + + +
-
+ `; exports[`FileAttachment should match snapshot, svg image 1`] = ` -
} > - - -
-
-
- - test.svg - - - SVG - - - 100B - -
-
- - - + +
+
+
+ + test.svg + + + SVG + + + 100B + +
+
+ + + +
-
+ `; exports[`FileAttachment should match snapshot, when file is not loaded 1`] = ` -
} > - -
-
-
- - test.pdf - - - JPG - - - 100B - -
-
- - - + +
+
+
+ + test.pdf + + + JPG + + + 100B + +
+
+ + + +
-
+ `; exports[`FileAttachment should match snapshot, with compact display 1`] = ` -
} >
- - - + + + +
-
+ `; exports[`FileAttachment should match snapshot, without compact display and without can download 1`] = ` -
} > - - -
-
+ + +
- - test.pdf - - - PDF - - - 100B - + + test.pdf + + + PDF + + + 100B + +
-
+ `; diff --git a/webapp/channels/src/components/file_attachment/archived_tooltip.tsx b/webapp/channels/src/components/file_attachment/archived_tooltip.tsx index ade4de043e..b73107d377 100644 --- a/webapp/channels/src/components/file_attachment/archived_tooltip.tsx +++ b/webapp/channels/src/components/file_attachment/archived_tooltip.tsx @@ -10,6 +10,7 @@ import {asGBString} from 'utils/limits'; export default function ArchivedTooltip() { const intl = useIntl(); + return ( <>
diff --git a/webapp/channels/src/components/file_attachment/file_attachment.tsx b/webapp/channels/src/components/file_attachment/file_attachment.tsx index a6f63924ff..9327962b9c 100644 --- a/webapp/channels/src/components/file_attachment/file_attachment.tsx +++ b/webapp/channels/src/components/file_attachment/file_attachment.tsx @@ -10,7 +10,6 @@ import type {FileInfo} from '@mattermost/types/files'; import {getFileThumbnailUrl, getFileUrl} from 'mattermost-redux/utils/file_utils'; -import useTooltip from 'components/common/hooks/useTooltip'; import GetPublicModal from 'components/get_public_link_modal'; import Menu from 'components/widgets/menu/menu'; import MenuWrapper from 'components/widgets/menu/menu_wrapper'; @@ -65,16 +64,6 @@ export default function FileAttachment(props: Props) { const [keepOpen, setKeepOpen] = useState(false); const [openUp, setOpenUp] = useState(false); - const { - setReference, - getReferenceProps, - tooltip: archivedTooltip, - } = useTooltip({ - message: , - placement: 'right', - allowedPlacements: ['right', 'top'], - }); - const buttonRef = useRef(null); const handleImageLoaded = () => { @@ -368,18 +357,19 @@ export default function FileAttachment(props: Props) { ); } - const content = - ( + return ( + } + disabled={!fileInfo.archived} + >
{fileThumbnail}
@@ -388,15 +378,6 @@ export default function FileAttachment(props: Props) { {filenameOverlay}
- ); - - if (fileInfo.archived) { - return ( - <> - {content} - {archivedTooltip} - - ); - } - return content; +
+ ); } diff --git a/webapp/channels/src/components/post_priority/post_priority_picker_overlay.tsx b/webapp/channels/src/components/post_priority/post_priority_picker_overlay.tsx index b3eed8dcae..ddefc450d0 100644 --- a/webapp/channels/src/components/post_priority/post_priority_picker_overlay.tsx +++ b/webapp/channels/src/components/post_priority/post_priority_picker_overlay.tsx @@ -22,7 +22,7 @@ import {AlertCircleOutlineIcon} from '@mattermost/compass-icons/components'; import type {PostPriorityMetadata} from '@mattermost/types/posts'; import {IconContainer} from 'components/advanced_text_editor/formatting_bar/formatting_icon'; -import useTooltip from 'components/common/hooks/useTooltip'; +import WithTooltip from 'components/with_tooltip'; import PostPriorityPicker from './post_priority_picker'; @@ -42,16 +42,6 @@ function PostPriorityPickerOverlay({ const [pickerOpen, setPickerOpen] = useState(false); const {formatMessage} = useIntl(); - const messagePriority = formatMessage({id: 'shortcuts.msgs.formatting_bar.post_priority', defaultMessage: 'Message priority'}); - const { - setReference: setTooltipRef, - getReferenceProps: getTooltipReferenceProps, - tooltip, - } = useTooltip({ - placement: 'top', - message: messagePriority, - }); - const handleClose = useCallback(() => { setPickerOpen(false); onClose(); @@ -91,11 +81,14 @@ function PostPriorityPickerOverlay({ useRole(pickerContext), ]); + const messagePriority = formatMessage({id: 'shortcuts.msgs.formatting_bar.post_priority', defaultMessage: 'Message priority'}); + return ( <> -
-
+ {pickerOpen && ( )} - {!pickerOpen && tooltip} ); }