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