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