mirror of
https://github.com/mastodon/mastodon.git
synced 2026-09-13 02:36:36 -05:00
Adds useToggle hook (#39729)
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { lazy, Suspense, useCallback, useState } from 'react';
|
||||
import { lazy, Suspense, useCallback } from 'react';
|
||||
|
||||
import { openModal } from '@/mastodon/actions/modal';
|
||||
import type { DeployPictureInPictureCallback } from '@/mastodon/actions/picture_in_picture';
|
||||
@@ -6,6 +6,7 @@ import { deployPictureInPicture } from '@/mastodon/actions/picture_in_picture';
|
||||
import { CollectionPreviewCard } from '@/mastodon/features/collections/components/collection_preview_card';
|
||||
import Card from '@/mastodon/features/status/components/card';
|
||||
import { useExpandedStatus } from '@/mastodon/hooks/useStatus';
|
||||
import { useToggle } from '@/mastodon/hooks/useToggle';
|
||||
import { displayMedia } from '@/mastodon/initial_state';
|
||||
import type {
|
||||
MediaAttachment,
|
||||
@@ -130,7 +131,7 @@ const MediaAttachments: React.FC<{
|
||||
selectPictureInPicture(state, statusId),
|
||||
);
|
||||
|
||||
const [showMedia, setShowMedia] = useState(
|
||||
const [showMedia, { onToggle: handleToggleMediaVisibility }] = useToggle(
|
||||
() =>
|
||||
mediaFilters.length === 0 &&
|
||||
((displayMedia !== 'hide_all' && !sensitive) ||
|
||||
@@ -138,9 +139,6 @@ const MediaAttachments: React.FC<{
|
||||
);
|
||||
|
||||
const dispatch = useAppDispatch();
|
||||
const handleToggleMediaVisibility = useCallback(() => {
|
||||
setShowMedia((prev) => !prev);
|
||||
}, []);
|
||||
const handleOpenMedia = useCallback(
|
||||
(index: number) => {
|
||||
dispatch(
|
||||
|
||||
16
app/javascript/mastodon/hooks/useToggle.ts
Normal file
16
app/javascript/mastodon/hooks/useToggle.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { useCallback, useState } from 'react';
|
||||
|
||||
export function useToggle(initialValue: boolean | (() => boolean) = false) {
|
||||
const [value, setValue] = useState(initialValue);
|
||||
const onTrue = useCallback(() => {
|
||||
setValue(true);
|
||||
}, []);
|
||||
const onFalse = useCallback(() => {
|
||||
setValue(false);
|
||||
}, []);
|
||||
const onToggle = useCallback(() => {
|
||||
setValue((prevValue) => !prevValue);
|
||||
}, []);
|
||||
|
||||
return [value, { setValue, onTrue, onFalse, onToggle }] as const;
|
||||
}
|
||||
Reference in New Issue
Block a user