╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2532 } 2533 2534 var id_bytes: [evt.event_id_size]u8 = undefined; 2535 io.random(&id_bytes); 2536 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2537 2538 const event = evt.EventWithId{ 2539 .id = event_id_hex, 2540 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2541 .author = author, 2542 .event = @unionInit(evt.Event, @tagName(kind), Event{ 2543 .title = title, 2544 .description = description, 2545 .tags = tags, 2546 }), 2547 }; 2548 2549 switch (src.repo_kind) { 2550 inline else => |repo_kind| { 2551 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2552 defer any_repo.deinit(io, allocator); 2553 switch (any_repo) { 2554 inline else => |*repo| try evt.consume(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2555 } 2556 }, 2557 } 2558 2559 // wipe the form so a return visit starts fresh 2560 title_input.clear(allocator); 2561 tags_input.clear(allocator); 2562 description_input.clear(allocator); 2563 2564 const route = listRoute(self.data.identity, @enumFromInt(0), "", &event_id_hex) orelse return; 2565 try self.session.navigate(route); 2566 } 2567 2568 // re-emit the selected thread's event with the form's content (status 2569 // preserved), then reload the thread's page. this is the terminal path; 2570 // the web posts the form to the edit route. 2571 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 2572 if (comptime wasm) return; 2573 const io = self.session.io orelse return; 2574 const entry = self.data.selectedThread() orelse return; 2575 2576 const form = self.threadForm() orelse return; 2577 const title_input = &form.children.values()[title_field_index].widget.text_input; 2578 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2579 const description_input = &form.children.values()[description_field_index].widget.text_input; 2580 2581 const title = try title_input.text(allocator); 2582 defer allocator.free(title); 2583 const tags = try tags_input.text(allocator); 2584 defer allocator.free(tags); 2585 const description = try description_input.text(allocator); 2586 defer allocator.free(description); 2587 const target_branch = if (comptime supports_drafts) try form.children.values()[target_branch_field_index].widget.text_input.text(allocator) else ""; 2588 defer if (comptime supports_drafts) allocator.free(target_branch); 2589 2590 if (!Event.fieldsValid(title, tags)) { 2591 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, tags, description, target_branch); 2592 return; 2593 } 2594 const src = self.data.repo_source orelse return; 2595 2596 if (comptime supports_drafts) { 2597 if (entryDraft(entry.*)) { 2598 self.data.editDraft(self.session, allocator, entry.id, title, tags, description, target_branch) catch |err| switch (err) { 2599 error.InvalidFields => { 2600 try self.rememberThreadFeedback(.invalid_target_branch, title, tags, description, target_branch); 2601 return; 2602 }, 2603 else => |other| return other, 2604 }; 2605 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2606 try self.session.navigate(route); 2607 return; 2608 } 2609 } 2610 2611 const id_bytes = try evt.parseEventId(entry.id); 2612 const author = (try self.session.eventAuthor()) orelse return; 2613 2614 switch (src.repo_kind) { 2615 inline else => |repo_kind| { 2616 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2617 defer any_repo.deinit(io, allocator); 2618 switch (any_repo) { 2619 inline else => |*repo| if (supports_drafts) { 2620 Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2621 .title = title, 2622 .tags = tags, 2623 .description = description, 2624 .target_branch = target_branch, 2625 } }, author) catch |err| switch (err) { 2626 error.InvalidFields => { 2627 try self.rememberThreadFeedback(.invalid_target_branch, title, tags, description, target_branch); 2628 return; 2629 }, 2630 else => |other| return other, 2631 }; 2632 } else if (has_status) 2633 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2634 .title = title, 2635 .tags = tags, 2636 .description = description, 2637 } }, author) 2638 else 2639 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, tags, description, author), 2640 } 2641 }, 2642 } 2643 2644 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2645 try self.session.navigate(route); 2646 } 2647 2648 fn rememberThreadFeedback(self: *This, failure: FeedbackFailure, title: []const u8, tags: []const u8, description: []const u8, target_branch: []const u8) !void { 2649 const aa = self.session.arena.allocator(); 2650 self.session.data.form_feedback = @unionInit(ui.Session.FormFeedback, @tagName(feedback_tag), .{ 2651 .failure = failure, 2652 .fields = if (comptime supports_drafts) .{ 2653 .title = try aa.dupe(u8, title), 2654 .tags = try aa.dupe(u8, tags), 2655 .description = try aa.dupe(u8, description), 2656 .target_branch = try aa.dupe(u8, target_branch), 2657 } else .{ 2658 .title = try aa.dupe(u8, title), 2659 .tags = try aa.dupe(u8, tags), 2660 .description = try aa.dupe(u8, description), 2661 }, 2662 }); 2663 } 2664 2665 fn submitConfirmation(self: *This, allocator: std.mem.Allocator, button_index: usize) !void { 2666 switch (confirmation(self.data.view) orelse return) { 2667 .publish => if (comptime supports_drafts) { 2668 if (comptime wasm) return; 2669 const entry = self.data.selectedThread() orelse return; 2670 if (!entryDraft(entry.*)) return; 2671 try self.data.publishDraft(self.session, allocator, entry.id); 2672 const route = listRoute(self.data.identity, @enumFromInt(0), "", entry.id) orelse return; 2673 try self.session.navigate(route); 2674 } else unreachable, 2675 .merge => if (comptime supports_merge) { 2676 if (comptime wasm) return; 2677 const entry = self.data.selectedThread() orelse return; 2678 try self.data.mergePatch(self.session, allocator, entry.id, button_index > 1); 2679 const merged = std.meta.stringToEnum(Status, "merged") orelse return; 2680 const route = listRoute(self.data.identity, merged, "", entry.id) orelse return; 2681 try self.session.navigate(route); 2682 } else unreachable, 2683 .remove => try self.submitRemove(allocator), 2684 } 2685 } 2686 2687 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 2688 const comment = self.data.comment_id.len != 0; 2689 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 2690 const id = evt.parseEventId(event_id) catch return; 2691 try self.removeEvent(allocator, if (comment) .comment else kind, id); 2692 } 2693 2694 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 2695 if (comptime wasm) return; 2696 2697 if (comptime supports_drafts) { 2698 const selected = self.data.selectedThread() orelse return; 2699 if (event_kind == kind and entryDraft(selected.*)) { 2700 try Data.removeDraft(self.session, allocator, &id); 2701 const route = draftsRoute(self.data.identity) orelse return; 2702 try self.session.navigate(route); 2703 return; 2704 } 2705 } 2706 2707 const io = self.session.io orelse return; 2708 const src = self.data.repo_source orelse return; 2709 const author = (try self.session.eventAuthor()) orelse return; 2710 2711 switch (src.repo_kind) { 2712 inline else => |repo_kind| { 2713 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2714 defer any_repo.deinit(io, allocator); 2715 switch (any_repo) { 2716 inline else => |*repo| try evt.remove(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, &id, event_kind, author), 2717 } 2718 }, 2719 } 2720 2721 const id_hex = std.fmt.bytesToHex(id, .lower); 2722 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex) orelse return; 2723 try self.session.navigate(route); 2724 } 2725 2726 // return to the list. 2727 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 2728 root_focus.setFocus(self.listScroll(index).getFocus().id); 2729 } 2730 2731 // cross to the header tabs above the stack. 2732 fn focusHeader(self: *This, root_focus: *Focus) void { 2733 root_focus.setFocus(self.box.children.keys()[header_index]); 2734 } 2735 2736 pub fn clearGrid(self: *This) void { 2737 self.box.clearGrid(); 2738 } 2739 2740 pub fn getGrid(self: This) ?Grid { 2741 return self.box.getGrid(); 2742 } 2743 2744 pub fn getFocus(self: *This) *Focus { 2745 return self.box.getFocus(); 2746 } 2747 2748 // for the parent's "scroll up at the top jumps to the header" check: at the 2749 // top only while the header tabs hold focus, so up from the split 2750 // crosses into the tabs first. 2751 pub fn atTop(self: *This) bool { 2752 return self.headerActive(); 2753 } 2754 }; 2755 } 2756