╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2143 if (failure == .invalid_target_branch) " target branch (invalid) " else if (self.data.view == .edit and failure == .same_branch) " target branch (same as source) " else if (self.data.view == .edit and failure == .unrelated_branches) " target branch (unrelated to source) " else if (self.data.view == .edit and failure == .invalid_source_branch) " target branch (source branch not found) " else " target branch "; 2144 } 2145 } 2146 if (self.formBox()) |form| { 2147 const inputs_arena = self.session.arena.allocator(); 2148 for (form.children.values()) |*child| switch (child.widget) { 2149 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 2150 else => {}, 2151 }; 2152 } 2153 2154 try self.box.build(allocator, constraint, root_focus); 2155 } 2156 2157 pub fn input(self: *This, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !void { 2158 // wheel input moves focus like the arrows 2159 const key: Key = if (raw_key == .mouse and raw_key.mouse.action == .scroll) 2160 (if (raw_key.mouse.action.scroll == .up) .arrow_up else .arrow_down) 2161 else 2162 raw_key; 2163 2164 if (self.headerActive()) { 2165 // down from the tabs re-enters the stack if the selected view has 2166 // something to focus; other keys move the tabs. 2167 if (inp.vertDirection(key) == .down) { 2168 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 2169 .tag_flow => |*tf| tf.text_boxes.items.len > 0, 2170 // nothing focusable in the unauthorized view 2171 .unauthorized => false, 2172 else => true, 2173 } else false; 2174 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 2175 } else { 2176 try self.header().input(allocator, key, root_focus); 2177 } 2178 return; 2179 } 2180 if (self.tagsViewActive()) { 2181 self.tagsViewInput(key, root_focus); 2182 return; 2183 } 2184 if (self.formViewActive()) { 2185 if (confirmation(self.data.view) != null) { 2186 try self.confirmationFormInput(allocator, key, root_focus); 2187 } else if (supports_conflicts and self.data.view == .resolve) { 2188 try self.resolveInput(allocator, key, root_focus); 2189 } else { 2190 try self.formInput(allocator, key, root_focus); 2191 } 2192 return; 2193 } 2194 const i = self.selectedSplitIndex() orelse return; 2195 if (self.detailActive(i)) { 2196 switch (try self.detail(i).inputWithExit(allocator, key, root_focus)) { 2197 .none => {}, 2198 .list => self.focusList(i, root_focus), 2199 .header => self.focusHeader(root_focus), 2200 } 2201 } else { 2202 self.listInput(i, key, root_focus); 2203 } 2204 } 2205 2206 // arrow keys move the tag selection; up from the top row crosses to the 2207 // header tabs. 2208 fn tagsViewInput(self: *This, key: Key, root_focus: *Focus) void { 2209 const tf = self.tagsView(); 2210 const cid = tf.focus.child_id orelse return; 2211 const cur = tf.indexOfFocusId(cid) orelse return; 2212 const count = tf.text_boxes.items.len; 2213 switch (key) { 2214 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 2215 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 2216 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 2217 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 2218 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 2219 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 2220 else => {}, 2221 } 2222 } 2223 2224 fn listInput(self: *This, index: usize, key: Key, root_focus: *Focus) void { 2225 // up/down (and the scroll wheel) move the selection a row; page up/down 2226 // jump a fixed amount. right/Enter cross into the detail pane. up from 2227 // the top row crosses into the header tabs. 2228 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 2229 const lb = self.listBox(index); 2230 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 2231 if (delta < 0 and at_top) return self.focusHeader(root_focus); 2232 moveRowFocus(lb, self.listScroll(index), root_focus, delta); 2233 return; 2234 } 2235 switch (key) { 2236 .enter, .arrow_right => _ = self.detail(index).focusFirst(root_focus), 2237 else => {}, 2238 } 2239 } 2240 2241 fn formInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2242 const form = self.formBox() orelse return; 2243 const cid = form.getFocus().child_id orelse return; 2244 const cur = form.children.getIndex(cid) orelse return; 2245 const child = &form.children.values()[cur]; 2246 2247 if (child.widget == .text_input) { 2248 const text_input = &child.widget.text_input; 2249 if (text_input.options.multiline and try multilineKeepsKey(text_input, allocator, key)) 2250 return child.widget.input(allocator, key, root_focus); 2251 } 2252 2253 const on_submit = child.widget == .submit_button; 2254 switch (key) { 2255 .arrow_up, .back_tab => return if (formStep(form, cur, false)) |i| 2256 root_focus.setFocus(form.children.keys()[i]) 2257 else 2258 self.focusHeader(root_focus), 2259 .arrow_down, .tab => return if (formStep(form, cur, true)) |i| root_focus.setFocus(form.children.keys()[i]), 2260 .enter => if (on_submit) return self.submitForm(allocator), 2261 .mouse => |mouse| if (on_submit) { 2262 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) return self.submitForm(allocator); 2263 }, 2264 else => {}, 2265 } 2266 try child.widget.input(allocator, key, root_focus); 2267 } 2268 2269 fn confirmationFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2270 const form = self.confirmationForm() orelse return; 2271 const child_id = form.getFocus().child_id orelse return; 2272 const index = form.children.getIndex(child_id) orelse return; 2273 switch (key) { 2274 .arrow_up, .back_tab => if (index > 1) 2275 root_focus.setFocus(form.children.keys()[index - 1]) 2276 else 2277 self.focusHeader(root_focus), 2278 .arrow_down, .tab => if (index + 1 < form.children.count()) root_focus.setFocus(form.children.keys()[index + 1]), 2279 .enter => try self.submitConfirmation(allocator, index), 2280 .mouse => |mouse| for (form.children.keys()[1..], 1..) |button_id, button_index| { 2281 if (inp.leftClickOn(root_focus, button_id, mouse)) { 2282 try self.submitConfirmation(allocator, button_index); 2283 break; 2284 } 2285 }, 2286 else => {}, 2287 } 2288 } 2289 2290 // the multiline hunk inputs keep enter and any up/down that has a row to 2291 // move to; enter on a "use this" link is a navigation the host follows 2292 // before this runs. 2293 fn resolveInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2294 const form = self.formBox() orelse return; 2295 const cid = form.getFocus().child_id orelse return; 2296 const cur = form.children.getIndex(cid) orelse return; 2297 const child = &form.children.values()[cur]; 2298 2299 if (child.widget == .text_input) { 2300 const ti = &child.widget.text_input; 2301 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 2302 return child.widget.input(allocator, key, root_focus); 2303 } 2304 2305 const on_submit = child.widget == .submit_button; 2306 2307 switch (key) { 2308 .arrow_up, .back_tab => if (formStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 2309 .arrow_down, .tab => if (formStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 2310 .arrow_left, .arrow_right => switch (child.widget) { 2311 .box => |*row| { 2312 const row_cid = row.getFocus().child_id orelse return; 2313 const row_index = row.children.getIndex(row_cid) orelse return; 2314 if (key == .arrow_left) { 2315 if (row_index > 0) root_focus.setFocus(row.children.keys()[row_index - 1]); 2316 } else if (row_index + 1 < row.children.count()) { 2317 root_focus.setFocus(row.children.keys()[row_index + 1]); 2318 } 2319 }, 2320 else => try child.widget.input(allocator, key, root_focus), 2321 }, 2322 .enter => if (on_submit) try self.submitResolution(allocator), 2323 .mouse => |mouse| if (on_submit) { 2324 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 2325 }, 2326 else => try child.widget.input(allocator, key, root_focus), 2327 } 2328 } 2329 2330 // a multiline input keeps enter and any up/down that has a row to move to 2331 fn multilineKeepsKey(text_input: *wgt.TextInput, allocator: std.mem.Allocator, key: Key) !bool { 2332 return switch (key) { 2333 .enter => true, 2334 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 2335 .arrow_down => !try text_input.cursorOnLastRow(allocator), 2336 else => false, 2337 }; 2338 } 2339 2340 // the neighboring form control, skipping spacers and blank rows 2341 fn formStep(form: *wgt.Box(Widget), cur: usize, down: bool) ?usize { 2342 var i = cur; 2343 while (true) { 2344 if (down) { 2345 i += 1; 2346 if (i >= form.children.count()) return null; 2347 } else { 2348 if (i == 0) return null; 2349 i -= 1; 2350 } 2351 const child = &form.children.values()[i]; 2352 if (child.hidden) continue; 2353 switch (child.widget) { 2354 .spacer, .text => continue, 2355 else => return i, 2356 } 2357 } 2358 } 2359 2360 // focus the resolve-form child at `i` (a version row focuses its selected 2361 // child) and keep it visible on the terminal 2362 fn focusResolveChild(self: *This, form: *wgt.Box(Widget), i: usize, root_focus: *Focus) void { 2363 const child = &form.children.values()[i]; 2364 switch (child.widget) { 2365 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2366 else => root_focus.setFocus(form.children.keys()[i]), 2367 } 2368 if (self.session.is_terminal) { 2369 if (self.resolveScroll()) |sc| { 2370 if (child.rect) |rect| sc.scrollToRect(rect); 2371 } 2372 } 2373 } 2374 2375 // re-emit the conflicted thread's event with the resolved content; any 2376 // event on the thread settles its conflict entry. this is the terminal 2377 // path; the web posts the form to the resolve route. 2378 fn submitResolution(self: *This, allocator: std.mem.Allocator) !void { 2379 if (comptime wasm) return; 2380 const io = self.session.io orelse return; 2381 const src = self.data.repo_source orelse return; 2382 const entry = self.data.selectedThread() orelse return; 2383 const author = ((try self.session.authorize(self.data.identity, .write)) orelse return).author; 2384 const form = self.formBox() orelse return; 2385 2386 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2387 var title: ?[]u8 = null; 2388 var tags: ?[]u8 = null; 2389 var hunks: std.ArrayList([]const u8) = .empty; 2390 defer { 2391 if (title) |t| allocator.free(t); 2392 if (tags) |t| allocator.free(t); 2393 for (hunks.items) |h| allocator.free(h); 2394 hunks.deinit(allocator); 2395 } 2396 for (form.children.values()) |*child| switch (child.widget) { 2397 .text_input => |*ti| { 2398 const text = try ti.text(allocator); 2399 errdefer allocator.free(text); 2400 if (std.mem.eql(u8, ti.options.name, "title")) { 2401 title = text; 2402 } else if (std.mem.eql(u8, ti.options.name, "tags")) { 2403 tags = text; 2404 } else { 2405 try hunks.append(allocator, text); 2406 } 2407 }, 2408 else => {}, 2409 }; 2410 2411 const id_bytes = try evt.parseEventId(entry.id); 2412 2413 switch (src.repo_kind) { 2414 inline else => |repo_kind| { 2415 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2416 defer any_repo.deinit(io, allocator); 2417 switch (any_repo) { 2418 inline else => |*repo| { 2419 var resolution: Event.Resolve = .{ 2420 .title = title, 2421 .tags = tags, 2422 .hunks = hunks.items, 2423 }; 2424 if (comptime @hasField(Event.Resolve, "theirs")) resolution.theirs = self.data.theirs_picks; 2425 const change = Event.Update{ .resolve = resolution }; 2426 Event.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, change, author) catch |err| switch (err) { 2427 // leave the form up for correction 2428 error.InvalidFields => return, 2429 else => |e| return e, 2430 }; 2431 }, 2432 } 2433 }, 2434 } 2435 2436 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2437 try self.session.navigate(route); 2438 } 2439 2440 // commit a new or edited comment and navigate to its permalink. this is the 2441 // terminal path; the web posts the form to its comment route. 2442 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 2443 if (comptime wasm) return; 2444 const io = self.session.io orelse return; 2445 const src = self.data.repo_source orelse return; 2446 const actor = (try self.session.authorize(self.data.identity, .read)) orelse return; 2447 const author = actor.author; 2448 const form = self.formBox() orelse return; 2449 const body_input = try formField(form, "body"); 2450 const body = try body_input.text(allocator); 2451 defer allocator.free(body); 2452 if (!evt.Comment.fieldsValid(body)) return; 2453 2454 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2455 @memcpy(&thread_id, self.data.selected_id); 2456 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2457 var parent: [evt.event_id_size * 2]u8 = undefined; 2458 @memcpy(&parent, self.data.comment_id); 2459 break :blk parent; 2460 }; 2461 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2462 const editing = self.data.view == .edit_comment; 2463 if (editing) { 2464 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2465 if (!try src.canModify(io, allocator, actor, .comment, &comment_id)) return; 2466 } 2467 switch (src.repo_kind) { 2468 inline else => |repo_kind| { 2469 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2470 defer any_repo.deinit(io, allocator); 2471 switch (any_repo) { 2472 inline else => |*repo| if (editing) { 2473 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2474 try evt.Comment.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &comment_id, body, author); 2475 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 2476 } else { 2477 event_id_hex = try evt.Comment.create(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &parent_id, body, author); 2478 }, 2479 } 2480 }, 2481 } 2482 2483 body_input.clear(allocator); 2484 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2485 try self.session.navigate(route); 2486 } 2487 2488 // terminal form submissions; the web posts to the forms' routes instead 2489 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 2490 switch (self.data.view) { 2491 .new_comment, .edit_comment => try self.submitComment(allocator), 2492 .edit => try self.submitEditedThread(allocator), 2493 else => try self.submitNewThread(allocator), 2494 } 2495 } 2496 2497 // commit the new thread to the repo's events branch and navigate to it. 2498 // this is the terminal path; the web posts the form to the thread route, 2499 // so the wasm side never runs (or compiles) the repo access below. 2500 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 2501 if (comptime wasm) return; 2502 const io = self.session.io orelse return; 2503 const src = self.data.repo_source orelse return; 2504 const author = ((try self.session.authorize(self.data.identity, .read)) orelse return).author; 2505 2506 const form = self.threadForm() orelse return; 2507 const title_input = try formField(form, "title"); 2508 const tags_input = try formField(form, "tags"); 2509 const description_input = try formField(form, "description"); 2510 2511 const title = try title_input.text(allocator); 2512 defer allocator.free(title); 2513 const tags = try tags_input.text(allocator); 2514 defer allocator.free(tags); 2515 const description = try description_input.text(allocator); 2516 defer allocator.free(description); 2517 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 2518 defer if (comptime supports_drafts) allocator.free(target_branch); 2519 2520 if (!Event.fieldsValid(title, tags)) { 2521 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, tags, description, target_branch); 2522 return; 2523 } 2524 2525 if (comptime supports_drafts) { 2526 const source = formWidget(form, .patch_source) orelse return error.MissingFormField; 2527 const source_branch = switch (source.selected) { 2528 .fork => null, 2529 .branch => try source.field().text(allocator), 2530 }; 2531 defer if (source_branch) |branch| allocator.free(branch); 2532 const event_id_hex = Data.create(self.data, self.session, allocator, author, title, tags, description, target_branch, source_branch) catch |err| { 2533 const failure = FeedbackFailure.fromError(err) orelse return err; 2534 try self.rememberThreadFeedback(failure, title, tags, description, target_branch); 2535 return; 2536 }; 2537 title_input.clear(allocator); 2538 tags_input.clear(allocator); 2539 description_input.clear(allocator); 2540 (try formField(form, "target_branch")).clear(allocator); 2541 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 2542 try self.session.navigate(route); 2543 return; 2544 } 2545 2546 var id_bytes: [evt.event_id_size]u8 = undefined; 2547 io.random(&id_bytes); 2548 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2549 2550 const event = evt.EventWithId{ 2551 .id = event_id_hex, 2552 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2553 .author = author, 2554 .event = @unionInit(evt.Event, @tagName(kind), Event{ 2555 .title = title, 2556 .description = description, 2557 .tags = tags, 2558 }), 2559 }; 2560 2561 switch (src.repo_kind) { 2562 inline else => |repo_kind| { 2563 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2564 defer any_repo.deinit(io, allocator); 2565 switch (any_repo) { 2566 inline else => |*repo| try evt.consume(self.session.data.host_kind, .repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2567 } 2568 }, 2569 } 2570 2571 // wipe the form so a return visit starts fresh 2572 title_input.clear(allocator); 2573 tags_input.clear(allocator); 2574 description_input.clear(allocator); 2575 2576 const route = listRoute(self.data.identity, @enumFromInt(0), "", &event_id_hex) orelse return; 2577 try self.session.navigate(route); 2578 } 2579 2580 // re-emit the selected thread's event with the form's content (status 2581 // preserved), then reload the thread's page. this is the terminal path; 2582 // the web posts the form to the edit route. 2583 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 2584 if (comptime wasm) return; 2585 const io = self.session.io orelse return; 2586 const entry = self.data.selectedThread() orelse return; 2587 const actor = (try self.session.authorize(self.data.identity, .read)) orelse return; 2588 const author = actor.author; 2589 2590 const form = self.threadForm() orelse return; 2591 const title_input = try formField(form, "title"); 2592 const tags_input = try formField(form, "tags"); 2593 const description_input = try formField(form, "description"); 2594 2595 const title = try title_input.text(allocator); 2596 defer allocator.free(title); 2597 const tags = try tags_input.text(allocator); 2598 defer allocator.free(tags); 2599 const description = try description_input.text(allocator); 2600 defer allocator.free(description); 2601 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 2602 defer if (comptime supports_drafts) allocator.free(target_branch); 2603 2604 if (!Event.fieldsValid(title, tags)) { 2605 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, tags, description, target_branch); 2606 return; 2607 } 2608 const src = self.data.repo_source orelse return; 2609 2610 if (comptime supports_drafts) { 2611 if (entryDraft(entry.*)) { 2612 self.data.editDraft(self.session, allocator, author, entry.id, title, tags, description, target_branch) catch |err| { 2613 const failure = FeedbackFailure.fromError(err) orelse return err; 2614 try self.rememberThreadFeedback(failure, title, tags, description, target_branch); 2615 return; 2616 }; 2617 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2618 try self.session.navigate(route); 2619 return; 2620 } 2621 } 2622 2623 const id_bytes = try evt.parseEventId(entry.id); 2624 if (!try src.canModify(io, allocator, actor, kind, &id_bytes)) return; 2625 2626 switch (src.repo_kind) { 2627 inline else => |repo_kind| { 2628 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2629 defer any_repo.deinit(io, allocator); 2630 switch (any_repo) { 2631 inline else => |*repo| if (supports_drafts) { 2632 Event.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2633 .title = title, 2634 .tags = tags, 2635 .description = description, 2636 .target_branch = target_branch, 2637 } }, author) catch |err| { 2638 const failure = FeedbackFailure.fromError(err) orelse return err; 2639 try self.rememberThreadFeedback(failure, title, tags, description, target_branch); 2640 return; 2641 }; 2642 } else if (has_status) 2643 try Event.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2644 .title = title, 2645 .tags = tags, 2646 .description = description, 2647 } }, author) 2648 else 2649 try Event.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, tags, description, author), 2650 } 2651 }, 2652 } 2653 2654 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2655 try self.session.navigate(route); 2656 } 2657 2658 fn rememberThreadFeedback(self: *This, failure: FeedbackFailure, title: []const u8, tags: []const u8, description: []const u8, target_branch: []const u8) !void { 2659 const aa = self.session.arena.allocator(); 2660 const source = if (supports_drafts) blk: { 2661 const form = self.threadForm() orelse break :blk null; 2662 break :blk formWidget(form, .patch_source); 2663 } else null; 2664 self.session.data.form_feedback = @unionInit(ui.Session.FormFeedback, @tagName(feedback_tag), .{ 2665 .failure = failure, 2666 .fields = if (comptime supports_drafts) .{ 2667 .title = try aa.dupe(u8, title), 2668 .tags = try aa.dupe(u8, tags), 2669 .description = try aa.dupe(u8, description), 2670 .target_branch = try aa.dupe(u8, target_branch), 2671 .source_branch = if (source) |value| try value.field().text(aa) else "", 2672 .source_kind = if (source) |value| value.selected else .fork, 2673 } else .{ 2674 .title = try aa.dupe(u8, title), 2675 .tags = try aa.dupe(u8, tags), 2676 .description = try aa.dupe(u8, description), 2677 }, 2678 }); 2679 } 2680 2681 fn submitConfirmation(self: *This, allocator: std.mem.Allocator, button_index: usize) !void { 2682 switch (confirmation(self.data.view) orelse return) { 2683 .publish => if (comptime supports_drafts) { 2684 if (comptime wasm) return; 2685 const entry = self.data.selectedThread() orelse return; 2686 if (!entryDraft(entry.*)) return; 2687 const author = ((try self.session.authorize(self.data.identity, .read)) orelse return).author; 2688 try self.data.publishDraft(self.session, allocator, author, entry.id); 2689 const route = listRoute(self.data.identity, @enumFromInt(0), "", entry.id) orelse return; 2690 try self.session.navigate(route); 2691 } else unreachable, 2692 .merge => if (comptime supports_merge) { 2693 if (comptime wasm) return; 2694 const entry = self.data.selectedThread() orelse return; 2695 const revision: evt.Patch.MergeRevision = if (button_index > 1) .squash else .source; 2696 if (!Data.canMerge(entry.*, self.session) or entry.mergeability.get(revision) != .clean) return; 2697 const author = ((try self.session.authorize(self.data.identity, .write)) orelse return).author; 2698 self.data.mergePatch(self.session, allocator, author, entry.id, revision) catch |err| switch (err) { 2699 error.MergeConflict, error.MergeCheckUnavailable, error.PatchOutOfDate, error.PatchDataUnavailable => { 2700 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2701 try self.session.navigate(route); 2702 return; 2703 }, 2704 else => return err, 2705 }; 2706 const merged = std.meta.stringToEnum(Status, "merged") orelse return; 2707 const route = listRoute(self.data.identity, merged, "", entry.id) orelse return; 2708 try self.session.navigate(route); 2709 } else unreachable, 2710 .remove => try self.submitRemove(allocator), 2711 } 2712 } 2713 2714 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 2715 const comment = self.data.comment_id.len != 0; 2716 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 2717 const id = evt.parseEventId(event_id) catch return; 2718 try self.removeEvent(allocator, if (comment) .comment else kind, id); 2719 } 2720 2721 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 2722 if (comptime wasm) return; 2723 const actor = (try self.session.authorize(self.data.identity, .read)) orelse return; 2724 const author = actor.author; 2725 2726 if (comptime supports_drafts) { 2727 const selected = self.data.selectedThread() orelse return; 2728 if (event_kind == kind and entryDraft(selected.*)) { 2729 try Data.removeDraft(self.session, allocator, author, &id); 2730 const route = draftsRoute(self.data.identity) orelse return; 2731 try self.session.navigate(route); 2732 return; 2733 } 2734 } 2735 2736 const io = self.session.io orelse return; 2737 const src = self.data.repo_source orelse return; 2738 if (!try src.canModify(io, allocator, actor, event_kind, &id)) return; 2739 2740 switch (src.repo_kind) { 2741 inline else => |repo_kind| { 2742 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2743 defer any_repo.deinit(io, allocator); 2744 switch (any_repo) { 2745 inline else => |*repo| try evt.remove(self.session.data.host_kind, .repo, repo_kind, repo.self_repo_opts, io, allocator, repo, &id, event_kind, author), 2746 } 2747 }, 2748 } 2749 2750 const id_hex = std.fmt.bytesToHex(id, .lower); 2751 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex) orelse return; 2752 try self.session.navigate(route); 2753 } 2754 2755 // return to the list. 2756 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 2757 root_focus.setFocus(self.listScroll(index).getFocus().id); 2758 } 2759 2760 // cross to the header tabs above the stack. 2761 fn focusHeader(self: *This, root_focus: *Focus) void { 2762 root_focus.setFocus(self.box.children.keys()[header_index]); 2763 } 2764 2765 pub fn clearGrid(self: *This) void { 2766 self.box.clearGrid(); 2767 } 2768 2769 pub fn getGrid(self: This) ?Grid { 2770 return self.box.getGrid(); 2771 } 2772 2773 pub fn getFocus(self: *This) *Focus { 2774 return self.box.getFocus(); 2775 } 2776 2777 // for the parent's "scroll up at the top jumps to the header" check: at the 2778 // top only while the header tabs hold focus, so up from the split 2779 // crosses into the tabs first. 2780 pub fn atTop(self: *This) bool { 2781 return self.headerActive(); 2782 } 2783 }; 2784 } 2785