import * as React from 'react'; import { StyleSheet, Text, View } from 'react-native'; import { useColors } from '../../hook'; import { usePaletteContext } from '../../theme'; import { Queue, timeoutTask } from '../../utils'; import type { SimpleToastProps, SimpleToastTask } from './types'; export function SimpleToast(props: SimpleToastProps) { const { propsRef, timeout = 3000, showPosition } = props; const tasks: Queue = React.useRef( new Queue() ).current; const preTask = React.useRef(undefined); const curTask = React.useRef(undefined); const { colors } = usePaletteContext(); const { getColor } = useColors({ bg5: { light: colors.barrage.onLight[3], dark: colors.barrage.onDark[3], }, text: { light: colors.neutral[98], dark: colors.barrage.onDark[8], }, }); const [text, setText] = React.useState(undefined); const [isShow, setIsShow] = React.useState(false); const showPositionRef = React.useRef(showPosition); const getPositionStyle = React.useCallback(() => { switch (showPositionRef.current) { case 'top': return '10%'; case 'center': return '50%'; case 'bottom': return '90%'; default: return '70%'; } }, []); const execTask = () => { if (curTask.current === undefined) { const task = tasks.dequeue(); if (task) { curTask.current = task; execAnimation(() => { execTask(); }); } else { setIsShow(false); } } }; const execAnimation = (onFinished?: () => void) => { if (curTask.current?.showPosition) { showPositionRef.current = curTask.current.showPosition; } else { if (showPosition) { showPositionRef.current = showPosition; } } setIsShow(true); setText(curTask.current?.message ?? ''); timeoutTask(curTask.current?.timeout ?? timeout, () => { preTask.current = curTask.current; curTask.current = undefined; onFinished?.(); }); }; if (propsRef.current) { propsRef.current.show = (task: SimpleToastTask) => { tasks.enqueue(task); execTask(); }; } return ( {text} ); }