╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2298 } 2299 } 2300 2301 // cap the list at list_max_width only while the detail pane fits 2302 // beside it. the box drops the detail when the width can't hold 2303 // both minimums, so when it's that narrow we lift the cap and let 2304 // the list fill the whole width. 2305 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 2306 self.resultsBox(i).children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 2307 2308 // stretch the detail pane across the rest of the width so it fills 2309 // the area rather than shrinking to its content; its scroll fills 2310 // the pane. 2311 if (constraint.max_size.width) |w| { 2312 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 2313 } else { 2314 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 2315 } 2316 } 2317 2318 // refresh the form inputs' entries in the session's focus-id -> input 2319 // map with this frame's addresses, so the web/wasm form handling can 2320 // find them by focus id 2321 if (self.threadForm()) |form| { 2322 const failure = if (self.session.formFeedback(feedback_tag)) |saved| saved.failure else null; 2323 (try formField(form, "title")).options.top_label.text = if (failure == .required_title) " title (required) " else " title "; 2324 if (comptime supports_drafts) { 2325 if (formWidget(form, .patch_source)) |source| { 2326 source.field().options.top_label.text = if (failure == .invalid_source_branch) " source branch (not found) " else if (failure == .same_branch) " source branch (same as target) " else if (failure == .unrelated_branches) " source branch (unrelated) " else " source branch "; 2327 const submit = formWidget(form, .submit_button) orelse return error.MissingFormField; 2328 try submit.setLabel(allocator, switch (source.selectedKind()) { 2329 .fork => "submit draft", 2330 .branch => "submit patch", 2331 }); 2332 } 2333 (try formField(form, "target_branch")).options.top_label.text = 2334 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 "; 2335 } 2336 } 2337 if (self.formBox()) |form| { 2338 const inputs_arena = self.session.arena.allocator(); 2339 for (form.children.values()) |*child| switch (child.widget) { 2340 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 2341 else => {}, 2342 }; 2343 } 2344 2345 try self.box.build(allocator, constraint, root_focus); 2346 } 2347 2348 pub fn input(self: *This, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !void { 2349 // wheel input moves focus like the arrows 2350 const key: Key = if (raw_key == .mouse and raw_key.mouse.action == .scroll) 2351 (if (raw_key.mouse.action.scroll == .up) .arrow_up else .arrow_down) 2352 else 2353 raw_key; 2354 2355 if (self.headerActive()) { 2356 // down from the sub-header re-enters the stack if the selected 2357 // view has something to focus; enter in the search box submits 2358 // the query; other keys move within the sub-header. 2359 if (inp.vertDirection(key) == .down) { 2360 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 2361 .word_flow => |*tf| tf.text_boxes.items.len > 0, 2362 // nothing focusable in the unauthorized view 2363 .unauthorized => false, 2364 else => true, 2365 } else false; 2366 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 2367 } else if (key == .enter) { 2368 if (try self.header().submittedText(allocator)) |text| { 2369 defer allocator.free(text); 2370 try self.submitSearch(text); 2371 } 2372 } else { 2373 try self.header().input(allocator, key, root_focus); 2374 } 2375 return; 2376 } 2377 if (self.labelsViewActive()) { 2378 self.labelsViewInput(key, root_focus); 2379 return; 2380 } 2381 if (self.formViewActive()) { 2382 if (confirmation(self.data.view) != null) { 2383 try self.confirmationFormInput(allocator, key, root_focus); 2384 } else if (supports_conflicts and self.data.view == .resolve) { 2385 try self.resolveInput(allocator, key, root_focus); 2386 } else { 2387 try self.formInput(allocator, key, root_focus); 2388 } 2389 return; 2390 } 2391 const i = self.selectedSplitIndex() orelse return; 2392 if (self.detailActive(i)) { 2393 switch (try self.detail(i).inputWithExit(allocator, key, root_focus)) { 2394 .none => {}, 2395 .list => self.focusList(i, root_focus), 2396 .header => self.focusHeader(root_focus), 2397 } 2398 } else { 2399 self.listInput(i, key, root_focus); 2400 } 2401 } 2402 2403 // navigate to the typed query's results, keeping the label filter and the 2404 // status list the page shows. an empty box clears a filtered page and 2405 // does nothing on any other one. 2406 fn submitSearch(self: *This, text: []const u8) !void { 2407 if (text.len == 0 and self.data.search == null) return; 2408 const selected = self.header().getSelectedIndex() orelse 0; 2409 const status: Status = if (selected < status_count) splitStatus(selected) else @fromBackingInt(@intCast(0)); 2410 const route = listRoute(self.data.identity, status, self.data.label, "") orelse return; 2411 try self.session.navigate(route.withSearch(text) orelse return); 2412 } 2413 2414 // arrow keys move the label selection; up from the top row crosses to the 2415 // header tabs. 2416 fn labelsViewInput(self: *This, key: Key, root_focus: *Focus) void { 2417 const tf = self.labelsView(); 2418 const cid = tf.focus.child_id orelse return; 2419 const cur = tf.indexOfFocusId(cid) orelse return; 2420 const count = tf.text_boxes.items.len; 2421 switch (key) { 2422 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 2423 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 2424 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 2425 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 2426 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 2427 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 2428 else => {}, 2429 } 2430 } 2431 2432 fn listInput(self: *This, index: usize, key: Key, root_focus: *Focus) void { 2433 // up/down (and the scroll wheel) move the selection a row; page up/down 2434 // jump a fixed amount. right/Enter cross into the detail pane. up from 2435 // the top row crosses into the header tabs. 2436 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 2437 const lb = self.listBox(index); 2438 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 2439 if (delta < 0 and at_top) return self.focusHeader(root_focus); 2440 moveRowFocus(lb, self.listScroll(index), root_focus, delta); 2441 return; 2442 } 2443 switch (key) { 2444 .enter, .arrow_right => _ = self.detail(index).focusFirst(root_focus), 2445 else => {}, 2446 } 2447 } 2448 2449 fn formInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2450 const form = self.formBox() orelse return; 2451 const cid = form.getFocus().child_id orelse return; 2452 const cur = form.children.getIndex(cid) orelse return; 2453 const child = &form.children.values()[cur]; 2454 2455 if (child.widget == .text_input) { 2456 const text_input = &child.widget.text_input; 2457 if (text_input.options.multiline and try multilineKeepsKey(text_input, allocator, key)) 2458 return child.widget.input(allocator, key, root_focus); 2459 } 2460 2461 const on_submit = child.widget == .submit_button; 2462 switch (key) { 2463 .arrow_up, .back_tab => return if (formStep(form, cur, false)) |i| 2464 root_focus.setFocus(form.children.keys()[i]) 2465 else 2466 self.focusHeader(root_focus), 2467 .arrow_down, .tab => return if (formStep(form, cur, true)) |i| root_focus.setFocus(form.children.keys()[i]), 2468 .enter => if (on_submit) return self.submitForm(allocator), 2469 .mouse => |mouse| if (on_submit) { 2470 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) return self.submitForm(allocator); 2471 }, 2472 else => {}, 2473 } 2474 try child.widget.input(allocator, key, root_focus); 2475 } 2476 2477 fn confirmationFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2478 const form = self.confirmationForm() orelse return; 2479 const child_id = form.getFocus().child_id orelse return; 2480 const index = form.children.getIndex(child_id) orelse return; 2481 switch (key) { 2482 .arrow_up, .back_tab => if (index > 1) 2483 root_focus.setFocus(form.children.keys()[index - 1]) 2484 else 2485 self.focusHeader(root_focus), 2486 .arrow_down, .tab => if (index + 1 < form.children.count()) root_focus.setFocus(form.children.keys()[index + 1]), 2487 .enter => try self.submitConfirmation(allocator, index), 2488 .mouse => |mouse| for (form.children.keys()[1..], 1..) |button_id, button_index| { 2489 if (inp.leftClickOn(root_focus, button_id, mouse)) { 2490 try self.submitConfirmation(allocator, button_index); 2491 break; 2492 } 2493 }, 2494 else => {}, 2495 } 2496 } 2497 2498 // the multiline hunk inputs keep enter and any up/down that has a row to 2499 // move to; enter on a "use this" link is a navigation the host follows 2500 // before this runs. 2501 fn resolveInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2502 const form = self.formBox() orelse return; 2503 const cid = form.getFocus().child_id orelse return; 2504 const cur = form.children.getIndex(cid) orelse return; 2505 const child = &form.children.values()[cur]; 2506 2507 if (child.widget == .text_input) { 2508 const ti = &child.widget.text_input; 2509 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 2510 return child.widget.input(allocator, key, root_focus); 2511 } 2512 2513 const on_submit = child.widget == .submit_button; 2514 2515 switch (key) { 2516 .arrow_up, .back_tab => if (formStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 2517 .arrow_down, .tab => if (formStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 2518 .arrow_left, .arrow_right => switch (child.widget) { 2519 .box => |*row| _ = widget.moveInRow(row, root_focus, key == .arrow_right), 2520 else => try child.widget.input(allocator, key, root_focus), 2521 }, 2522 .enter => if (on_submit) try self.submitResolution(allocator), 2523 .mouse => |mouse| if (on_submit) { 2524 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 2525 }, 2526 else => try child.widget.input(allocator, key, root_focus), 2527 } 2528 } 2529 2530 // a multiline input keeps enter and any up/down that has a row to move to 2531 fn multilineKeepsKey(text_input: *wgt.TextInput, allocator: std.mem.Allocator, key: Key) !bool { 2532 return switch (key) { 2533 .enter => true, 2534 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 2535 .arrow_down => !try text_input.cursorOnLastRow(allocator), 2536 else => false, 2537 }; 2538 } 2539 2540 // the neighboring form control, skipping spacers and blank rows 2541 fn formStep(form: *wgt.Box(Widget), cur: usize, down: bool) ?usize { 2542 var i = cur; 2543 while (true) { 2544 if (down) { 2545 i += 1; 2546 if (i >= form.children.count()) return null; 2547 } else { 2548 if (i == 0) return null; 2549 i -= 1; 2550 } 2551 const child = &form.children.values()[i]; 2552 if (child.hidden) continue; 2553 switch (child.widget) { 2554 .spacer, .text => continue, 2555 else => return i, 2556 } 2557 } 2558 } 2559 2560 // focus the resolve-form child at `i` (a version row focuses its selected 2561 // child) and keep it visible on the terminal 2562 fn focusResolveChild(self: *This, form: *wgt.Box(Widget), i: usize, root_focus: *Focus) void { 2563 const child = &form.children.values()[i]; 2564 switch (child.widget) { 2565 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2566 else => root_focus.setFocus(form.children.keys()[i]), 2567 } 2568 if (self.session.is_terminal) { 2569 if (self.resolveScroll()) |sc| { 2570 if (child.rect) |rect| sc.scrollToRect(rect); 2571 } 2572 } 2573 } 2574 2575 // re-emit the conflicted thread's event with the resolved content; any 2576 // event on the thread settles its conflict entry. this is the terminal 2577 // path; the web posts the form to the resolve route. 2578 fn submitResolution(self: *This, allocator: std.mem.Allocator) !void { 2579 if (comptime wasm) return; 2580 const io = self.session.io orelse return; 2581 const src = self.data.repo_source orelse return; 2582 const entry = self.data.selectedThread() orelse return; 2583 const author = ((try self.session.authorize(self.data.identity, .write, kind)) orelse return).author; 2584 const form = self.formBox() orelse return; 2585 2586 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2587 var title: ?[]u8 = null; 2588 var labels: ?[]u8 = null; 2589 var hunks: std.ArrayList([]const u8) = .empty; 2590 defer { 2591 if (title) |t| allocator.free(t); 2592 if (labels) |t| allocator.free(t); 2593 for (hunks.items) |h| allocator.free(h); 2594 hunks.deinit(allocator); 2595 } 2596 for (form.children.values()) |*child| switch (child.widget) { 2597 .text_input => |*ti| { 2598 const text = try ti.text(allocator); 2599 errdefer allocator.free(text); 2600 if (std.mem.eql(u8, ti.options.name, "title")) { 2601 title = text; 2602 } else if (std.mem.eql(u8, ti.options.name, "labels")) { 2603 labels = text; 2604 } else { 2605 try hunks.append(allocator, text); 2606 } 2607 }, 2608 else => {}, 2609 }; 2610 2611 const id_bytes = try evt.parseEventId(entry.id); 2612 2613 switch (src.repo_kind) { 2614 inline else => |repo_kind| { 2615 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2616 defer any_repo.deinit(io, allocator); 2617 switch (any_repo) { 2618 inline else => |*repo| { 2619 var resolution: Event.Resolve = .{ 2620 .title = title, 2621 .labels = labels, 2622 .hunks = hunks.items, 2623 }; 2624 if (comptime @hasField(Event.Resolve, "theirs")) resolution.theirs = self.data.theirs_picks; 2625 const change = Event.Update{ .resolve = resolution }; 2626 Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, change, author) catch |err| switch (err) { 2627 // leave the form up for correction 2628 error.InvalidFields => return, 2629 else => |e| return e, 2630 }; 2631 }, 2632 } 2633 }, 2634 } 2635 2636 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2637 try self.session.navigate(route); 2638 } 2639 2640 // commit a new or edited comment and navigate to its permalink. this is the 2641 // terminal path; the web posts the form to its comment route. 2642 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 2643 if (comptime wasm) return; 2644 const io = self.session.io orelse return; 2645 const src = self.data.repo_source orelse return; 2646 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 2647 const author = actor.author; 2648 const form = self.formBox() orelse return; 2649 const body_input = try formField(form, "body"); 2650 const body = try body_input.text(allocator); 2651 defer allocator.free(body); 2652 if (!evt.Comment.fieldsValid(body)) return; 2653 2654 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2655 @memcpy(&thread_id, self.data.selected_id); 2656 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2657 var parent: [evt.event_id_size * 2]u8 = undefined; 2658 @memcpy(&parent, self.data.comment_id); 2659 break :blk parent; 2660 }; 2661 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2662 const editing = self.data.view == .edit_comment; 2663 if (editing) { 2664 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2665 if (!try src.canModify(io, allocator, actor, .comment, &comment_id)) return; 2666 } 2667 switch (src.repo_kind) { 2668 inline else => |repo_kind| { 2669 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2670 defer any_repo.deinit(io, allocator); 2671 switch (any_repo) { 2672 inline else => |*repo| if (editing) { 2673 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2674 try evt.Comment.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id, &comment_id, body, author); 2675 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 2676 } else { 2677 event_id_hex = try evt.Comment.create(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id, &parent_id, body, author); 2678 }, 2679 } 2680 }, 2681 } 2682 2683 body_input.clear(allocator); 2684 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2685 try self.session.navigate(route); 2686 } 2687 2688 // terminal form submissions; the web posts to the forms' routes instead 2689 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 2690 switch (self.data.view) { 2691 .new_comment, .edit_comment => try self.submitComment(allocator), 2692 .edit => try self.submitEditedThread(allocator), 2693 else => try self.submitNewThread(allocator), 2694 } 2695 } 2696 2697 // commit the new thread to the repo's events branch and navigate to it. 2698 // this is the terminal path; the web posts the form to the thread route, 2699 // so the wasm side never runs (or compiles) the repo access below. 2700 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 2701 if (comptime wasm) return; 2702 const io = self.session.io orelse return; 2703 const src = self.data.repo_source orelse return; 2704 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 2705 2706 const form = self.threadForm() orelse return; 2707 const title_input = try formField(form, "title"); 2708 const labels_input = try formField(form, "labels"); 2709 const description_input = try formField(form, "description"); 2710 2711 const title = try title_input.text(allocator); 2712 defer allocator.free(title); 2713 const labels = try labels_input.text(allocator); 2714 defer allocator.free(labels); 2715 const description = try description_input.text(allocator); 2716 defer allocator.free(description); 2717 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 2718 defer if (comptime supports_drafts) allocator.free(target_branch); 2719 2720 if (!Event.fieldsValid(title, labels)) { 2721 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, labels, description, target_branch); 2722 return; 2723 } 2724 2725 if (comptime supports_drafts) { 2726 const source = formWidget(form, .patch_source) orelse return error.MissingFormField; 2727 const source_branch = switch (source.selectedKind()) { 2728 .fork => null, 2729 .branch => try source.field().text(allocator), 2730 }; 2731 defer if (source_branch) |branch| allocator.free(branch); 2732 const event_id_hex = Data.create(self.data, self.session, allocator, actor, title, labels, description, target_branch, source_branch) catch |err| { 2733 const failure = FeedbackFailure.fromError(err) orelse return err; 2734 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 2735 return; 2736 }; 2737 title_input.clear(allocator); 2738 labels_input.clear(allocator); 2739 description_input.clear(allocator); 2740 (try formField(form, "target_branch")).clear(allocator); 2741 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 2742 try self.session.navigate(route); 2743 return; 2744 } 2745 2746 var id_bytes: [evt.event_id_size]u8 = undefined; 2747 io.random(&id_bytes); 2748 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2749 2750 const event = evt.EventWithId{ 2751 .id = event_id_hex, 2752 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2753 .author = actor.author, 2754 .event = @unionInit(evt.Event, @tagName(kind), Event{ 2755 .title = title, 2756 .description = description, 2757 .labels = labels, 2758 }), 2759 }; 2760 2761 switch (src.repo_kind) { 2762 inline else => |repo_kind| { 2763 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2764 defer any_repo.deinit(io, allocator); 2765 switch (any_repo) { 2766 inline else => |*repo| try evt.consume(self.session.eventHost(), .repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2767 } 2768 }, 2769 } 2770 2771 // wipe the form so a return visit starts fresh 2772 title_input.clear(allocator); 2773 labels_input.clear(allocator); 2774 description_input.clear(allocator); 2775 2776 const route = listRoute(self.data.identity, @fromBackingInt(@intCast(0)), "", &event_id_hex) orelse return; 2777 try self.session.navigate(route); 2778 } 2779 2780 // re-emit the selected thread's event with the form's content (status 2781 // preserved), then reload the thread's page. this is the terminal path; 2782 // the web posts the form to the edit route. 2783 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 2784 if (comptime wasm) return; 2785 const io = self.session.io orelse return; 2786 const entry = self.data.selectedThread() orelse return; 2787 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 2788 const author = actor.author; 2789 2790 const form = self.threadForm() orelse return; 2791 const title_input = try formField(form, "title"); 2792 const labels_input = try formField(form, "labels"); 2793 const description_input = try formField(form, "description"); 2794 2795 const title = try title_input.text(allocator); 2796 defer allocator.free(title); 2797 const labels = try labels_input.text(allocator); 2798 defer allocator.free(labels); 2799 const description = try description_input.text(allocator); 2800 defer allocator.free(description); 2801 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 2802 defer if (comptime supports_drafts) allocator.free(target_branch); 2803 2804 if (!Event.fieldsValid(title, labels)) { 2805 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, labels, description, target_branch); 2806 return; 2807 } 2808 const src = self.data.repo_source orelse return; 2809 2810 if (comptime supports_drafts) { 2811 if (entryDraft(entry.*)) { 2812 self.data.editDraft(self.session, allocator, author, entry.id, title, labels, description, target_branch) catch |err| { 2813 const failure = FeedbackFailure.fromError(err) orelse return err; 2814 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 2815 return; 2816 }; 2817 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2818 try self.session.navigate(route); 2819 return; 2820 } 2821 } 2822 2823 const id_bytes = try evt.parseEventId(entry.id); 2824 if (!try src.canModify(io, allocator, actor, kind, &id_bytes)) return; 2825 2826 switch (src.repo_kind) { 2827 inline else => |repo_kind| { 2828 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2829 defer any_repo.deinit(io, allocator); 2830 switch (any_repo) { 2831 inline else => |*repo| if (supports_drafts) { 2832 Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2833 .title = title, 2834 .labels = labels, 2835 .description = description, 2836 .target_branch = target_branch, 2837 } }, author) catch |err| { 2838 const failure = FeedbackFailure.fromError(err) orelse return err; 2839 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 2840 return; 2841 }; 2842 } else if (has_status) 2843 try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2844 .title = title, 2845 .labels = labels, 2846 .description = description, 2847 } }, author) 2848 else 2849 try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, labels, description, author), 2850 } 2851 }, 2852 } 2853 2854 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2855 try self.session.navigate(route); 2856 } 2857 2858 fn rememberThreadFeedback(self: *This, failure: FeedbackFailure, title: []const u8, labels: []const u8, description: []const u8, target_branch: []const u8) !void { 2859 const aa = self.session.arena.allocator(); 2860 const source = if (supports_drafts) blk: { 2861 const form = self.threadForm() orelse break :blk null; 2862 break :blk formWidget(form, .patch_source); 2863 } else null; 2864 self.session.data.form_feedback = @unionInit(ui.Session.FormFeedback, @tagName(feedback_tag), .{ 2865 .failure = failure, 2866 .fields = if (comptime supports_drafts) .{ 2867 .title = try aa.dupe(u8, title), 2868 .labels = try aa.dupe(u8, labels), 2869 .description = try aa.dupe(u8, description), 2870 .target_branch = try aa.dupe(u8, target_branch), 2871 .source_branch = if (source) |value| try value.field().text(aa) else "", 2872 .source_kind = if (source) |value| value.selectedKind() else .fork, 2873 } else .{ 2874 .title = try aa.dupe(u8, title), 2875 .labels = try aa.dupe(u8, labels), 2876 .description = try aa.dupe(u8, description), 2877 }, 2878 }); 2879 } 2880 2881 fn submitConfirmation(self: *This, allocator: std.mem.Allocator, button_index: usize) !void { 2882 switch (confirmation(self.data.view) orelse return) { 2883 .publish => if (comptime supports_drafts) { 2884 if (comptime wasm) return; 2885 const entry = self.data.selectedThread() orelse return; 2886 if (!entryDraft(entry.*)) return; 2887 const author = ((try self.session.authorize(self.data.identity, .read, kind)) orelse return).author; 2888 try self.data.publishDraft(self.session, allocator, author, entry.id); 2889 const route = listRoute(self.data.identity, @fromBackingInt(@intCast(0)), "", entry.id) orelse return; 2890 try self.session.navigate(route); 2891 } else unreachable, 2892 .merge => if (comptime supports_merge) { 2893 if (comptime wasm) return; 2894 const entry = self.data.selectedThread() orelse return; 2895 const revision: evt.Patch.MergeRevision = if (button_index > 1) .squash else .source; 2896 if (!Data.canMerge(entry.*, self.session) or entry.mergeability.get(revision) != .clean) return; 2897 const author = ((try self.session.authorize(self.data.identity, .write, kind)) orelse return).author; 2898 self.data.mergePatch(self.session, allocator, author, entry.id, revision) catch |err| switch (err) { 2899 error.MergeConflict, error.MergeCheckUnavailable, error.PatchOutOfDate, error.PatchDataUnavailable => { 2900 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2901 try self.session.navigate(route); 2902 return; 2903 }, 2904 else => return err, 2905 }; 2906 const merged = std.meta.stringToEnum(Status, "merged") orelse return; 2907 const route = listRoute(self.data.identity, merged, "", entry.id) orelse return; 2908 try self.session.navigate(route); 2909 } else unreachable, 2910 .remove => try self.submitRemove(allocator), 2911 } 2912 } 2913 2914 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 2915 const comment = self.data.comment_id.len != 0; 2916 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 2917 const id = evt.parseEventId(event_id) catch return; 2918 try self.removeEvent(allocator, if (comment) .comment else kind, id); 2919 } 2920 2921 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 2922 if (comptime wasm) return; 2923 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 2924 const author = actor.author; 2925 2926 if (comptime supports_drafts) { 2927 const selected = self.data.selectedThread() orelse return; 2928 if (event_kind == kind and entryDraft(selected.*)) { 2929 try Data.removeDraft(self.session, allocator, author, &id); 2930 const route = draftsRoute(self.data.identity) orelse return; 2931 try self.session.navigate(route); 2932 return; 2933 } 2934 } 2935 2936 const io = self.session.io orelse return; 2937 const src = self.data.repo_source orelse return; 2938 if (!try src.canModify(io, allocator, actor, event_kind, &id)) return; 2939 const thread_id = evt.parseEventId(self.data.selected_id) catch return; 2940 2941 switch (src.repo_kind) { 2942 inline else => |repo_kind| { 2943 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2944 defer any_repo.deinit(io, allocator); 2945 switch (any_repo) { 2946 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), 2947 } 2948 }, 2949 } 2950 2951 const id_hex = std.fmt.bytesToHex(id, .lower); 2952 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex, null) orelse return; 2953 try self.session.navigate(route); 2954 } 2955 2956 // return to the list. 2957 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 2958 root_focus.setFocus(self.listScroll(index).getFocus().id); 2959 } 2960 2961 // cross to the header tabs above the stack. 2962 fn focusHeader(self: *This, root_focus: *Focus) void { 2963 root_focus.setFocus(self.box.children.keys()[header_index]); 2964 } 2965 2966 pub fn clearGrid(self: *This) void { 2967 self.box.clearGrid(); 2968 } 2969 2970 pub fn getGrid(self: This) ?Grid { 2971 return self.box.getGrid(); 2972 } 2973 2974 pub fn getFocus(self: *This) *Focus { 2975 return self.box.getFocus(); 2976 } 2977 2978 // for the parent's "scroll up at the top jumps to the header" check: at the 2979 // top only while the header tabs hold focus, so up from the split 2980 // crosses into the tabs first. 2981 pub fn atTop(self: *This) bool { 2982 return self.headerActive(); 2983 } 2984 }; 2985 } 2986