From a2d2d694c3d90f6805cbd4ddee76ad10e58c04eb Mon Sep 17 00:00:00 2001 From: danidfra Date: Mon, 10 Mar 2025 16:48:03 -0300 Subject: [PATCH] Refactor: Move InstanceFavicon to its own file --- src/components/account.tsx | 50 ++----------------- src/components/instance-favicon.tsx | 49 ++++++++++++++++++ .../explorer/components/popular-accounts.tsx | 2 +- 3 files changed, 53 insertions(+), 48 deletions(-) create mode 100644 src/components/instance-favicon.tsx diff --git a/src/components/account.tsx b/src/components/account.tsx index 60a1f9b2d..48db4b3bd 100644 --- a/src/components/account.tsx +++ b/src/components/account.tsx @@ -1,9 +1,10 @@ import pencilIcon from '@tabler/icons/outline/pencil.svg'; -import { useRef, useState } from 'react'; +import { useRef } from 'react'; import { defineMessages, useIntl, FormattedMessage } from 'react-intl'; -import { Link, useHistory } from 'react-router-dom'; +import { Link } from 'react-router-dom'; import HoverRefWrapper from 'soapbox/components/hover-ref-wrapper.tsx'; +import { InstanceFavicon } from 'soapbox/components/instance-favicon.tsx'; import Markup from 'soapbox/components/markup.tsx'; import Avatar from 'soapbox/components/ui/avatar.tsx'; import Emoji from 'soapbox/components/ui/emoji.tsx'; @@ -25,55 +26,10 @@ import RelativeTimestamp from './relative-timestamp.tsx'; import type { StatusApprovalStatus } from 'soapbox/normalizers/status.ts'; import type { Account as AccountSchema } from 'soapbox/schemas/index.ts'; -interface IInstanceFavicon { - account: AccountSchema; - disabled?: boolean; -} - const messages = defineMessages({ bot: { id: 'account.badges.bot', defaultMessage: 'Bot' }, }); -export const InstanceFavicon: React.FC = ({ account, disabled }) => { - const history = useHistory(); - const [missing, setMissing] = useState(false); - - const handleError = () => setMissing(true); - - const handleClick: React.MouseEventHandler = (e) => { - e.stopPropagation(); - - if (disabled) return; - - const timelineUrl = `/timeline/${account.domain}`; - if (!(e.ctrlKey || e.metaKey)) { - history.push(timelineUrl); - } else { - window.open(timelineUrl, '_blank'); - } - }; - - if (missing || !account.pleroma?.favicon) { - return null; - } - - return ( - - ); -}; - interface IProfilePopper { condition: boolean; wrapper: (children: React.ReactNode) => React.ReactNode; diff --git a/src/components/instance-favicon.tsx b/src/components/instance-favicon.tsx new file mode 100644 index 000000000..551903d9e --- /dev/null +++ b/src/components/instance-favicon.tsx @@ -0,0 +1,49 @@ +import { useState } from 'react'; +import { useHistory } from 'react-router-dom'; + +import type { Account as AccountSchema } from 'soapbox/schemas/index.ts'; + +interface IInstanceFavicon { + account: AccountSchema; + disabled?: boolean; +} + +export const InstanceFavicon: React.FC = ({ account, disabled }) => { + const history = useHistory(); + const [missing, setMissing] = useState(false); + + const handleError = () => setMissing(true); + + const handleClick: React.MouseEventHandler = (e) => { + e.stopPropagation(); + + if (disabled) return; + + const timelineUrl = `/timeline/${account.domain}`; + if (!(e.ctrlKey || e.metaKey)) { + history.push(timelineUrl); + } else { + window.open(timelineUrl, '_blank'); + } + }; + + if (missing || !account.pleroma?.favicon) { + return null; + } + + return ( + + ); +}; \ No newline at end of file diff --git a/src/features/explorer/components/popular-accounts.tsx b/src/features/explorer/components/popular-accounts.tsx index 02b839405..8d3d4e458 100644 --- a/src/features/explorer/components/popular-accounts.tsx +++ b/src/features/explorer/components/popular-accounts.tsx @@ -5,7 +5,7 @@ import { Link } from 'react-router-dom'; import { Swiper, SwiperSlide } from 'swiper/react'; import { useAccount } from 'soapbox/api/hooks/index.ts'; -import { InstanceFavicon } from 'soapbox/components/account.tsx'; +import { InstanceFavicon } from 'soapbox/components/instance-favicon.tsx'; import Avatar from 'soapbox/components/ui/avatar.tsx'; import HStack from 'soapbox/components/ui/hstack.tsx'; import IconButton from 'soapbox/components/ui/icon-button.tsx';