Prevent incorrect animation config overwrites

This commit is contained in:
Sam Becker 2025-04-05 16:11:16 -05:00
parent 3152c4f2aa
commit b9b1c82c39
4 changed files with 32 additions and 5 deletions

View File

@ -48,9 +48,12 @@ function AnimateItems({
const { const {
hasLoaded, hasLoaded,
nextPhotoAnimation, nextPhotoAnimation,
getNextPhotoAnimationId,
clearNextPhotoAnimation, clearNextPhotoAnimation,
} = useAppState(); } = useAppState();
const nextPhotoAnimationId = useRef<string>(undefined);
const prefersReducedMotion = usePrefersReducedMotion(); const prefersReducedMotion = usePrefersReducedMotion();
const hasLoadedInitial = useRef(hasLoaded); const hasLoadedInitial = useRef(hasLoaded);
@ -104,9 +107,12 @@ function AnimateItems({
}, },
}, },
} : undefined} } : undefined}
onAnimationStart={() => {
nextPhotoAnimationId.current = getNextPhotoAnimationId?.();
}}
onAnimationComplete={() => { onAnimationComplete={() => {
if (animateFromAppState) { if (animateFromAppState) {
clearNextPhotoAnimation?.(); clearNextPhotoAnimation?.(nextPhotoAnimationId.current);
} }
onAnimationComplete?.(); onAnimationComplete?.();
}} }}

View File

@ -39,6 +39,7 @@ export default function PhotoLink({
href: pathForPhoto({ photo, ...categories }), href: pathForPhoto({ photo, ...categories }),
onClick: () => { onClick: () => {
if (nextPhotoAnimation) { if (nextPhotoAnimation) {
console.log('setNextPhotoAnimation', nextPhotoAnimation);
setNextPhotoAnimation?.(nextPhotoAnimation); setNextPhotoAnimation?.(nextPhotoAnimation);
} }
}, },

View File

@ -21,8 +21,9 @@ export type AppStateContextType = {
swrTimestamp?: number swrTimestamp?: number
invalidateSwr?: () => void invalidateSwr?: () => void
nextPhotoAnimation?: AnimationConfig nextPhotoAnimation?: AnimationConfig
setNextPhotoAnimation?: Dispatch<SetStateAction<AnimationConfig | undefined>> setNextPhotoAnimation?: (animationConfig?: AnimationConfig) => void
clearNextPhotoAnimation?: () => void getNextPhotoAnimationId?: () => string
clearNextPhotoAnimation?: (id?: string) => void
shouldRespondToKeyboardCommands?: boolean shouldRespondToKeyboardCommands?: boolean
setShouldRespondToKeyboardCommands?: Dispatch<SetStateAction<boolean>> setShouldRespondToKeyboardCommands?: Dispatch<SetStateAction<boolean>>
categoriesWithCounts?: categoriesWithCounts?:

View File

@ -25,6 +25,7 @@ import { isPathAdmin, PATH_ROOT } from '@/app/paths';
import { INITIAL_UPLOAD_STATE, UploadState } from '@/admin/upload'; import { INITIAL_UPLOAD_STATE, UploadState } from '@/admin/upload';
import { RecipeProps } from '@/recipe'; import { RecipeProps } from '@/recipe';
import { getCountsForCategoriesCachedAction } from '@/category/actions'; import { getCountsForCategoriesCachedAction } from '@/category/actions';
import { nanoid } from 'nanoid';
export default function AppStateProvider({ export default function AppStateProvider({
children, children,
@ -42,8 +43,25 @@ export default function AppStateProvider({
useState(false); useState(false);
const [swrTimestamp, setSwrTimestamp] = const [swrTimestamp, setSwrTimestamp] =
useState(Date.now()); useState(Date.now());
const [nextPhotoAnimation, setNextPhotoAnimation] = const [nextPhotoAnimation, _setNextPhotoAnimation] =
useState<AnimationConfig>(); useState<AnimationConfig>();
const setNextPhotoAnimation = useCallback((animation?: AnimationConfig) => {
_setNextPhotoAnimation(animation);
setNextPhotoAnimationId(undefined);
}, []);
const [nextPhotoAnimationId, setNextPhotoAnimationId] =
useState<string>();
const getNextPhotoAnimationId = useCallback(() => {
const id = nanoid();
setNextPhotoAnimationId(id);
return id;
}, []);
const clearNextPhotoAnimation = useCallback((id?: string) => {
if (id === nextPhotoAnimationId) {
setNextPhotoAnimation(undefined);
setNextPhotoAnimationId(undefined);
}
}, [nextPhotoAnimationId, setNextPhotoAnimation]);
const [shouldRespondToKeyboardCommands, setShouldRespondToKeyboardCommands] = const [shouldRespondToKeyboardCommands, setShouldRespondToKeyboardCommands] =
useState(true); useState(true);
// MODAL // MODAL
@ -172,7 +190,8 @@ export default function AppStateProvider({
invalidateSwr, invalidateSwr,
nextPhotoAnimation, nextPhotoAnimation,
setNextPhotoAnimation, setNextPhotoAnimation,
clearNextPhotoAnimation: () => setNextPhotoAnimation?.(undefined), getNextPhotoAnimationId,
clearNextPhotoAnimation,
shouldRespondToKeyboardCommands, shouldRespondToKeyboardCommands,
setShouldRespondToKeyboardCommands, setShouldRespondToKeyboardCommands,
categoriesWithCounts, categoriesWithCounts,