import { emitChange } from '@tanstack/pacer' import { shallow, useSelector } from '@tanstack/solid-store' import { useStyles } from '../styles/use-styles' import { getPacerUtilStoreState, isPacerUtilTanStackStore, } from '../utils/read-pacer-store-state' import type { PacerEventName } from '@tanstack/pacer' type ActionButtonsProps = { instance: any utilName: string } export function ActionButtons(props: ActionButtonsProps) { const styles = useStyles() const utilInstance = props.instance const store = utilInstance?.store const stateAccessor = isPacerUtilTanStackStore(store) ? useSelector( store as never, (s: unknown) => s !== null && s !== undefined && typeof s === 'object' ? (s as Record) : {}, { compare: shallow }, ) : () => getPacerUtilStoreState(utilInstance) as Record const getState = () => stateAccessor() const hasPending = () => { const u = getState() return 'isPending' in u } const isPending = () => { const u = getState() return 'isPending' in u ? !!u.isPending : false } const isEmptyFlag = () => { const u = getState() return 'isEmpty' in u ? !!u.isEmpty : false } const hasFlush = typeof utilInstance.flush === 'function' const hasCancel = typeof utilInstance.cancel === 'function' const hasReset = typeof utilInstance.reset === 'function' const hasClear = typeof utilInstance.clear === 'function' const hasStart = typeof utilInstance.start === 'function' const hasStop = typeof utilInstance.stop === 'function' const hasStartStop = hasStart && hasStop const isRunning = () => { const u = getState() return 'isRunning' in u ? !!u.isRunning : true } if (!hasPending() && !hasFlush && !hasCancel && !hasReset && !hasClear) { return (
No actions available for this util
) } const emitName = `d-${props.utilName}` as PacerEventName const utilNameLower = props.utilName.toLowerCase() return (
{hasPending() && ( )} {hasFlush && ( )} {hasCancel && ( )} {hasReset && ( )} {hasClear && ( )} {hasStartStop && ( )}
) }