linear-tui 0.13.0

A TUI client for Linear.app — manage issues, projects, and cycles from your terminal
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
//! Projects: a team's, and a saved project view's, and each with its
//! milestones.
//!
//! **Lists.** [`open_team_projects`] and [`open_view_projects`] list them;
//! [`next_page`], [`reload`], and [`take_page`] follow the list as pages
//! land. A project's issues are an issue list: `issue::open_project_issues`.
//!
//! **One project.** [`find`] finds one by name, [`open`] reads it with its
//! milestones, and [`open_url`] shows it on linear.app. [`load_statuses`]
//! asks for the statuses a project can be in.
//!
//! **Changes.** [`create`], [`update`], and [`delete`] a project;
//! [`add_milestone`], [`update_milestone`], and [`delete_milestone`] its
//! milestones. Nothing on screen changes before Linear answers, except that
//! a deleted project leaves the lists at once.

use super::{Open, Refusal};
use crate::core::entity::{
    CustomViewId, MilestoneId, Page, Priority, Project, ProjectId, ProjectStatusId, TeamId, UserId,
};
use crate::core::store::{List, ListOf, Store};

/// What the project use cases ask of Linear, and of the desktop.
#[derive(Debug, Clone, PartialEq)]
pub enum Request {
    /// A page of a team's projects.
    TeamProjects {
        team_id: TeamId,
        after: Option<String>,
    },
    /// A page of a saved project view's projects. Linear evaluates the view's
    /// filter.
    ViewProjects {
        view_id: CustomViewId,
        after: Option<String>,
    },
    /// Show the project's page on linear.app in the desktop's browser.
    OpenInBrowser(String),
    /// The projects of the workspace with this name, in any case.
    Find {
        name: String,
    },
    /// One project, with its teams and milestones.
    Detail {
        project_id: ProjectId,
    },
    /// The statuses a project can be in.
    Statuses,
    Create {
        draft: Draft,
    },
    Update {
        project_id: ProjectId,
        changes: Changes,
    },
    Delete {
        project_id: ProjectId,
    },
    CreateMilestone {
        project_id: ProjectId,
        draft: MilestoneDraft,
    },
    UpdateMilestone {
        milestone_id: MilestoneId,
        changes: MilestoneChanges,
    },
    DeleteMilestone {
        milestone_id: MilestoneId,
    },
}

impl Request {
    /// The page cursor this request continues from, if it asks for a next
    /// page.
    pub fn cursor(&self) -> Option<&str> {
        match self {
            Self::TeamProjects { after, .. } | Self::ViewProjects { after, .. } => after.as_deref(),
            _ => None,
        }
    }
}

/// What a new project is made with. Dates are `YYYY-MM-DD`.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Draft {
    /// Required.
    pub name: String,
    /// At least one.
    pub team_ids: Vec<TeamId>,
    /// Its summary, a line or two.
    pub description: Option<String>,
    /// Its body, in Markdown.
    pub content: Option<String>,
    pub lead_id: Option<UserId>,
    pub status_id: Option<ProjectStatusId>,
    pub priority: Option<Priority>,
    pub start_date: Option<String>,
    pub target_date: Option<String>,
}

/// What an update changes on a project. A field left `None` stays as it
/// is; for one that can be emptied, `Some(None)` empties it.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Changes {
    pub name: Option<String>,
    pub description: Option<String>,
    /// The whole body, replacing what it was; empty text empties it.
    pub content: Option<String>,
    pub lead_id: Option<Option<UserId>>,
    pub status_id: Option<ProjectStatusId>,
    pub priority: Option<Priority>,
    pub start_date: Option<Option<String>>,
    pub target_date: Option<Option<String>>,
}

impl Changes {
    fn is_empty(&self) -> bool {
        *self == Self::default()
    }
}

/// What a new milestone is made with.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct MilestoneDraft {
    /// Required.
    pub name: String,
    pub description: Option<String>,
    /// `YYYY-MM-DD`.
    pub target_date: Option<String>,
}

/// What an update changes on a milestone, as [`Changes`] does.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct MilestoneChanges {
    pub name: Option<String>,
    pub description: Option<String>,
    pub target_date: Option<Option<String>>,
}

/// Refuse a name given, and empty.
fn named(name: Option<&String>) -> Result<(), Refusal> {
    match name {
        Some(name) if name.trim().is_empty() => Err(Refusal::NameRequired),
        _ => Ok(()),
    }
}

/// Refuse a target date before the start date, when both are known.
/// Dates in `YYYY-MM-DD` sort as text.
fn in_order(start: Option<&str>, target: Option<&str>) -> Result<(), Refusal> {
    match (start, target) {
        (Some(start), Some(target)) if target < start => Err(Refusal::EndsBeforeItStarts {
            start: start.to_string(),
            target: target.to_string(),
        }),
        _ => Ok(()),
    }
}

/// Which list of projects: the team's, or a saved view's.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Projects {
    Team,
    View,
}

impl Projects {
    fn list(self) -> List {
        match self {
            Self::Team => List::Projects,
            Self::View => List::ViewProjects,
        }
    }
}

/// **Browse a team's projects.** A list already holding them is not fetched
/// again; another team's projects are dropped first.
pub fn open_team_projects(store: &mut Store, team_id: TeamId) -> Open {
    store
        .open_list(List::Projects, ListOf::TeamProjects(team_id))
        .into()
}

/// **Open a saved project view.** Linear evaluates its filter; another
/// view's projects are dropped first.
pub fn open_view_projects(store: &mut Store, view_id: CustomViewId) -> Open {
    store
        .open_list(List::ViewProjects, ListOf::ViewProjects(view_id))
        .into()
}

/// **Scroll on to the next page** of projects, asked for once.
pub fn next_page(store: &mut Store, projects: Projects) -> Option<super::Request> {
    store.next_page(projects.list()).map(super::Request::page)
}

/// **Refresh a list of projects**: its first page again.
pub fn reload(store: &mut Store, projects: Projects) -> Option<super::Request> {
    store.reload_list(projects.list()).map(super::Request::page)
}

/// **Find a project by name**, anywhere in the workspace, in any case.
///
/// Linear answers every project so named: several teams can each have one,
/// and telling them apart is for whoever asked. An empty name finds nothing
/// and is refused.
pub fn find(name: &str) -> Result<Request, Refusal> {
    let name = name.trim();
    if name.is_empty() {
        return Err(Refusal::NameRequired);
    }
    Ok(Request::Find {
        name: name.to_string(),
    })
}

/// **Read a project**: its fields, the teams it belongs to, and its
/// milestones.
pub fn open(project_id: ProjectId) -> Request {
    Request::Detail { project_id }
}

/// **Know the statuses a project can be in**: the workspace's, from
/// Backlog to Completed.
pub fn load_statuses() -> Request {
    Request::Statuses
}

/// **Create a project** in one or more teams.
///
/// It needs a name and a team, and a target date on or after its start
/// date. An empty description or content is sent as none.
pub fn create(mut draft: Draft) -> Result<Request, Refusal> {
    if draft.name.trim().is_empty() {
        return Err(Refusal::NameRequired);
    }
    if draft.team_ids.is_empty() {
        return Err(Refusal::TeamRequired);
    }
    in_order(draft.start_date.as_deref(), draft.target_date.as_deref())?;
    draft.description = draft.description.filter(|d| !d.trim().is_empty());
    draft.content = draft.content.filter(|c| !c.trim().is_empty());
    Ok(Request::Create { draft })
}

/// **Change a project**: its name, description, content, lead, status,
/// priority, and dates.
///
/// A change of nothing is refused, and so is an empty name. Its content is
/// replaced whole, and empty content empties it. A target date
/// before the start date is refused when both are given; one given alone is
/// Linear's to check against the date it holds. Every list holding the
/// project shows the new name at once.
pub fn update(
    store: &mut Store,
    project_id: &ProjectId,
    changes: Changes,
) -> Result<Request, Refusal> {
    if changes.is_empty() {
        return Err(Refusal::NothingToChange);
    }
    named(changes.name.as_ref())?;
    in_order(
        changes.start_date.clone().flatten().as_deref(),
        changes.target_date.clone().flatten().as_deref(),
    )?;
    if let Some(name) = &changes.name {
        for rows in [&mut store.projects, &mut store.view_projects] {
            for project in rows.items.iter_mut().filter(|p| &p.id == project_id) {
                project.name = name.clone();
            }
        }
    }
    Ok(Request::Update {
        project_id: project_id.clone(),
        changes,
    })
}

/// **Delete a project.** Linear moves it to its trash, where it can be
/// restored for a while; its issues stay, without a project. It leaves every
/// list at once.
pub fn delete(store: &mut Store, project_id: &ProjectId) -> Request {
    for rows in [&mut store.projects, &mut store.view_projects] {
        rows.items.retain(|p| &p.id != project_id);
    }
    Request::Delete {
        project_id: project_id.clone(),
    }
}

/// **Add a milestone** to a project. It needs a name.
pub fn add_milestone(project_id: ProjectId, mut draft: MilestoneDraft) -> Result<Request, Refusal> {
    if draft.name.trim().is_empty() {
        return Err(Refusal::NameRequired);
    }
    draft.description = draft.description.filter(|d| !d.trim().is_empty());
    Ok(Request::CreateMilestone { project_id, draft })
}

/// **Change a milestone**: its name, description, or target date. A change
/// of nothing is refused, and so is an empty name.
pub fn update_milestone(
    milestone_id: MilestoneId,
    changes: MilestoneChanges,
) -> Result<Request, Refusal> {
    if changes == MilestoneChanges::default() {
        return Err(Refusal::NothingToChange);
    }
    named(changes.name.as_ref())?;
    Ok(Request::UpdateMilestone {
        milestone_id,
        changes,
    })
}

/// **Delete a milestone.** Its issues stay in the project, under no
/// milestone.
pub fn delete_milestone(milestone_id: MilestoneId) -> Request {
    Request::DeleteMilestone { milestone_id }
}

/// **Open a project on linear.app** (`o`). Without a URL there is nothing
/// to open.
pub fn open_url(url: Option<String>) -> Result<Request, Refusal> {
    url.map(Request::OpenInBrowser)
        .ok_or(Refusal::NothingToOpen)
}

/// **A page of projects lands**, and is taken only while the list still
/// belongs to what it was fetched for. Returns whether it was taken.
pub fn take_page(store: &mut Store, projects: Projects, of: &ListOf, page: Page<Project>) -> bool {
    match projects {
        Projects::Team => store.projects.accept_for(of, page),
        Projects::View => store.view_projects.accept_for(of, page),
    }
}

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

    fn project(id: &str) -> Project {
        serde_json::from_str(&format!(r#"{{"id":"{id}","name":"{id}"}}"#)).unwrap()
    }

    fn draft(name: &str) -> Draft {
        Draft {
            name: name.into(),
            team_ids: vec![TeamId::from("t")],
            ..Draft::default()
        }
    }

    /// A project is found by name, workspace-wide; an empty name is refused.
    #[test]
    fn a_project_is_found_by_its_name() {
        assert_eq!(
            find("  Forecast v2 "),
            Ok(Request::Find {
                name: "Forecast v2".into()
            })
        );
        assert_eq!(find(" "), Err(Refusal::NameRequired));
    }

    /// Reading a project asks for it, milestones and all; its statuses are
    /// the workspace's.
    #[test]
    fn a_project_is_read_with_its_milestones() {
        assert_eq!(
            open(ProjectId::from("p")),
            Request::Detail {
                project_id: "p".into()
            }
        );
        assert_eq!(load_statuses(), Request::Statuses);
    }

    /// A new project needs a name and a team.
    #[test]
    fn a_new_project_needs_a_name_and_a_team() {
        assert_eq!(create(draft(" ")), Err(Refusal::NameRequired));
        let teamless = Draft {
            team_ids: Vec::new(),
            ..draft("Launch")
        };
        assert_eq!(create(teamless), Err(Refusal::TeamRequired));
    }

    /// A project cannot be due before it starts.
    #[test]
    fn a_project_cannot_end_before_it_starts() {
        let backwards = Draft {
            start_date: Some("2026-10-01".into()),
            target_date: Some("2026-09-01".into()),
            ..draft("Launch")
        };
        assert_eq!(
            create(backwards),
            Err(Refusal::EndsBeforeItStarts {
                start: "2026-10-01".into(),
                target: "2026-09-01".into()
            })
        );
    }

    /// A project is made as drafted, an empty description or content sent
    /// as none.
    #[test]
    fn a_new_project_is_made_as_drafted() {
        let described = Draft {
            description: Some("".into()),
            content: Some(" \n".into()),
            target_date: Some("2026-12-01".into()),
            ..draft("Launch")
        };
        let Ok(Request::Create { draft: made }) = create(described) else {
            panic!("expected a create");
        };
        assert_eq!(made.description, None);
        assert_eq!(made.content, None);
        assert_eq!(made.target_date.as_deref(), Some("2026-12-01"));
        let written = Draft {
            content: Some("## Why\n\nBecause.".into()),
            ..draft("Launch")
        };
        let Ok(Request::Create { draft: made }) = create(written) else {
            panic!("expected a create");
        };
        assert_eq!(made.content.as_deref(), Some("## Why\n\nBecause."));
    }

    /// A change of nothing, or to an empty name, is refused.
    #[test]
    fn an_empty_project_change_is_refused() {
        let mut store = Store::default();
        let p = ProjectId::from("p");
        assert_eq!(
            update(&mut store, &p, Changes::default()),
            Err(Refusal::NothingToChange)
        );
        let unnamed = Changes {
            name: Some("".into()),
            ..Changes::default()
        };
        assert_eq!(update(&mut store, &p, unnamed), Err(Refusal::NameRequired));
    }

    /// A project's content is replaced whole; empty content is asked of
    /// Linear as empty, which empties it.
    #[test]
    fn a_projects_content_is_replaced_whole() {
        let mut store = Store::default();
        let p = ProjectId::from("p");
        for text in ["## Why\n\nBecause.", ""] {
            let changes = Changes {
                content: Some(text.into()),
                ..Changes::default()
            };
            assert_eq!(
                update(&mut store, &p, changes.clone()),
                Ok(Request::Update {
                    project_id: p.clone(),
                    changes
                })
            );
        }
    }

    /// A renamed project shows its new name in the lists at once, and an
    /// emptied date is asked of Linear as none.
    #[test]
    fn a_renamed_project_shows_at_once() {
        let mut store = Store::default();
        store.projects.items = vec![project("p")];
        let changes = Changes {
            name: Some("Renamed".into()),
            target_date: Some(None),
            ..Changes::default()
        };
        let request = update(&mut store, &ProjectId::from("p"), changes.clone());
        assert_eq!(
            request,
            Ok(Request::Update {
                project_id: "p".into(),
                changes
            })
        );
        assert_eq!(store.projects.items[0].name, "Renamed");
    }

    /// A deleted project leaves the lists at once.
    #[test]
    fn a_deleted_project_leaves_the_lists() {
        let mut store = Store::default();
        store.projects.items = vec![project("p"), project("q")];
        assert_eq!(
            delete(&mut store, &ProjectId::from("p")),
            Request::Delete {
                project_id: "p".into()
            }
        );
        assert_eq!(store.projects.items.len(), 1);
    }

    /// A milestone needs a name; one is added as drafted.
    #[test]
    fn a_milestone_needs_a_name() {
        let p = || ProjectId::from("p");
        assert_eq!(
            add_milestone(p(), MilestoneDraft::default()),
            Err(Refusal::NameRequired)
        );
        let beta = MilestoneDraft {
            name: "Beta".into(),
            ..MilestoneDraft::default()
        };
        assert_eq!(
            add_milestone(p(), beta.clone()),
            Ok(Request::CreateMilestone {
                project_id: p(),
                draft: beta
            })
        );
    }

    /// A milestone change of nothing, or to an empty name, is refused; a
    /// milestone is deleted by its id.
    #[test]
    fn milestones_are_changed_and_deleted() {
        let m = || MilestoneId::from("m");
        assert_eq!(
            update_milestone(m(), MilestoneChanges::default()),
            Err(Refusal::NothingToChange)
        );
        let unnamed = MilestoneChanges {
            name: Some(" ".into()),
            ..MilestoneChanges::default()
        };
        assert_eq!(update_milestone(m(), unnamed), Err(Refusal::NameRequired));
        let due = MilestoneChanges {
            target_date: Some(Some("2026-11-01".into())),
            ..MilestoneChanges::default()
        };
        assert!(update_milestone(m(), due).is_ok());
        assert_eq!(
            delete_milestone(m()),
            Request::DeleteMilestone { milestone_id: m() }
        );
    }

    /// A project opens on linear.app by its URL; without one there is
    /// nothing to open.
    #[test]
    fn a_project_opens_by_its_url() {
        assert_eq!(
            open_url(Some("https://linear.app/p".into())),
            Ok(Request::OpenInBrowser("https://linear.app/p".into()))
        );
        assert_eq!(open_url(None), Err(Refusal::NothingToOpen));
    }

    /// A team's projects are asked for once, then shown from what is held.
    #[test]
    fn a_teams_projects_are_fetched_once() {
        let mut store = Store::default();
        assert_eq!(
            open_team_projects(&mut store, TeamId::from("t")).request(),
            Some(crate::core::usecase::Request::Project(
                Request::TeamProjects {
                    team_id: "t".into(),
                    after: None
                }
            ))
        );
        let of = ListOf::TeamProjects(TeamId::from("t"));
        take_page(
            &mut store,
            Projects::Team,
            &of,
            Page::new(vec![project("p")], Default::default(), false),
        );
        assert_eq!(
            open_team_projects(&mut store, TeamId::from("t")),
            Open::Cached
        );
    }

    /// Projects fetched for a team the user has left are dropped.
    #[test]
    fn projects_for_a_team_left_behind_are_dropped() {
        let mut store = Store::default();
        open_team_projects(&mut store, TeamId::from("u"));
        let of = ListOf::TeamProjects(TeamId::from("t"));
        let page = Page::new(vec![project("p")], Default::default(), false);
        assert!(!take_page(&mut store, Projects::Team, &of, page));
    }

    /// A project view asks for its own projects, apart from the team's.
    #[test]
    fn a_project_view_lists_its_own_projects() {
        let mut store = Store::default();
        assert_eq!(
            open_view_projects(&mut store, CustomViewId::from("v")).request(),
            Some(crate::core::usecase::Request::Project(
                Request::ViewProjects {
                    view_id: "v".into(),
                    after: None
                }
            ))
        );
        assert!(store.projects.of.is_none());
        assert_eq!(
            reload(&mut store, Projects::View),
            Some(crate::core::usecase::Request::Project(
                Request::ViewProjects {
                    view_id: "v".into(),
                    after: None
                }
            ))
        );
        assert_eq!(next_page(&mut store, Projects::View), None);
    }

    /// A team's projects reload from their first page, and ask for no next
    /// page before one has landed.
    #[test]
    fn a_teams_projects_reload_from_the_first_page() {
        let mut store = Store::default();
        open_team_projects(&mut store, TeamId::from("t"));
        assert_eq!(
            reload(&mut store, Projects::Team),
            Some(crate::core::usecase::Request::Project(
                Request::TeamProjects {
                    team_id: "t".into(),
                    after: None
                }
            ))
        );
        assert_eq!(next_page(&mut store, Projects::Team), None);
    }
}