"use client"; import { useCallback, useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import { IconArrowLeft } from "@tabler/icons-react"; import { Button } from "@kandev/ui/button"; import { DrawerHeader, DrawerTitle } from "@kandev/ui/drawer"; import { SheetHeader, SheetTitle } from "@kandev/ui/sheet"; import { useTaskMoveOptions, type TaskMoveStep } from "../task-move-context-menu"; import { WorkflowMoveOptionsForm, type WorkflowMoveOptionsSubmit } from "../workflow-move-options"; export type MobileMoveOptionsRequest = { taskId: string; workflowId: string; targetStep: TaskMoveStep; }; export function useMobileTaskMoveOptions({ open, stepsByWorkflowId, }: { open: boolean; stepsByWorkflowId: Record; }) { const [request, setRequest] = useState(null); const moveOptions = useTaskMoveOptions({ taskId: request?.taskId ?? "", workflowId: request?.workflowId, steps: request ? [request.targetStep] : undefined, }); const handleRequest = useCallback( (taskId: string, workflowId: string, targetStepId: string) => { const targetStep = stepsByWorkflowId[workflowId]?.find((step) => step.id === targetStepId); if (!targetStep) return; const targetWithWorkflow = { ...targetStep, workflow_id: workflowId, task_id: taskId }; setRequest({ taskId, workflowId, targetStep: targetWithWorkflow }); moveOptions.openMoveOptionsStep(targetWithWorkflow); }, [moveOptions.openMoveOptionsStep, stepsByWorkflowId], ); const handleClose = useCallback(() => { moveOptions.closeMoveOptions(); setRequest(null); }, [moveOptions.closeMoveOptions]); const handleSubmit = useCallback( async (entryOptions: Parameters[0]) => { const moved = await moveOptions.submitMoveOptions(entryOptions); if (moved) setRequest(null); return moved; }, [moveOptions.submitMoveOptions], ); useEffect(() => { if (open || !request) return; moveOptions.closeMoveOptions(); setRequest(null); }, [moveOptions.closeMoveOptions, open, request]); return { request, moveOptionsStep: moveOptions.moveOptionsStep, isMoving: moveOptions.isMoving, handleRequest, handleClose, handleSubmit, }; } export function MobileTaskMoveOptionsSurface({ presentation, step, isMoving, onBack, onSubmit, }: { presentation: "sheet" | "drawer"; step: TaskMoveStep; isMoving: boolean; onBack: () => void; onSubmit: WorkflowMoveOptionsSubmit; }) { const { t } = useTranslation(); const header = (
{presentation === "drawer" ? ( {t("task:workflowMoveOptionsTitle", { step: step.title })} ) : ( {t("task:workflowMoveOptionsTitle", { step: step.title })} )}

{t("task:workflowMoveOptionsDescription")}

); return ( <> {presentation === "drawer" ? ( {header} ) : ( {header} )}
); }