╭ Issues.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const builtin = @import("builtin"); 3 const evt = @import("../../event.zig"); 4 const ui = @import("../../ui.zig"); 5 const xit = @import("xit"); 6 const rp = xit.repo; 7 const hash = xit.hash; 8 const xitui = xit.xitui; 9 const wgt = xitui.widget; 10 const layout = xitui.layout; 11 const Key = xitui.input.Key; 12 const Grid = xitui.grid.Grid; 13 const Focus = xitui.focus.Focus; 14 const inp = @import("../input.zig"); 15 16 const wasm = builtin.target.cpu.arch == .wasm32; 17 18 // how many issues one window shows before a "next" link appears. 19 pub const page_size = 20; 20 21 // how many tags the tags view shows at most. 22 pub const max_tags = 1000; 23 24 // one issue from the repo's consumed event database, with its hex event id 25 // (the id lives in the event envelope, not the payload). 26 pub const IssueWithId = struct { 27 id: []const u8, 28 issue: evt.Issue, 29 }; 30 31 // one status's windowed listing. 32 pub const Window = struct { 33 issues: []const IssueWithId, 34 // the id of the previous window's first issue, or null when this window 35 // is already the first. 36 prev_id: ?[]const u8, 37 // the id of the next window's first issue, or null when this is the last 38 // window. 39 next_id: ?[]const u8, 40 // how many issues the listing holds across all windows. 41 count: usize, 42 43 pub const empty: Window = .{ .issues = &.{}, .prev_id = null, .next_id = null, .count = 0 }; 44 }; 45 46 // "owner/name", so the view can build /repo/owner/name/issues/... links. 47 identity: []const u8, 48 // the url-encoded tag the lists are filtered to ("" = unfiltered). 49 tag: []const u8, 50 // the hex event id of the issue its status's window is rooted at ("" = the 51 // first window), mirrored into the url. 52 selected_id: []const u8, 53 open: Window, 54 closed: Window, 55 // the view the page shows initially (a selected issue's status overrides the route's) 56 view: ui.RoutablePage.IssuesView, 57 // every tag in the repo, in sorted order, for the tags view. 58 tags: []const []const u8, 59 // the on-disk repo this page was read from, for the terminal submit path 60 // (the web posts the new-issue form to the issue route instead). 61 repo_source: ?ui.RepoSource = null, 62 63 const Self = @This(); 64 65 // `status`'s windowed listing. 66 pub fn window(self: *const Self, status: evt.Issue.Status) *const Window { 67 return switch (status) { 68 .open => &self.open, 69 .closed => &self.closed, 70 }; 71 } 72 73 // an empty listing, for the wasm / no-repo paths. 74 pub fn emptyResult(aa: std.mem.Allocator, identity: []const u8, tag: []const u8, selected_id: []const u8, view: ui.RoutablePage.IssuesView) !Self { 75 return .{ 76 .identity = try aa.dupe(u8, identity), 77 .tag = try aa.dupe(u8, tag), 78 .selected_id = try aa.dupe(u8, selected_id), 79 .open = .empty, 80 .closed = .empty, 81 .view = view, 82 .tags = &.{}, 83 }; 84 } 85 86 // read one window per status of an opened repo's issues (filtered to `tag` 87 // when set), ordered by creation time (newest first). the window of the issue 88 // `selected_id` names starts at it ("" = the beginning). a local session 89 // reads the event db next to the repo (synced from the events branch on each 90 // page build); a server repo reads its own db. 91 pub fn init( 92 comptime repo_kind: rp.RepoKind, 93 comptime repo_opts: rp.RepoOpts(repo_kind), 94 arena: *std.heap.ArenaAllocator, 95 repo: *rp.Repo(repo_kind, repo_opts), 96 io: std.Io, 97 is_local: bool, 98 identity: []const u8, 99 tag: []const u8, 100 selected_id: []const u8, 101 view: ui.RoutablePage.IssuesView, 102 ) !Self { 103 const empty = try emptyResult(arena.allocator(), identity, tag, selected_id, view); 104 105 const aa = arena.allocator(); 106 const DB = evt.EventDB(repo_opts.hash); 107 const rooted = empty.selected_id.len != 0; 108 const tagged = empty.tag.len != 0; 109 // an explicitly named issue or tag that doesn't exist is a bad url 110 // (NotFound -> 404); the bare route falls through to an empty listing. 111 const strict = rooted or tagged; 112 113 // a repo with no consumed events has no moment yet. 114 const gpa = arena.child_allocator; 115 var event_db_maybe = if (is_local) try evt.LocalEventDB(repo_opts.hash).open(io, gpa, repo.core.repo_dir) else null; 116 defer if (event_db_maybe) |*event_db| event_db.deinit(io, gpa); 117 const haxy_moment = (if (event_db_maybe) |*event_db| 118 evt.currentMomentFromDb(repo_opts.hash, event_db.db) 119 else if (repo_kind == .git) 120 return empty 121 else if (is_local) 122 return empty 123 else 124 evt.currentMoment(repo_opts, repo)) catch { 125 if (strict) return error.NotFound; 126 return empty; 127 }; 128 129 // the sorted sets to window, per status: the tag's sets when filtered, 130 // else the top-level per-status sets. a missing set is an empty window. 131 var open_set: ?DB.SortedSet(.read_only) = null; 132 var closed_set: ?DB.SortedSet(.read_only) = null; 133 if (tagged) { 134 const tag_to_issues_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "tag+status->issue-id-set")) orelse return error.NotFound; 135 const tag_to_issues = try DB.SortedMap(.read_only).init(tag_to_issues_cursor); 136 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, empty.tag)); 137 open_set = try tagStatusSet(DB, tag_to_issues, decoded, .open); 138 closed_set = try tagStatusSet(DB, tag_to_issues, decoded, .closed); 139 // a tag no issue carries is a bad url 140 if (open_set == null and closed_set == null) return error.NotFound; 141 } else if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "status->issue-id-set"))) |status_to_issues_cursor| { 142 const status_to_issues = try DB.SortedMap(.read_only).init(status_to_issues_cursor); 143 open_set = try statusSet(DB, status_to_issues, .open); 144 closed_set = try statusSet(DB, status_to_issues, .closed); 145 } else if (rooted) return error.NotFound; 146 147 const event_id_to_issue_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "event-id->issue")) orelse { 148 if (strict) return error.NotFound; 149 return empty; 150 }; 151 const event_id_to_issue = try DB.HashMap(.read_only).init(event_id_to_issue_cursor); 152 153 // a named issue roots its own status's window at itself; the other window 154 // starts at the beginning. 155 var resolved_view = empty.view; 156 var open_root: ?[]const u8 = null; 157 var closed_root: ?[]const u8 = null; 158 if (rooted) { 159 if (empty.selected_id.len != evt.event_id_size * 2) return error.NotFound; 160 var id_bytes: [evt.event_id_size]u8 = undefined; 161 _ = std.fmt.hexToBytes(&id_bytes, empty.selected_id) catch return error.NotFound; 162 const issue_cursor = try event_id_to_issue.getCursor(hash.hashInt(repo_opts.hash, &id_bytes)) orelse return error.NotFound; 163 const issue_map = try DB.HashMap(.read_only).init(issue_cursor); 164 const issue_event = try evt.read(evt.Issue, DB, repo_opts.hash, arena, issue_map); 165 const order_key = try aa.dupe(u8, &evt.orderKeyDesc(issue_event.created_ts, &id_bytes)); 166 167 // the named issue must be in its windowed set (a tag url can name an 168 // issue that doesn't carry the tag). 169 const set = (switch (issue_event.status) { 170 .open => open_set, 171 .closed => closed_set, 172 }) orelse return error.NotFound; 173 if (!try set.contains(order_key)) return error.NotFound; 174 175 switch (issue_event.status) { 176 .open => { 177 open_root = order_key; 178 resolved_view = .open; 179 }, 180 .closed => { 181 closed_root = order_key; 182 resolved_view = .closed; 183 }, 184 } 185 } 186 187 const open_window = try loadWindow(repo_opts.hash, arena, event_id_to_issue, open_set, open_root); 188 const closed_window = try loadWindow(repo_opts.hash, arena, event_id_to_issue, closed_set, closed_root); 189 190 // every tag in the repo, in the tag map's sorted order. the keys are 191 // "tag,status", so a tag's entries are adjacent and dedup by prefix. 192 var tags: std.ArrayList([]const u8) = .empty; 193 if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "tag+status->issue-id-set"))) |tag_to_issues_cursor| { 194 const tag_to_issues = try DB.SortedMap(.read_only).init(tag_to_issues_cursor); 195 var tag_iter = try tag_to_issues.iterator(); 196 while (try tag_iter.next()) |kv_pair_cursor| { 197 if (tags.items.len == max_tags) break; 198 var kv_cursor = kv_pair_cursor; 199 const kv_pair = try kv_cursor.readKeyValuePair(); 200 const key = try kv_pair.key_cursor.readBytesAlloc(aa, null); 201 const space = std.mem.indexOfScalar(u8, key, ' ') orelse continue; 202 const key_tag = key[0..space]; 203 if (tags.getLastOrNull()) |last| { 204 if (std.mem.eql(u8, last, key_tag)) continue; 205 } 206 try tags.append(aa, key_tag); 207 } 208 } 209 210 return .{ 211 .identity = empty.identity, 212 .tag = empty.tag, 213 .selected_id = empty.selected_id, 214 .open = open_window, 215 .closed = closed_window, 216 .view = resolved_view, 217 .tags = tags.items, 218 }; 219 } 220 221 // `status`'s sorted set within `statuses` (null when the status has no issues) 222 fn statusSet( 223 comptime DB: type, 224 statuses: DB.SortedMap(.read_only), 225 status: evt.Issue.Status, 226 ) !?DB.SortedSet(.read_only) { 227 const cursor = (try statuses.getCursor(@tagName(status))) orelse return null; 228 return try DB.SortedSet(.read_only).init(cursor); 229 } 230 231 // the set at `tag_to_issues`'s "tag,status" key (null when no issue carries 232 // `tag` with `status`, or the tag is too long to exist) 233 fn tagStatusSet( 234 comptime DB: type, 235 tag_to_issues: DB.SortedMap(.read_only), 236 tag: []const u8, 237 status: evt.Issue.Status, 238 ) !?DB.SortedSet(.read_only) { 239 var key_buffer: evt.Issue.TagStatusKey = undefined; 240 const key = evt.Issue.tagStatusKey(&key_buffer, tag, status) catch return null; 241 const cursor = (try tag_to_issues.getCursor(key)) orelse return null; 242 return try DB.SortedSet(.read_only).init(cursor); 243 } 244 245 // read one window of `set_maybe` (null = an empty listing) starting at 246 // `root_key` (null = the beginning) 247 fn loadWindow( 248 comptime hash_kind: hash.HashKind, 249 arena: *std.heap.ArenaAllocator, 250 event_id_to_issue: evt.EventDB(hash_kind).HashMap(.read_only), 251 set_maybe: ?evt.EventDB(hash_kind).SortedSet(.read_only), 252 root_key: ?[]const u8, 253 ) !Window { 254 const set = set_maybe orelse return .empty; 255 const DB = evt.EventDB(hash_kind); 256 const aa = arena.allocator(); 257 258 // seek once to the window start: the root key, or the set's first entry. 259 var prev_id: ?[]const u8 = null; 260 var iter = if (root_key) |key| blk: { 261 // the previous window starts page_size ranks back ("" = the first 262 // window, linked as the bare list route). 263 const rank = try set.rank(key); 264 if (rank > 0 and rank <= page_size) { 265 prev_id = ""; 266 } else if (rank > page_size) { 267 const kv = try set.getIndexKeyValuePair(@intCast(rank - page_size)) orelse return error.NotFound; 268 var prev_key: [@sizeOf(u64) + evt.event_id_size]u8 = undefined; 269 _ = try kv.key_cursor.readBytes(&prev_key); 270 const prev_hex = std.fmt.bytesToHex(prev_key[@sizeOf(u64)..].*, .lower); 271 prev_id = try aa.dupe(u8, &prev_hex); 272 } 273 break :blk try set.iteratorFrom(key); 274 } else try set.iteratorFromIndex(0); 275 276 // collect this window's issues, plus a peek at the one after it (its id is 277 // the next window's start). the trailing bytes of each set key are the 278 // issue event id. 279 var issues: std.ArrayList(IssueWithId) = .empty; 280 var next_id: ?[]const u8 = null; 281 while (try iter.next()) |id_cursor_val| { 282 var id_cursor = id_cursor_val; 283 const id_kv = try id_cursor.readKeyValuePair(); 284 var order_key: [@sizeOf(u64) + evt.event_id_size]u8 = undefined; 285 _ = try id_kv.key_cursor.readBytes(&order_key); 286 const id_hex = std.fmt.bytesToHex(order_key[@sizeOf(u64)..].*, .lower); 287 if (issues.items.len == page_size) { 288 next_id = try aa.dupe(u8, &id_hex); 289 break; 290 } 291 const issue_cursor = try event_id_to_issue.getCursor(hash.hashInt(hash_kind, order_key[@sizeOf(u64)..])) orelse continue; 292 const issue_map = try DB.HashMap(.read_only).init(issue_cursor); 293 try issues.append(aa, .{ 294 .id = try aa.dupe(u8, &id_hex), 295 .issue = try evt.read(evt.Issue, DB, hash_kind, arena, issue_map), 296 }); 297 } 298 299 return .{ 300 .issues = issues.items, 301 .prev_id = prev_id, 302 .next_id = next_id, 303 .count = @intCast(try set.count()), 304 }; 305 } 306 307 pub const View = struct { 308 // a vertical box: the header tabs on top, then a stack holding a 309 // master-detail split (issue list + description pane) per status list, 310 // plus the tags view. 311 box: wgt.Box(ui.Widget), // vert: [header_index] = tabs, [stack_index] = stack 312 data: *const Self, 313 session: *ui.Session, 314 // per-split state, indexed like the stack's split children: the issue the 315 // pane shows, and its description text box's focus id. 316 detailed_index: [split_count]?usize, 317 description_id: [split_count]?usize, 318 319 const header_index: usize = 0; 320 const stack_index: usize = 1; 321 // indices within the stack, 1:1 with the header tabs. 322 const open_view_index: usize = 0; 323 const closed_view_index: usize = 1; 324 const tags_view_index: usize = 2; 325 const new_view_index: usize = 3; 326 const split_count: usize = 2; 327 // indices within a split (the horizontal box inside the stack). 328 const list_index: usize = 0; 329 const detail_index: usize = 1; 330 const list_max_width: usize = 40; 331 const detail_min_width: usize = 40; 332 // indices within the new-issue form. 333 const title_field_index: usize = 0; 334 const tags_field_index: usize = 1; 335 const description_field_index: usize = 2; 336 const submit_field_index: usize = 3; 337 338 fn viewIndex(view: ui.RoutablePage.IssuesView) usize { 339 return switch (view) { 340 .open => open_view_index, 341 .closed => closed_view_index, 342 .tags => tags_view_index, 343 .new => new_view_index, 344 }; 345 } 346 347 fn splitStatus(index: usize) evt.Issue.Status { 348 return if (index == open_view_index) .open else .closed; 349 } 350 351 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 352 var outer = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 353 errdefer outer.deinit(allocator); 354 355 // the tabs at the top. 356 { 357 var hdr = try Header.init(allocator, session, data); 358 errdefer hdr.deinit(allocator); 359 try outer.children.put(allocator, hdr.getFocus().id, .{ .widget = .{ .repo_issues_header = hdr }, .rect = null, .min_size = null }); 360 } 361 362 // the stack enters `outer` before its children enter it, so an error 363 // frees each child exactly once. 364 { 365 var stack = try wgt.Stack(ui.Widget).init(allocator); 366 errdefer stack.deinit(allocator); 367 try outer.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 368 } 369 const stack = &outer.children.values()[stack_index].widget.stack; 370 371 // a master-detail split per status list. 372 for ([_]evt.Issue.Status{ .open, .closed }) |status| { 373 var split = try initSplit(allocator, session, data, status); 374 errdefer split.deinit(allocator); 375 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 376 } 377 378 // the tags view 379 { 380 var tf = try ui.TagFlow.init(allocator); 381 errdefer tf.deinit(allocator); 382 var items: std.ArrayList(ui.TagFlow.Item) = .empty; 383 defer items.deinit(allocator); 384 // when filtered, the first item clears the filter 385 if (data.tag.len != 0) 386 try items.append(allocator, .{ .text = "╳", .link = try issuesLink(session.page_arena, data.identity, .open, "", "") }); 387 for (data.tags) |tag| 388 try items.append(allocator, .{ .text = tag, .link = try tagLink(session.page_arena, data.identity, .open, tag) }); 389 try tf.setItems(allocator, items.items); 390 try stack.children.put(allocator, tf.getFocus().id, .{ .tag_flow = tf }); 391 } 392 393 // the new-issue form 394 { 395 var form = try initNewForm(allocator, session, data); 396 errdefer form.deinit(allocator); 397 try stack.children.put(allocator, form.getFocus().id, .{ .box = form }); 398 } 399 400 // the stack starts on the page's view. 401 stack.getFocus().child_id = stack.children.keys()[viewIndex(data.view)]; 402 403 // focus entering the view lands on the tabs first. 404 outer.getFocus().child_id = outer.children.keys()[header_index]; 405 406 return .{ 407 .box = outer, 408 .data = data, 409 .session = session, 410 .detailed_index = .{ null, null }, 411 .description_id = .{ null, null }, 412 }; 413 } 414 415 // the master-detail split showing `status`'s window. 416 fn initSplit(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self, status: evt.Issue.Status) !wgt.Box(ui.Widget) { 417 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 418 errdefer box.deinit(allocator); 419 420 const win = data.window(status); 421 422 // the issue list (one focusable row per title), plus a "next" link that 423 // reloads the page rooted at the following issue. 424 { 425 var list_scroll = blk: { 426 var list_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 427 errdefer list_box.deinit(allocator); 428 if (win.prev_id) |prev| 429 try addRow(allocator, &list_box, "← previous", try issuesLink(session.page_arena, data.identity, status, data.tag, prev)); 430 for (win.issues) |entry| 431 try addRow(allocator, &list_box, entry.issue.title, try issueRowLink(session.page_arena, data.identity, entry.id)); 432 if (win.next_id) |next| 433 try addRow(allocator, &list_box, "next →", try issuesLink(session.page_arena, data.identity, status, data.tag, next)); 434 // select the window's first issue (past a leading "previous" 435 // row) so its description shows on load. 436 if (win.issues.len > 0) 437 list_box.getFocus().child_id = list_box.children.keys()[if (win.prev_id != null) 1 else 0] 438 else if (list_box.children.count() > 0) 439 list_box.getFocus().child_id = list_box.children.keys()[0]; 440 break :blk try wgt.Scroll(ui.Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert, .web_native = !session.is_terminal }); 441 }; 442 errdefer list_scroll.deinit(allocator); 443 try box.children.put(allocator, list_scroll.getFocus().id, .{ .widget = .{ .scroll = list_scroll }, .rect = null, .min_size = .{ .width = list_max_width, .height = null }, .max_size = .{ .width = list_max_width, .height = null } }); 444 } 445 446 // the detail pane — a frame around a scroll of the description 447 { 448 var detail_outer = blk: { 449 var detail_scroll = blk2: { 450 var detail_inner = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 451 errdefer detail_inner.deinit(allocator); 452 // fill the pane (content top-left, scroll bar pinned to the 453 // edge) rather than shrinking to the description. 454 break :blk2 try wgt.Scroll(ui.Widget).init(allocator, .{ .box = detail_inner }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 455 }; 456 errdefer detail_scroll.deinit(allocator); 457 var frame = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = .hidden, .direction = .vert }); 458 errdefer frame.deinit(allocator); 459 // the frame's selected child is its scroll, so the focus chain 460 // reaches the description (populateDetail points the scroll's 461 // inner box at it), letting focus recovery descend into the pane. 462 frame.getFocus().child_id = detail_scroll.getFocus().id; 463 try frame.children.put(allocator, detail_scroll.getFocus().id, .{ .widget = .{ .scroll = detail_scroll }, .rect = null, .min_size = null }); 464 break :blk frame; 465 }; 466 errdefer detail_outer.deinit(allocator); 467 try box.children.put(allocator, detail_outer.getFocus().id, .{ .widget = .{ .box = detail_outer }, .rect = null, .min_size = .{ .width = detail_min_width, .height = null } }); 468 } 469 470 box.getFocus().child_id = box.children.keys()[list_index]; 471 return box; 472 } 473 474 // the new-issue form: title/tags/description inputs and a submit button. 475 // its form: subtree makes the web overlay wrap them in a <form> POSTing to 476 // the issue route. 477 fn initNewForm(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !wgt.Box(ui.Widget) { 478 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 479 errdefer box.deinit(allocator); 480 box.getFocus().kind = .{ .custom = if (data.identity.len == 0) 481 "form:/issue" 482 else 483 try std.fmt.allocPrint(session.page_arena.allocator(), "form:/repo/{s}/issue", .{data.identity}) }; 484 485 { 486 var title = try wgt.TextInput(ui.Widget).init(allocator, .{ .label = " title ", .name = "title", .visible_width = null, .rounded_corners = true, .render_content = !wasm }); 487 errdefer title.deinit(allocator); 488 title.getFocus().focusable = true; 489 try box.children.put(allocator, title.getFocus().id, .{ .widget = .{ .text_input = title }, .rect = null, .min_size = null }); 490 } 491 492 { 493 var tags = try wgt.TextInput(ui.Widget).init(allocator, .{ .label = " tags (separate with spaces) ", .name = "tags", .visible_width = null, .rounded_corners = true, .render_content = !wasm }); 494 errdefer tags.deinit(allocator); 495 tags.getFocus().focusable = true; 496 try box.children.put(allocator, tags.getFocus().id, .{ .widget = .{ .text_input = tags }, .rect = null, .min_size = null }); 497 } 498 499 { 500 var description = try wgt.TextInput(ui.Widget).init(allocator, .{ .label = " description ", .name = "description", .visible_width = null, .rounded_corners = true, .render_content = !wasm, .multiline = true, .visible_height = 5 }); 501 errdefer description.deinit(allocator); 502 description.getFocus().focusable = true; 503 try box.children.put(allocator, description.getFocus().id, .{ .widget = .{ .text_input = description }, .rect = null, .min_size = null }); 504 } 505 506 { 507 var button = try wgt.TextBox(ui.Widget).init(allocator, "submit", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 508 errdefer button.deinit(allocator); 509 button.getFocus().focusable = true; 510 // the renderer distinguishes plain clickables from buttons that 511 // should POST to a server route by this kind. 512 button.getFocus().kind = .{ .custom = "submit" }; 513 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 514 } 515 516 // absorbs the leftover min-height the box hands its last child, so 517 // the button keeps its natural height 518 { 519 var spacer = try ui.Spacer.init(allocator); 520 errdefer spacer.deinit(allocator); 521 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 522 } 523 524 box.getFocus().child_id = box.children.keys()[title_field_index]; 525 return box; 526 } 527 528 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, link: []const u8) !void { 529 var row = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word }); 530 errdefer row.deinit(allocator); 531 row.getFocus().focusable = true; 532 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 533 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 534 } 535 536 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 537 self.box.deinit(allocator); 538 } 539 540 fn header(self: *View) *Header { 541 return &self.box.children.values()[header_index].widget.repo_issues_header; 542 } 543 544 fn viewStack(self: *View) *wgt.Stack(ui.Widget) { 545 return &self.box.children.values()[stack_index].widget.stack; 546 } 547 548 // `index`'s master-detail split inside the stack. 549 fn resultsBox(self: *View, index: usize) *wgt.Box(ui.Widget) { 550 return &self.viewStack().children.values()[index].box; 551 } 552 553 // the tags view's flow inside the stack. 554 fn tagsView(self: *View) *ui.TagFlow { 555 return &self.viewStack().children.values()[tags_view_index].tag_flow; 556 } 557 558 // the new-issue form inside the stack. 559 fn newForm(self: *View) *wgt.Box(ui.Widget) { 560 return &self.viewStack().children.values()[new_view_index].box; 561 } 562 563 fn listScroll(self: *View, index: usize) *wgt.Scroll(ui.Widget) { 564 return &self.resultsBox(index).children.values()[list_index].widget.scroll; 565 } 566 567 fn listBox(self: *View, index: usize) *wgt.Box(ui.Widget) { 568 return &self.listScroll(index).child.box; 569 } 570 571 fn detailOuter(self: *View, index: usize) *wgt.Box(ui.Widget) { 572 return &self.resultsBox(index).children.values()[detail_index].widget.box; 573 } 574 575 fn detailScroll(self: *View, index: usize) *wgt.Scroll(ui.Widget) { 576 return &self.detailOuter(index).children.values()[0].widget.scroll; 577 } 578 579 fn detailInner(self: *View, index: usize) *wgt.Box(ui.Widget) { 580 return &self.detailScroll(index).child.box; 581 } 582 583 fn window(self: *View, index: usize) *const Window { 584 return self.data.window(splitStatus(index)); 585 } 586 587 fn stackSelectedIndex(self: *View) ?usize { 588 const stack = self.viewStack(); 589 const cid = stack.getFocus().child_id orelse return null; 590 return stack.children.getIndex(cid); 591 } 592 593 // the stack's selected master-detail split (null when the tags view or the 594 // new-issue form shows). 595 fn selectedSplitIndex(self: *View) ?usize { 596 const idx = self.stackSelectedIndex() orelse return null; 597 return if (idx < split_count) idx else null; 598 } 599 600 fn detailActive(self: *View, index: usize) bool { 601 const rb = self.resultsBox(index); 602 const cid = rb.getFocus().child_id orelse return false; 603 return rb.children.getIndex(cid) == detail_index; 604 } 605 606 fn headerActive(self: *View) bool { 607 const cid = self.box.getFocus().child_id orelse return false; 608 return self.box.children.getIndex(cid) == header_index; 609 } 610 611 fn tagsViewActive(self: *View) bool { 612 if (self.headerActive()) return false; 613 return self.stackSelectedIndex() == tags_view_index; 614 } 615 616 fn newViewActive(self: *View) bool { 617 if (self.headerActive()) return false; 618 return self.stackSelectedIndex() == new_view_index; 619 } 620 621 // the selected issue's index, or null when a window-navigation row is 622 // selected (a leading "previous" row shifts the issue rows down by one). 623 fn selectedIssueIndex(self: *View, index: usize) ?usize { 624 const lb = self.listBox(index); 625 const cid = lb.getFocus().child_id orelse return null; 626 const idx = lb.children.getIndex(cid) orelse return null; 627 const win = self.window(index); 628 const lead: usize = if (win.prev_id != null) 1 else 0; 629 if (idx < lead or idx - lead >= win.issues.len) return null; 630 return idx - lead; 631 } 632 633 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 634 self.clearGrid(); 635 636 // each header tab maps 1:1 to a stack child by position. mirror the 637 // selection into the url: the view the page's window is rooted at 638 // keeps its rooted url, the others get their list route (keeping the 639 // tag filter). 640 if (self.header().getSelectedIndex()) |index| { 641 const stack = self.viewStack(); 642 stack.getFocus().child_id = stack.children.keys()[index]; 643 self.session.data.current_page = (if (index == viewIndex(self.data.view) and self.data.selected_id.len != 0) 644 ui.RoutablePage.repoIssuesRoute(self.data.identity, .open, self.data.tag, self.data.selected_id) 645 else if (index == tags_view_index) 646 ui.RoutablePage.repoIssuesTagsRoute(self.data.identity, self.data.tag) 647 else if (index == new_view_index) 648 ui.RoutablePage.repoIssuesNewRoute(self.data.identity) 649 else 650 ui.RoutablePage.repoIssuesRoute(self.data.identity, splitStatus(index), self.data.tag, "")) orelse self.session.data.current_page; 651 } 652 653 if (self.selectedSplitIndex()) |i| { 654 // swap the detail pane to the selected issue when it changes. 655 try self.refreshDetail(allocator, i); 656 657 // mirror the focused issue into the url, but only while focus is 658 // inside the split. an issue's url is the same whether or not the 659 // list is filtered, so the mirror drops the tag. 660 if (root_focus.grandchild_id) |g| { 661 if (self.resultsBox(i).getFocus().children.contains(g)) { 662 if (self.selectedIssueIndex(i)) |sel| { 663 if (ui.RoutablePage.repoIssuesRoute(self.data.identity, splitStatus(i), "", self.window(i).issues[sel].id)) |route| 664 self.session.data.current_page = route; 665 } 666 } 667 } 668 669 // the selected list row shows a border (the focused TextBox 670 // upgrades it to a double border itself); the rest stay borderless. 671 const lb = self.listBox(i); 672 for (lb.children.keys(), lb.children.values()) |id, *child| { 673 switch (child.widget) { 674 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 675 else => {}, 676 } 677 } 678 679 // the pane's selected child shows the selection border: the 680 // description directly, the tags via the flow's selected item. 681 const inner = self.detailInner(i); 682 for (inner.children.keys(), inner.children.values()) |id, *child| { 683 switch (child.widget) { 684 .text_box => |*tb| tb.options.border_style = if (inner.getFocus().child_id == id) .single else .hidden, 685 else => {}, 686 } 687 } 688 if (self.tagFlow(i)) |tf| tf.selected = self.tagsFocused(i); 689 690 // cap the list at list_max_width only while the detail pane fits 691 // beside it. the box drops the detail when the width can't hold 692 // both minimums, so when it's that narrow we lift the cap and let 693 // the list fill the whole width. 694 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 695 self.resultsBox(i).children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 696 697 // stretch the detail pane across the rest of the width so it fills 698 // the area rather than shrinking to its content; its scroll fills 699 // the pane. 700 if (constraint.max_size.width) |w| { 701 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 702 } else { 703 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 704 } 705 } 706 707 self.tagsView().selected = self.tagsViewActive(); 708 709 // refresh the form inputs' entries in the session's focus-id -> input 710 // map with this frame's addresses, so the web/wasm form handling can 711 // find them by focus id 712 const form = self.newForm(); 713 const inputs_arena = self.session.arena.allocator(); 714 for (form.children.values()) |*child| switch (child.widget) { 715 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 716 else => {}, 717 }; 718 719 try self.box.build(allocator, constraint, root_focus); 720 } 721 722 fn refreshDetail(self: *View, allocator: std.mem.Allocator, index: usize) !void { 723 const sel = self.selectedIssueIndex(index) orelse return; 724 if (self.detailed_index[index]) |d| if (d == sel) return; 725 try self.populateDetail(allocator, index, sel); 726 self.detailed_index[index] = sel; 727 } 728 729 fn populateDetail(self: *View, allocator: std.mem.Allocator, index: usize, sel: usize) !void { 730 const entry = self.window(index).issues[sel]; 731 const inner = self.detailInner(index); 732 733 for (inner.children.values()) |*child| child.widget.deinit(allocator); 734 inner.children.clearAndFree(allocator); 735 inner.getFocus().child_id = null; 736 737 // the issue's tags, each linking to this status's list filtered to 738 // that tag. 739 { 740 var items: std.ArrayList(ui.TagFlow.Item) = .empty; 741 defer items.deinit(allocator); 742 var tag_iter = evt.Issue.tagIterator(entry.issue.tags); 743 while (tag_iter.next()) |tag| { 744 if (tag.len == 0) continue; 745 try items.append(allocator, .{ .text = tag, .link = try tagLink(self.session.page_arena, self.data.identity, splitStatus(index), tag) }); 746 } 747 if (items.items.len > 0) { 748 var tf = try ui.TagFlow.init(allocator); 749 errdefer tf.deinit(allocator); 750 try tf.setItems(allocator, items.items); 751 try inner.children.put(allocator, tf.getFocus().id, .{ .widget = .{ .tag_flow = tf }, .rect = null, .min_size = null }); 752 } 753 } 754 755 // the description as a focusable word-wrapped text box. its hidden 756 // border reserves the space the border occupies when focused, so 757 // focusing doesn't shift layout. 758 self.description_id[index] = blk: { 759 const description = if (entry.issue.description.len == 0) "(no description)" else entry.issue.description; 760 var tb = try wgt.TextBox(ui.Widget).init(allocator, description, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word }); 761 errdefer tb.deinit(allocator); 762 tb.getFocus().focusable = true; 763 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 764 break :blk tb.getFocus().id; 765 }; 766 767 // select the description by default 768 inner.getFocus().child_id = self.description_id[index]; 769 770 // reset the scroll to the top for the newly-shown issue: directly on the 771 // terminal (the wasm offset), and via a version bump on the web (so the 772 // renderer's scroll id changes and JS drops the preserved position). 773 const sc = self.detailScroll(index); 774 sc.x = 0; 775 sc.y = 0; 776 sc.getFocus().version +%= 1; 777 } 778 779 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 780 if (self.headerActive()) { 781 // down from the tabs re-enters the stack if the selected view has 782 // something to focus; other keys move the tabs. 783 if (inp.vertDirection(key) == .down) { 784 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 785 .tag_flow => |*tf| tf.text_boxes.items.len > 0, 786 else => true, 787 } else false; 788 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 789 } else { 790 try self.header().input(allocator, key, root_focus); 791 } 792 return; 793 } 794 if (self.tagsViewActive()) { 795 self.tagsViewInput(key, root_focus); 796 return; 797 } 798 if (self.newViewActive()) { 799 try self.newViewInput(allocator, key, root_focus); 800 return; 801 } 802 const i = self.selectedSplitIndex() orelse return; 803 if (self.detailActive(i)) { 804 try self.detailInput(i, key, root_focus); 805 } else { 806 try self.listInput(i, key, root_focus); 807 } 808 } 809 810 // arrow keys move the tag selection; up from the top row crosses to the 811 // header tabs. 812 fn tagsViewInput(self: *View, key: Key, root_focus: *Focus) void { 813 const tf = self.tagsView(); 814 const cid = tf.focus.child_id orelse return; 815 const cur = tf.indexOfFocusId(cid) orelse return; 816 const count = tf.text_boxes.items.len; 817 switch (key) { 818 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 819 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 820 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 821 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 822 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 823 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 824 else => {}, 825 } 826 } 827 828 fn listInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 829 // up/down (and the scroll wheel) move the selection a row; page up/down 830 // jump a fixed amount. right/Enter cross into the detail pane. up from 831 // the top row crosses into the header tabs. 832 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 833 const lb = self.listBox(index); 834 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 835 if (delta < 0 and at_top) return self.focusHeader(root_focus); 836 ui.moveRowFocus(lb, self.listScroll(index), root_focus, delta); 837 return; 838 } 839 switch (key) { 840 .enter, .arrow_right => try self.focusDetail(index, root_focus), 841 else => {}, 842 } 843 } 844 845 fn detailInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 846 if (self.tagsFocused(index)) { 847 try self.tagsInput(index, key, root_focus); 848 } else { 849 try self.descriptionInput(index, key, root_focus); 850 } 851 } 852 853 fn descriptionInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 854 const sc = self.detailScroll(index); 855 switch (key) { 856 .arrow_left => return self.focusList(index, root_focus), 857 // once the scroll can't move further, cross into the tags (or the 858 // header tabs when the issue has none). 859 .arrow_up => { 860 const before = sc.y; 861 sc.y -= 1; 862 sc.clampToContent(); 863 if (sc.y == before) { 864 if (self.tagFlow(index) != null) try self.focusTags(index, root_focus) else self.focusHeader(root_focus); 865 } 866 return; 867 }, 868 .arrow_down => sc.y += 1, 869 .page_up => sc.y -= 10, 870 .page_down => sc.y += 10, 871 .home => sc.y = 0, 872 .end => sc.y = std.math.maxInt(isize), 873 .mouse => |mouse| switch (mouse.action) { 874 .scroll => |dir| sc.y += if (dir == .up) @as(isize, -1) else 1, 875 else => {}, 876 }, 877 else => return, 878 } 879 sc.clampToContent(); 880 } 881 882 // up/down (and tab/shift+tab) move between the form's fields; up from the 883 // title crosses into the header tabs. the multiline description keeps 884 // enter and any up/down that has a row to move to. 885 fn newViewInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 886 const form = self.newForm(); 887 const cid = form.getFocus().child_id orelse return; 888 const cur = form.children.getIndex(cid) orelse return; 889 890 if (cur == description_field_index) { 891 const child = &form.children.values()[cur]; 892 const description = &child.widget.text_input; 893 switch (key) { 894 .enter => return child.widget.input(allocator, key, root_focus), 895 .arrow_up => if (!try description.cursorOnFirstRow(allocator)) 896 return child.widget.input(allocator, key, root_focus), 897 .arrow_down => if (!try description.cursorOnLastRow(allocator)) 898 return child.widget.input(allocator, key, root_focus), 899 else => {}, 900 } 901 } 902 903 switch (key) { 904 .arrow_up, .back_tab => if (cur > 0) 905 root_focus.setFocus(form.children.keys()[cur - 1]) 906 else 907 self.focusHeader(root_focus), 908 .arrow_down, .tab => if (cur < submit_field_index) { 909 root_focus.setFocus(form.children.keys()[cur + 1]); 910 }, 911 .enter => if (cur == submit_field_index) try self.submitNewIssue(allocator), 912 .mouse => |mouse| if (cur == submit_field_index and inp.leftClickOn(root_focus, cid, mouse)) { 913 try self.submitNewIssue(allocator); 914 }, 915 else => try form.children.values()[cur].widget.input(allocator, key, root_focus), 916 } 917 } 918 919 // commit the new issue to the repo's events branch and navigate to it. 920 // this is the terminal path; the web posts the form to the issue route, 921 // so the wasm side never runs (or compiles) the repo access below. 922 fn submitNewIssue(self: *View, allocator: std.mem.Allocator) !void { 923 if (comptime wasm) return; 924 const io = self.session.io orelse return; 925 const src = self.data.repo_source orelse return; 926 927 const form = self.newForm(); 928 const title_input = &form.children.values()[title_field_index].widget.text_input; 929 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 930 const description_input = &form.children.values()[description_field_index].widget.text_input; 931 932 const title = try title_input.text(allocator); 933 defer allocator.free(title); 934 const tags = try tags_input.text(allocator); 935 defer allocator.free(tags); 936 const description = try description_input.text(allocator); 937 defer allocator.free(description); 938 939 // a title is required, and a too-long tag would fail consumption 940 // after the event is already committed 941 if (title.len == 0) return; 942 var tag_iter = evt.Issue.tagIterator(tags); 943 while (tag_iter.next()) |tag| { 944 if (tag.len > evt.Issue.tag_max_len) return; 945 } 946 947 var id_bytes: [evt.event_id_size]u8 = undefined; 948 io.random(&id_bytes); 949 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 950 951 const event = evt.EventWithId{ 952 .id = event_id_hex, 953 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 954 .event = .{ .issue = .{ 955 .title = title, 956 .description = description, 957 .tags = tags, 958 } }, 959 }; 960 961 switch (src.repo_kind) { 962 inline else => |repo_kind| { 963 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 964 defer any_repo.deinit(io, allocator); 965 switch (any_repo) { 966 inline else => |*repo| try evt.commitAndConsume(repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}, self.session.local != null), 967 } 968 }, 969 } 970 971 // wipe the form so a return visit starts fresh 972 title_input.clear(allocator); 973 tags_input.clear(allocator); 974 description_input.clear(allocator); 975 976 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, .open, "", &event_id_hex) orelse return; 977 try self.session.navigate(route); 978 } 979 980 // arrow keys move the tag selection; at the flow's edges focus crosses to 981 // the neighboring widgets. 982 fn tagsInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 983 const tf = self.tagFlow(index) orelse return; 984 const cid = tf.focus.child_id orelse return; 985 const cur = tf.indexOfFocusId(cid) orelse return; 986 const count = tf.text_boxes.items.len; 987 const sc = self.detailScroll(index); 988 switch (key) { 989 .arrow_left => if (cur > 0) self.focusTag(index, tf, root_focus, cur - 1) else try self.focusList(index, root_focus), 990 .arrow_right => if (cur + 1 < count) self.focusTag(index, tf, root_focus, cur + 1), 991 .arrow_up => if (tf.rowStep(cur, false)) |i| self.focusTag(index, tf, root_focus, i) else self.focusHeader(root_focus), 992 .arrow_down => if (tf.rowStep(cur, true)) |i| self.focusTag(index, tf, root_focus, i) else try self.focusDescription(index, root_focus), 993 .home => self.focusTag(index, tf, root_focus, 0), 994 .end => self.focusTag(index, tf, root_focus, count - 1), 995 .mouse => |mouse| switch (mouse.action) { 996 .scroll => |dir| { 997 sc.y += if (dir == .up) @as(isize, -1) else 1; 998 sc.clampToContent(); 999 }, 1000 else => {}, 1001 }, 1002 else => {}, 1003 } 1004 } 1005 1006 const tags_child_index: usize = 0; 1007 1008 fn tagFlow(self: *View, index: usize) ?*ui.TagFlow { 1009 const inner = self.detailInner(index); 1010 if (inner.children.count() == 0) return null; 1011 return switch (inner.children.values()[tags_child_index].widget) { 1012 .tag_flow => |*tf| tf, 1013 else => null, 1014 }; 1015 } 1016 1017 fn tagsFocused(self: *View, index: usize) bool { 1018 const inner = self.detailInner(index); 1019 const cid = inner.getFocus().child_id orelse return false; 1020 return inner.children.getIndex(cid) == tags_child_index and self.tagFlow(index) != null; 1021 } 1022 1023 fn focusTag(self: *View, index: usize, tf: *ui.TagFlow, root_focus: *Focus, item: usize) void { 1024 root_focus.setFocus(tf.text_boxes.items[item].getFocus().id); 1025 // keep the tag visible on the terminal: its rect offset by the flow's 1026 // position in the pane. 1027 if (self.session.is_terminal and item < tf.rects.items.len) { 1028 if (self.detailInner(index).children.values()[tags_child_index].rect) |flow_rect| { 1029 var rect = tf.rects.items[item]; 1030 rect.x += flow_rect.x; 1031 rect.y += flow_rect.y; 1032 self.detailScroll(index).scrollToRect(rect); 1033 } 1034 } 1035 } 1036 1037 fn focusTags(self: *View, index: usize, root_focus: *Focus) !void { 1038 const tf = self.tagFlow(index) orelse return; 1039 if (tf.text_boxes.items.len == 0) return; 1040 const cid = tf.focus.child_id orelse tf.text_boxes.items[0].getFocus().id; 1041 const item = tf.indexOfFocusId(cid) orelse 0; 1042 self.focusTag(index, tf, root_focus, item); 1043 } 1044 1045 fn focusDescription(self: *View, index: usize, root_focus: *Focus) !void { 1046 if (self.description_id[index]) |id| root_focus.setFocus(id); 1047 } 1048 1049 // enter the detail pane. an empty pane (no issues) can't be entered. 1050 fn focusDetail(self: *View, index: usize, root_focus: *Focus) !void { 1051 if (self.detailInner(index).children.count() == 0) return; 1052 root_focus.setFocus(self.detailOuter(index).getFocus().id); 1053 } 1054 1055 // return to the list. 1056 fn focusList(self: *View, index: usize, root_focus: *Focus) !void { 1057 root_focus.setFocus(self.listScroll(index).getFocus().id); 1058 } 1059 1060 // cross to the header tabs above the stack. 1061 fn focusHeader(self: *View, root_focus: *Focus) void { 1062 root_focus.setFocus(self.box.children.keys()[header_index]); 1063 } 1064 1065 pub fn clearGrid(self: *View) void { 1066 self.box.clearGrid(); 1067 } 1068 1069 pub fn getGrid(self: View) ?Grid { 1070 return self.box.getGrid(); 1071 } 1072 1073 pub fn getFocus(self: *View) *Focus { 1074 return self.box.getFocus(); 1075 } 1076 1077 // for the parent's "scroll up at the top jumps to the header" check: at the 1078 // top only while the header tabs hold focus, so up from the split 1079 // crosses into the tabs first. 1080 pub fn getSelectedIndex(self: *View) ?usize { 1081 return if (self.headerActive()) 0 else 1; 1082 } 1083 }; 1084 1085 // the "a:" navigation link for `status`'s issues page filtered to the 1086 // url-encoded `tag` and rooted at issue `id` within `identity` ("owner/name"). 1087 // a rooted url ignores `status` (it derives its view from the issue's own); 1088 // with an empty `id` this is the bare list link. 1089 fn issuesLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8, id: []const u8) ![]const u8 { 1090 const route = ui.RoutablePage.repoIssuesRoute(identity, status, tag, id) orelse return error.RouteTooLong; 1091 const url = try route.toUrl(page_arena); 1092 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 1093 } 1094 1095 // the in-page "ai:" anchor for selecting issue `id` in `identity`'s list; the 1096 // href is only followed with js off. 1097 fn issueRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 1098 const route = ui.RoutablePage.repoIssuesRoute(identity, .open, "", id) orelse return error.RouteTooLong; 1099 const url = try route.toUrl(page_arena); 1100 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 1101 } 1102 1103 // the "a:" link to `status`'s issues list filtered to `tag` (raw; encoded here). 1104 fn tagLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8) ![]const u8 { 1105 const encoded = try ui.urlEncodeRef(page_arena.allocator(), tag); 1106 const route = ui.RoutablePage.repoIssuesRoute(identity, status, encoded, "") orelse return error.RouteTooLong; 1107 const url = try route.toUrl(page_arena); 1108 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 1109 } 1110 1111 // tabs switching between the issues page's views. 1112 pub const Header = struct { 1113 box: wgt.Box(ui.Widget), 1114 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 1115 1116 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 1117 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1118 errdefer box.deinit(allocator); 1119 1120 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 1121 errdefer tab_ids.deinit(allocator); 1122 1123 const aa = session.page_arena.allocator(); 1124 1125 // a list tab per status, labeled with its listing's issue count 1126 for ([_]evt.Issue.Status{ .open, .closed }) |status| { 1127 const route = ui.RoutablePage.repoIssuesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 1128 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 1129 const label = try std.fmt.allocPrint(aa, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 1130 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1131 errdefer text_box.deinit(allocator); 1132 text_box.getFocus().focusable = true; 1133 text_box.getFocus().kind = .{ .custom = link }; 1134 try tab_ids.put(allocator, text_box.getFocus().id, {}); 1135 try box.children.put(allocator, text_box.getFocus().id, .{ 1136 .widget = .{ .text_box = text_box }, 1137 .rect = null, 1138 .min_size = .{ .width = label.len + 2, .height = null }, 1139 }); 1140 } 1141 1142 // tags tab, labeled with the active tag filter 1143 { 1144 const tags_route = ui.RoutablePage.repoIssuesTagsRoute(data.identity, data.tag) orelse return error.RouteTooLong; 1145 const tags_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try tags_route.toUrl(session.page_arena)}); 1146 const label = if (data.tag.len == 0) "tags" else blk: { 1147 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 1148 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 1149 }; 1150 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1151 errdefer text_box.deinit(allocator); 1152 text_box.getFocus().focusable = true; 1153 text_box.getFocus().kind = .{ .custom = tags_link }; 1154 try tab_ids.put(allocator, text_box.getFocus().id, {}); 1155 try box.children.put(allocator, text_box.getFocus().id, .{ 1156 .widget = .{ .text_box = text_box }, 1157 .rect = null, 1158 .min_size = .{ .width = label.len + 2, .height = null }, 1159 }); 1160 } 1161 1162 // new-issue tab 1163 { 1164 const new_route = ui.RoutablePage.repoIssuesNewRoute(data.identity) orelse return error.RouteTooLong; 1165 const new_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try new_route.toUrl(session.page_arena)}); 1166 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "new", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1167 errdefer text_box.deinit(allocator); 1168 text_box.getFocus().focusable = true; 1169 text_box.getFocus().kind = .{ .custom = new_link }; 1170 try tab_ids.put(allocator, text_box.getFocus().id, {}); 1171 try box.children.put(allocator, text_box.getFocus().id, .{ 1172 .widget = .{ .text_box = text_box }, 1173 .rect = null, 1174 .min_size = .{ .width = "new".len + 2, .height = null }, 1175 }); 1176 } 1177 1178 var self = Header{ .box = box, .tab_ids = tab_ids }; 1179 // the tab matching the page's view is selected initially. 1180 self.getFocus().child_id = self.tab_ids.keys()[View.viewIndex(data.view)]; 1181 return self; 1182 } 1183 1184 pub fn deinit(self: *Header, allocator: std.mem.Allocator) void { 1185 self.box.deinit(allocator); 1186 self.tab_ids.deinit(allocator); 1187 } 1188 1189 pub fn build(self: *Header, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1190 self.clearGrid(); 1191 // only the selected tab shows its border 1192 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 1193 switch (child.widget) { 1194 .text_box => |*tb| tb.options.border_style = if (self.getFocus().child_id == id) .single else .hidden, 1195 else => {}, 1196 } 1197 } 1198 try self.box.build(allocator, constraint, root_focus); 1199 } 1200 1201 pub fn input(self: *Header, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1202 _ = allocator; 1203 const current_tab = self.currentTabIndex() orelse return; 1204 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 1205 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 1206 } 1207 } 1208 1209 pub fn clearGrid(self: *Header) void { 1210 self.box.clearGrid(); 1211 } 1212 1213 pub fn getGrid(self: Header) ?Grid { 1214 return self.box.getGrid(); 1215 } 1216 1217 pub fn getFocus(self: *Header) *Focus { 1218 return self.box.getFocus(); 1219 } 1220 1221 pub fn getSelectedIndex(self: Header) ?usize { 1222 return self.currentTabIndex(); 1223 } 1224 1225 fn currentTabIndex(self: Header) ?usize { 1226 const child_id = self.box.focus.child_id orelse return null; 1227 return self.tab_ids.getIndex(child_id); 1228 } 1229 }; 1230