╭ Repo.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const evt = @import("../event.zig"); 3 const pch = @import("../patch.zig"); 4 const notify = @import("../notify.zig"); 5 const ui = @import("../ui.zig"); 6 const xit = @import("xit"); 7 const rp = xit.repo; 8 const hash = xit.hash; 9 const xitui = xit.xitui; 10 const wgt = xitui.widget; 11 const layout = xitui.layout; 12 const Key = xitui.input.Key; 13 const Grid = xitui.grid.Grid; 14 const Focus = xitui.focus.Focus; 15 const inp = @import("./input.zig"); 16 17 pub const Header = @import("./Repo/Header.zig"); 18 pub const Files = @import("./Repo/Files.zig"); 19 pub const Markdown = @import("./Repo/Markdown.zig"); 20 pub const Commits = @import("./Repo/Commits.zig"); 21 pub const Diff = @import("./Repo/Diff.zig"); 22 pub const Refs = @import("./Repo/Refs.zig"); 23 pub const Discussions = @import("./Repo/Discussions.zig"); 24 pub const Issues = @import("./Repo/Issues.zig"); 25 pub const Patches = @import("./Repo/Patches.zig"); 26 pub const Comment = @import("./Repo/Comment.zig"); 27 pub const Undo = @import("./Repo/Undo.zig"); 28 pub const Events = @import("./Repo/Events.zig"); 29 pub const Settings = @import("./Repo/Settings.zig"); 30 pub const Roles = @import("./Repo/Roles.zig"); 31 pub const Quit = @import("./Quit.zig"); 32 33 header: Header, 34 repo: evt.Repo.Record, 35 handle: ui.RepoHandle, 36 files: Files, 37 changes: Changes, 38 refs: Refs, 39 discussions: Discussions, 40 issues: Issues, 41 patches: Patches, 42 events: Events, 43 undo: ?Undo = null, 44 settings: ?Settings = null, 45 user_notifications: ui.UserNotifications, 46 quit: Quit, 47 48 const Self = @This(); 49 50 const Changes = union(enum) { 51 commits: Commits, 52 diff: Diff, 53 }; 54 55 pub fn init( 56 arena: *std.heap.ArenaAllocator, 57 session: *ui.Session, 58 route: ui.RoutablePage, 59 ) !Self { 60 const DB = evt.AdminDB; 61 const hash_kind = evt.admin_repo_opts.hash; 62 63 // every repo route stores its identity as "owner:name" (or elides it in local mode) 64 const name_str = route.repoIdentity() orelse return error.UnexpectedRoute; 65 const repo_identity = ui.RoutablePage.RepoIdentity.parse(name_str) orelse return error.NotFound; 66 // files, commits and diff share the requested ref, or the default branch. 67 // directories and hunk windows only apply to their own tab. 68 const requested_ref_or_oid: ?ui.RoutablePage.RefOrOid = switch (route) { 69 .repo_files => |f| f.ref_kind, 70 .repo_commits => |c| c.ref_or_oid, 71 .repo_diff => |d| d.ref_or_oid, 72 else => null, 73 }; 74 const requested_ref_value: []const u8 = switch (route) { 75 .repo_files => |*f| f.ref_value.slice(), 76 .repo_commits => |*c| c.value.slice(), 77 .repo_diff => |*d| d.value.slice(), 78 else => "", 79 }; 80 const patchrev_id: []const u8 = switch (route) { 81 .repo_files => |*f| f.patchrev_id.slice(), 82 .repo_diff => |*d| d.patchrev_id.slice(), 83 else => "", 84 }; 85 const files_dir = switch (route) { 86 .repo_files => |*f| f.path.slice(), 87 else => "", 88 }; 89 const files_line = switch (route) { 90 .repo_files => |f| f.line, 91 else => 0, 92 }; 93 const files_find: []const u8 = switch (route) { 94 .repo_files => |*f| f.find.slice(), 95 else => "", 96 }; 97 // the commits view's pane shows the whole message of the commit it walks from 98 const commits_message = route == .repo_commits and route.repo_commits.message; 99 const commits_base_oid: []const u8 = switch (route) { 100 .repo_commits => |*c| c.base_oid.slice(), 101 else => "", 102 }; 103 const commits_search: []const u8 = switch (route) { 104 .repo_commits => |*c| c.search.slice(), 105 else => "", 106 }; 107 const commits_from: []const u8 = switch (route) { 108 .repo_commits => |*c| c.from.slice(), 109 else => "", 110 }; 111 const commits_merge: []const u8 = switch (route) { 112 .repo_commits => |*c| c.merge.slice(), 113 else => "", 114 }; 115 // the refs tab windows one column at a time: `refs_from` (a url-encoded 116 // ref name) roots `refs_kind`'s column, the other stays at its first window. 117 const refs_kind: ui.RoutablePage.RefKind = switch (route) { 118 .repo_refs => |r| r.kind, 119 else => .branch, 120 }; 121 const refs_from: []const u8 = switch (route) { 122 .repo_refs => |*r| r.from.slice(), 123 else => "", 124 }; 125 const refs_search: []const u8 = switch (route) { 126 .repo_refs => |*r| r.search.slice(), 127 else => "", 128 }; 129 const discussions_label: []const u8 = switch (route) { 130 .repo_discussions => |*t| t.label.slice(), 131 else => "", 132 }; 133 const discussions_search: []const u8 = switch (route) { 134 .repo_discussions => |*t| t.search.slice(), 135 else => "", 136 }; 137 const discussions_selected: []const u8 = switch (route) { 138 .repo_discussions => |*t| t.selected.slice(), 139 else => "", 140 }; 141 const discussions_comment: []const u8 = switch (route) { 142 .repo_discussions => |*t| t.comment.slice(), 143 else => "", 144 }; 145 const discussions_view: ui.RoutablePage.DiscussionsView = switch (route) { 146 .repo_discussions => |t| t.view, 147 else => .recent, 148 }; 149 const discussions_comments_start: usize = switch (route) { 150 .repo_discussions => |t| t.comments_start, 151 else => 0, 152 }; 153 // the issues tab's label filter, the issue its window is rooted at, and the 154 // view it shows. 155 const issues_label: []const u8 = switch (route) { 156 .repo_issues => |*i| i.label.slice(), 157 else => "", 158 }; 159 const issues_assignee: []const u8 = switch (route) { 160 .repo_issues => |*i| i.assignee.slice(), 161 else => "", 162 }; 163 const issues_search: []const u8 = switch (route) { 164 .repo_issues => |*i| i.search.slice(), 165 else => "", 166 }; 167 const issues_selected: []const u8 = switch (route) { 168 .repo_issues => |*i| i.selected.slice(), 169 else => "", 170 }; 171 const issues_comment: []const u8 = switch (route) { 172 .repo_issues => |*i| i.comment.slice(), 173 else => "", 174 }; 175 const issues_theirs: []const u8 = switch (route) { 176 .repo_issues => |*i| i.theirs.slice(), 177 else => "", 178 }; 179 const issues_view: ui.RoutablePage.IssuesView = switch (route) { 180 .repo_issues => |i| i.view, 181 else => .open, 182 }; 183 const issues_comments_start: usize = switch (route) { 184 .repo_issues => |i| i.comments_start, 185 else => 0, 186 }; 187 const patches_label: []const u8 = switch (route) { 188 .repo_patches => |*p| p.label.slice(), 189 else => "", 190 }; 191 const patches_search: []const u8 = switch (route) { 192 .repo_patches => |*p| p.search.slice(), 193 else => "", 194 }; 195 const patches_selected: []const u8 = switch (route) { 196 .repo_patches => |*p| p.selected.slice(), 197 else => "", 198 }; 199 const patches_comment: []const u8 = switch (route) { 200 .repo_patches => |*p| p.comment.slice(), 201 else => "", 202 }; 203 const patches_theirs: []const u8 = switch (route) { 204 .repo_patches => |*p| p.theirs.slice(), 205 else => "", 206 }; 207 const patches_view: ui.RoutablePage.PatchesView = switch (route) { 208 .repo_patches => |p| p.view, 209 else => .open, 210 }; 211 const patches_comments_start: usize = switch (route) { 212 .repo_patches => |p| p.comments_start, 213 else => 0, 214 }; 215 const events_kind: ?evt.EventKind = switch (route) { 216 .repo_events => |e| e.kind, 217 else => null, 218 }; 219 const events_view: ui.RoutablePage.EventsView = switch (route) { 220 .repo_events => |e| e.view, 221 else => .active, 222 }; 223 const events_moment: ?u64 = switch (route) { 224 .repo_events => |e| e.moment, 225 else => null, 226 }; 227 const events_selected: []const u8 = switch (route) { 228 .repo_events => |*e| e.selected.slice(), 229 else => "", 230 }; 231 const roles_start: usize = switch (route) { 232 .repo_roles => |r| r.start, 233 else => 0, 234 }; 235 // the thread the route shows, whose notif viewing it reads 236 const viewed_thread: []const u8 = switch (route) { 237 .repo_issues => |*i| if (i.view == .conflicts) "" else i.selected.slice(), 238 .repo_patches => |*p| if (p.view == .conflicts) "" else p.selected.slice(), 239 .repo_discussions => |*t| t.selected.slice(), 240 else => "", 241 }; 242 243 // where the on-disk repo lives (null keeps the views' empty fallback), plus 244 // the repo and owner-name metadata the header shows. local mode already 245 // knows all three; the server paths resolve them from the admin db. 246 var source: ?ui.RepoSource = null; 247 var repo_id_maybe: ?[evt.event_id_size]u8 = null; 248 var repo: evt.Repo.Record = undefined; 249 // local mode owns its repo 250 var role: evt.Repo.Role = .owner; 251 var owner_name: []const u8 = undefined; 252 if (session.local) |local| { 253 source = local; 254 // local routes elide the identity, so the display name comes from the 255 // repo's directory rather than the route. 256 repo = .{ 257 .event = .{ 258 .user_id = "", 259 .name = try arena.allocator().dupe(u8, std.Io.Dir.path.basename(local.path)), 260 .description = "", 261 // local mode shows every tab 262 .discuss_role = .read, 263 .issue_role = .read, 264 .patch_role = .read, 265 }, 266 }; 267 owner_name = ""; 268 } else { 269 const haxy_moment = session.haxy_moment orelse return error.NoMoment; 270 const io = session.io orelse return error.NoMoment; 271 const users_dir = session.users_dir orelse return error.NoMoment; 272 const found = (try evt.readRepoByOwnerAndName(io, arena.child_allocator, arena, haxy_moment, users_dir, repo_identity.owner, repo_identity.name, session.userId())) orelse return error.NotFound; 273 // a repo the session can't read doesn't exist to it 274 role = found.role orelse return error.NotFound; 275 repo = found.repo; 276 repo_id_maybe = found.event_id; 277 278 // resolve the creating user so the header can show their name to the left 279 // of the repo title. 280 const owner = (try evt.User.readById(DB, hash_kind, haxy_moment, arena, repo.event.user_id)) orelse return error.NotFound; 281 owner_name = owner.event.name; 282 283 // the repo's working copy lives in its owner's dir. 284 source = .{ 285 .path = try evt.repoPath(arena.allocator(), users_dir, repo.event.user_id, &found.event_id), 286 .repo_kind = .xit, 287 }; 288 } 289 290 const handle = ui.RepoHandle{ 291 .location = .{ .repo = try arena.allocator().dupe(u8, repo_identity.identity) }, 292 .viewer = try ui.Viewer.init(session, arena, role), 293 }; 294 295 // open the repo once for every tab. files and changes share a ref or revision. 296 // no filesystem (wasm), nowhere to look, or a failed open: empty tabs. 297 // the events tab needs write access, like undo 298 if (route == .repo_events and !handle.canWrite()) return error.NotFound; 299 // a tab that is off doesn't exist 300 const route_thread_kind: ?evt.EventKind = switch (route) { 301 .repo_discussions => .discuss, 302 .repo_issues => .issue, 303 .repo_patches => .patch, 304 else => null, 305 }; 306 if (route_thread_kind) |kind| if (repo.event.threadRole(kind) == null) return error.NotFound; 307 const undo_index = if (route == .repo_undo) route.repo_undo.index else null; 308 const undo_clear = route == .repo_undo and route.repo_undo.clear; 309 // only xit repos have undo history, so it's read in their branch below 310 var undo_data: ?Undo = null; 311 // only the owner sees the settings, which show the repo's hash kind 312 var settings: ?Settings = null; 313 const files, const changes, const refs, var discussions, var issues, var patches, const events = blk: { 314 read: { 315 const io = session.io orelse break :read; 316 const src = source orelse break :read; 317 const gpa = arena.child_allocator; 318 switch (src.repo_kind) { 319 inline else => |repo_kind| { 320 var any_repo = rp.AnyRepo(repo_kind, .{}).open(io, gpa, src.localInitOpts()) catch break :read; 321 defer any_repo.deinit(io, gpa); 322 switch (any_repo) { 323 inline else => |*opened| { 324 // local mode: bring the event db up to date with the events branch 325 if (session.local != null) { 326 try evt.consume(.local, .repo, repo_kind, opened.self_repo_opts, io, gpa, opened, evt.events_ref, &.{}); 327 try pch.refreshBranches(.local, repo_kind, opened.self_repo_opts, io, gpa, opened, null, null); 328 } 329 if (session.data.host_kind == .server and handle.canUndo()) { 330 // a server page always resolved its repo id 331 const repo_id = repo_id_maybe orelse unreachable; 332 settings = .{ 333 .identity = try arena.allocator().dupe(u8, repo_identity.identity), 334 .name = repo.event.name, 335 .description = repo.event.description, 336 .access = repo.event.read_access, 337 .discuss_role = repo.event.discuss_role, 338 .issue_role = repo.event.issue_role, 339 .patch_role = repo.event.patch_role, 340 .hash_kind = opened.self_repo_opts.hash, 341 .view = if (route == .repo_roles) .roles else .settings, 342 .roles = try Roles.init(io, gpa, arena, session.haxy_moment orelse return error.NoMoment, session.users_dir orelse return error.NoMoment, repo_identity.identity, repo.event.user_id[0..evt.event_id_size], &repo_id, roles_start), 343 }; 344 } 345 // tabs switch in-page, so every tab's data is read here 346 if (repo_kind == .xit and handle.canWrite()) { 347 undo_data = Undo.init(opened.self_repo_opts, arena, opened, session.haxy_moment, repo_identity.identity, undo_index, handle) catch |err| switch (err) { 348 error.OutOfMemory => return err, 349 else => .{ .identity = try arena.allocator().dupe(u8, repo_identity.identity), .handle = handle, .failure = @errorName(err) }, 350 }; 351 } 352 // a merge: route stands for the log of what the merge brought in 353 const ref_or_oid: ?ui.RoutablePage.RefOrOid, const ref_value, const base_oid = if (commits_merge.len == 0) 354 .{ requested_ref_or_oid, requested_ref_value, commits_base_oid } 355 else merged: { 356 const merged = try Commits.resolveMerge(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, commits_merge); 357 break :merged .{ .object, merged.oid, merged.base_oid }; 358 }; 359 const files_data = if (patchrev_id.len != 0) 360 try Files.initPatchRev(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, handle, patchrev_id, files_dir, files_line) 361 else 362 try Files.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, handle, ref_or_oid, ref_value, files_dir, files_line, files_find); 363 const changes_data: Changes = if (route == .repo_diff) 364 .{ .diff = try Diff.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, route.repo_diff) } 365 else if (patchrev_id.len != 0) diff: { 366 const diff_route = ui.RoutablePage.repoPatchRevDiffRoute(repo_identity.identity, patchrev_id, 0, "") orelse return error.RouteTooLong; 367 break :diff .{ .diff = try Diff.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, diff_route.repo_diff) }; 368 } else .{ .commits = try Commits.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, session.haxy_moment, handle, ref_or_oid, ref_value, commits_message, base_oid, commits_search, commits_from) }; 369 const target_branch = if (files_data.ref_or_oid == .branch) files_data.ref_or_oid_value else ""; 370 var discussions_data = try Discussions.init(repo_kind, opened.self_repo_opts, arena, opened, io, session.haxy_moment, repo_identity.identity, discussions_label, discussions_search, discussions_selected, discussions_comment, discussions_comments_start, discussions_view, tabViewer(handle, repo, .discuss)); 371 var issues_data = try Issues.init(repo_kind, opened.self_repo_opts, arena, opened, io, session.haxy_moment, repo_identity.identity, issues_label, issues_assignee, issues_search, issues_selected, issues_comment, issues_comments_start, issues_theirs, issues_view, tabViewer(handle, repo, .issue)); 372 var patches_data = try Patches.init(repo_kind, opened.self_repo_opts, arena, opened, io, session.haxy_moment, session, repo_id_maybe, repo_identity.identity, target_branch, patches_label, patches_search, patches_selected, patches_comment, patches_comments_start, patches_theirs, patches_view, tabViewer(handle, repo, .patch)); 373 if (repo_kind == .xit and session.data.host_kind == .server) { 374 // a server page always resolved its repo id 375 const repo_id = repo_id_maybe orelse unreachable; 376 readNotify(opened.self_repo_opts, session, arena, opened, repo.event.user_id[0..evt.event_id_size], &repo_id, viewed_thread, &discussions_data, &issues_data, &patches_data) catch |err| { 377 std.log.warn("failed to read thread notifications: {s}", .{@errorName(err)}); 378 }; 379 } 380 break :blk .{ 381 files_data, 382 changes_data, 383 try Refs.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, repo_identity.identity, refs_kind, refs_from, refs_search), 384 discussions_data, 385 issues_data, 386 patches_data, 387 try Events.init(repo_kind, opened.self_repo_opts, arena, opened, io, session.haxy_moment, repo_identity.identity, repo.event, events_view, events_kind, events_selected, events_moment, session.local != null, session.data.sync_failure), 388 }; 389 }, 390 } 391 }, 392 } 393 } 394 const aa = arena.allocator(); 395 break :blk .{ 396 try Files.emptyResult(aa, handle, requested_ref_or_oid orelse .branch, requested_ref_value, files_dir), 397 Changes{ .commits = try Commits.emptyResult(aa, handle, requested_ref_or_oid orelse .branch, requested_ref_value, commits_base_oid) }, 398 try Refs.emptyResult(arena, repo_identity.identity, refs_kind, refs_from, refs_search), 399 try Discussions.emptyResult(aa, repo_identity.identity, discussions_label, discussions_search, discussions_selected, discussions_comment, discussions_comments_start, discussions_view), 400 try Issues.emptyResult(aa, repo_identity.identity, issues_label, issues_assignee, issues_search, issues_selected, issues_comment, issues_comments_start, issues_theirs, issues_view), 401 try Patches.emptyResult(aa, repo_identity.identity, patches_label, patches_search, patches_selected, patches_comment, patches_comments_start, patches_theirs, patches_view), 402 try Events.empty(aa, repo_identity.identity, events_view, session.local != null, session.data.sync_failure), 403 }; 404 }; 405 if (route == .repo_undo and undo_data == null) return error.NotFound; 406 if ((route == .repo_settings or route == .repo_roles) and settings == null) return error.NotFound; 407 if (undo_data) |*undo| undo.clear = undo_clear; 408 discussions.repo_source = source; 409 issues.repo_source = source; 410 patches.repo_source = source; 411 patches.repo_id = repo_id_maybe; 412 413 return .{ 414 // use the files tab's resolved ref for the header 415 .header = try Header.init(arena, repo.event.name, owner_name, files.ref_or_oid, files.ref_or_oid_value, discussions.label, issues.label, patches.label), 416 .repo = repo, 417 .handle = handle, 418 .files = files, 419 .changes = changes, 420 .refs = refs, 421 .discussions = discussions, 422 .issues = issues, 423 .patches = patches, 424 .events = events, 425 .undo = undo_data, 426 .settings = settings, 427 // after the shown thread's notif is marked read 428 .user_notifications = try ui.UserNotifications.init(arena, session, route.userTab()), 429 .quit = Quit.init(), 430 }; 431 } 432 433 // set the viewer's notify button on every listed thread and mark the viewed thread's notif read 434 fn readNotify( 435 comptime repo_opts: rp.RepoOpts(.xit), 436 session: *ui.Session, 437 arena: *std.heap.ArenaAllocator, 438 repo: *rp.Repo(.xit, repo_opts), 439 owner_id: *const [evt.event_id_size]u8, 440 repo_id: *const [evt.event_id_size]u8, 441 viewed_thread: []const u8, 442 discussions: *Discussions, 443 issues: *Issues, 444 patches: *Patches, 445 ) !void { 446 const user_id = session.userId() orelse return; 447 const io = session.io orelse return error.NoMoment; 448 const users_dir = session.users_dir orelse return error.NoMoment; 449 const gpa = arena.child_allocator; 450 451 { 452 // the owner's plain data, which holds the subscriber index 453 var owner_repo = (try evt.openUserRepo(io, gpa, users_dir, owner_id)) orelse return error.NotFound; 454 defer owner_repo.deinit(io, gpa); 455 const owner_moment = try notify.latestMoment(&owner_repo); 456 457 try setNotifyActions(arena, &discussions.recent, owner_moment, repo_id, &user_id); 458 for ([_]*Issues.Window{ &issues.open, &issues.closed, &issues.conflicts }) |window| try setNotifyActions(arena, window, owner_moment, repo_id, &user_id); 459 for ([_]*Patches.Window{ &patches.open, &patches.closed, &patches.merged, &patches.conflicts }) |window| try setNotifyActions(arena, window, owner_moment, repo_id, &user_id); 460 } 461 462 if (viewed_thread.len == 0) return; 463 var user_repo = (try evt.openUserRepo(io, gpa, users_dir, &user_id)) orelse return; 464 defer user_repo.deinit(io, gpa); 465 const latest = (try notify.latestMoment(&user_repo)) orelse return; 466 const notif_id = notify.idOf(repo_id, &try evt.parseEventId(viewed_thread)); 467 const item = (try notify.readItem(latest, arena, &notif_id)) orelse return; 468 if (!item.unread) return; 469 // the page reads this moment or a newer one, so a child in it is shown 470 const moment = try evt.currentMoment(repo_opts, repo); 471 const kinds_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, evt.event_index_key)) orelse return error.CursorNotFound; 472 const kinds = try evt.EventDB(repo_opts.hash).HashMap(.read_only).init(kinds_cursor); 473 // a child the page can't show leaves it unread 474 if (try evt.readEventKind(repo_opts.hash, kinds, &item.notif.last_child_id) == null) return; 475 try notify.markRead(io, gpa, users_dir, &user_id, &notif_id, &item.notif.last_child_id); 476 } 477 478 // set the viewer's notify button on each of a window's threads 479 fn setNotifyActions( 480 arena: *std.heap.ArenaAllocator, 481 window: anytype, 482 owner_moment: ?evt.UserDB.HashMap(.read_only), 483 repo_id: *const [evt.event_id_size]u8, 484 user_id: *const [evt.event_id_size]u8, 485 ) !void { 486 const items = try arena.allocator().dupe(std.meta.Elem(@TypeOf(window.items)), window.items); 487 for (items) |*entry| { 488 const thread_id = try evt.parseEventId(entry.id); 489 // muting drops the user from the index, so membership alone decides 490 const subscribed = if (owner_moment) |moment| try notify.isSubscribed(moment, &notify.idOf(repo_id, &thread_id), user_id) else false; 491 entry.notify_action = if (subscribed) .mute else .subscribe; 492 } 493 window.items = items; 494 } 495 496 // the viewer a tab's comment bar sees, which is none below the tab's role 497 fn tabViewer(handle: ui.RepoHandle, repo: evt.Repo.Record, kind: evt.EventKind) ?ui.Viewer { 498 const viewer = handle.viewer orelse return null; 499 const tab_role = repo.event.threadRole(kind) orelse return null; 500 return if (viewer.role.atLeast(tab_role)) viewer else null; 501 } 502 503 pub const View = struct { 504 box: wgt.Box(ui.Widget), 505 506 const header_index: usize = 0; 507 const stack_index: usize = 1; 508 509 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 510 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .round_corners = true, .direction = .vert }); 511 errdefer box.deinit(allocator); 512 513 // build the header first so we can grab the files-tab id for the user 514 // view (it focuses there after login). 515 { 516 var header_view = try Header.View.init(allocator, data, session); 517 errdefer header_view.deinit(allocator); 518 try box.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .repo_header = header_view }, .rect = null, .min_size = null }); 519 } 520 521 { 522 var stack = try wgt.Stack(ui.Widget).init(allocator); 523 errdefer stack.deinit(allocator); 524 525 // files — the default tab: the current directory's listing. 526 { 527 var files_view = try Files.View.init(allocator, &data.files, session); 528 errdefer files_view.deinit(allocator); 529 try stack.children.put(allocator, files_view.getFocus().id, .{ .repo_files = files_view }); 530 } 531 532 switch (data.changes) { 533 .diff => |*diff| { 534 var diff_view = try Diff.View.init(allocator, diff, session); 535 errdefer diff_view.deinit(allocator); 536 try stack.children.put(allocator, diff_view.getFocus().id, .{ .diff_view = diff_view }); 537 }, 538 .commits => |*commits| { 539 var commits_view = try Commits.View.init(allocator, commits, session); 540 errdefer commits_view.deinit(allocator); 541 try stack.children.put(allocator, commits_view.getFocus().id, .{ .repo_commits = commits_view }); 542 }, 543 } 544 545 // refs — the repo's branches and tags. 546 { 547 var refs_view = try Refs.View.init(allocator, &data.refs, session); 548 errdefer refs_view.deinit(allocator); 549 try stack.children.put(allocator, refs_view.getFocus().id, .{ .repo_refs = refs_view }); 550 } 551 552 // discussions and their comment permalinks. 553 if (data.repo.event.discuss_role != null) { 554 var discussions_view = try Discussions.View.init(allocator, &data.discussions, session); 555 errdefer discussions_view.deinit(allocator); 556 try stack.children.put(allocator, discussions_view.getFocus().id, .{ .repo_discussions = discussions_view }); 557 } 558 559 // issues — the repo's issue tracker and comment permalinks. 560 if (data.repo.event.issue_role != null) { 561 var issues_view = try Issues.View.init(allocator, &data.issues, session); 562 errdefer issues_view.deinit(allocator); 563 try stack.children.put(allocator, issues_view.getFocus().id, .{ .repo_issues = issues_view }); 564 } 565 566 // patches and their comment permalinks. 567 if (data.repo.event.patch_role != null) { 568 var patches_view = try Patches.View.init(allocator, &data.patches, session); 569 errdefer patches_view.deinit(allocator); 570 try stack.children.put(allocator, patches_view.getFocus().id, .{ .repo_patches = patches_view }); 571 } 572 573 if (data.handle.canWrite()) { 574 var events_view = try Events.View.init(allocator, &data.events, session); 575 errdefer events_view.deinit(allocator); 576 try stack.children.put(allocator, events_view.getFocus().id, .{ .repo_events = events_view }); 577 } 578 579 if (data.undo) |*undo| { 580 var undo_view = try Undo.View.init(allocator, undo, session); 581 errdefer undo_view.deinit(allocator); 582 try stack.children.put(allocator, undo_view.getFocus().id, .{ .repo_undo = undo_view }); 583 } 584 585 if (data.settings) |*settings| { 586 var settings_view = try Settings.View.init(allocator, settings, session); 587 errdefer settings_view.deinit(allocator); 588 try stack.children.put(allocator, settings_view.getFocus().id, .{ .repo_settings = settings_view }); 589 } 590 591 // the header shows new repo with a login, and new user without one 592 // and user only outside local mode, so keep the stack's children 593 // 1:1 with the tabs by skipping the same views. 594 const identity = try session.page_arena.allocator().print("{s}:{s}", .{ data.header.owner_name, data.header.name }); 595 if (session.data.user_id != null) { 596 const route = ui.RoutablePage{ .repo_repo_new = ui.RoutablePage.Array(ui.RoutablePage.repo_route_max_len).from(identity) orelse return error.RouteTooLong }; 597 var new_repo_view = try ui.NewRepo.View.init(allocator, session, route); 598 errdefer new_repo_view.deinit(allocator); 599 try stack.children.put(allocator, new_repo_view.getFocus().id, .{ .new_repo = new_repo_view }); 600 } else if (session.data.host_kind == .server) { 601 const route = ui.RoutablePage{ .repo_user_new = ui.RoutablePage.Array(ui.RoutablePage.repo_route_max_len).from(identity) orelse return error.RouteTooLong }; 602 var new_user_view = try ui.NewUser.View.init(allocator, session, route); 603 errdefer new_user_view.deinit(allocator); 604 try stack.children.put(allocator, new_user_view.getFocus().id, .{ .new_user = new_user_view }); 605 } 606 607 if (session.data.host_kind == .server) { 608 const route = ui.RoutablePage{ .repo_user = .{ .name = ui.RoutablePage.Array(ui.RoutablePage.repo_route_max_len).from(identity) orelse return error.RouteTooLong } }; 609 var user_view = try ui.UserSettings.initView(allocator, session, &data.user_notifications, route); 610 errdefer user_view.deinit(allocator); 611 try stack.children.put(allocator, user_view.getFocus().id, user_view); 612 } 613 614 if (session.is_terminal) { 615 var quit_view = try Quit.View.init(allocator, session); 616 errdefer quit_view.deinit(allocator); 617 try stack.children.put(allocator, quit_view.getFocus().id, .{ .quit = quit_view }); 618 } 619 620 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 621 } 622 623 var self = View{ .box = box }; 624 // a page opens on its tabs, except that search results open in the 625 // tab's own search box. 626 const results = data.files.find != null or 627 (data.changes == .commits and data.changes.commits.search != null) or 628 data.discussions.search != null or data.issues.search != null or data.patches.search != null; 629 self.getFocus().child_id = box.children.keys()[if (results) stack_index else header_index]; 630 return self; 631 } 632 633 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 634 self.box.deinit(allocator); 635 } 636 637 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 638 self.clearGrid(); 639 const header = &self.box.children.values()[header_index].widget.repo_header; 640 const stack = &self.box.children.values()[stack_index].widget.stack; 641 642 // each header tab maps 1:1 to a stack child by position 643 if (header.getSelectedIndex()) |index| 644 stack.getFocus().child_id = stack.children.keys()[index]; 645 try self.box.build(allocator, constraint, root_focus); 646 } 647 648 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 649 const stack = &self.box.children.values()[stack_index].widget.stack; 650 if (self.getFocus().child_id) |child_id| { 651 if (self.box.children.getIndex(child_id)) |current_index| { 652 const child = &self.box.children.values()[current_index].widget; 653 var index = current_index; 654 655 const direction = inp.vertDirection(key); 656 657 switch (direction) { 658 .up => { 659 switch (child.*) { 660 .repo_header => { 661 try child.input(allocator, key, root_focus); 662 }, 663 .stack => { 664 if (child.stack.getSelected()) |selected_widget| { 665 if (selected_widget.atTop(root_focus)) { 666 index = header_index; 667 } else { 668 try child.input(allocator, key, root_focus); 669 } 670 } 671 }, 672 else => {}, 673 } 674 }, 675 .down => { 676 switch (child.*) { 677 .repo_header => { 678 if (stack.getSelected()) |selected_widget| switch (selected_widget.*) { 679 .repo_files => |*v| if (v.focusHeader(root_focus)) return, 680 .repo_commits => |*v| if (v.focusHeader(root_focus)) return, 681 .repo_refs => |*v| if (v.focusHeader(root_focus)) return, 682 .repo_events => |*v| if (v.focusHeader(root_focus)) return, 683 .repo_undo => |*v| if (v.focusHeader(root_focus)) return, 684 .repo_settings => |*v| if (v.focusHeader(root_focus)) return, 685 .user_notifications => |*v| if (v.focusHeader(root_focus)) return, 686 else => {}, 687 }; 688 index = stack_index; 689 }, 690 .stack => { 691 try child.input(allocator, key, root_focus); 692 }, 693 else => {}, 694 } 695 }, 696 .none => { 697 try child.input(allocator, key, root_focus); 698 }, 699 } 700 701 if (index != current_index) { 702 root_focus.setFocus(self.box.children.keys()[index]); 703 } 704 } 705 } 706 } 707 708 pub fn clearGrid(self: *View) void { 709 self.box.clearGrid(); 710 } 711 712 pub fn getGrid(self: View) ?Grid { 713 return self.box.getGrid(); 714 } 715 716 pub fn getFocus(self: *View) *Focus { 717 return self.box.getFocus(); 718 } 719 }; 720