╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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