╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2448 const form = self.formBox() orelse return; 2449 const body_input = try formField(form, "body"); 2450 const body = try body_input.text(allocator); 2451 defer allocator.free(body); 2452 if (!evt.Comment.fieldsValid(body)) return; 2453 2454 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2455 @memcpy(&thread_id, self.data.selected_id); 2456 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2457 var parent: [evt.event_id_size * 2]u8 = undefined; 2458 @memcpy(&parent, self.data.comment_id); 2459 break :blk parent; 2460 }; 2461 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2462 const editing = self.data.view == .edit_comment; 2463 switch (src.repo_kind) { 2464 inline else => |repo_kind| { 2465 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2466 defer any_repo.deinit(io, allocator); 2467 switch (any_repo) { 2468 inline else => |*repo| if (editing) { 2469 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2470 try evt.Comment.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &comment_id, body, author); 2471 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 2472 } else { 2473 event_id_hex = try evt.Comment.create(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &parent_id, body, author); 2474 }, 2475 } 2476 }, 2477 } 2478 2479 body_input.clear(allocator); 2480 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2481 try self.session.navigate(route); 2482 } 2483 2484 // terminal form submissions; the web posts to the forms' routes instead 2485 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 2486 switch (self.data.view) { 2487 .new_comment, .edit_comment => try self.submitComment(allocator), 2488 .edit => try self.submitEditedThread(allocator), 2489 else => try self.submitNewThread(allocator), 2490 } 2491 } 2492 2493 // commit the new thread to the repo's events branch and navigate to it. 2494 // this is the terminal path; the web posts the form to the thread route, 2495 // so the wasm side never runs (or compiles) the repo access below. 2496 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 2497 if (comptime wasm) return; 2498 const io = self.session.io orelse return; 2499 const src = self.data.repo_source orelse return; 2500 const author = (try self.session.eventAuthor()) orelse return; 2501 2502 const form = self.threadForm() orelse return; 2503 const title_input = try formField(form, "title"); 2504 const tags_input = try formField(form, "tags"); 2505 const description_input = try formField(form, "description"); 2506 2507 const title = try title_input.text(allocator); 2508 defer allocator.free(title); 2509 const tags = try tags_input.text(allocator); 2510 defer allocator.free(tags); 2511 const description = try description_input.text(allocator); 2512 defer allocator.free(description); 2513 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 2514 defer if (comptime supports_drafts) allocator.free(target_branch); 2515 2516 if (!Event.fieldsValid(title, tags)) { 2517 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, tags, description, target_branch); 2518 return; 2519 } 2520 2521 if (comptime supports_drafts) { 2522 const source = formWidget(form, .patch_source) orelse return error.MissingFormField; 2523 const source_branch = switch (source.selected) { 2524 .fork => null, 2525 .branch => try source.field().text(allocator), 2526 }; 2527 defer if (source_branch) |branch| allocator.free(branch); 2528 const event_id_hex = Data.create(self.data, self.session, allocator, title, tags, description, target_branch, source_branch) catch |err| { 2529 const failure = FeedbackFailure.fromError(err) orelse return err; 2530 try self.rememberThreadFeedback(failure, title, tags, description, target_branch); 2531 return; 2532 }; 2533 title_input.clear(allocator); 2534 tags_input.clear(allocator); 2535 description_input.clear(allocator); 2536 (try formField(form, "target_branch")).clear(allocator); 2537 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 2538 try self.session.navigate(route); 2539 return; 2540 } 2541 2542 var id_bytes: [evt.event_id_size]u8 = undefined; 2543 io.random(&id_bytes); 2544 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2545 2546 const event = evt.EventWithId{ 2547 .id = event_id_hex, 2548 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2549 .author = author, 2550 .event = @unionInit(evt.Event, @tagName(kind), Event{ 2551 .title = title, 2552 .description = description, 2553 .tags = tags, 2554 }), 2555 }; 2556 2557 switch (src.repo_kind) { 2558 inline else => |repo_kind| { 2559 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2560 defer any_repo.deinit(io, allocator); 2561 switch (any_repo) { 2562 inline else => |*repo| try evt.consume(self.session.data.host_kind, .repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2563 } 2564 }, 2565 } 2566 2567 // wipe the form so a return visit starts fresh 2568 title_input.clear(allocator); 2569 tags_input.clear(allocator); 2570 description_input.clear(allocator); 2571 2572 const route = listRoute(self.data.identity, @enumFromInt(0), "", &event_id_hex) orelse return; 2573 try self.session.navigate(route); 2574 } 2575 2576 // re-emit the selected thread's event with the form's content (status 2577 // preserved), then reload the thread's page. this is the terminal path; 2578 // the web posts the form to the edit route. 2579 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 2580 if (comptime wasm) return; 2581 const io = self.session.io orelse return; 2582 const entry = self.data.selectedThread() orelse return; 2583 2584 const form = self.threadForm() orelse return; 2585 const title_input = try formField(form, "title"); 2586 const tags_input = try formField(form, "tags"); 2587 const description_input = try formField(form, "description"); 2588 2589 const title = try title_input.text(allocator); 2590 defer allocator.free(title); 2591 const tags = try tags_input.text(allocator); 2592 defer allocator.free(tags); 2593 const description = try description_input.text(allocator); 2594 defer allocator.free(description); 2595 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 2596 defer if (comptime supports_drafts) allocator.free(target_branch); 2597 2598 if (!Event.fieldsValid(title, tags)) { 2599 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, tags, description, target_branch); 2600 return; 2601 } 2602 const src = self.data.repo_source orelse return; 2603 2604 if (comptime supports_drafts) { 2605 if (entryDraft(entry.*)) { 2606 self.data.editDraft(self.session, allocator, entry.id, title, tags, description, target_branch) catch |err| { 2607 const failure = FeedbackFailure.fromError(err) orelse return err; 2608 try self.rememberThreadFeedback(failure, title, tags, description, target_branch); 2609 return; 2610 }; 2611 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2612 try self.session.navigate(route); 2613 return; 2614 } 2615 } 2616 2617 const id_bytes = try evt.parseEventId(entry.id); 2618 const author = (try self.session.eventAuthor()) orelse return; 2619 2620 switch (src.repo_kind) { 2621 inline else => |repo_kind| { 2622 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2623 defer any_repo.deinit(io, allocator); 2624 switch (any_repo) { 2625 inline else => |*repo| if (supports_drafts) { 2626 Event.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2627 .title = title, 2628 .tags = tags, 2629 .description = description, 2630 .target_branch = target_branch, 2631 } }, author) catch |err| { 2632 const failure = FeedbackFailure.fromError(err) orelse return err; 2633 try self.rememberThreadFeedback(failure, title, tags, description, target_branch); 2634 return; 2635 }; 2636 } else if (has_status) 2637 try Event.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2638 .title = title, 2639 .tags = tags, 2640 .description = description, 2641 } }, author) 2642 else 2643 try Event.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, tags, description, author), 2644 } 2645 }, 2646 } 2647 2648 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2649 try self.session.navigate(route); 2650 } 2651 2652 fn rememberThreadFeedback(self: *This, failure: FeedbackFailure, title: []const u8, tags: []const u8, description: []const u8, target_branch: []const u8) !void { 2653 const aa = self.session.arena.allocator(); 2654 const source = if (supports_drafts) blk: { 2655 const form = self.threadForm() orelse break :blk null; 2656 break :blk formWidget(form, .patch_source); 2657 } else null; 2658 self.session.data.form_feedback = @unionInit(ui.Session.FormFeedback, @tagName(feedback_tag), .{ 2659 .failure = failure, 2660 .fields = if (comptime supports_drafts) .{ 2661 .title = try aa.dupe(u8, title), 2662 .tags = try aa.dupe(u8, tags), 2663 .description = try aa.dupe(u8, description), 2664 .target_branch = try aa.dupe(u8, target_branch), 2665 .source_branch = if (source) |value| try value.field().text(aa) else "", 2666 .source_kind = if (source) |value| value.selected else .fork, 2667 } else .{ 2668 .title = try aa.dupe(u8, title), 2669 .tags = try aa.dupe(u8, tags), 2670 .description = try aa.dupe(u8, description), 2671 }, 2672 }); 2673 } 2674 2675 fn submitConfirmation(self: *This, allocator: std.mem.Allocator, button_index: usize) !void { 2676 switch (confirmation(self.data.view) orelse return) { 2677 .publish => if (comptime supports_drafts) { 2678 if (comptime wasm) return; 2679 const entry = self.data.selectedThread() orelse return; 2680 if (!entryDraft(entry.*)) return; 2681 try self.data.publishDraft(self.session, allocator, entry.id); 2682 const route = listRoute(self.data.identity, @enumFromInt(0), "", entry.id) orelse return; 2683 try self.session.navigate(route); 2684 } else unreachable, 2685 .merge => if (comptime supports_merge) { 2686 if (comptime wasm) return; 2687 const entry = self.data.selectedThread() orelse return; 2688 const revision: evt.Patch.MergeRevision = if (button_index > 1) .squash else .source; 2689 if (!Data.canMerge(entry.*, self.session) or entry.mergeability.get(revision) != .clean) return; 2690 self.data.mergePatch(self.session, allocator, entry.id, revision) catch |err| switch (err) { 2691 error.MergeConflict, error.MergeCheckUnavailable, error.PatchOutOfDate, error.PatchDataUnavailable => { 2692 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2693 try self.session.navigate(route); 2694 return; 2695 }, 2696 else => return err, 2697 }; 2698 const merged = std.meta.stringToEnum(Status, "merged") orelse return; 2699 const route = listRoute(self.data.identity, merged, "", entry.id) orelse return; 2700 try self.session.navigate(route); 2701 } else unreachable, 2702 .remove => try self.submitRemove(allocator), 2703 } 2704 } 2705 2706 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 2707 const comment = self.data.comment_id.len != 0; 2708 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 2709 const id = evt.parseEventId(event_id) catch return; 2710 try self.removeEvent(allocator, if (comment) .comment else kind, id); 2711 } 2712 2713 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 2714 if (comptime wasm) return; 2715 2716 if (comptime supports_drafts) { 2717 const selected = self.data.selectedThread() orelse return; 2718 if (event_kind == kind and entryDraft(selected.*)) { 2719 try Data.removeDraft(self.session, allocator, &id); 2720 const route = draftsRoute(self.data.identity) orelse return; 2721 try self.session.navigate(route); 2722 return; 2723 } 2724 } 2725 2726 const io = self.session.io orelse return; 2727 const src = self.data.repo_source orelse return; 2728 const author = (try self.session.eventAuthor()) orelse return; 2729 2730 switch (src.repo_kind) { 2731 inline else => |repo_kind| { 2732 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2733 defer any_repo.deinit(io, allocator); 2734 switch (any_repo) { 2735 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), 2736 } 2737 }, 2738 } 2739 2740 const id_hex = std.fmt.bytesToHex(id, .lower); 2741 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex) orelse return; 2742 try self.session.navigate(route); 2743 } 2744 2745 // return to the list. 2746 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 2747 root_focus.setFocus(self.listScroll(index).getFocus().id); 2748 } 2749 2750 // cross to the header tabs above the stack. 2751 fn focusHeader(self: *This, root_focus: *Focus) void { 2752 root_focus.setFocus(self.box.children.keys()[header_index]); 2753 } 2754 2755 pub fn clearGrid(self: *This) void { 2756 self.box.clearGrid(); 2757 } 2758 2759 pub fn getGrid(self: This) ?Grid { 2760 return self.box.getGrid(); 2761 } 2762 2763 pub fn getFocus(self: *This) *Focus { 2764 return self.box.getFocus(); 2765 } 2766 2767 // for the parent's "scroll up at the top jumps to the header" check: at the 2768 // top only while the header tabs hold focus, so up from the split 2769 // crosses into the tabs first. 2770 pub fn atTop(self: *This) bool { 2771 return self.headerActive(); 2772 } 2773 }; 2774 } 2775