Refactor <ActionsModal> to TypeScript (#34559)

This commit is contained in:
Eugen Rochko 2025-04-28 13:43:42 +02:00 committed by GitHub
parent 17e4345eb2
commit 926c67c648
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 127 additions and 135 deletions

View file

@ -26,11 +26,12 @@ import {
import { openModal, closeModal } from 'mastodon/actions/modal';
import { CircularProgress } from 'mastodon/components/circular_progress';
import { isUserTouching } from 'mastodon/is_mobile';
import type {
MenuItem,
ActionMenuItem,
ExternalLinkMenuItem,
import {
isMenuItem,
isActionItem,
isExternalLinkItem,
} from 'mastodon/models/dropdown_menu';
import type { MenuItem } from 'mastodon/models/dropdown_menu';
import { useAppDispatch, useAppSelector } from 'mastodon/store';
import type { IconProp } from './icon';
@ -38,30 +39,6 @@ import { IconButton } from './icon_button';
let id = 0;
const isMenuItem = (item: unknown): item is MenuItem => {
if (item === null) {
return true;
}
return typeof item === 'object' && 'text' in item;
};
const isActionItem = (item: unknown): item is ActionMenuItem => {
if (!item || !isMenuItem(item)) {
return false;
}
return 'action' in item;
};
const isExternalLinkItem = (item: unknown): item is ExternalLinkMenuItem => {
if (!item || !isMenuItem(item)) {
return false;
}
return 'href' in item;
};
type RenderItemFn<Item = MenuItem> = (
item: Item,
index: number,
@ -354,6 +331,9 @@ export const Dropdown = <Item = MenuItem,>({
const open = currentId === openDropdownId;
const activeElement = useRef<HTMLElement | null>(null);
const targetRef = useRef<HTMLButtonElement | null>(null);
const prefetchAccountId = status
? status.getIn(['account', 'id'])
: undefined;
const handleClose = useCallback(() => {
if (activeElement.current) {
@ -402,8 +382,8 @@ export const Dropdown = <Item = MenuItem,>({
} else {
onOpen?.();
if (status) {
dispatch(fetchRelationships([status.getIn(['account', 'id'])]));
if (prefetchAccountId) {
dispatch(fetchRelationships([prefetchAccountId]));
}
if (isUserTouching()) {
@ -411,7 +391,6 @@ export const Dropdown = <Item = MenuItem,>({
openModal({
modalType: 'ACTIONS',
modalProps: {
status,
actions: items,
onClick: handleItemClick,
},
@ -431,11 +410,11 @@ export const Dropdown = <Item = MenuItem,>({
[
dispatch,
currentId,
prefetchAccountId,
scrollKey,
onOpen,
handleItemClick,
open,
status,
items,
handleClose,
],