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