╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2768 2769 // wipe the form so a return visit starts fresh 2770 title_input.clear(allocator); 2771 tags_input.clear(allocator); 2772 description_input.clear(allocator); 2773 2774 const route = listRoute(self.data.identity, @enumFromInt(0), "", &event_id_hex) orelse return; 2775 try self.session.navigate(route); 2776 } 2777 2778 // re-emit the selected thread's event with the form's content (status 2779 // preserved), then reload the thread's page. this is the terminal path; 2780 // the web posts the form to the edit route. 2781 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 2782 if (comptime wasm) return; 2783 const io = self.session.io orelse return; 2784 const entry = self.data.selectedThread() orelse return; 2785 const actor = (try self.session.authorize(self.data.identity, .read)) orelse return; 2786 const author = actor.author; 2787 2788 const form = self.threadForm() orelse return; 2789 const title_input = try formField(form, "title"); 2790 const tags_input = try formField(form, "tags"); 2791 const description_input = try formField(form, "description"); 2792 2793 const title = try title_input.text(allocator); 2794 defer allocator.free(title); 2795 const tags = try tags_input.text(allocator); 2796 defer allocator.free(tags); 2797 const description = try description_input.text(allocator); 2798 defer allocator.free(description); 2799 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 2800 defer if (comptime supports_drafts) allocator.free(target_branch); 2801 2802 if (!Event.fieldsValid(title, tags)) { 2803 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, tags, description, target_branch); 2804 return; 2805 } 2806 const src = self.data.repo_source orelse return; 2807 2808 if (comptime supports_drafts) { 2809 if (entryDraft(entry.*)) { 2810 self.data.editDraft(self.session, allocator, author, entry.id, title, tags, description, target_branch) catch |err| { 2811 const failure = FeedbackFailure.fromError(err) orelse return err; 2812 try self.rememberThreadFeedback(failure, title, tags, description, target_branch); 2813 return; 2814 }; 2815 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2816 try self.session.navigate(route); 2817 return; 2818 } 2819 } 2820 2821 const id_bytes = try evt.parseEventId(entry.id); 2822 if (!try src.canModify(io, allocator, actor, kind, &id_bytes)) return; 2823 2824 switch (src.repo_kind) { 2825 inline else => |repo_kind| { 2826 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2827 defer any_repo.deinit(io, allocator); 2828 switch (any_repo) { 2829 inline else => |*repo| if (supports_drafts) { 2830 Event.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2831 .title = title, 2832 .tags = tags, 2833 .description = description, 2834 .target_branch = target_branch, 2835 } }, author) catch |err| { 2836 const failure = FeedbackFailure.fromError(err) orelse return err; 2837 try self.rememberThreadFeedback(failure, title, tags, description, target_branch); 2838 return; 2839 }; 2840 } else if (has_status) 2841 try Event.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2842 .title = title, 2843 .tags = tags, 2844 .description = description, 2845 } }, author) 2846 else 2847 try Event.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, tags, description, author), 2848 } 2849 }, 2850 } 2851 2852 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2853 try self.session.navigate(route); 2854 } 2855 2856 fn rememberThreadFeedback(self: *This, failure: FeedbackFailure, title: []const u8, tags: []const u8, description: []const u8, target_branch: []const u8) !void { 2857 const aa = self.session.arena.allocator(); 2858 const source = if (supports_drafts) blk: { 2859 const form = self.threadForm() orelse break :blk null; 2860 break :blk formWidget(form, .patch_source); 2861 } else null; 2862 self.session.data.form_feedback = @unionInit(ui.Session.FormFeedback, @tagName(feedback_tag), .{ 2863 .failure = failure, 2864 .fields = if (comptime supports_drafts) .{ 2865 .title = try aa.dupe(u8, title), 2866 .tags = try aa.dupe(u8, tags), 2867 .description = try aa.dupe(u8, description), 2868 .target_branch = try aa.dupe(u8, target_branch), 2869 .source_branch = if (source) |value| try value.field().text(aa) else "", 2870 .source_kind = if (source) |value| value.selected else .fork, 2871 } else .{ 2872 .title = try aa.dupe(u8, title), 2873 .tags = try aa.dupe(u8, tags), 2874 .description = try aa.dupe(u8, description), 2875 }, 2876 }); 2877 } 2878 2879 fn submitConfirmation(self: *This, allocator: std.mem.Allocator, button_index: usize) !void { 2880 switch (confirmation(self.data.view) orelse return) { 2881 .publish => if (comptime supports_drafts) { 2882 if (comptime wasm) return; 2883 const entry = self.data.selectedThread() orelse return; 2884 if (!entryDraft(entry.*)) return; 2885 const author = ((try self.session.authorize(self.data.identity, .read)) orelse return).author; 2886 try self.data.publishDraft(self.session, allocator, author, entry.id); 2887 const route = listRoute(self.data.identity, @enumFromInt(0), "", entry.id) orelse return; 2888 try self.session.navigate(route); 2889 } else unreachable, 2890 .merge => if (comptime supports_merge) { 2891 if (comptime wasm) return; 2892 const entry = self.data.selectedThread() orelse return; 2893 const revision: evt.Patch.MergeRevision = if (button_index > 1) .squash else .source; 2894 if (!Data.canMerge(entry.*, self.session) or entry.mergeability.get(revision) != .clean) return; 2895 const author = ((try self.session.authorize(self.data.identity, .write)) orelse return).author; 2896 self.data.mergePatch(self.session, allocator, author, entry.id, revision) catch |err| switch (err) { 2897 error.MergeConflict, error.MergeCheckUnavailable, error.PatchOutOfDate, error.PatchDataUnavailable => { 2898 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2899 try self.session.navigate(route); 2900 return; 2901 }, 2902 else => return err, 2903 }; 2904 const merged = std.meta.stringToEnum(Status, "merged") orelse return; 2905 const route = listRoute(self.data.identity, merged, "", entry.id) orelse return; 2906 try self.session.navigate(route); 2907 } else unreachable, 2908 .remove => try self.submitRemove(allocator), 2909 } 2910 } 2911 2912 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 2913 const comment = self.data.comment_id.len != 0; 2914 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 2915 const id = evt.parseEventId(event_id) catch return; 2916 try self.removeEvent(allocator, if (comment) .comment else kind, id); 2917 } 2918 2919 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 2920 if (comptime wasm) return; 2921 const actor = (try self.session.authorize(self.data.identity, .read)) orelse return; 2922 const author = actor.author; 2923 2924 if (comptime supports_drafts) { 2925 const selected = self.data.selectedThread() orelse return; 2926 if (event_kind == kind and entryDraft(selected.*)) { 2927 try Data.removeDraft(self.session, allocator, author, &id); 2928 const route = draftsRoute(self.data.identity) orelse return; 2929 try self.session.navigate(route); 2930 return; 2931 } 2932 } 2933 2934 const io = self.session.io orelse return; 2935 const src = self.data.repo_source orelse return; 2936 if (!try src.canModify(io, allocator, actor, event_kind, &id)) return; 2937 2938 switch (src.repo_kind) { 2939 inline else => |repo_kind| { 2940 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2941 defer any_repo.deinit(io, allocator); 2942 switch (any_repo) { 2943 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), 2944 } 2945 }, 2946 } 2947 2948 const id_hex = std.fmt.bytesToHex(id, .lower); 2949 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex, null) orelse return; 2950 try self.session.navigate(route); 2951 } 2952 2953 // return to the list. 2954 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 2955 root_focus.setFocus(self.listScroll(index).getFocus().id); 2956 } 2957 2958 // cross to the header tabs above the stack. 2959 fn focusHeader(self: *This, root_focus: *Focus) void { 2960 root_focus.setFocus(self.box.children.keys()[header_index]); 2961 } 2962 2963 pub fn clearGrid(self: *This) void { 2964 self.box.clearGrid(); 2965 } 2966 2967 pub fn getGrid(self: This) ?Grid { 2968 return self.box.getGrid(); 2969 } 2970 2971 pub fn getFocus(self: *This) *Focus { 2972 return self.box.getFocus(); 2973 } 2974 2975 // for the parent's "scroll up at the top jumps to the header" check: at the 2976 // top only while the header tabs hold focus, so up from the split 2977 // crosses into the tabs first. 2978 pub fn atTop(self: *This) bool { 2979 return self.headerActive(); 2980 } 2981 }; 2982 } 2983