ghtui 0.2.0

A keyboard-driven terminal client for GitHub, with a pull request diff viewer that goes further than GitHub's
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
//! Reviewing: comment targets, drafts, suggestions, and turning GitHub
//! threads and local drafts into annotations.

use std::collections::{HashMap, HashSet};

use crate::api::model::{NewThread, ReviewEvent, ReviewThread, Side as ApiSide};
use crate::api::model::{NodeId, PendingReview, PrDetail, PrRef};
use crate::api::{ApiError, GitHub};
use crate::data::{DataKey, Picked};
use crate::diff::anchor::{AnchorError, LinePos, Side};
use crate::store::{DraftComment, DraftSide, ReviewState};
use crate::theme::{Bg, Theme};
use crate::ui::annotations::{Annotation, AnnotationComment, AnnotationKey, Place};
use crate::ui::diff_doc::Viewed;
use crate::ui::diff_doc::{Doc, Pos, Row};
use crate::ui::text::short_sha;
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use ratatui_textarea::TextArea;

use crate::api::change::Change;
use crate::compose::{Compose, Target, editor};
use crate::diff_screen::{self, DiffOf, DiffScreen, LastReview};
use crate::forms::Sent;
use crate::keymap::Action;
use crate::picker::{self, PickItem};
use crate::route::{Route, pulls};
use crate::state::{Api, Cmd, Git, OutdatedThread, Overlay, Screen, State};
use crate::ui::page::{Link, ReviewLink};
use crate::ui::pages::PrTab;
use crate::work::{Cx, Failure, Job};

/// A review's comment being written: where it goes, or what it answers.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Comment {
    /// One line (`start == end`) or a range, by file line numbers.
    Line {
        path: String,
        start: LinePos,
        end: LinePos,
    },
    /// The whole file; `reason` says why when a line was asked for.
    File {
        path: String,
        reason: Option<String>,
    },
    /// A reply, posted right away.
    Reply { thread_id: NodeId },
    /// Editing an existing draft.
    Draft { id: u64 },
}

impl Comment {
    /// The composer's title.
    pub fn title(&self) -> String {
        let side = |s: Side| match s {
            Side::Left => "old",
            Side::Right => "new",
        };
        match self {
            Comment::Line { path, start, end } if start == end => {
                format!("Comment on {path}:{} ({})", end.line, side(end.side))
            }
            Comment::Line { path, start, end } => format!(
                "Comment on {path}, lines {}–{} ({})",
                start.line,
                end.line,
                side(end.side)
            ),
            Comment::File { path, .. } => format!("Comment on the file {path}"),
            Comment::Reply { .. } => "Reply".to_owned(),
            Comment::Draft { .. } => "Edit draft".to_owned(),
        }
    }

    /// What ctrl-s does, for the composer's hint.
    pub fn save(&self) -> &'static str {
        match self {
            Comment::Reply { .. } => "post reply",
            Comment::Draft { .. } => "save draft",
            Comment::Line { .. } | Comment::File { .. } => "add to review",
        }
    }

    /// Why a comment asked for on lines is on the whole file.
    pub fn note(&self) -> Option<&str> {
        match self {
            Comment::File {
                reason: Some(reason),
                ..
            } => Some(reason),
            _ => None,
        }
    }
}

/// A suggested change and what it replaces.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Preview {
    pub start_line: u32,
    pub original: Vec<String>,
    pub suggested: Vec<String>,
}

/// The row of the diff the composer opens under: the last selected line,
/// the end of the thread or draft it's about, or the file's header.
pub fn compose_anchor(doc: &Doc, screen: &DiffScreen, target: &Target) -> Pos {
    let cursor = screen.cursor;
    match target {
        Target::Review(Comment::File { .. }) => Pos { row: 0, ..cursor },
        Target::Review(Comment::Reply { .. } | Comment::Draft { .. }) | Target::Edit(_) => {
            let ann = doc.annotation_under(cursor, screen.half);
            (cursor.row + 1..)
                .map(|row| Pos { row, ..cursor })
                .take_while(|&p| {
                    matches!(doc.row(p), Some(Row::Thread(_))) && doc.annotation_at(p) == ann
                })
                .last()
                .unwrap_or(cursor)
        }
        _ => screen
            .selection
            .filter(|s| doc.to_global(*s) > doc.to_global(cursor))
            .unwrap_or(cursor),
    }
}

pub struct SubmitDialog {
    pub pr: PrRef,
    pub event: ReviewEvent,
    pub input: TextArea<'static>,
    pub sent: Sent,
    /// Enter submits: opened to approve from the pull request's page.
    pub quick: bool,
    /// Your saved review (its drafts go with it), read from disk when the
    /// pull request's diff isn't open to hold it.
    pub saved: Option<ReviewState>,
    /// The key (esc or ctrl-c) pressed once with a summary typed: only that
    /// key, pressed again, throws the summary away.
    pub discard_armed: Option<&'static str>,
    /// The comments your pending review on GitHub holds, which go with
    /// it; `None` until GitHub says.
    pub on_github: Option<u64>,
}

impl SubmitDialog {
    pub fn new(theme: &Theme, pr: PrRef) -> Self {
        let mut input = editor(theme, "");
        input.set_placeholder_text("Summary (optional for comments and approvals)");
        input.set_placeholder_style(theme.meta(Bg::ContainerHigh));
        Self {
            pr,
            event: ReviewEvent::Comment,
            input,
            sent: Sent::default(),
            quick: false,
            saved: None,
            discard_armed: None,
            on_github: None,
        }
    }

    /// Your pending drafts, wherever they're held: `None` while they're
    /// still being read.
    pub fn drafts<'a>(&'a self, state: &'a State) -> Option<&'a [DraftComment]> {
        match state.diffs.get(&DiffOf::Pr(self.pr.clone())) {
            Some(diff) => Some(&diff.inputs().review.pending),
            None => self.saved.as_ref().map(|s| s.pending.as_slice()),
        }
    }

    pub fn cycle(&mut self, forward: bool) {
        const ORDER: [ReviewEvent; 3] = [
            ReviewEvent::Comment,
            ReviewEvent::Approve,
            ReviewEvent::RequestChanges,
        ];
        let i = ORDER.iter().position(|e| *e == self.event).unwrap_or(0);
        let steps = if forward { 1 } else { ORDER.len() - 1 };
        if let Some(&next) = ORDER.iter().cycle().nth(i + steps) {
            self.event = next;
        }
    }
}

/// A box for writing Markdown: long lines wrap, at words where they can.
/// Why `$EDITOR` was opened, and where its text goes.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum EditPurpose {
    /// Continue the comment being composed.
    Compose,
    /// A suggested change for these new-side lines.
    Suggest {
        path: String,
        start: LinePos,
        end: LinePos,
        original: Vec<String>,
    },
    /// The review summary.
    Summary,
}

/// What happened on submit.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct SubmitOutcome {
    /// Drafts GitHub accepted (now in the pending review there).
    pub accepted: Vec<u64>,
    /// Drafts GitHub rejected, with its reason.
    pub rejected: Vec<(u64, String)>,
    /// The review was submitted.
    pub submitted: bool,
    /// The submission as a whole failed.
    pub error: Option<String>,
    /// The comments your pending review on GitHub held after, if known.
    pub on_github: Option<u64>,
    /// GitHub couldn't be reached, and nothing was sent: the review, to
    /// queue ([`crate::offline`]).
    pub unsent: Option<Box<crate::offline::queue::Review>>,
}

pub fn suggestion_body(suggested: &str) -> String {
    format!("```suggestion\n{}\n```", suggested.trim_end_matches('\n'))
}

/// Where a new comment goes, from the cursor or a visual selection (on
/// `half` of split rows). Lines GitHub won't accept become a file comment
/// (with the reason); a range that crosses GitHub's hunks is refused.
pub fn target(
    doc: &Doc,
    cursor: Pos,
    half: Side,
    selection: Option<Pos>,
) -> Result<Comment, String> {
    let file = doc.files().get(cursor.file).ok_or("No file here")?;
    let path = file.meta.path().to_owned();
    if selection.is_none() && doc.row(cursor) == Some(Row::Spacer) {
        return Err("This is between files: move into one to comment".into());
    }
    let from = match selection {
        Some(anchor) if anchor.file == cursor.file => anchor,
        Some(_) => return Err("A selection can't span files".into()),
        None => cursor,
    };
    let Some((start, end)) = doc.line_range(from, cursor, half) else {
        return Ok(Comment::File { path, reason: None });
    };
    let Some(commentable) = doc.commentable(cursor.file) else {
        return Err("This file's diff hasn't loaded yet".into());
    };
    match commentable.validate(start, end) {
        Ok(()) => Ok(Comment::Line { path, start, end }),
        Err(AnchorError::NotCommentable) => Ok(Comment::File {
            path,
            reason: Some(
                "That line isn't part of GitHub's diff, so this will be a comment on the file."
                    .into(),
            ),
        }),
        Err(err) => Err(format!("Can't comment there: {err}")),
    }
}

/// Where a draft is: its lines, or its file.
fn place(d: &DraftComment) -> Comment {
    let path = d.path.clone();
    let Some(line) = d.line else {
        return Comment::File { path, reason: None };
    };
    let end = LinePos {
        side: side_of(d.side),
        line,
    };
    let start = LinePos {
        side: side_of(d.start_side.unwrap_or(d.side)),
        line: d.start_line.unwrap_or(line),
    };
    Comment::Line { path, start, end }
}

fn draft_side(side: Side) -> DraftSide {
    match side {
        Side::Left => DraftSide::Left,
        Side::Right => DraftSide::Right,
    }
}

fn side_of(side: DraftSide) -> Side {
    match side {
        DraftSide::Left => Side::Left,
        DraftSide::Right => Side::Right,
    }
}

/// A new draft for a line or file target.
pub fn draft(target: &Comment, body: &str, id: u64, commit: &str) -> Option<DraftComment> {
    let base = |path: &str| DraftComment {
        id,
        path: path.to_owned(),
        body: body.to_owned(),
        side: DraftSide::Right,
        commit: commit.to_owned(),
        ..Default::default()
    };
    match target {
        Comment::Line { path, start, end } => {
            let ranged = start != end;
            Some(DraftComment {
                side: draft_side(end.side),
                line: Some(end.line),
                start_line: ranged.then_some(start.line),
                start_side: ranged.then_some(draft_side(start.side)),
                ..base(path)
            })
        }
        Comment::File { path, .. } => Some(base(path)),
        Comment::Reply { .. } | Comment::Draft { .. } => None,
    }
}

pub fn new_thread(draft: &DraftComment) -> NewThread {
    let side = |s: DraftSide| match s {
        DraftSide::Left => ApiSide::Left,
        DraftSide::Right => ApiSide::Right,
    };
    NewThread {
        path: draft.path.clone(),
        body: draft.body.clone(),
        line: draft.line,
        side: side(draft.side),
        start_line: draft.start_line,
        start_side: draft.start_side.map(side),
    }
}

/// GitHub threads and local drafts as annotations. Outdated threads whose
/// line was mapped forward sit on their new line; unmapped ones go under the
/// file header.
/// Threads and comments in `fresh` came in since you started looking.
pub fn annotations(
    threads: &[ReviewThread],
    mapped: &HashMap<NodeId, Option<u32>>,
    drafts: &[DraftComment],
    fresh: &HashSet<NodeId>,
) -> Vec<Annotation> {
    let mut out: Vec<Annotation> = threads
        .iter()
        .map(|t| {
            let side = match t.side {
                ApiSide::Left => Side::Left,
                ApiSide::Right => Side::Right,
            };
            let moved_to = mapped.get(&t.id).copied().flatten();
            let place = match (t.file_level, t.outdated, t.line, moved_to) {
                (true, outdated, ..) => Place::File { outdated },
                (_, false, Some(line), _) => Place::Line {
                    at: LinePos { side, line },
                    start: t.start_line,
                },
                (.., Some(line)) => Place::Moved(LinePos { side, line }),
                _ => Place::Outdated(t.original_line),
            };
            Annotation {
                key: AnnotationKey::Thread(t.id.clone()),
                path: t.path.clone(),
                place,
                resolved: t.resolved,
                comments: t
                    .comments
                    .iter()
                    .map(|c| AnnotationComment {
                        author: c.author.clone(),
                        body: c.body.clone(),
                        created_at: c.created_at.clone(),
                        pending: c.pending,
                        new: fresh.contains(&c.id),
                        editable: Some(c.editable.clone()),
                    })
                    .collect(),
                left_out: t.comments.left_out(),
                error: None,
                can_reply: t.can_reply,
                can_resolve: t.can_resolve,
                can_unresolve: t.can_unresolve,
                new: fresh.contains(&t.id),
            }
        })
        .collect();
    out.extend(drafts.iter().map(|d| Annotation {
        key: AnnotationKey::Draft(d.id),
        path: d.path.clone(),
        place: match d.line {
            Some(line) => Place::Line {
                at: LinePos {
                    side: side_of(d.side),
                    line,
                },
                start: d.start_line,
            },
            None => Place::File { outdated: false },
        },
        resolved: false,
        comments: vec![AnnotationComment {
            author: "You".into(),
            body: d.body.clone(),
            created_at: String::new(),
            pending: false,
            new: false,
            editable: None,
        }],
        left_out: 0,
        error: d.error.clone(),
        can_reply: false,
        can_resolve: false,
        can_unresolve: false,
        new: false,
    }));
    out
}

/// Outdated right-side threads worth mapping onto the current diff.
pub fn outdated_to_map(threads: &[ReviewThread]) -> Vec<OutdatedThread> {
    threads
        .iter()
        .filter(|t| t.outdated && !t.file_level && t.side == ApiSide::Right)
        .filter_map(|t| {
            Some(OutdatedThread {
                thread: t.id.clone(),
                path: t.path.clone(),
                commit: t.comments.first()?.original_commit.clone()?,
                line: t.original_line?,
            })
        })
        .collect()
}

/// Applies the result of submitting a review: accepted drafts leave the
/// local queue, rejected ones keep their text and GitHub's reason.
#[must_use]
pub(crate) fn on_submitted(state: &mut State, pr: &PrRef, outcome: &SubmitOutcome) -> Vec<Cmd> {
    // GitHub couldn't be reached: the review and its drafts move to the
    // queue, to be sent when it's back.
    if let Some(review) = &outcome.unsent {
        let moved = SubmitOutcome {
            accepted: review.drafts.iter().map(|d| d.id).collect(),
            ..SubmitOutcome::default()
        };
        let mut cmds = on_submitted(state, pr, &moved);
        if matches!(state.overlay, Some(Overlay::Submit(_))) {
            state.overlay = None;
        }
        cmds.push(crate::offline::queue_review(state, review));
        return cmds;
    }
    let apply = |review: &mut ReviewState, head: Option<String>| {
        review.pending.retain(|d| !outcome.accepted.contains(&d.id));
        for draft in &mut review.pending {
            draft.error = outcome
                .rejected
                .iter()
                .find(|(id, _)| *id == draft.id)
                .map(|(_, reason)| reason.clone());
        }
        if outcome.submitted {
            review.last_reviewed_head = head;
        }
    };
    let mut cmds = vec![Cmd::Api(Api::FetchThreads(pr.clone()))];
    if let Some(diff) = state.diffs.get_mut(&DiffOf::Pr(pr.clone())) {
        let head = diff.head().map(|h| h.to_string());
        cmds.extend(diff.edit_review(pr, |i| apply(&mut i.review, head)));
    } else if let Some(Overlay::Submit(dialog)) = &mut state.overlay
        && let Some(saved) = &mut dialog.saved
    {
        // Without the diff, the dialog holds what's saved: save it back.
        let key = DataKey::Pr(pr.clone());
        let detail = (state.data.get(&key)).and_then(|r| r.data.as_ref().and_then(PrDetail::pick));
        let head = detail.map(|d| d.head_oid.clone());
        apply(saved, head);
        cmds.push(Cmd::SaveReview(pr.clone(), saved.clone()));
    }
    if outcome.submitted {
        let approved = matches!(
            &state.overlay,
            Some(Overlay::Submit(d)) if d.event == ReviewEvent::Approve
        );
        state.overlay = None;
        state.info(if approved {
            format!("Approved {pr}")
        } else {
            "Review submitted".to_owned()
        });
        cmds.extend(state.load_visible(false));
    } else if let Some(Overlay::Submit(dialog)) = &mut state.overlay {
        dialog.sent.sending = false;
        dialog.on_github = outcome.on_github.or(dialog.on_github);
        dialog.sent.error = Some(match (&outcome.error, outcome.rejected.len()) {
            (Some(err), _) => err.clone(),
            (None, n) => format!(
                "GitHub rejected {n} comment{}; they're marked in the diff. The review stays pending on GitHub until you submit again.",
                if n == 1 { "" } else { "s" }
            ),
        });
    }
    cmds
}

#[must_use]
pub(crate) fn on_edited(
    state: &mut State,
    purpose: EditPurpose,
    result: Result<String, crate::work::Failure>,
) -> Vec<Cmd> {
    let text = match result {
        Ok(text) => text,
        Err(err) => {
            state.error(format!("Couldn't run the editor: {err}"));
            return Vec::new();
        }
    };
    let theme = &state.theme;
    match purpose {
        EditPurpose::Compose => {
            if let Some(Overlay::Compose(compose)) = &mut state.overlay {
                let target = compose.target.clone();
                let preview = compose.preview.clone();
                **compose = Compose::new(theme, target, &text);
                compose.preview = preview;
            }
        }
        EditPurpose::Summary => {
            if let Some(Overlay::Submit(dialog)) = &mut state.overlay {
                dialog.input = editor(theme, text.trim_end());
            }
        }
        EditPurpose::Suggest {
            path,
            start,
            end,
            original,
        } => {
            let suggested: Vec<String> = text
                .trim_end_matches('\n')
                .lines()
                .map(str::to_owned)
                .collect();
            if suggested == original {
                return state.just_say("No change suggested");
            }
            let body = suggestion_body(&suggested.join("\n"));
            let target = Target::Review(Comment::Line { path, start, end });
            let mut compose = Compose::new(theme, target, &body);
            compose.preview = Some(Preview {
                start_line: start.line,
                original,
                suggested,
            });
            state.overlay = Some(Overlay::Compose(Box::new(compose)));
        }
    }
    Vec::new()
}

/// How to get back to the whole PR, for notices.
pub(crate) fn all_changes(state: &State) -> String {
    let key = state.first_key(Action::PickCommits);
    format!("pick “All changes” ({key})")
}

/// Review actions on the diff screen: commenting, threads, drafts and
/// submitting, and which changes are shown.
#[must_use]
pub(crate) fn review_action(state: &mut State, action: Action) -> Vec<Cmd> {
    let viewer = state.viewer.clone();
    let Some((screen, diff)) = state.diff_parts() else {
        return state.just_say("The diff hasn't loaded yet");
    };
    let Some(pr) = screen.of.pr().cloned() else {
        return state.just_say(format!("{}: on pull requests", action.description()));
    };
    let (cursor, half) = (screen.cursor, screen.half);
    // The thread or draft under the cursor.
    let at = diff.doc.annotation_under(cursor, half);
    let annotation = at
        .and_then(|i| diff.doc.annotations().get(i as usize))
        .cloned();
    let error = |state: &mut State, text: &str| {
        state.error(text);
        Vec::new()
    };
    let in_range = diff.range().is_some();
    if in_range
        && matches!(
            action,
            Action::Comment | Action::Suggest | Action::FileComment | Action::SubmitReview
        )
    {
        let all = all_changes(state);
        return error(
            state,
            &format!("Comments anchor to the whole PR: {all} to comment"),
        );
    }
    match action {
        Action::ToggleSinceReview => {
            if in_range {
                let all = all_changes(state);
                return error(state, &format!("First {all}"));
            }
            // Another review's comparison isn't yours: yours is made anew.
            if diff.since_commit.take().is_some() {
                diff.doc.set_since(None, false);
            }
            // A push since the diff was made: the diff starts again at
            // it, and compares once it's in.
            let head = diff.head().map(|h| h.to_string());
            if crate::live::pushed_past(state, &pr, head.as_deref())
                && let Some(base) = state.pr_base(&pr)
                && let Some((screen, diff)) = state.diff_parts()
            {
                diff.restart(None);
                diff.since_requested = true;
                screen.restart();
                let job = diff.job;
                state.info("Loading the new push, to compare with your last review…");
                return vec![Cmd::Git(Git::LoadDiff {
                    of: pr.into(),
                    job,
                    base,
                    range: None,
                })];
            }
            let Some((screen, diff)) = state.diff_parts() else {
                return Vec::new();
            };
            let active = !diff.doc.since_active();
            if diff_screen::preserving_position(screen, diff, |d| d.doc.show_since(active)) {
                return state.just_say(if active {
                    "Showing changes since your last review"
                } else {
                    "Showing all changes"
                });
            }
            diff.since_requested = true;
            match (&diff.inputs().last_review, viewer) {
                // GitHub hasn't answered, or couldn't: ask it (again), and
                // it isn't known until it answers.
                (LastReview::Unknown | LastReview::Failed(_), Some(login)) => {
                    diff.edit(|i| i.last_review = LastReview::Unknown);
                    state.info("Looking up your last review…");
                    vec![Cmd::Api(Api::FetchLastReview { pr, login })]
                }
                // Without a login, only the local record can say.
                (LastReview::Unknown | LastReview::Failed(_), None) => {
                    diff.edit(|i| i.last_review = LastReview::None);
                    Vec::new()
                }
                (LastReview::None | LastReview::At(_), _) => {
                    if !diff.listed() {
                        state.info("Comparing once the diff has loaded");
                    }
                    Vec::new()
                }
            }
        }
        Action::PickCommits => {
            if diff.inputs().commits.is_empty() {
                state.info("Listing commits…");
                return vec![Cmd::Git(Git::ListCommits(pr))];
            }
            state.open_picker(picker::Kind::Commits { mark: None })
        }
        // `c` on a thread replies to it; with lines selected, it comments
        // on them, wherever the cursor has gone.
        Action::Comment
            if screen.selection.is_none()
                && let Some(Annotation {
                    key: AnnotationKey::Thread(thread_id),
                    can_reply,
                    ..
                }) = &annotation =>
        {
            if !can_reply {
                return state.just_say("You can't reply to this one");
            }
            let thread_id = thread_id.clone();
            state.compose(Target::Review(Comment::Reply { thread_id }), "")
        }
        // `c` on a draft's rows comments where it is, as on its line.
        Action::Comment
            if screen.selection.is_none()
                && matches!(diff.doc.row(cursor), Some(Row::Thread(_)))
                && let Some(&AnnotationKey::Draft(id)) = annotation.as_ref().map(|a| &a.key) =>
        {
            let pending = &diff.inputs().review.pending;
            let Some(target) = pending.iter().find(|d| d.id == id).map(place) else {
                return Vec::new();
            };
            state.compose(Target::Review(target), "")
        }
        Action::Comment => {
            // The selection stays until the comment is added.
            match target(&diff.doc, cursor, half, screen.selection) {
                Ok(target) => {
                    if let Some(reason) = target.note() {
                        state.info(reason);
                    }
                    state.compose(Target::Review(target), "")
                }
                Err(err) => error(state, &err),
            }
        }
        Action::FileComment => {
            // On a draft: turn it into a file comment (e.g. after GitHub
            // rejected its line). Elsewhere: comment on the file.
            if let Some(ann) = &annotation
                && let AnnotationKey::Draft(id) = ann.key
            {
                let save = diff.edit_review(&pr, |i| {
                    if let Some(d) = i.review.pending.iter_mut().find(|d| d.id == id) {
                        d.line = None;
                        d.start_line = None;
                        d.start_side = None;
                        d.error = None;
                    }
                });
                state.info("Now a comment on the file");
                return save;
            }
            let Some(file) = diff.doc.files().get(cursor.file) else {
                return Vec::new();
            };
            let path = file.meta.path().to_owned();
            state.compose(Target::Review(Comment::File { path, reason: None }), "")
        }
        Action::Suggest => {
            let target = match target(&diff.doc, cursor, half, screen.selection) {
                Ok(target) => target,
                Err(err) => return error(state, &err),
            };
            let Comment::Line { path, start, end } = target else {
                return error(state, "Suggestions need lines GitHub can comment on");
            };
            if start.side != Side::Right || end.side != Side::Right {
                return error(state, "Suggestions apply to new lines (not deleted ones)");
            }
            let Some(text) = diff.doc.files().get(cursor.file).and_then(|f| f.text()) else {
                return error(state, "Suggestions need the file's text");
            };
            let original: Vec<String> = (start.line..=end.line)
                .map(|line| text.line(LinePos { line, ..start }).to_owned())
                .collect();
            vec![Cmd::Edit {
                text: original.join("\n") + "\n",
                purpose: EditPurpose::Suggest {
                    path,
                    start,
                    end,
                    original,
                },
            }]
        }
        Action::ResolveThread => {
            let Some(ann) = annotation else {
                return state.just_say("Move to a thread to resolve it");
            };
            let AnnotationKey::Thread(thread_id) = ann.key else {
                return state.just_say("Drafts can't be resolved");
            };
            let resolved = !ann.resolved;
            if (resolved && !ann.can_resolve) || (!resolved && !ann.can_unresolve) {
                return state.just_say("You can't change this thread");
            }
            // Optimistic; rolled back if GitHub refuses. The cursor stays
            // on the thread as it folds or unfolds.
            diff_screen::preserving_position(screen, diff, |d| {
                d.edit(|i| i.set_resolved(&thread_id, resolved));
            });
            crate::work::start(Resolve {
                pr,
                thread: thread_id,
                resolved,
            })
        }
        Action::Delete => match annotation.map(|a| a.key) {
            Some(AnnotationKey::Draft(id)) => delete_draft(state, &pr, id),
            _ => state.just_say("Move to a draft comment to delete it"),
        },
        Action::UndoDelete => restore_draft(state, &pr),
        // On a thread or draft (the diff handles the rest).
        Action::Open => {
            if let Some(&AnnotationKey::Draft(id)) = annotation.as_ref().map(|a| &a.key) {
                return edit_draft(state, &pr, id);
            }
            // A line of a comment with a link follows it.
            let url = match (diff.doc.row(cursor), diff.doc.files().get(cursor.file)) {
                (Some(Row::Thread(t)), Some(file)) => file
                    .thread_row(t)
                    .and_then(|r| r.line.links.first().cloned()),
                _ => None,
            };
            if let Some(url) = url {
                return state.follow(&crate::ui::page::Link::Url(url));
            }
            if let Some(at) = at {
                diff.doc.toggle_thread(at);
            }
            Vec::new()
        }
        Action::SubmitReview => {
            if diff.refs.is_none() {
                return error(state, "The diff hasn't loaded yet");
            }
            state.overlay = Some(Overlay::Submit(Box::new(SubmitDialog::new(
                &state.theme,
                pr.clone(),
            ))));
            vec![Cmd::Api(Api::FetchPendingReview(pr))]
        }
        _ => state.just_say(action.not_here()),
    }
}

/// Opens the composer on your draft `id`. From the Review tab, it opens
/// Files changed on the draft first, where the composer goes.
#[must_use]
fn edit_draft(state: &mut State, pr: &PrRef, id: u64) -> Vec<Cmd> {
    let mut cmds = match state.screen() {
        Screen::Page(p) => match p.selected_link().cloned() {
            Some(link) => state.follow(&link),
            None => Vec::new(),
        },
        Screen::Diff(_) => Vec::new(),
    };
    let of = DiffOf::Pr(pr.clone());
    let pending = state.diffs.get(&of).map(|d| &d.inputs().review.pending);
    let Some(body) = pending
        .and_then(|p| p.iter().find(|d| d.id == id))
        .map(|d| d.body.clone())
    else {
        return cmds;
    };
    cmds.extend(state.compose(Target::Review(Comment::Draft { id }), &body));
    cmds
}

/// Deletes your draft `id`, which `ctrl-z` brings back.
#[must_use]
fn delete_draft(state: &mut State, pr: &PrRef, id: u64) -> Vec<Cmd> {
    let Some(diff) = state.diffs.get_mut(&DiffOf::Pr(pr.clone())) else {
        return Vec::new();
    };
    let Some(at) = diff.inputs().review.pending.iter().position(|d| d.id == id) else {
        return Vec::new();
    };
    let save = diff.edit_review(pr, |i| i.deleted = Some(i.review.pending.remove(at)));
    let undo = state.first_key(Action::UndoDelete);
    state.info(format!("Draft deleted · {undo} brings it back"));
    save
}

/// Brings back the draft just deleted.
#[must_use]
fn restore_draft(state: &mut State, pr: &PrRef) -> Vec<Cmd> {
    let Some(diff) = state.diffs.get_mut(&DiffOf::Pr(pr.clone())) else {
        return Vec::new();
    };
    let Some(mut draft) = diff.edit(|i| i.deleted.take()) else {
        return state.just_say("No deleted draft to bring back");
    };
    let save = diff.edit_review(pr, |i| {
        if i.review.pending.iter().any(|d| d.id == draft.id) {
            draft.id = i.review.next_draft_id();
        }
        i.review.pending.push(draft);
    });
    state.info("Draft restored");
    save
}

/// Your draft that's selected: under the diff's cursor, or a row of the
/// Review tab.
fn draft_here(state: &State) -> Option<(PrRef, u64)> {
    match state.screen() {
        Screen::Diff(screen) if screen.focus == diff_screen::Pane::Diff => {
            let doc = &state.diffs.get(&screen.of)?.doc;
            let ann = doc.annotation_under(screen.cursor, screen.half)?;
            match doc.annotations().get(ann as usize)?.key {
                AnnotationKey::Draft(id) => Some((screen.of.pr()?.clone(), id)),
                AnnotationKey::Thread(_) | AnnotationKey::Check(_) => None,
            }
        }
        Screen::Diff(_) => None,
        Screen::Page(p) => match p.selected_link()? {
            Link::Review(ReviewLink::Draft { pr, id, .. }) => Some((pr.clone(), *id)),
            _ => None,
        },
    }
}

/// The pull request whose Review tab is on screen.
fn review_tab(state: &State) -> Option<&PrRef> {
    match state.route()? {
        Route::Pulls(pulls::Pr {
            pr,
            tab: PrTab::Review,
        }) => Some(pr),
        _ => None,
    }
}

/// Whether your drafts' keys take `action` here: `,e` and `,D` on a
/// draft, as Enter and del do in Files changed, and on the Review tab del
/// on a draft and `ctrl-z` after deleting one.
pub fn takes(state: &State, action: Action) -> bool {
    match action {
        Action::EditComment | Action::DeleteComment => draft_here(state).is_some(),
        Action::Delete => review_tab(state).is_some() && draft_here(state).is_some(),
        Action::UndoDelete => review_tab(state).is_some(),
        _ => false,
    }
}

#[must_use]
fn draft_action(state: &mut State, action: Action) -> Vec<Cmd> {
    if action == Action::UndoDelete {
        return match review_tab(state).cloned() {
            Some(pr) => restore_draft(state, &pr),
            None => Vec::new(),
        };
    }
    let Some((pr, id)) = draft_here(state) else {
        return Vec::new();
    };
    match action {
        Action::EditComment => edit_draft(state, &pr, id),
        _ => delete_draft(state, &pr, id),
    }
}

/// Your drafts' rows in the menu, where one is selected.
fn offers(state: &State, out: &mut Vec<crate::nav::Doable>) {
    if draft_here(state).is_some() {
        for (action, label) in [
            (Action::EditComment, "Edit your draft"),
            (Action::DeleteComment, "Delete your draft"),
        ] {
            out.push(crate::nav::row("Edit", action, label, "", None));
        }
    }
}

/// Your drafts, edited and deleted where they're selected.
pub const FEATURE: crate::features::Feature = crate::features::Feature {
    takes,
    act: draft_action,
    offers,
    ..crate::features::Feature::NONE
};

/// Shows what `choice` picks: the whole PR, the changes since your last
/// review, or a commit (from `mark` to it, if a range was marked).
#[must_use]
pub(crate) fn apply_commit_choice(
    state: &mut State,
    choice: PickItem,
    mark: Option<usize>,
) -> Vec<Cmd> {
    let pr = match state.screen() {
        Screen::Diff(screen) => match screen.of.pr() {
            Some(pr) => pr.clone(),
            None => return Vec::new(),
        },
        Screen::Page(_) => return Vec::new(),
    };
    let Some(base) = state.pr_base(&pr) else {
        return Vec::new();
    };
    let Some((screen, diff)) = state.diff_parts() else {
        return Vec::new();
    };
    let since = choice == PickItem::SinceReview;
    let range = match choice {
        // Already on the whole PR: only "since your review" changes.
        PickItem::All | PickItem::SinceReview if diff.range().is_none() => {
            if diff.doc.since_active() == since {
                return Vec::new();
            }
            return review_action(state, Action::ToggleSinceReview);
        }
        PickItem::All | PickItem::SinceReview => None,
        PickItem::Commit(i) => {
            let j = mark.unwrap_or(i);
            let (first, last) = (i.min(j), i.max(j));
            // The picker's indices, into the list it showed.
            let commits = &diff.inputs().commits;
            let (Some(from), Some(to)) = (commits.get(first), commits.get(last)) else {
                return Vec::new();
            };
            let (from, to) = (&from.oid, &to.oid);
            let label = if first == last {
                short_sha(to).to_owned()
            } else {
                format!("{}..{}", short_sha(from), short_sha(to))
            };
            Some(diff_screen::RangeView {
                label,
                from: format!("{from}^"),
                to: to.to_string(),
            })
        }
    };
    let cmd_range = range.as_ref().map(|r| (r.from.clone(), r.to.clone()));
    diff.restart(range);
    // Since your review: compared once the whole PR is back.
    diff.since_requested = since;
    let job = diff.job;
    screen.restart();
    vec![Cmd::Git(Git::LoadDiff {
        of: pr.into(),
        job,
        base,
        range: cmd_range,
    })]
}

/// `ctrl-s` in the composer on a review's comment: drafts join the
/// pending review (and are saved); replies post right away.
#[must_use]
pub(crate) fn save(state: &mut State, comment: Comment, body: String) -> Vec<Cmd> {
    let Some((screen, diff)) = state.diff_parts() else {
        return Vec::new();
    };
    let Some(pr) = screen.of.pr().cloned() else {
        return Vec::new();
    };
    match comment {
        Comment::Reply { thread_id } => {
            crate::forms::sending(state);
            let reply = Change::Reply {
                thread: thread_id,
                body,
            };
            vec![crate::act::send(reply, crate::act::By::Reply(pr))]
        }
        Comment::Draft { id } => {
            let save = diff.edit_review(&pr, |i| {
                if let Some(d) = i.review.pending.iter_mut().find(|d| d.id == id) {
                    d.body = body;
                    d.error = None;
                }
            });
            state.overlay = None;
            save
        }
        target @ (Comment::Line { .. } | Comment::File { .. }) => {
            let Some(head) = diff.head() else {
                return Vec::new();
            };
            let id = diff.inputs().review.next_draft_id();
            let Some(draft) = draft(&target, &body, id, &head) else {
                return Vec::new();
            };
            let save = diff.edit_review(&pr, |i| i.review.pending.push(draft));
            let count = diff.inputs().review.pending.len();
            screen.selection = None;
            state.overlay = None;
            state.info(format!(
                "Added to your review ({count} pending). Submit with {}",
                state.first_key(Action::SubmitReview)
            ));
            save
        }
    }
}

#[must_use]
pub(crate) fn on_submit_key(state: &mut State, key: KeyEvent) -> Vec<Cmd> {
    let Some(Overlay::Submit(dialog)) = &mut state.overlay else {
        return Vec::new();
    };
    if dialog.sent.sending {
        return Vec::new();
    }
    let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
    match key.code {
        KeyCode::Esc => {
            if crate::compose::may_discard(state, "esc", "discard") {
                state.overlay = None;
            }
        }
        KeyCode::Tab => dialog.cycle(true),
        KeyCode::BackTab => dialog.cycle(false),
        KeyCode::Char('e') if ctrl => {
            return vec![Cmd::Edit {
                purpose: EditPurpose::Summary,
                text: dialog.input.lines().join("\n"),
            }];
        }
        KeyCode::Char('s') if ctrl => return submit(state),
        KeyCode::Enter if dialog.quick => return submit(state),
        _ => {
            dialog.discard_armed = None;
            dialog.sent.error = None;
            dialog.input.input(key);
        }
    }
    Vec::new()
}

/// Submits the review in the dialog, with your drafts, on the head they
/// were written against: the diff's, or the pull request's from its page.
#[must_use]
fn submit(state: &mut State) -> Vec<Cmd> {
    let Some(Overlay::Submit(dialog)) = &state.overlay else {
        return Vec::new();
    };
    let (event, body, pr, seen) = (
        dialog.event,
        dialog.input.lines().join("\n"),
        dialog.pr.clone(),
        dialog.on_github.unwrap_or(0),
    );
    let drafts = dialog.drafts(state).map(<[DraftComment]>::to_vec);
    let head = match state.diffs.get(&DiffOf::Pr(pr.clone())) {
        Some(diff) => diff.head(),
        None => (state.pr(&pr)).and_then(|d| crate::git::Oid::parse(&d.head_oid).ok()),
    };
    let problem = match (&drafts, &head) {
        _ if event == ReviewEvent::RequestChanges && body.trim().is_empty() => {
            Some("Requesting changes needs a summary")
        }
        (None, _) => Some("Still reading your saved drafts; try again in a moment"),
        (_, None) => Some("The pull request hasn't loaded yet"),
        _ => None,
    };
    let Some(Overlay::Submit(dialog)) = &mut state.overlay else {
        return Vec::new();
    };
    let (None, Some(drafts), Some(head)) = (problem, drafts, head) else {
        dialog.sent.error = problem.map(Into::into);
        return Vec::new();
    };
    dialog.sent.sending = true;
    dialog.sent.error = None;
    crate::work::start(Submit {
        pr,
        head,
        drafts,
        event,
        body,
        seen,
    })
}

/// Opens the review dialog set to approve, from anywhere `pr` is on
/// screen. Your saved drafts go with it; without its diff open, they're
/// read from disk first.
#[must_use]
pub(crate) fn open_approve(state: &mut State, pr: PrRef) -> Vec<Cmd> {
    let mut dialog = SubmitDialog::new(&state.theme, pr.clone());
    dialog.event = ReviewEvent::Approve;
    dialog.quick = true;
    state.overlay = Some(Overlay::Submit(Box::new(dialog)));
    let mut cmds = vec![Cmd::Api(Api::FetchPendingReview(pr.clone()))];
    if !state.diffs.contains_key(&DiffOf::Pr(pr.clone())) {
        cmds.push(Cmd::LoadReview(pr));
    }
    cmds
}

/// Marks a file viewed on GitHub, or not: shown at once, and put back
/// as it was if GitHub refuses.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SetViewed {
    pub pr: PrRef,
    pub pull_request_id: NodeId,
    pub path: String,
    pub viewed: bool,
    pub previous: Viewed,
}

impl Job for SetViewed {
    type Out = ();
    /// Only the diff shows it, and it already does.
    const WRITES: bool = false;

    async fn run(&self, cx: &Cx) -> Result<(), Failure> {
        let id = &self.pull_request_id;
        Ok(cx.gh.set_viewed(id, &self.path, self.viewed).await?)
    }

    fn done(self, state: &mut State, result: Result<(), Failure>) -> Vec<Cmd> {
        if let Err(err) = result {
            tracing::warn!(pr = %self.pr, %err, "viewed state update failed");
            let of = DiffOf::Pr(self.pr);
            diff_screen::edit(state, &of, |i| i.set_viewed(self.path, self.previous));
            state.error(format!("GitHub didn't save “viewed”: {err}"));
        }
        Vec::new()
    }
}

/// Resolves a review thread on GitHub, or unresolves it: shown at once,
/// and put back if GitHub refuses.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Resolve {
    pub pr: PrRef,
    pub thread: NodeId,
    pub resolved: bool,
}

impl Job for Resolve {
    type Out = ();

    async fn run(&self, cx: &Cx) -> Result<(), Failure> {
        Ok(cx.gh.set_resolved(&self.thread, self.resolved).await?)
    }

    fn done(self, state: &mut State, result: Result<(), Failure>) -> Vec<Cmd> {
        if let Err(err) = result {
            let of = DiffOf::Pr(self.pr);
            diff_screen::edit(state, &of, |i| i.set_resolved(&self.thread, !self.resolved));
            state.error(format!("GitHub didn't save that: {err}"));
        }
        Vec::new()
    }
}

/// Submits a review ([`submit_review`]).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Submit {
    pub pr: PrRef,
    pub head: crate::git::Oid,
    pub drafts: Vec<DraftComment>,
    pub event: ReviewEvent,
    pub body: String,
    /// The comments in your pending review on GitHub you were shown: if
    /// it holds more, it isn't submitted.
    pub seen: u64,
}

impl Job for Submit {
    type Out = SubmitOutcome;

    /// Drafts GitHub accepted wait in a pending review there.
    fn wrote(result: &Result<SubmitOutcome, Failure>) -> bool {
        matches!(result, Ok(o) if o.error.is_none() || !o.accepted.is_empty())
    }

    async fn run(&self, cx: &Cx) -> Result<SubmitOutcome, Failure> {
        let review = Review {
            drafts: self.drafts.clone(),
            event: self.event,
            body: &self.body,
            seen: self.seen,
        };
        Ok(submit_review(&cx.gh, &self.pr, &self.head, review).await)
    }

    fn done(self, state: &mut State, result: Result<SubmitOutcome, Failure>) -> Vec<Cmd> {
        let outcome = result.unwrap_or_else(|err| SubmitOutcome {
            error: Some(format!("Couldn't submit: {err}")),
            ..SubmitOutcome::default()
        });
        on_submitted(state, &self.pr, &outcome)
    }
}

fn api_message(err: &ApiError) -> String {
    match err {
        ApiError::GraphQl(messages) => messages.join("; "),
        err => err.to_string(),
    }
}

/// A review to submit.
struct Review<'a> {
    drafts: Vec<DraftComment>,
    event: ReviewEvent,
    body: &'a str,
    /// The comments in your pending review on GitHub you were shown.
    seen: u64,
}

/// Submits a review: reuses the viewer's pending review on GitHub (or
/// starts one on `head`), adds each draft as a thread, and submits only if
/// GitHub accepted every draft. Each draft's fate is reported, so rejected
/// ones keep their text. A pending review holding comments you weren't
/// shown isn't submitted: they'd be published unseen.
async fn submit_review(gh: &GitHub, pr: &PrRef, head: &str, review: Review<'_>) -> SubmitOutcome {
    let mut outcome = SubmitOutcome::default();
    // GitHub can't be reached, and nothing was sent: to queue.
    let unsent = |review: &Review<'_>| {
        Some(Box::new(crate::offline::queue::Review {
            pr: pr.clone(),
            head: head.to_owned(),
            drafts: review.drafts.clone(),
            event: review.event,
            body: review.body.to_owned(),
            seen: review.seen,
        }))
    };
    let (review_id, held) = match gh.pending_review(pr).await {
        Ok((_, Some(pending))) if pending.comments > review.seen => {
            outcome.on_github = Some(pending.comments);
            outcome.error = Some(
                "Not sent: your pending review on GitHub holds comments you hadn't seen".into(),
            );
            return outcome;
        }
        Ok((_, Some(pending))) => match reusable(&pending, head) {
            Ok(()) => (pending.id, pending.comments),
            Err(message) => {
                outcome.error = Some(message);
                return outcome;
            }
        },
        Ok((pr_id, None)) => match gh.start_review(&pr_id, head).await {
            Ok(id) => (id, 0),
            Err(err) => {
                if let ApiError::Offline(_) = err {
                    outcome.unsent = unsent(&review);
                }
                outcome.error = Some(format!("Couldn't start the review: {}", api_message(&err)));
                return outcome;
            }
        },
        Err(err) => {
            if let ApiError::Offline(_) = err {
                outcome.unsent = unsent(&review);
            }
            outcome.error = Some(format!("Couldn't reach the PR: {}", api_message(&err)));
            return outcome;
        }
    };
    for draft in review.drafts {
        match gh.add_review_thread(&review_id, &new_thread(&draft)).await {
            Ok(_) => outcome.accepted.push(draft.id),
            Err(err) => outcome.rejected.push((draft.id, api_message(&err))),
        }
    }
    // Those accepted are in it now, and were yours to see.
    outcome.on_github = Some(held + outcome.accepted.len() as u64);
    if !outcome.rejected.is_empty() {
        return outcome;
    }
    match gh
        .submit_review(&review_id, review.event, review.body)
        .await
    {
        Ok(()) => outcome.submitted = true,
        Err(err) => outcome.error = Some(format!("Couldn't submit: {}", api_message(&err))),
    }
    outcome
}

/// Submits a queued review ([`crate::offline::queue`]). What GitHub
/// refuses stays queued as refused, without the comments it took (they
/// wait in your pending review there, counted as seen).
pub(crate) async fn send_review(
    gh: &GitHub,
    queued: crate::offline::queue::Review,
) -> crate::offline::queue::Sent {
    use crate::offline::queue::Sent;
    let review = Review {
        drafts: queued.drafts.clone(),
        event: queued.event,
        body: &queued.body,
        seen: queued.seen,
    };
    let outcome = submit_review(gh, &queued.pr, &queued.head, review).await;
    if outcome.unsent.is_some() {
        return Sent::Unsent;
    }
    if outcome.submitted {
        return Sent::Taken;
    }
    let why = match (&outcome.error, outcome.rejected.as_slice()) {
        (Some(err), _) => err.clone(),
        (None, rejected) => {
            let reasons: Vec<&str> = rejected.iter().map(|(_, r)| r.as_str()).collect();
            format!("GitHub refused comments: {}", reasons.join("; "))
        }
    };
    let seen = outcome.on_github.unwrap_or(queued.seen);
    let mut rest = queued;
    rest.drafts.retain(|d| !outcome.accepted.contains(&d.id));
    for draft in &mut rest.drafts {
        draft.error = (outcome.rejected.iter())
            .find(|(id, _)| *id == draft.id)
            .map(|(_, reason)| reason.clone());
    }
    rest.seen = seen;
    Sent::Partly(rest, why)
}

/// Whether the drafts can join your pending review: it must be on `head`.
/// One on another commit has its comments placed on that commit's lines,
/// and the drafts' line numbers are from this diff, so they're not mixed;
/// nor is the pending review discarded, since it may hold comments written
/// on GitHub.
fn reusable(pending: &PendingReview, head: &str) -> Result<(), String> {
    match &pending.commit {
        Some(commit) if commit != head => Err(format!(
            "You have a pending review on another commit ({}): finish or discard it on GitHub, then submit again",
            crate::ui::text::short_sha(commit)
        )),
        _ => Ok(()),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn a_pending_review_on_another_commit_is_not_reused() {
        let on = |commit: Option<&str>| PendingReview {
            id: NodeId::new("R_1"),
            commit: commit.map(Into::into),
            comments: 0,
        };
        assert_eq!(reusable(&on(Some("abc")), "abc"), Ok(()));
        assert_eq!(reusable(&on(None), "abc"), Ok(()));
        let err = reusable(&on(Some("0123456789")), "abc").unwrap_err();
        assert!(err.contains("another commit (0123456)"), "{err}");
    }
    use crate::fixtures::thread;

    #[test]
    fn outdated_threads_move_when_mapped() {
        let threads = vec![
            thread("current", Some(3), false, false),
            thread("mapped", None, false, true),
            thread("lost", None, false, true),
        ];
        let mapped = HashMap::from([
            (NodeId::new("mapped"), Some(12)),
            (NodeId::new("lost"), None),
        ]);
        let anns = annotations(&threads, &mapped, &[], &HashSet::new());
        assert!(matches!(anns[0].place, Place::Line { at, .. } if at.line == 3));
        assert!(matches!(anns[1].place, Place::Moved(at) if at.line == 12));
        assert_eq!(anns[2].place, Place::Outdated(Some(3)));
        assert_eq!(outdated_to_map(&threads).len(), 2);
    }

    #[test]
    fn drafts_become_ranges_or_file_comments() {
        let right = |line| LinePos {
            side: Side::Right,
            line,
        };
        let range = Comment::Line {
            path: "a.rs".into(),
            start: right(3),
            end: right(5),
        };
        let d = draft(&range, "x", 1, "head").unwrap();
        assert_eq!(
            (d.line, d.start_line, d.start_side),
            (Some(5), Some(3), Some(DraftSide::Right))
        );
        let thread = new_thread(&d);
        assert_eq!((thread.line, thread.start_line), (Some(5), Some(3)));

        let single = Comment::Line {
            path: "a.rs".into(),
            start: right(4),
            end: right(4),
        };
        let d = draft(&single, "x", 2, "head").unwrap();
        assert_eq!(
            (d.start_line, d.start_side),
            (None, None),
            "single lines have no start"
        );

        let file = Comment::File {
            path: "a.rs".into(),
            reason: None,
        };
        assert_eq!(draft(&file, "x", 3, "head").unwrap().line, None);
        assert!(
            draft(
                &Comment::Reply {
                    thread_id: NodeId::new("t")
                },
                "x",
                4,
                "h"
            )
            .is_none()
        );

        let anns = annotations(&[], &HashMap::new(), &[d_with_error()], &HashSet::new());
        assert!(anns[0].is_draft());
        assert_eq!(anns[0].error.as_deref(), Some("nope"));
    }

    fn d_with_error() -> DraftComment {
        DraftComment {
            id: 9,
            path: "a.rs".into(),
            body: "b".into(),
            line: Some(1),
            commit: "h".into(),
            error: Some("nope".into()),
            ..Default::default()
        }
    }

    #[test]
    fn suggestion_fences() {
        assert_eq!(
            suggestion_body("let x = 1;\n"),
            "```suggestion\nlet x = 1;\n```"
        );
    }

    #[test]
    fn submit_events_cycle() {
        let theme = Theme::new(
            crate::theme::DEFAULT_SEED,
            crate::theme::Mode::Dark,
            crate::theme::ColorDepth::TrueColor,
        );
        let mut dialog = SubmitDialog::new(&theme, PrRef::parse("o/r#1").unwrap());
        dialog.cycle(true);
        assert_eq!(dialog.event, ReviewEvent::Approve);
        dialog.cycle(true);
        assert_eq!(dialog.event, ReviewEvent::RequestChanges);
        dialog.cycle(true);
        assert_eq!(dialog.event, ReviewEvent::Comment);
        dialog.cycle(false);
        assert_eq!(dialog.event, ReviewEvent::RequestChanges);
    }
}