Skip to content

Commit

Permalink
Fix linting
Browse files Browse the repository at this point in the history
  • Loading branch information
renchap committed Jun 18, 2024
1 parent 2bdaac0 commit c215bbf
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 56 deletions.
37 changes: 19 additions & 18 deletions app/javascript/mastodon/components/account_bio.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,35 @@ export const AccountBio: React.FC<{
note: string;
className: string;
}> = ({ note, className }) => {
const ref = useRef(null);
const ref = useRef<HTMLDivElement>(null);
const history = useHistory();
const dispatch = useAppDispatch();

const handleHashtagClick = useCallback(
(e) => {
if (e.button === 0 && !(e.ctrlKey || e.metaKey)) {
(e: MouseEvent) => {
const { currentTarget } = e;
if (!(currentTarget instanceof HTMLElement)) return;
const { textContent } = currentTarget;

if (textContent && e.button === 0 && !(e.ctrlKey || e.metaKey)) {
e.preventDefault();
history.push(`/tags/${e.currentTarget.textContent.replace(/^#/, '')}`);
history.push(`/tags/${textContent.replace(/^#/, '')}`);
}
},
[history],
);

const handleMentionClick = useCallback(
(e) => {
(e: MouseEvent) => {
const { currentTarget } = e;
if (!(currentTarget instanceof HTMLAnchorElement)) return;

if (e.button === 0 && !(e.ctrlKey || e.metaKey)) {
e.preventDefault();

dispatch(
openURL(e.currentTarget.href, history, () => {
window.location = e.currentTarget.href;
openURL(currentTarget.href, history, () => {
window.location.href = currentTarget.href;
}),
);
}
Expand All @@ -43,15 +50,12 @@ export const AccountBio: React.FC<{
return;
}

const links = ref.current.querySelectorAll('a');
const links = ref.current.querySelectorAll<HTMLAnchorElement>('a');

for (const link of links) {
if (
link.textContent[0] === '#' ||
(link.previousSibling?.textContent &&
link.previousSibling.textContent[
link.previousSibling.textContent.length - 1
] === '#')
link.textContent?.[0] === '#' ||
link.previousSibling?.textContent?.endsWith('#')
) {
link.addEventListener('click', handleHashtagClick, false);
} else if (link.classList.contains('mention')) {
Expand All @@ -62,11 +66,8 @@ export const AccountBio: React.FC<{
return () => {
for (const link of links) {
if (
link.textContent[0] === '#' ||
(link.previousSibling?.textContent &&
link.previousSibling.textContent[
link.previousSibling.textContent.length - 1
] === '#')
link.textContent?.[0] === '#' ||
link.previousSibling?.textContent?.endsWith('#')
) {
link.removeEventListener('click', handleHashtagClick);
} else if (link.classList.contains('mention')) {
Expand Down
47 changes: 25 additions & 22 deletions app/javascript/mastodon/components/account_fields.tsx
Original file line number Diff line number Diff line change
@@ -1,40 +1,49 @@

import { useEffect, useRef, useCallback } from 'react';

import classNames from 'classnames';
import { useHistory } from 'react-router-dom';

import CheckIcon from '@/material-icons/400-24px/check.svg?react';
import { openURL } from 'mastodon/actions/search';
import type { ApiAccountFieldJSON } from 'mastodon/api_types/accounts';
import { Icon } from 'mastodon/components/icon';
import type { Account } from 'mastodon/models/account';
import { useAppDispatch } from 'mastodon/store';

export const AccountFields: React.FC<{
fields: ApiAccountFieldJSON[];
fields: Account['fields'];
}> = ({ fields }) => {
const ref = useRef(null);
const ref = useRef<HTMLDivElement>(null);
const history = useHistory();
const dispatch = useAppDispatch();

const handleHashtagClick = useCallback(
(e) => {
(e: MouseEvent) => {
const { currentTarget } = e;
if (!(currentTarget instanceof HTMLElement)) return;

if (e.button === 0 && !(e.ctrlKey || e.metaKey)) {
const { textContent } = currentTarget;
if (!textContent) return;

e.preventDefault();
history.push(`/tags/${e.currentTarget.textContent.replace(/^#/, '')}`);
history.push(`/tags/${textContent.replace(/^#/, '')}`);
}
},
[history],
);

const handleMentionClick = useCallback(
(e) => {
(e: MouseEvent) => {
const { currentTarget } = e;

if (!(currentTarget instanceof HTMLAnchorElement)) return;

if (e.button === 0 && !(e.ctrlKey || e.metaKey)) {
e.preventDefault();

dispatch(
openURL(e.currentTarget.href, history, () => {
window.location = e.currentTarget.href;
openURL(currentTarget.href, history, () => {
window.location.href = currentTarget.href;
}),
);
}
Expand All @@ -43,19 +52,16 @@ export const AccountFields: React.FC<{
);

useEffect(() => {
if (ref.current === null) {
if (!ref.current) {
return;
}

const links = ref.current.querySelectorAll('a');
const links = ref.current.querySelectorAll<HTMLAnchorElement>('a');

for (const link of links) {
if (
link.textContent[0] === '#' ||
(link.previousSibling?.textContent &&
link.previousSibling.textContent[
link.previousSibling.textContent.length - 1
] === '#')
link.textContent?.[0] === '#' ||
link.previousSibling?.textContent?.endsWith('#')
) {
link.addEventListener('click', handleHashtagClick, false);
} else if (link.classList.contains('mention')) {
Expand All @@ -66,11 +72,8 @@ export const AccountFields: React.FC<{
return () => {
for (const link of links) {
if (
link.textContent[0] === '#' ||
(link.previousSibling?.textContent &&
link.previousSibling.textContent[
link.previousSibling.textContent.length - 1
] === '#')
link.textContent?.[0] === '#' ||
link.previousSibling?.textContent?.endsWith('#')
) {
link.removeEventListener('click', handleHashtagClick);
} else if (link.classList.contains('mention')) {
Expand All @@ -96,7 +99,7 @@ export const AccountFields: React.FC<{
className='translate'
/>

<dd className='translate' title={pair.get('value_plain')}>
<dd className='translate' title={pair.get('value_plain') ?? ''}>
{pair.get('verified_at') && (
<Icon id='check' icon={CheckIcon} className='verified__mark' />
)}
Expand Down
4 changes: 2 additions & 2 deletions app/javascript/mastodon/components/follow_button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { useCallback, useEffect } from 'react';

import { useIntl, defineMessages } from 'react-intl';


import {
fetchRelationships,
followAccount,
Expand All @@ -11,7 +10,7 @@ import {
import { Button } from 'mastodon/components/button';
import { LoadingIndicator } from 'mastodon/components/loading_indicator';
import { me } from 'mastodon/initial_state';
import { useAppDispatch , useAppSelector } from 'mastodon/store';
import { useAppDispatch, useAppSelector } from 'mastodon/store';

const messages = defineMessages({
unfollow: { id: 'account.unfollow', defaultMessage: 'Unfollow' },
Expand All @@ -35,6 +34,7 @@ export const FollowButton: React.FC<{
}, [dispatch, accountId]);

const handleClick = useCallback(() => {
if (!relationship) return;
if (accountId === me) {
return;
} else if (relationship.following || relationship.requested) {
Expand Down
35 changes: 22 additions & 13 deletions app/javascript/mastodon/components/hover_card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import classNames from 'classnames';
import { Link } from 'react-router-dom';

import Overlay from 'react-overlays/Overlay';
import type { OffsetValue } from 'react-overlays/esm/usePopper';

import { fetchAccount } from 'mastodon/actions/accounts';
import { AccountBio } from 'mastodon/components/account_bio';
Expand All @@ -17,42 +18,48 @@ import { ShortNumber } from 'mastodon/components/short_number';
import { domain } from 'mastodon/initial_state';
import { useAppSelector, useAppDispatch } from 'mastodon/store';

const offset = [-12, 4];
const offset = [-12, 4] as OffsetValue;
const enterDelay = 100;
const leaveDelay = 500;

export const HoverCard: React.FC = () => {
const [open, setOpen] = useState(false);
const [accountId, setAccountId] = useState();
const cardRef = useRef();
const anchorRef = useRef();
const [accountId, setAccountId] = useState<string | undefined>();
const cardRef = useRef<HTMLDivElement>(null);
const anchorRef = useRef<HTMLElement | null>(null);
const dispatch = useAppDispatch();
const account = useAppSelector((state) => state.accounts.get(accountId));
const leaveTimerRef = useRef();
const enterTimerRef = useRef();
const account = useAppSelector((state) =>
accountId ? state.accounts.get(accountId) : undefined,
);
const leaveTimerRef = useRef<ReturnType<typeof setTimeout>>();
const enterTimerRef = useRef<ReturnType<typeof setTimeout>>();

const handleAnchorMouseEnter = useCallback(
(e) => {
if (e.target.matches('[data-hover-card]')) {
(e: MouseEvent) => {
const { target } = e;

if (!(target instanceof HTMLElement)) return;

if (target.matches('[data-hover-card]')) {
clearTimeout(leaveTimerRef.current);
clearTimeout(enterTimerRef.current);

enterTimerRef.current = setTimeout(() => {
anchorRef.current = e.target;
anchorRef.current = target;
setOpen(true);
setAccountId(e.target.getAttribute('data-hover-card'));
setAccountId(target.getAttribute('data-hover-card') ?? undefined);
}, enterDelay);
}

if (e.target === cardRef.current?.parentNode) {
if (target === cardRef.current?.parentNode) {
clearTimeout(leaveTimerRef.current);
}
},
[setOpen, setAccountId],
);

const handleAnchorMouseLeave = useCallback(
(e) => {
(e: MouseEvent) => {
if (
e.target === anchorRef.current ||
e.target === cardRef.current?.parentNode
Expand Down Expand Up @@ -96,6 +103,8 @@ export const HoverCard: React.FC = () => {
dispatch(fetchAccount(accountId));
}, [dispatch, accountId]);

if (!accountId) return null;

return (
<Overlay
rootClose
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ Source.propTypes = {
const Card = ({ id, sources }) => {
const intl = useIntl();
const account = useSelector(state => state.getIn(['accounts', id]));
const relationship = useSelector(state => state.getIn(['relationships', id]));
const firstVerifiedField = account.get('fields').find(item => !!item.get('verified_at'));
const dispatch = useDispatch();

Expand Down

0 comments on commit c215bbf

Please sign in to comment.