|
45 | 45 | let error: KilnError | null = null |
46 | 46 | let submitting = false |
47 | 47 | export let saved: boolean = false |
48 | | - // Warn before unload if there's any user input |
| 48 | +
|
| 49 | + // Track initial values to detect actual changes from the loaded state. |
| 50 | + // This prevents false "unsaved changes" warnings when opening an existing task. |
| 51 | + let initial_name: string |
| 52 | + let initial_description: string | null | undefined |
| 53 | + let initial_instruction: string |
| 54 | + let initial_thinking_instruction: string | null | undefined |
| 55 | + let initial_requirements: Array<{ |
| 56 | + name: string | undefined |
| 57 | + instruction: string | undefined |
| 58 | + type: string | undefined |
| 59 | + priority: number | undefined |
| 60 | + }> |
| 61 | +
|
| 62 | + function reset_initial_values() { |
| 63 | + initial_name = task.name |
| 64 | + initial_description = task.description |
| 65 | + initial_instruction = task.instruction |
| 66 | + initial_thinking_instruction = task.thinking_instruction |
| 67 | + initial_requirements = task.requirements.map((r) => ({ |
| 68 | + name: r.name, |
| 69 | + instruction: r.instruction, |
| 70 | + type: r.type, |
| 71 | + priority: r.priority, |
| 72 | + })) |
| 73 | + } |
| 74 | + reset_initial_values() |
| 75 | +
|
| 76 | + function requirements_changed( |
| 77 | + reqs: Task["requirements"], |
| 78 | + initial: typeof initial_requirements, |
| 79 | + ): boolean { |
| 80 | + if (reqs.length !== initial.length) return true |
| 81 | + return reqs.some( |
| 82 | + (r, i) => |
| 83 | + (r.name || "") !== (initial[i].name || "") || |
| 84 | + (r.instruction || "") !== (initial[i].instruction || "") || |
| 85 | + (r.type || "") !== (initial[i].type || "") || |
| 86 | + r.priority !== initial[i].priority, |
| 87 | + ) |
| 88 | + } |
| 89 | +
|
| 90 | + // Warn before unload only if there are actual changes from the initial state |
49 | 91 | $: warn_before_unload = |
50 | | - !saved && |
51 | | - ([task.name, task.description, task.instruction].some((value) => !!value) || |
52 | | - task.requirements.some((req) => !!req.name || !!req.instruction)) |
| 92 | + task.name !== initial_name || |
| 93 | + (task.description || "") !== (initial_description || "") || |
| 94 | + task.instruction !== initial_instruction || |
| 95 | + (task.thinking_instruction || "") !== |
| 96 | + (initial_thinking_instruction || "") || |
| 97 | + requirements_changed(task.requirements, initial_requirements) |
53 | 98 |
|
54 | 99 | // Allow explicitly setting project ID, or infer current project ID |
55 | 100 | export let explicit_project_id: string | undefined = undefined |
|
145 | 190 | current_task_rating_options: null, |
146 | 191 | }) |
147 | 192 | saved = true |
| 193 | + reset_initial_values() |
148 | 194 |
|
149 | 195 | // reload the current task to make sure changes propagate throughout the UI |
150 | 196 | // e.g. the rating options |
|
0 commit comments