╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2702 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 2703 switch (self.data.view) { 2704 .new_comment, .edit_comment => try self.submitComment(allocator), 2705 .edit => try self.submitEditedThread(allocator), 2706 else => try self.submitNewThread(allocator), 2707 } 2708 } 2709 2710 // commit the new thread to the repo's events branch and navigate to it. 2711 // this is the terminal path; the web posts the form to the thread route, 2712 // so the wasm side never runs (or compiles) the repo access below. 2713 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 2714 if (comptime wasm) return; 2715 const io = self.session.io orelse return; 2716 const src = self.data.repo_source orelse return; 2717 const actor = (try self.session.authorize(self.data.identity, .read)) orelse return; 2718 2719 const form = self.threadForm() orelse return; 2720 const title_input = try formField(form, "title"); 2721 const labels_input = try formField(form, "labels"); 2722 const description_input = try formField(form, "description"); 2723 2724 const title = try title_input.text(allocator); 2725 defer allocator.free(title); 2726 const labels = try labels_input.text(allocator); 2727 defer allocator.free(labels); 2728 const description = try description_input.text(allocator); 2729 defer allocator.free(description); 2730 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 2731 defer if (comptime supports_drafts) allocator.free(target_branch); 2732 2733 if (!Event.fieldsValid(title, labels)) { 2734 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, labels, description, target_branch); 2735 return; 2736 } 2737 2738 if (comptime supports_drafts) { 2739 const source = formWidget(form, .patch_source) orelse return error.MissingFormField; 2740 const source_branch = switch (source.selectedKind()) { 2741 .fork => null, 2742 .branch => try source.field().text(allocator), 2743 }; 2744 defer if (source_branch) |branch| allocator.free(branch); 2745 const event_id_hex = Data.create(self.data, self.session, allocator, actor, title, labels, description, target_branch, source_branch) catch |err| { 2746 const failure = FeedbackFailure.fromError(err) orelse return err; 2747 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 2748 return; 2749 }; 2750 title_input.clear(allocator); 2751 labels_input.clear(allocator); 2752 description_input.clear(allocator); 2753 (try formField(form, "target_branch")).clear(allocator); 2754 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 2755 try self.session.navigate(route); 2756 return; 2757 } 2758 2759 var id_bytes: [evt.event_id_size]u8 = undefined; 2760 io.random(&id_bytes); 2761 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2762 2763 const event = evt.EventWithId{ 2764 .id = event_id_hex, 2765 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2766 .author = actor.author, 2767 .event = @unionInit(evt.Event, @tagName(kind), Event{ 2768 .title = title, 2769 .description = description, 2770 .labels = labels, 2771 }), 2772 }; 2773 2774 switch (src.repo_kind) { 2775 inline else => |repo_kind| { 2776 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2777 defer any_repo.deinit(io, allocator); 2778 switch (any_repo) { 2779 inline else => |*repo| try evt.consume(self.session.eventHost(), .repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2780 } 2781 }, 2782 } 2783 2784 // wipe the form so a return visit starts fresh 2785 title_input.clear(allocator); 2786 labels_input.clear(allocator); 2787 description_input.clear(allocator); 2788 2789 const route = listRoute(self.data.identity, @enumFromInt(0), "", &event_id_hex) orelse return; 2790 try self.session.navigate(route); 2791 } 2792 2793 // re-emit the selected thread's event with the form's content (status 2794 // preserved), then reload the thread's page. this is the terminal path; 2795 // the web posts the form to the edit route. 2796 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 2797 if (comptime wasm) return; 2798 const io = self.session.io orelse return; 2799 const entry = self.data.selectedThread() orelse return; 2800 const actor = (try self.session.authorize(self.data.identity, .read)) orelse return; 2801 const author = actor.author; 2802 2803 const form = self.threadForm() orelse return; 2804 const title_input = try formField(form, "title"); 2805 const labels_input = try formField(form, "labels"); 2806 const description_input = try formField(form, "description"); 2807 2808 const title = try title_input.text(allocator); 2809 defer allocator.free(title); 2810 const labels = try labels_input.text(allocator); 2811 defer allocator.free(labels); 2812 const description = try description_input.text(allocator); 2813 defer allocator.free(description); 2814 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 2815 defer if (comptime supports_drafts) allocator.free(target_branch); 2816 2817 if (!Event.fieldsValid(title, labels)) { 2818 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, labels, description, target_branch); 2819 return; 2820 } 2821 const src = self.data.repo_source orelse return; 2822 2823 if (comptime supports_drafts) { 2824 if (entryDraft(entry.*)) { 2825 self.data.editDraft(self.session, allocator, author, entry.id, title, labels, description, target_branch) catch |err| { 2826 const failure = FeedbackFailure.fromError(err) orelse return err; 2827 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 2828 return; 2829 }; 2830 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2831 try self.session.navigate(route); 2832 return; 2833 } 2834 } 2835 2836 const id_bytes = try evt.parseEventId(entry.id); 2837 if (!try src.canModify(io, allocator, actor, kind, &id_bytes)) return; 2838 2839 switch (src.repo_kind) { 2840 inline else => |repo_kind| { 2841 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2842 defer any_repo.deinit(io, allocator); 2843 switch (any_repo) { 2844 inline else => |*repo| if (supports_drafts) { 2845 Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2846 .title = title, 2847 .labels = labels, 2848 .description = description, 2849 .target_branch = target_branch, 2850 } }, author) catch |err| { 2851 const failure = FeedbackFailure.fromError(err) orelse return err; 2852 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 2853 return; 2854 }; 2855 } else if (has_status) 2856 try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2857 .title = title, 2858 .labels = labels, 2859 .description = description, 2860 } }, author) 2861 else 2862 try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, labels, description, author), 2863 } 2864 }, 2865 } 2866 2867 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2868 try self.session.navigate(route); 2869 } 2870 2871 fn rememberThreadFeedback(self: *This, failure: FeedbackFailure, title: []const u8, labels: []const u8, description: []const u8, target_branch: []const u8) !void { 2872 const aa = self.session.arena.allocator(); 2873 const source = if (supports_drafts) blk: { 2874 const form = self.threadForm() orelse break :blk null; 2875 break :blk formWidget(form, .patch_source); 2876 } else null; 2877 self.session.data.form_feedback = @unionInit(ui.Session.FormFeedback, @tagName(feedback_tag), .{ 2878 .failure = failure, 2879 .fields = if (comptime supports_drafts) .{ 2880 .title = try aa.dupe(u8, title), 2881 .labels = try aa.dupe(u8, labels), 2882 .description = try aa.dupe(u8, description), 2883 .target_branch = try aa.dupe(u8, target_branch), 2884 .source_branch = if (source) |value| try value.field().text(aa) else "", 2885 .source_kind = if (source) |value| value.selectedKind() else .fork, 2886 } else .{ 2887 .title = try aa.dupe(u8, title), 2888 .labels = try aa.dupe(u8, labels), 2889 .description = try aa.dupe(u8, description), 2890 }, 2891 }); 2892 } 2893 2894 fn submitConfirmation(self: *This, allocator: std.mem.Allocator, button_index: usize) !void { 2895 switch (confirmation(self.data.view) orelse return) { 2896 .publish => if (comptime supports_drafts) { 2897 if (comptime wasm) return; 2898 const entry = self.data.selectedThread() orelse return; 2899 if (!entryDraft(entry.*)) return; 2900 const author = ((try self.session.authorize(self.data.identity, .read)) orelse return).author; 2901 try self.data.publishDraft(self.session, allocator, author, entry.id); 2902 const route = listRoute(self.data.identity, @enumFromInt(0), "", entry.id) orelse return; 2903 try self.session.navigate(route); 2904 } else unreachable, 2905 .merge => if (comptime supports_merge) { 2906 if (comptime wasm) return; 2907 const entry = self.data.selectedThread() orelse return; 2908 const revision: evt.Patch.MergeRevision = if (button_index > 1) .squash else .source; 2909 if (!Data.canMerge(entry.*, self.session) or entry.mergeability.get(revision) != .clean) return; 2910 const author = ((try self.session.authorize(self.data.identity, .write)) orelse return).author; 2911 self.data.mergePatch(self.session, allocator, author, entry.id, revision) catch |err| switch (err) { 2912 error.MergeConflict, error.MergeCheckUnavailable, error.PatchOutOfDate, error.PatchDataUnavailable => { 2913 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2914 try self.session.navigate(route); 2915 return; 2916 }, 2917 else => return err, 2918 }; 2919 const merged = std.meta.stringToEnum(Status, "merged") orelse return; 2920 const route = listRoute(self.data.identity, merged, "", entry.id) orelse return; 2921 try self.session.navigate(route); 2922 } else unreachable, 2923 .remove => try self.submitRemove(allocator), 2924 } 2925 } 2926 2927 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 2928 const comment = self.data.comment_id.len != 0; 2929 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 2930 const id = evt.parseEventId(event_id) catch return; 2931 try self.removeEvent(allocator, if (comment) .comment else kind, id); 2932 } 2933 2934 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 2935 if (comptime wasm) return; 2936 const actor = (try self.session.authorize(self.data.identity, .read)) orelse return; 2937 const author = actor.author; 2938 2939 if (comptime supports_drafts) { 2940 const selected = self.data.selectedThread() orelse return; 2941 if (event_kind == kind and entryDraft(selected.*)) { 2942 try Data.removeDraft(self.session, allocator, author, &id); 2943 const route = draftsRoute(self.data.identity) orelse return; 2944 try self.session.navigate(route); 2945 return; 2946 } 2947 } 2948 2949 const io = self.session.io orelse return; 2950 const src = self.data.repo_source orelse return; 2951 if (!try src.canModify(io, allocator, actor, event_kind, &id)) return; 2952 2953 switch (src.repo_kind) { 2954 inline else => |repo_kind| { 2955 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2956 defer any_repo.deinit(io, allocator); 2957 switch (any_repo) { 2958 inline else => |*repo| try evt.remove(self.session.eventHost(), .repo, repo_kind, repo.self_repo_opts, io, allocator, repo, &id, event_kind, author), 2959 } 2960 }, 2961 } 2962 2963 const id_hex = std.fmt.bytesToHex(id, .lower); 2964 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex, null) orelse return; 2965 try self.session.navigate(route); 2966 } 2967 2968 // return to the list. 2969 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 2970 root_focus.setFocus(self.listScroll(index).getFocus().id); 2971 } 2972 2973 // cross to the header tabs above the stack. 2974 fn focusHeader(self: *This, root_focus: *Focus) void { 2975 root_focus.setFocus(self.box.children.keys()[header_index]); 2976 } 2977 2978 pub fn clearGrid(self: *This) void { 2979 self.box.clearGrid(); 2980 } 2981 2982 pub fn getGrid(self: This) ?Grid { 2983 return self.box.getGrid(); 2984 } 2985 2986 pub fn getFocus(self: *This) *Focus { 2987 return self.box.getFocus(); 2988 } 2989 2990 // for the parent's "scroll up at the top jumps to the header" check: at the 2991 // top only while the header tabs hold focus, so up from the split 2992 // crosses into the tabs first. 2993 pub fn atTop(self: *This) bool { 2994 return self.headerActive(); 2995 } 2996 }; 2997 } 2998