Repository navigation
Add a Bubble Up scheduling menu to the TUI #670
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
WhiskeyTuesday
wants to merge
6
commits into
basecamp:main
Choose a base branch
from
WhiskeyTuesday:tui-bubble-up
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f7f1700
Add a Bubble Up scheduling menu to the TUI
WhiskeyTuesday 5c5c9be
Match Bubble Up success mocks to the HTTP 200 contract
WhiskeyTuesday 58d710b
Resolve Bubble Up state for search results and ignore hidden selections
WhiskeyTuesday cf6d381
Preserve the active mail screen behind Bubble Up
WhiskeyTuesday 21db033
Preserve thread scroll through modal layout changes
WhiskeyTuesday 67ad6bb
Cover Bubble Up scroll preservation through the root model
WhiskeyTuesday File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,281 @@ | ||
| package tui | ||
|
|
||
| import ( | ||
| "context" | ||
| "strings" | ||
| "time" | ||
|
|
||
| tea "charm.land/bubbletea/v2" | ||
| hey "github.com/basecamp/hey-sdk/go/pkg/hey" | ||
|
|
||
| "github.com/basecamp/hey-cli/internal/mail" | ||
| ) | ||
|
|
||
| type bubbleAction int | ||
|
|
||
| const ( | ||
| bubbleNow bubbleAction = iota | ||
| bubbleSchedule | ||
| bubbleChooseDate | ||
| bubbleCancel | ||
| ) | ||
|
|
||
| type bubbleChoice struct { | ||
| action bubbleAction | ||
| name string | ||
| slot hey.BubbleUpSlot | ||
| at time.Time | ||
| } | ||
|
|
||
| type bubblePicker struct { | ||
| plainModal | ||
| postingID int64 | ||
| choices []bubbleChoice | ||
| cursor int | ||
| date *dateTimePicker | ||
| busy bool | ||
| loading bool | ||
| unknownState bool | ||
| lookupCancel context.CancelFunc | ||
| problem string | ||
| now func() time.Time | ||
| openedDay string | ||
| } | ||
|
|
||
| type bubbleDoneMsg struct { | ||
| picker *bubblePicker | ||
| message string | ||
| err error | ||
| } | ||
|
|
||
| func newBubblePicker(posting mail.Posting, scheduled bool, now func() time.Time) *bubblePicker { | ||
| at := now().UTC() | ||
| morning := time.Date(at.Year(), at.Month(), at.Day(), 8, 0, 0, 0, time.UTC) | ||
| p := &bubblePicker{postingID: posting.ID, now: now, openedDay: at.Format("2006-01-02")} | ||
| p.choices = append(p.choices, bubbleChoice{name: "Now"}) | ||
| if evening := morning.Add(10 * time.Hour); evening.After(at) { | ||
| p.choices = append(p.choices, bubbleChoice{name: "Later today", action: bubbleSchedule, slot: hey.BubbleUpLaterToday, at: evening}) | ||
| } | ||
| p.choices = append(p.choices, | ||
| bubbleChoice{name: "Tomorrow", action: bubbleSchedule, slot: hey.BubbleUpTomorrow, at: morning.AddDate(0, 0, 1)}, | ||
| bubbleChoice{name: "This weekend", action: bubbleSchedule, slot: hey.BubbleUpThisWeekend, at: morning.AddDate(0, 0, nextBubbleDay(at.Weekday(), time.Saturday))}, | ||
| bubbleChoice{name: "Next week", action: bubbleSchedule, slot: hey.BubbleUpNextWeek, at: morning.AddDate(0, 0, nextBubbleDay(at.Weekday(), time.Monday))}, | ||
| bubbleChoice{name: "Choose a date…", action: bubbleChooseDate}, | ||
| ) | ||
| if scheduled || posting.BubbledUp { | ||
| p.choices = append(p.choices, bubbleChoice{name: "Cancel bubble-up", action: bubbleCancel}) | ||
| } | ||
| return p | ||
| } | ||
|
|
||
| func nextBubbleDay(from, to time.Weekday) int { return (int(to)-int(from)+6)%7 + 1 } | ||
|
|
||
| func (v *mailView) startBubblePicker() tea.Cmd { | ||
| if !v.inThread && !v.searchActive && !v.bundleActive && v.refuseForSelection() { | ||
| return nil | ||
| } | ||
| if v.requests.loading { | ||
| return nil | ||
| } | ||
| posting := v.actionPosting() | ||
| if !v.inThread && v.searchActive { | ||
| posting = v.searchList.selectedPosting() | ||
| } | ||
| if !v.inThread && v.bundleActive { | ||
| posting = v.bundleList.selectedPosting() | ||
| } | ||
| if posting == nil || posting.ID == 0 || posting.IsBundle { | ||
| v.notice = "Open an individual email thread to bubble it up" | ||
| return nil | ||
| } | ||
| scheduled := posting.BubbleScheduled | ||
| for _, box := range v.boxes { | ||
| if box.ID == posting.BoxID && box.Kind == mail.KindBox && box.BoxKind == hey.BoxKindBubbleUp { | ||
| scheduled = true | ||
| } | ||
| } | ||
| picker := newBubblePicker(*posting, scheduled, time.Now) | ||
| v.openModal(picker) | ||
| if v.searchActive && posting.BoxID == 0 && !posting.BubbledUp && !posting.BubbleScheduled { | ||
| return v.loadBubbleState(picker) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (p *bubblePicker) handleKey(v *mailView, msg tea.KeyPressMsg) (tea.Cmd, bool) { | ||
| if p.unknownState { | ||
| if msg.Key().Code == tea.KeyEscape { | ||
| if p.lookupCancel != nil { | ||
| p.lookupCancel() | ||
| } | ||
| return nil, false | ||
| } | ||
| if !p.loading && msg.String() == "ctrl+r" { | ||
| return v.loadBubbleState(p), true | ||
| } | ||
| return nil, true | ||
| } | ||
| if p.busy { | ||
| return nil, true | ||
| } | ||
| if p.date != nil { | ||
| switch msg.Key().Code { | ||
| case tea.KeyEscape: | ||
| p.date = nil | ||
| p.problem = "" | ||
| return nil, true | ||
| case tea.KeyEnter: | ||
| return p.submit(v, bubbleChoice{name: "Choose a date…", action: bubbleChooseDate}), true | ||
| } | ||
| return p.date.handleKey(msg), true | ||
| } | ||
| switch msg.Key().Code { | ||
| case tea.KeyEscape: | ||
| return nil, false | ||
| case tea.KeyEnter: | ||
| choice := p.choices[p.cursor] | ||
| if choice.action == bubbleChooseDate { | ||
| p.date = newDateTimePicker(p.now().UTC().AddDate(0, 0, 1), true) | ||
| return p.date.focusFirst(), true | ||
| } | ||
| return p.submit(v, choice), true | ||
| } | ||
| p.cursor = stepListCursor(p.cursor, len(p.choices), msg) | ||
| p.problem = "" | ||
| return nil, true | ||
| } | ||
|
|
||
| func (p *bubblePicker) submit(v *mailView, choice bubbleChoice) tea.Cmd { | ||
| sdk, ctx, id := v.vc.sdk, v.vc.ctx, p.postingID | ||
| now := p.now().UTC() | ||
| date := "" | ||
| if p.date != nil { | ||
| at, err := time.Parse("2006-01-02", p.date.date()) | ||
| if err != nil || at.Format("2006-01-02") != p.date.date() { | ||
| p.problem = "Enter a date as YYYY-MM-DD" | ||
| return nil | ||
| } | ||
| choice.at = at.Add(8 * time.Hour) | ||
| if at.Format("2006-01-02") == now.Format("2006-01-02") { | ||
| choice.slot = hey.BubbleUpLaterToday | ||
| choice.at = at.Add(18 * time.Hour) | ||
| } else { | ||
| date = at.Format("2006-01-02") | ||
| } | ||
| } else if choice.slot != "" && now.Format("2006-01-02") != p.openedDay { | ||
| p.problem = "The date changed. Close and reopen Bubble up." | ||
| return nil | ||
| } | ||
| if !choice.at.IsZero() && !choice.at.After(now) { | ||
| p.problem = "Choose a future date; today's slot is 18:00 UTC" | ||
| return nil | ||
| } | ||
| p.busy = true | ||
| p.problem = "" | ||
| v.pendingMutations++ | ||
| return func() tea.Msg { | ||
| var err error | ||
| message := "Thread will bubble up" | ||
| switch { | ||
| case choice.action == bubbleCancel: | ||
| err = sdk.Postings().CancelBubbleUp(ctx, id) | ||
| message = "Bubble-up canceled; thread returned to the Imbox, marked seen" | ||
| case choice.action == bubbleNow: | ||
| err = sdk.Postings().BubbleUpNow(ctx, id) | ||
| message = "Thread bubbled up" | ||
| case date != "": | ||
| err = sdk.Postings().ScheduleBubbleUp(ctx, date, id) | ||
| default: | ||
| err = sdk.Postings().ScheduleBubbleUpFor(ctx, choice.slot, id) | ||
| } | ||
| return bubbleDoneMsg{picker: p, message: message, err: err} | ||
| } | ||
| } | ||
|
|
||
| func (v *mailView) bubbleDone(msg bubbleDoneMsg) tea.Cmd { | ||
| v.finishMutation() | ||
| p := msg.picker | ||
| p.busy = false | ||
| if msg.err != nil { | ||
| p.problem = errorNotice("Could not change bubble-up", msg.err) | ||
| return nil | ||
| } | ||
| if v.modal == p { | ||
| v.modal = nil | ||
| } | ||
| if v.inThread && v.threadPosting.ID == p.postingID { | ||
| v.ExitThread() | ||
| } | ||
| v.notice = msg.message | ||
| // Re-read the source instead of guessing its membership and ordering after HEY | ||
| // schedules, pops, or immediately resurfaces a thread. | ||
| if v.seenActive { | ||
| requestID, ctx := v.requests.begin(v.vc.ctx, mailRequestSeen) | ||
| v.seenNextPage = "" | ||
| v.seenLoadingMore = false | ||
| v.seenMoreID++ | ||
| return v.fetchSeenPostings(ctx, requestID) | ||
| } | ||
| if v.searchActive { | ||
| return v.requestSearch(v.searchQuery) | ||
| } | ||
| if v.bundleActive { | ||
| if v.bundleContactID != 0 { | ||
| return v.requestContactThreads(v.bundleContactID) | ||
| } | ||
| return v.requestBundle(v.bundlePostingID) | ||
| } | ||
| return v.reloadPostings() | ||
| } | ||
|
|
||
| func (p *bubblePicker) handleMsg(msg tea.Msg) (tea.Cmd, bool) { | ||
| if p.date == nil || p.busy { | ||
| return nil, false | ||
| } | ||
| var cmd tea.Cmd | ||
| p.date.dateInput, cmd = p.date.dateInput.Update(msg) | ||
| return cmd, true | ||
| } | ||
|
|
||
| func (p *bubblePicker) draw(v *mailView) string { | ||
| var body string | ||
| if p.loading { | ||
| body = "Loading Bubble Up…" | ||
| } else if p.unknownState { | ||
| body = "Press Ctrl+R to retry, or Esc to cancel." | ||
| } else if p.date != nil { | ||
| body = "Date (YYYY-MM-DD, on HEY's UTC calendar)\n" + p.date.view() + "\n\n08:00 UTC; today's date uses 18:00 UTC." | ||
| } else { | ||
| names := make([]string, len(p.choices)) | ||
| for i, choice := range p.choices { | ||
| names[i] = choice.name | ||
| if !choice.at.IsZero() { | ||
| names[i] += " · " + choice.at.Local().Format("Mon Jan 2, 15:04 MST") | ||
| } | ||
| } | ||
| body = strings.Join(modalListRows(names, p.cursor, modalContentWidth(v.vc.width), max(modalContentRows(v.vc.height)-3, 1)), "\n") | ||
| } | ||
| if p.busy { | ||
| body += "\n\nSaving…" | ||
| } | ||
| if p.problem != "" { | ||
| body += "\n\n" + p.problem | ||
| } | ||
| return overlayModal(v.contentView(), modalFrame("Bubble up", body, v.vc.width), v.vc.width, v.vc.height) | ||
| } | ||
|
|
||
| func (p *bubblePicker) helpBindings() []helpBinding { | ||
| if p.loading { | ||
| return []helpBinding{{"esc", "cancel"}} | ||
| } | ||
| if p.unknownState { | ||
| return []helpBinding{{"ctrl+r", "retry"}, {"esc", "cancel"}} | ||
| } | ||
| if p.busy { | ||
| return []helpBinding{{"", "saving bubble-up…"}} | ||
| } | ||
| if p.date != nil { | ||
| return []helpBinding{{"↑/↓", "change day"}, {"enter", "schedule"}, {"esc", "back"}} | ||
| } | ||
| return []helpBinding{{"↑/↓", "choose"}, {"enter", "apply"}, {"esc", "cancel"}} | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 21db033, with the root-model regression in 67ad6bb. Opening the picker changed the help/footer height, and the resulting resize revealed the selected link or changed the offset at the bottom. The fix preserves the reading position across those modal layout changes and restores it on dismissal.
The regression sends Tab, scrolling,
w, and Escape throughmodel.Update, covering a wrapping selected-link footer and an unselected thread, both partway down and at the bottom. Actual terminal resizing still lays out the thread at its new dimensions.TUI/mail tests and lint pass. Offline PTY checks on the combined build also verified that Bubble Up restores the rendered screen after dismissal, including at the bottom.