|
| 1 | +import { IconButton } from '@invoke-ai/ui-library'; |
| 2 | +import { useAppDispatch } from 'app/store/storeHooks'; |
| 3 | +import { useFieldInputTemplate } from 'features/nodes/hooks/useFieldInputTemplate'; |
| 4 | +import { useFieldValue } from 'features/nodes/hooks/useFieldValue'; |
| 5 | +import { fieldValueReset } from 'features/nodes/store/nodesSlice'; |
| 6 | +import { isEqual } from 'lodash-es'; |
| 7 | +import { memo, useCallback, useMemo } from 'react'; |
| 8 | +import { useTranslation } from 'react-i18next'; |
| 9 | +import { PiArrowCounterClockwiseBold } from 'react-icons/pi'; |
| 10 | + |
| 11 | +type Props = { |
| 12 | + nodeId: string; |
| 13 | + fieldName: string; |
| 14 | +}; |
| 15 | + |
| 16 | +const FieldResetToDefaultValueButton = ({ nodeId, fieldName }: Props) => { |
| 17 | + const dispatch = useAppDispatch(); |
| 18 | + const { t } = useTranslation(); |
| 19 | + const value = useFieldValue(nodeId, fieldName); |
| 20 | + const fieldTemplate = useFieldInputTemplate(nodeId, fieldName); |
| 21 | + const isDisabled = useMemo(() => { |
| 22 | + return isEqual(value, fieldTemplate.default); |
| 23 | + }, [value, fieldTemplate.default]); |
| 24 | + const onClick = useCallback(() => { |
| 25 | + dispatch(fieldValueReset({ nodeId, fieldName, value: fieldTemplate.default })); |
| 26 | + }, [dispatch, fieldName, fieldTemplate.default, nodeId]); |
| 27 | + |
| 28 | + return ( |
| 29 | + <IconButton |
| 30 | + variant="ghost" |
| 31 | + tooltip={t('nodes.resetToDefaultValue')} |
| 32 | + aria-label={t('nodes.resetToDefaultValue')} |
| 33 | + icon={<PiArrowCounterClockwiseBold />} |
| 34 | + onClick={onClick} |
| 35 | + isDisabled={isDisabled} |
| 36 | + pointerEvents="auto" |
| 37 | + size="xs" |
| 38 | + /> |
| 39 | + ); |
| 40 | +}; |
| 41 | + |
| 42 | +export default memo(FieldResetToDefaultValueButton); |
0 commit comments