import type {Fiber} from './ReactInternalTypes';
import type {ViewTransitionProps} from 'shared/ReactTypes';
import {runWithFiberInDEV} from './ReactCurrentFiber';
const mountedNamedViewTransitions: Map<string, Fiber> = __DEV__
? new Map()
: (null: any);
const didWarnAboutName: {[string]: boolean} = __DEV__ ? {} : (null: any);
export function trackNamedViewTransition(fiber: Fiber): void {
if (__DEV__) {
const name = (fiber.memoizedProps: ViewTransitionProps).name;
if (name != null && name !== 'auto') {
const existing = mountedNamedViewTransitions.get(name);
if (existing !== undefined) {
if (existing !== fiber && existing !== fiber.alternate) {
if (!didWarnAboutName[name]) {
didWarnAboutName[name] = true;
const stringifiedName = JSON.stringify(name);
runWithFiberInDEV(fiber, () => {
console.error(
'There are two <ViewTransition name=%s> components with the same name mounted ' +
'at the same time. This is not supported and will cause View Transitions ' +
'to error. Try to use a more unique name e.g. by using a namespace prefix ' +
'and adding the id of an item to the name.',
stringifiedName,
);
});
runWithFiberInDEV(existing, () => {
console.error(
'The existing <ViewTransition name=%s> duplicate has this stack trace.',
stringifiedName,
);
});
}
}
} else {
mountedNamedViewTransitions.set(name, fiber);
}
}
}
}
export function untrackNamedViewTransition(fiber: Fiber): void {
if (__DEV__) {
const name = (fiber.memoizedProps: ViewTransitionProps).name;
if (name != null && name !== 'auto') {
const existing = mountedNamedViewTransitions.get(name);
if (
existing !== undefined &&
(existing === fiber || existing === fiber.alternate)
) {
mountedNamedViewTransitions.delete(name);
}
}
}
}