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