╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2968 if (comptime supports_drafts) { 2969 const source = formWidget(form, .patch_source) orelse return error.MissingFormField; 2970 const source_branch = switch (source.selectedKind()) { 2971 .fork => null, 2972 .branch => try source.field().text(allocator), 2973 }; 2974 defer if (source_branch) |branch| allocator.free(branch); 2975 const event_id_hex = Data.create(self.data, self.session, allocator, actor, title, labels, description, target_branch, source_branch) catch |err| { 2976 const failure = FeedbackFailure.fromError(err) orelse return err; 2977 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 2978 return; 2979 }; 2980 title_input.clear(allocator); 2981 labels_input.clear(allocator); 2982 description_input.clear(allocator); 2983 (try formField(form, "target_branch")).clear(allocator); 2984 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 2985 try self.session.navigate(route); 2986 return; 2987 } 2988 2989 var id_bytes: [evt.event_id_size]u8 = undefined; 2990 io.random(&id_bytes); 2991 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2992 2993 const event = evt.EventWithId{ 2994 .id = event_id_hex, 2995 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2996 .author = actor.author, 2997 .event = @unionInit(evt.Event, @tagName(kind), Event{ 2998 .title = title, 2999 .description = description, 3000 .labels = labels, 3001 }), 3002 }; 3003 3004 switch (src.repo_kind) { 3005 inline else => |repo_kind| { 3006 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3007 defer any_repo.deinit(io, allocator); 3008 switch (any_repo) { 3009 inline else => |*repo| try evt.consume(self.session.eventHost(), .repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 3010 } 3011 }, 3012 } 3013 3014 // wipe the form so a return visit starts fresh 3015 title_input.clear(allocator); 3016 labels_input.clear(allocator); 3017 description_input.clear(allocator); 3018 3019 const route = listRoute(self.data.identity, @fromBackingInt(@intCast(0)), "", &event_id_hex) orelse return; 3020 try self.session.navigate(route); 3021 } 3022 3023 // re-emit the selected thread's event with the form's content (status 3024 // preserved), then reload the thread's page. this is the terminal path; 3025 // the web posts the form to the edit route. 3026 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 3027 if (comptime wasm) return; 3028 const io = self.session.io orelse return; 3029 const entry = self.data.selectedThread() orelse return; 3030 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3031 const author = actor.author; 3032 3033 const form = self.threadForm() orelse return; 3034 const title_input = try formField(form, "title"); 3035 const labels_input = try formField(form, "labels"); 3036 const description_input = try formField(form, "description"); 3037 3038 const title = try title_input.text(allocator); 3039 defer allocator.free(title); 3040 const labels = try labels_input.text(allocator); 3041 defer allocator.free(labels); 3042 const description = try description_input.text(allocator); 3043 defer allocator.free(description); 3044 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 3045 defer if (comptime supports_drafts) allocator.free(target_branch); 3046 3047 if (!Event.fieldsValid(title, labels)) { 3048 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, labels, description, target_branch); 3049 return; 3050 } 3051 const src = self.data.repo_source orelse return; 3052 3053 if (comptime supports_drafts) { 3054 if (entryDraft(entry.*)) { 3055 self.data.editDraft(self.session, allocator, author, entry.id, title, labels, description, target_branch) catch |err| { 3056 const failure = FeedbackFailure.fromError(err) orelse return err; 3057 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 3058 return; 3059 }; 3060 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 3061 try self.session.navigate(route); 3062 return; 3063 } 3064 } 3065 3066 const id_bytes = try evt.parseEventId(entry.id); 3067 if (!try src.canModify(io, allocator, actor, kind, &id_bytes)) return; 3068 3069 switch (src.repo_kind) { 3070 inline else => |repo_kind| { 3071 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3072 defer any_repo.deinit(io, allocator); 3073 switch (any_repo) { 3074 inline else => |*repo| if (supports_drafts) { 3075 Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 3076 .title = title, 3077 .labels = labels, 3078 .description = description, 3079 .target_branch = target_branch, 3080 } }, author) catch |err| { 3081 const failure = FeedbackFailure.fromError(err) orelse return err; 3082 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 3083 return; 3084 }; 3085 } else if (has_status) 3086 try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 3087 .title = title, 3088 .labels = labels, 3089 .description = description, 3090 } }, author) 3091 else 3092 try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, labels, description, author), 3093 } 3094 }, 3095 } 3096 3097 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 3098 try self.session.navigate(route); 3099 } 3100 3101 fn rememberThreadFeedback(self: *This, failure: FeedbackFailure, title: []const u8, labels: []const u8, description: []const u8, target_branch: []const u8) !void { 3102 const aa = self.session.arena.allocator(); 3103 const source = if (supports_drafts) blk: { 3104 const form = self.threadForm() orelse break :blk null; 3105 break :blk formWidget(form, .patch_source); 3106 } else null; 3107 self.session.data.form_feedback = @unionInit(ui.Session.FormFeedback, @tagName(feedback_tag), .{ 3108 .failure = failure, 3109 .fields = if (comptime supports_drafts) .{ 3110 .title = try aa.dupe(u8, title), 3111 .labels = try aa.dupe(u8, labels), 3112 .description = try aa.dupe(u8, description), 3113 .target_branch = try aa.dupe(u8, target_branch), 3114 .source_branch = if (source) |value| try value.field().text(aa) else "", 3115 .source_kind = if (source) |value| value.selectedKind() else .fork, 3116 } else .{ 3117 .title = try aa.dupe(u8, title), 3118 .labels = try aa.dupe(u8, labels), 3119 .description = try aa.dupe(u8, description), 3120 }, 3121 }); 3122 } 3123 3124 fn submitConfirmation(self: *This, allocator: std.mem.Allocator, button_index: usize) !void { 3125 switch (confirmation(self.data.view) orelse return) { 3126 .publish => if (comptime supports_drafts) { 3127 if (comptime wasm) return; 3128 const entry = self.data.selectedThread() orelse return; 3129 if (!entryDraft(entry.*)) return; 3130 const author = ((try self.session.authorize(self.data.identity, .read, kind)) orelse return).author; 3131 try self.data.publishDraft(self.session, allocator, author, entry.id); 3132 const route = listRoute(self.data.identity, @fromBackingInt(@intCast(0)), "", entry.id) orelse return; 3133 try self.session.navigate(route); 3134 } else unreachable, 3135 .merge => if (comptime supports_merge) { 3136 if (comptime wasm) return; 3137 const entry = self.data.selectedThread() orelse return; 3138 const revision: evt.Patch.MergeRevision = if (button_index > 1) .squash else .source; 3139 if (!Data.canMerge(entry.*, self.session) or entry.mergeability.get(revision) != .clean) return; 3140 const author = ((try self.session.authorize(self.data.identity, .write, kind)) orelse return).author; 3141 self.data.mergePatch(self.session, allocator, author, entry.id, revision) catch |err| switch (err) { 3142 error.MergeConflict, error.MergeCheckUnavailable, error.PatchOutOfDate, error.PatchDataUnavailable => { 3143 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 3144 try self.session.navigate(route); 3145 return; 3146 }, 3147 else => return err, 3148 }; 3149 const merged = std.meta.stringToEnum(Status, "merged") orelse return; 3150 const route = listRoute(self.data.identity, merged, "", entry.id) orelse return; 3151 try self.session.navigate(route); 3152 } else unreachable, 3153 .remove => try self.submitRemove(allocator), 3154 } 3155 } 3156 3157 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 3158 const comment = self.data.comment_id.len != 0; 3159 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 3160 const id = evt.parseEventId(event_id) catch return; 3161 try self.removeEvent(allocator, if (comment) .comment else kind, id); 3162 } 3163 3164 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 3165 if (comptime wasm) return; 3166 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3167 const author = actor.author; 3168 3169 if (comptime supports_drafts) { 3170 const selected = self.data.selectedThread() orelse return; 3171 if (event_kind == kind and entryDraft(selected.*)) { 3172 try Data.removeDraft(self.session, allocator, author, &id); 3173 const route = draftsRoute(self.data.identity) orelse return; 3174 try self.session.navigate(route); 3175 return; 3176 } 3177 } 3178 3179 const io = self.session.io orelse return; 3180 const src = self.data.repo_source orelse return; 3181 if (!try src.canModify(io, allocator, actor, event_kind, &id)) return; 3182 const thread_id = evt.parseEventId(self.data.selected_id) catch return; 3183 3184 switch (src.repo_kind) { 3185 inline else => |repo_kind| { 3186 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3187 defer any_repo.deinit(io, allocator); 3188 switch (any_repo) { 3189 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), 3190 } 3191 }, 3192 } 3193 3194 const id_hex = std.fmt.bytesToHex(id, .lower); 3195 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex, null) orelse return; 3196 try self.session.navigate(route); 3197 } 3198 3199 // return to the list. 3200 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 3201 root_focus.setFocus(self.listScroll(index).getFocus().id); 3202 } 3203 3204 // cross to the header tabs above the stack. 3205 fn focusHeader(self: *This, root_focus: *Focus) void { 3206 root_focus.setFocus(self.box.children.keys()[header_index]); 3207 } 3208 3209 pub fn clearGrid(self: *This) void { 3210 self.box.clearGrid(); 3211 } 3212 3213 pub fn getGrid(self: This) ?Grid { 3214 return self.box.getGrid(); 3215 } 3216 3217 pub fn getFocus(self: *This) *Focus { 3218 return self.box.getFocus(); 3219 } 3220 3221 // for the parent's "scroll up at the top jumps to the header" check: at the 3222 // top only while the header tabs hold focus, so up from the split 3223 // crosses into the tabs first. 3224 pub fn atTop(self: *This) bool { 3225 return self.headerActive(); 3226 } 3227 }; 3228 } 3229