╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
3120 try self.session.navigate(route); 3121 } 3122 3123 // re-emit the selected thread's event with the form's content (status 3124 // preserved), then reload the thread's page. this is the terminal path; 3125 // the web posts the form to the edit route. 3126 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 3127 if (comptime wasm) return; 3128 const io = self.session.io orelse return; 3129 const entry = self.data.selectedThread() orelse return; 3130 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3131 const author = actor.author; 3132 3133 const form = self.threadForm() orelse return; 3134 const title_input = try formField(form, "title"); 3135 const labels_input = try formField(form, "labels"); 3136 const description_input = try formField(form, "description"); 3137 3138 const title = try title_input.text(allocator); 3139 defer allocator.free(title); 3140 const labels = try labels_input.text(allocator); 3141 defer allocator.free(labels); 3142 const description = try description_input.text(allocator); 3143 defer allocator.free(description); 3144 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 3145 defer if (comptime supports_drafts) allocator.free(target_branch); 3146 3147 if (!Event.fieldsValid(title, labels)) { 3148 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, labels, description, target_branch); 3149 return; 3150 } 3151 const src = self.data.repo_source orelse return; 3152 3153 if (comptime supports_drafts) { 3154 if (entryDraft(entry.*)) { 3155 self.data.editDraft(self.session, allocator, author, entry.id, title, labels, description, target_branch) catch |err| { 3156 const failure = FeedbackFailure.fromError(err) orelse return err; 3157 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 3158 return; 3159 }; 3160 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 3161 try self.session.navigate(route); 3162 return; 3163 } 3164 } 3165 3166 const id_bytes = try evt.parseEventId(entry.id); 3167 if (!try src.canModify(io, allocator, actor, kind, &id_bytes)) return; 3168 3169 switch (src.repo_kind) { 3170 inline else => |repo_kind| { 3171 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3172 defer any_repo.deinit(io, allocator); 3173 switch (any_repo) { 3174 inline else => |*repo| if (supports_drafts) { 3175 Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 3176 .title = title, 3177 .labels = labels, 3178 .description = description, 3179 .target_branch = target_branch, 3180 } }, author) catch |err| { 3181 const failure = FeedbackFailure.fromError(err) orelse return err; 3182 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 3183 return; 3184 }; 3185 } else if (has_status) 3186 try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 3187 .title = title, 3188 .labels = labels, 3189 .description = description, 3190 } }, author) 3191 else 3192 try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, labels, description, author), 3193 } 3194 }, 3195 } 3196 3197 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 3198 try self.session.navigate(route); 3199 } 3200 3201 fn rememberThreadFeedback(self: *This, failure: FeedbackFailure, title: []const u8, labels: []const u8, description: []const u8, target_branch: []const u8) !void { 3202 const aa = self.session.arena.allocator(); 3203 const source = if (supports_drafts) blk: { 3204 const form = self.threadForm() orelse break :blk null; 3205 break :blk formWidget(form, .patch_source); 3206 } else null; 3207 self.session.data.form_feedback = @unionInit(ui.Session.FormFeedback, @tagName(feedback_tag), .{ 3208 .failure = failure, 3209 .fields = if (comptime supports_drafts) .{ 3210 .title = try aa.dupe(u8, title), 3211 .labels = try aa.dupe(u8, labels), 3212 .description = try aa.dupe(u8, description), 3213 .target_branch = try aa.dupe(u8, target_branch), 3214 .source_branch = if (source) |value| try value.field().text(aa) else "", 3215 .source_kind = if (source) |value| value.selectedKind() else .fork, 3216 } else .{ 3217 .title = try aa.dupe(u8, title), 3218 .labels = try aa.dupe(u8, labels), 3219 .description = try aa.dupe(u8, description), 3220 }, 3221 }); 3222 } 3223 3224 fn submitConfirmation(self: *This, allocator: std.mem.Allocator, button_index: usize) !void { 3225 switch (confirmation(self.data.view) orelse return) { 3226 .publish => if (comptime supports_drafts) { 3227 if (comptime wasm) return; 3228 const entry = self.data.selectedThread() orelse return; 3229 if (!entryDraft(entry.*)) return; 3230 const author = ((try self.session.authorize(self.data.identity, .read, kind)) orelse return).author; 3231 try self.data.publishDraft(self.session, allocator, author, entry.id); 3232 const route = listRoute(self.data.identity, @fromBackingInt(@intCast(0)), "", entry.id) orelse return; 3233 try self.session.navigate(route); 3234 } else unreachable, 3235 .merge => if (comptime supports_merge) { 3236 if (comptime wasm) return; 3237 const entry = self.data.selectedThread() orelse return; 3238 const revision: evt.Patch.MergeRevision = if (button_index > 1) .squash else .source; 3239 if (!Data.canMerge(entry.*, self.session) or entry.mergeability.get(revision) != .clean) return; 3240 const author = ((try self.session.authorize(self.data.identity, .write, kind)) orelse return).author; 3241 self.data.mergePatch(self.session, allocator, author, entry.id, revision) catch |err| switch (err) { 3242 error.MergeConflict, error.MergeCheckUnavailable, error.PatchOutOfDate, error.PatchDataUnavailable => { 3243 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 3244 try self.session.navigate(route); 3245 return; 3246 }, 3247 else => return err, 3248 }; 3249 const merged = std.meta.stringToEnum(Status, "merged") orelse return; 3250 const route = listRoute(self.data.identity, merged, "", entry.id) orelse return; 3251 try self.session.navigate(route); 3252 } else unreachable, 3253 .remove => try self.submitRemove(allocator), 3254 } 3255 } 3256 3257 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 3258 const comment = self.data.comment_id.len != 0; 3259 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 3260 const id = evt.parseEventId(event_id) catch return; 3261 try self.removeEvent(allocator, if (comment) .comment else kind, id); 3262 } 3263 3264 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 3265 if (comptime wasm) return; 3266 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3267 const author = actor.author; 3268 3269 if (comptime supports_drafts) { 3270 const selected = self.data.selectedThread() orelse return; 3271 if (event_kind == kind and entryDraft(selected.*)) { 3272 try Data.removeDraft(self.session, allocator, author, &id); 3273 const route = draftsRoute(self.data.identity) orelse return; 3274 try self.session.navigate(route); 3275 return; 3276 } 3277 } 3278 3279 const io = self.session.io orelse return; 3280 const src = self.data.repo_source orelse return; 3281 if (!try src.canModify(io, allocator, actor, event_kind, &id)) return; 3282 const thread_id = evt.parseEventId(self.data.selected_id) catch return; 3283 3284 switch (src.repo_kind) { 3285 inline else => |repo_kind| { 3286 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3287 defer any_repo.deinit(io, allocator); 3288 switch (any_repo) { 3289 inline else => |*repo| try evt.removeInThread(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id, event_kind, &id, author), 3290 } 3291 }, 3292 } 3293 3294 const id_hex = std.fmt.bytesToHex(id, .lower); 3295 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex, null) orelse return; 3296 try self.session.navigate(route); 3297 } 3298 3299 // enter the detail pane, landing on the way back to the list when it shows 3300 fn focusDetail(self: *This, index: usize, root_focus: *Focus) void { 3301 if (widget.listBackId(self.detailFrame(index)) != null) return widget.focusListBack(self.detailFrame(index), root_focus); 3302 _ = self.detail(index).focusFirst(root_focus); 3303 } 3304 3305 // return to the list. 3306 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 3307 root_focus.setFocus(self.listScroll(index).getFocus().id); 3308 } 3309 3310 // cross to the header tabs above the stack. 3311 fn focusHeader(self: *This, root_focus: *Focus) void { 3312 root_focus.setFocus(self.box.children.keys()[header_index]); 3313 } 3314 3315 pub fn clearGrid(self: *This) void { 3316 self.box.clearGrid(); 3317 } 3318 3319 pub fn getGrid(self: This) ?Grid { 3320 return self.box.getGrid(); 3321 } 3322 3323 pub fn getFocus(self: *This) *Focus { 3324 return self.box.getFocus(); 3325 } 3326 3327 // for the parent's "scroll up at the top jumps to the header" check: at the 3328 // top only while the header tabs hold focus, so up from the split 3329 // crosses into the tabs first. 3330 pub fn atTop(self: *This) bool { 3331 return self.headerActive(); 3332 } 3333 }; 3334 } 3335