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