diff --git a/src/components/StateDropdown/StateDropdown.tsx b/src/components/StateDropdown/StateDropdown.tsx new file mode 100644 index 000000000..b4e80846f --- /dev/null +++ b/src/components/StateDropdown/StateDropdown.tsx @@ -0,0 +1,73 @@ +import { State as StateType } from '@prisma/client'; +import { ComponentProps, useCallback, useEffect } from 'react'; +import { State } from '@taskany/bricks/harmony'; +import { nullable } from '@taskany/bricks'; + +import { trpc } from '../../utils/trpcClient'; +import { Dropdown, DropdownTrigger, DropdownPanel } from '../Dropdown/Dropdown'; +import { StateWrapper } from '../StateWrapper'; + +interface StateDropdownProps { + error?: ComponentProps['error']; + label?: ComponentProps['label']; + disabled?: boolean; + value?: Partial; + flowId?: string; + + onChange?: (state: StateType) => void; +} + +export const StateDropdown = ({ label, value, flowId, disabled, onChange, ...props }: StateDropdownProps) => { + const defaultFlowEnabled = !flowId && !disabled; + + const onStateChange = useCallback( + (s: Partial) => { + onChange?.(s as StateType); + }, + [onChange], + ); + + const { data: flowById } = trpc.flow.getById.useQuery(flowId, { + enabled: !!flowId, + }); + + const { data: defaultFlow } = trpc.flow.recommedations.useQuery(undefined, { + enabled: defaultFlowEnabled, + refetchOnMount: 'always', + select([flow]) { + return flow; + }, + }); + + const flow = defaultFlowEnabled ? defaultFlow : flowById; + + useEffect(() => { + if (value) return; + const defaultState = flow?.states.find((state) => state.default === true); + defaultState && onStateChange(defaultState); + }, [flow, onStateChange, value]); + + return ( + + + {nullable(value, ({ hue, title }) => ( + + + + ))} + + ( + + + + )} + /> + + ); +};