2025-08-21 09:54:27 -07:00
|
|
|
import React, { useState, useEffect } from "react";
|
2025-07-24 12:11:32 -07:00
|
|
|
|
|
|
|
interface Step {
|
|
|
|
step_number: number;
|
|
|
|
instruction: string;
|
|
|
|
}
|
2025-07-10 16:34:53 -07:00
|
|
|
|
|
|
|
interface AddBulkStepsProps {
|
|
|
|
steps: Step[];
|
|
|
|
onChange?: (steps: Step[]) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
const AddBulkSteps: React.FC<AddBulkStepsProps> = ({ steps, onChange }) => {
|
2025-08-21 09:54:27 -07:00
|
|
|
const [textValue, setTextValue] = useState<string>("");
|
2025-07-10 16:34:53 -07:00
|
|
|
|
|
|
|
useEffect(() => {
|
2025-08-21 09:54:27 -07:00
|
|
|
const textRepresentation = steps
|
|
|
|
.map((step) => `${step.instruction}`)
|
|
|
|
.join("\n");
|
2025-07-10 16:34:53 -07:00
|
|
|
setTextValue(textRepresentation);
|
|
|
|
}, [steps]);
|
|
|
|
|
|
|
|
const handleInputChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
|
|
|
|
setTextValue(e.target.value);
|
|
|
|
};
|
|
|
|
|
|
|
|
const parseAndUpdate = (value: string) => {
|
2025-08-21 09:54:27 -07:00
|
|
|
const lines = value.split("\n").filter((line) => line.trim() !== "");
|
2025-07-10 16:34:53 -07:00
|
|
|
|
2025-07-24 12:11:32 -07:00
|
|
|
const parsedSteps: Step[] = lines.map((line, idx) => {
|
2025-08-21 09:54:27 -07:00
|
|
|
return { step_number: idx + 1, instruction: line };
|
|
|
|
});
|
2025-07-10 16:34:53 -07:00
|
|
|
|
|
|
|
if (onChange) onChange(parsedSteps);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
|
2025-08-21 09:54:27 -07:00
|
|
|
if (e.key === "Enter") {
|
2025-07-10 16:34:53 -07:00
|
|
|
parseAndUpdate(textValue);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleBlur = () => {
|
|
|
|
parseAndUpdate(textValue);
|
2025-08-21 09:54:27 -07:00
|
|
|
document.body.style.overflow = "";
|
|
|
|
};
|
|
|
|
|
|
|
|
const manageOverflow = () => {
|
|
|
|
const lineCount = textValue
|
|
|
|
.split("\n")
|
|
|
|
.filter((line) => line.trim() !== "").length;
|
|
|
|
if (lineCount > 8) {
|
|
|
|
document.body.style.overflow = "hidden";
|
|
|
|
} else {
|
|
|
|
document.body.style.overflow = "";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleFocus = () => {
|
|
|
|
manageOverflow();
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleTouchEnd = () => {
|
|
|
|
manageOverflow();
|
2025-07-10 16:34:53 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
2025-08-21 09:54:27 -07:00
|
|
|
<h3 className="text-xl font-bold text-[var(--color-secondaryTextDark)]">
|
|
|
|
Steps:
|
|
|
|
</h3>
|
2025-07-10 16:34:53 -07:00
|
|
|
<textarea
|
2025-07-11 17:06:41 -07:00
|
|
|
rows={8}
|
2025-07-10 16:34:53 -07:00
|
|
|
value={textValue}
|
|
|
|
onChange={handleInputChange}
|
|
|
|
onKeyDown={handleKeyDown}
|
|
|
|
onBlur={handleBlur}
|
2025-08-21 09:54:27 -07:00
|
|
|
onFocus={handleFocus}
|
|
|
|
onTouchEnd={handleTouchEnd}
|
2025-08-05 15:09:52 -07:00
|
|
|
placeholder="Enter steps separated by new line"
|
2025-08-20 10:06:23 -07:00
|
|
|
className="mb-4 p-2 border border-[var(--color-primaryBorder)] rounded w-full"
|
2025-07-10 16:34:53 -07:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default AddBulkSteps;
|