Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion packages/features/schedules/components/Schedule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ export type FieldPathByValue<TFieldValues extends FieldValues, TValue> = {
: never;
}[FieldPath<TFieldValues>];

// Store preserved time slots for each day to restore when toggling back on
const preservedTimeSlotsRef = new Map<string, TimeRange[]>();
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot Jan 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1: Module-level Map will share preserved time slots across all component instances, causing data corruption when multiple schedules are edited. This should be scoped per form/schedule instance using React Context or a ref passed from the parent ScheduleComponent.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At packages/features/schedules/components/Schedule.tsx, line 57:

<comment>Module-level `Map` will share preserved time slots across all component instances, causing data corruption when multiple schedules are edited. This should be scoped per form/schedule instance using React Context or a ref passed from the parent `ScheduleComponent`.</comment>

<file context>
@@ -53,6 +53,9 @@ export type FieldPathByValue&lt;TFieldValues extends FieldValues, TValue&gt; = {
 }[FieldPath&lt;TFieldValues&gt;];
 
+// Store preserved time slots for each day to restore when toggling back on
+const preservedTimeSlotsRef = new Map&lt;string, TimeRange[]&gt;();
+
 export const ScheduleDay = &lt;TFieldValues extends FieldValues&gt;({
</file context>
Fix with Cubic


export const ScheduleDay = <TFieldValues extends FieldValues>({
name,
weekday,
Expand All @@ -74,6 +77,7 @@ export const ScheduleDay = <TFieldValues extends FieldValues>({
}) => {
const { watch, setValue } = useFormContext();
const watchDayRange = watch(name);
const fieldName = String(name);

return (
<div
Expand All @@ -97,7 +101,26 @@ export const ScheduleDay = <TFieldValues extends FieldValues>({
checked={watchDayRange && !!watchDayRange.length}
data-testid={`${weekday}-switch`}
onCheckedChange={(isChecked) => {
setValue(name, (isChecked ? [DEFAULT_DAY_RANGE] : []) as TFieldValues[typeof name]);
if (isChecked) {
// restore previous time slots if available, otherwise use default
const preserved = preservedTimeSlotsRef.get(fieldName);
if (preserved && preserved.length > 0) {
setValue(name, preserved as TFieldValues[typeof name]);
} else {
setValue(name, [
DEFAULT_DAY_RANGE,
] as TFieldValues[typeof name]);
}
} else {
// save current time slots before clearing
if (watchDayRange && watchDayRange.length > 0) {
preservedTimeSlotsRef.set(
fieldName,
watchDayRange as TimeRange[]
);
}
setValue(name, [] as TFieldValues[typeof name]);
}
}}
/>
</div>
Expand Down
Loading