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