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