knotra 0.29.0

knotra: multi-project VCS dashboard and bulk-operation tool.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
//! 2. "Save release point" modal (Freezer / Tag) — RFC-037 Stage 4.
//!
//! `modal_shell` replaced with `knotra_ui::widget::overlay::surface`.
//! `guided_field`/`guided_field_focused`/`guided_button` call sites are
//! untouched (D6/R11, D7/R12 — both are still-current vocabulary, not
//! legacy helpers to migrate off this stage). Each phase's own local
//! `footer` row (`ValidationReady`, `Done`) maps directly onto `surface()`'s
//! `footer` parameter, the same mapping Stage 2 used for `conflict.rs`;
//! phases with no such row (`Idle`/`Validating`, `Executing`) pass an empty
//! `Space` instead, the same choice Stage 3 made for `changelog.rs`'s
//! footer-less phases.

use iced::{
    Alignment, Element, Length,
    widget::{Space, column, row, text},
};

use knotra_ui::widget::{
    BUTTON_HEIGHT, Tokens, guided_field, guided_field_focused,
    overlay::{OverlayWidth, surface},
    reasoned, style,
};
use knotra_vcs::model::operation::FreezeOutcome;

use crate::{
    message::{FreezerMessage, Message, TagPushMessage},
    state::AppState,
};

pub fn tag_modal(state: &AppState) -> Element<'_, Message> {
    use crate::state::freezer::FreezerPhase;

    let tokens = &state.theme.tokens;
    let freezer = &state.freezer;

    let (inner, footer): (Element<'_, Message>, Element<'_, Message>) = match &freezer.phase {
        // ── Input + auto-validation ───────────────────────────────────────
        FreezerPhase::Idle | FreezerPhase::Validating { .. } => {
            let name_error: Option<&str> = if freezer.freeze_name.is_empty() {
                None // no error until user has typed
            } else if !freezer.freeze_name_is_valid() {
                Some(state.t("plain.release.name_invalid"))
            } else {
                None
            };

            let name_field = guided_field_focused(
                tokens,
                state.t("plain.release.name_label"),
                state.t("plain.release.name_hint"),
                &freezer.freeze_name,
                |s| Message::Freezer(FreezerMessage::NameChanged(s)),
                name_error,
                knotra_ui::widget::focus_id::RELEASE_NAME.clone(),
            );

            let msg_field = guided_field(
                tokens,
                state.t("plain.release.note_label"),
                state.t("plain.release.note_hint"),
                &freezer.tag_message,
                |s| Message::Freezer(FreezerMessage::TagMessageChanged(s)),
                None,
            );

            let validate_or_spinner: Element<'_, Message> =
                if matches!(freezer.phase, FreezerPhase::Validating { .. }) {
                    text(state.t("plain.release.checking"))
                        .size(snora::design::style::text::body_size(tokens))
                        .line_height(snora::design::style::text::body_line_height(tokens))
                        .into()
                } else if freezer.freeze_name_is_valid() {
                    reasoned(
                        tokens,
                        state.t("plain.release.check_readiness"),
                        (!state.operation_interlock.is_busy())
                            .then_some(Message::Freezer(FreezerMessage::ValidateRequested)),
                        state
                            .operation_interlock
                            .is_busy()
                            .then_some(state.t("plain.activity.busy")),
                        false,
                        style::primary,
                    )
                } else {
                    Space::new().into()
                };

            (
                column![name_field, msg_field, validate_or_spinner]
                    .spacing(14)
                    .into(),
                Space::new().into(),
            )
        }

        // ── Validation result + execute ───────────────────────────────────
        FreezerPhase::ValidationReady(validation) => {
            let blocked_count = validation.blocked_count();
            let ready_count = validation.ready_count();
            let can_save =
                validation.all_ready() && ready_count > 0 && !state.operation_interlock.is_busy();

            let val_rows: Vec<Element<'_, Message>> = validation
                .entries
                .iter()
                .map(|entry| {
                    let (icon, msg) = if entry.is_blocked() {
                        (
                            "!",
                            entry
                                .blockers
                                .first()
                                .map(|s| plain_blocker(state, s.as_str()))
                                .unwrap_or(""),
                        )
                    } else if !entry.included {
                        ("—", state.t("plain.release.row_excluded"))
                    } else {
                        ("✓", state.t("plain.release.row_ready"))
                    };

                    row![
                        text(icon)
                            .size(snora::design::style::text::body_size(tokens))
                            .line_height(snora::design::style::text::body_line_height(tokens))
                            .width(Length::Fixed(22.0)),
                        text(&entry.project_name)
                            .size(snora::design::style::text::body_size(tokens))
                            .line_height(snora::design::style::text::body_line_height(tokens))
                            .width(Length::FillPortion(2)),
                        text(msg)
                            .size(snora::design::style::text::body_small_size(tokens))
                            .line_height(snora::design::style::text::body_small_line_height(tokens))
                            .width(Length::FillPortion(3)),
                    ]
                    .spacing(8)
                    .align_y(Alignment::Center)
                    .into()
                })
                .collect();

            let save_reason: Option<&str> = if state.operation_interlock.is_busy() {
                Some(state.t("plain.activity.busy"))
            } else if can_save {
                None
            } else if ready_count == 0 && blocked_count == 0 {
                Some(state.t("plain.disabled.choose_one"))
            } else if blocked_count == 1 {
                Some(state.t("plain.release.fix_one"))
            } else {
                Some(state.t("plain.release.fix_some"))
            };

            let footer = row![
                reasoned(
                    tokens,
                    state.t("plain.save_release_point"),
                    can_save.then_some(Message::Freezer(FreezerMessage::ExecuteConfirmed)),
                    save_reason,
                    false,
                    style::primary,
                ),
                Space::new().width(Length::Fill),
                styled_button(
                    tokens,
                    state.t("action.cancel"),
                    Some(Message::Freezer(FreezerMessage::BulkModalClosed)),
                    style::ghost,
                ),
            ]
            .align_y(Alignment::Center);

            // No inner `scrollable` around the row list (unlike the
            // pre-migration `.height(Length::Fixed(200.0))` box) —
            // `surface()`'s own body scrollable now covers the whole body,
            // same reasoning as Stage 2/3 (review `132` §4).
            (
                column![
                    text(state.t("plain.release.ready_check"))
                        .size(snora::design::style::text::body_size(tokens))
                        .line_height(snora::design::style::text::body_line_height(tokens)),
                    column(val_rows).spacing(6),
                    impact_warnings_section(tokens, state),
                ]
                .spacing(12)
                .into(),
                footer.into(),
            )
        }

        // ── Executing ─────────────────────────────────────────────────────
        FreezerPhase::Executing => (
            column![
                text(state.t("plain.release.saving"))
                    .size(snora::design::style::text::body_size(tokens))
                    .line_height(snora::design::style::text::body_line_height(tokens)),
                text(state.t("plain.release.saving_hint"))
                    .size(snora::design::style::text::body_small_size(tokens))
                    .line_height(snora::design::style::text::body_small_line_height(tokens)),
            ]
            .spacing(8)
            .into(),
            Space::new().into(),
        ),

        // ── Result ────────────────────────────────────────────────────────
        FreezerPhase::Done(result) => {
            let push_is_running = state
                .pending_tag_push
                .as_ref()
                .is_some_and(|push| push.is_pushing);
            let outcome_title = match result.outcome {
                FreezeOutcome::Success => state.t("plain.release.outcome_success"),
                FreezeOutcome::RolledBack => state.t("plain.release.outcome_undone"),
                FreezeOutcome::RollbackFailed => state.t("plain.release.outcome_partial"),
                FreezeOutcome::NothingDone => state.t("plain.release.outcome_nothing"),
            };

            let outcome_body = match result.outcome {
                FreezeOutcome::Success => state.t("plain.no_next_step"),
                FreezeOutcome::RolledBack => state.t("plain.release.outcome_undone_hint"),
                FreezeOutcome::RollbackFailed => state.t("plain.release.outcome_partial_hint"),
                FreezeOutcome::NothingDone => "",
            };

            let rows: Vec<Element<'_, Message>> = result
                .project_results
                .iter()
                .map(|pr| {
                    let icon = if pr.success {
                        "✓"
                    } else if pr.rollback_attempted && pr.rollback_succeeded == Some(true) {
                        "⟲"
                    } else {
                        "!"
                    };
                    let msg = if pr.success {
                        state.t("plain.release.row_saved")
                    } else if pr.rollback_attempted && pr.rollback_succeeded == Some(true) {
                        state.t("plain.release.row_undone")
                    } else {
                        state.t("plain.needs_help")
                    };

                    let mut row_col = column![
                        row![
                            text(icon)
                                .size(snora::design::style::text::body_size(tokens))
                                .line_height(snora::design::style::text::body_line_height(tokens))
                                .width(Length::Fixed(22.0)),
                            text(&pr.project_name)
                                .size(snora::design::style::text::body_size(tokens))
                                .line_height(snora::design::style::text::body_line_height(tokens))
                                .width(Length::FillPortion(2)),
                            text(msg)
                                .size(snora::design::style::text::body_size(tokens))
                                .line_height(snora::design::style::text::body_line_height(tokens))
                                .width(Length::FillPortion(2)),
                        ]
                        .spacing(8),
                    ]
                    .spacing(4);

                    if !pr.success
                        && state.show_op_details
                        && let Some(hint) = &pr.recovery_hint
                    {
                        for cmd in &hint.suggested_commands {
                            row_col = row_col.push(
                                text(format!("  {}", cmd))
                                    .size(snora::design::style::text::body_small_size(tokens))
                                    .line_height(
                                        snora::design::style::text::body_small_line_height(tokens),
                                    ),
                            );
                        }
                    }

                    row_col.into()
                })
                .collect();

            let details_label = if state.show_op_details {
                state.t("plain.hide_details")
            } else {
                state.t("plain.show_details")
            };

            let push_offer: Element<'_, Message> = match &state.pending_tag_push {
                Some(push)
                    if push.freeze_name == result.freeze_name && !push.project_ids.is_empty() =>
                {
                    if push.is_pushing {
                        text(state.t("plain.release.sharing"))
                            .size(snora::design::style::text::body_size(tokens))
                            .line_height(snora::design::style::text::body_line_height(tokens))
                            .into()
                    } else {
                        column![
                            text(state.t("plain.release.share_offer"))
                                .size(snora::design::style::text::body_size(tokens))
                                .line_height(snora::design::style::text::body_line_height(tokens)),
                            row![
                                styled_button(
                                    tokens,
                                    state.t("plain.release.share_action"),
                                    (!state.operation_interlock.is_busy())
                                        .then_some(Message::TagPush(TagPushMessage::PushConfirmed)),
                                    style::primary,
                                ),
                                styled_button(
                                    tokens,
                                    state.t("plain.release.share_decline"),
                                    Some(Message::TagPush(TagPushMessage::PushDeclined)),
                                    style::ghost,
                                ),
                            ]
                            .spacing(8)
                            .align_y(Alignment::Center),
                        ]
                        .spacing(8)
                        .into()
                    }
                }
                _ => Space::new().height(Length::Fixed(0.0)).into(),
            };

            let footer = row![
                styled_button(
                    tokens,
                    details_label,
                    Some(Message::ToggleOpDetails),
                    style::ghost,
                ),
                Space::new().width(Length::Fill),
                styled_button(
                    tokens,
                    state.t("action.close"),
                    (!push_is_running).then_some(Message::Freezer(FreezerMessage::BulkModalClosed)),
                    style::ghost,
                ),
            ]
            .align_y(Alignment::Center);

            // No inner `scrollable` around the row list — same reasoning as
            // the `ValidationReady` branch above.
            (
                column![
                    text(outcome_title).size(snora::design::style::text::title_size(tokens)),
                    text(outcome_body)
                        .size(snora::design::style::text::body_size(tokens))
                        .line_height(snora::design::style::text::body_line_height(tokens)),
                    column(rows).spacing(8),
                    push_offer,
                ]
                .spacing(12)
                .into(),
                footer.into(),
            )
        }
    };

    // R2/§2: both clauses, unchanged. The second is not a phase check — it
    // reads `state.pending_tag_push` directly — so a close during a running
    // tag push stays blocked even though `freezer.phase` itself has already
    // moved past `Executing` into `Done`.
    let close_msg = if matches!(freezer.phase, FreezerPhase::Executing)
        || state
            .pending_tag_push
            .as_ref()
            .is_some_and(|push| push.is_pushing)
    {
        None
    } else {
        Some(Message::Freezer(FreezerMessage::BulkModalClosed))
    };

    surface(
        tokens,
        OverlayWidth::Large.resolve(state.window_width),
        state.t("plain.save_release_point"),
        close_msg,
        false,
        inner,
        footer,
    )
}

/// A button styled with one of `knotra_ui::widget::style`'s semantic
/// functions plus a focus ring — the same shape `conflict.rs` (Stage 2) and
/// `changelog.rs` (Stage 3) use. `is_focused` is always `false`: no real
/// focus-order wiring exists or is permitted for this overlay this stage
/// (R3 forbids `app/`/`state/`).
fn styled_button<'a>(
    tokens: &Tokens,
    label: &'a str,
    on_press: Option<Message>,
    style_fn: fn(&Tokens, iced::widget::button::Status) -> iced::widget::button::Style,
) -> Element<'a, Message> {
    let t = tokens.clone();
    iced::widget::button(
        text(label)
            .size(snora::design::style::text::body_size(tokens))
            .line_height(snora::design::style::text::body_line_height(tokens)),
    )
    .height(BUTTON_HEIGHT)
    .padding([0, 18])
    .on_press_maybe(on_press)
    .style(move |_theme, status| style::with_focus_ring(&t, false, style_fn(&t, status)))
    .into()
}

/// RFC-044 D2/D3: dependency impact, beside the per-project blockers above.
/// `state.freezer.topology_checked` distinguishes "not checked" from
/// "checked, found nothing" — both are stated explicitly, never left as an
/// empty section a silent absence could be misread from (D3, R3).
fn impact_warnings_section<'a>(tokens: &Tokens, state: &'a AppState) -> Element<'a, Message> {
    if !state.freezer.topology_checked {
        return text(state.t("plain.release.impact_unchecked"))
            .size(snora::design::style::text::body_small_size(tokens))
            .line_height(snora::design::style::text::body_small_line_height(tokens))
            .into();
    }
    if state.freezer.impact_warnings.is_empty() {
        return text(state.t("plain.release.impact_clear"))
            .size(snora::design::style::text::body_small_size(tokens))
            .line_height(snora::design::style::text::body_small_line_height(tokens))
            .into();
    }

    let rows: Vec<Element<'_, Message>> = state
        .freezer
        .impact_warnings
        .iter()
        .map(|w| {
            row![
                text("⚠")
                    .size(snora::design::style::text::body_size(tokens))
                    .line_height(snora::design::style::text::body_line_height(tokens))
                    .width(Length::Fixed(22.0)),
                text(&w.frozen_project_name)
                    .size(snora::design::style::text::body_size(tokens))
                    .line_height(snora::design::style::text::body_line_height(tokens))
                    .width(Length::FillPortion(2)),
                text(format!(
                    "{}: {}",
                    state.t("plain.release.impact_depended_on_by"),
                    w.dependent_projects.join(", ")
                ))
                .size(snora::design::style::text::body_small_size(tokens))
                .line_height(snora::design::style::text::body_small_line_height(tokens))
                .width(Length::FillPortion(3)),
            ]
            .spacing(8)
            .align_y(Alignment::Center)
            .into()
        })
        .collect();

    column![
        text(state.t("plain.release.impact_title"))
            .size(snora::design::style::text::body_small_size(tokens))
            .line_height(snora::design::style::text::body_small_line_height(tokens)),
        column(rows).spacing(6),
    ]
    .spacing(6)
    .into()
}

/// Map a technical blocker string to a plain-language message.
fn plain_blocker<'a>(state: &'a AppState, blocker: &str) -> &'a str {
    let lower = blocker.to_lowercase();
    if lower.contains("tag") && lower.contains("exist") {
        state.t("plain.release.blocker_name_used")
    } else if lower.contains("conflict") || lower.contains("merge") {
        state.t("plain.release.blocker_needs_choice")
    } else if lower.contains("dirty") || lower.contains("uncommitted") {
        state.t("plain.release.blocker_unsaved")
    } else {
        state.t("plain.needs_help") // safe fallback
    }
}