╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2357 if (child.rect) |rect| sc.scrollToRect(rect); 2358 } 2359 } 2360 } 2361 2362 // re-emit the conflicted thread's event with the resolved content; any 2363 // event on the thread settles its conflict entry. this is the terminal 2364 // path; the web posts the form to the resolve route. 2365 fn submitResolution(self: *This, allocator: std.mem.Allocator) !void { 2366 if (comptime wasm) return; 2367 const io = self.session.io orelse return; 2368 const src = self.data.repo_source orelse return; 2369 const entry = self.data.selectedThread() orelse return; 2370 const author = (try self.session.eventAuthor()) orelse return; 2371 const form = self.threadForm() orelse return; 2372 2373 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2374 var title: ?[]u8 = null; 2375 var tags: ?[]u8 = null; 2376 var hunks: std.ArrayList([]const u8) = .empty; 2377 defer { 2378 if (title) |t| allocator.free(t); 2379 if (tags) |t| allocator.free(t); 2380 for (hunks.items) |h| allocator.free(h); 2381 hunks.deinit(allocator); 2382 } 2383 for (form.children.values()) |*child| switch (child.widget) { 2384 .text_input => |*ti| { 2385 const text = try ti.text(allocator); 2386 errdefer allocator.free(text); 2387 if (std.mem.eql(u8, ti.options.name, "title")) { 2388 title = text; 2389 } else if (std.mem.eql(u8, ti.options.name, "tags")) { 2390 tags = text; 2391 } else { 2392 try hunks.append(allocator, text); 2393 } 2394 }, 2395 else => {}, 2396 }; 2397 2398 const id_bytes = try evt.parseEventId(entry.id); 2399 2400 switch (src.repo_kind) { 2401 inline else => |repo_kind| { 2402 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2403 defer any_repo.deinit(io, allocator); 2404 switch (any_repo) { 2405 inline else => |*repo| { 2406 var resolution: Event.Resolve = .{ 2407 .title = title, 2408 .tags = tags, 2409 .hunks = hunks.items, 2410 }; 2411 if (comptime @hasField(Event.Resolve, "theirs")) resolution.theirs = self.data.theirs_picks; 2412 const change = Event.Update{ .resolve = resolution }; 2413 Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, change, author) catch |err| switch (err) { 2414 // leave the form up for correction 2415 error.InvalidFields => return, 2416 else => |e| return e, 2417 }; 2418 }, 2419 } 2420 }, 2421 } 2422 2423 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2424 try self.session.navigate(route); 2425 } 2426 2427 // commit a new or edited comment and navigate to its permalink. this is the 2428 // terminal path; the web posts the form to its comment route. 2429 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 2430 if (comptime wasm) return; 2431 const io = self.session.io orelse return; 2432 const src = self.data.repo_source orelse return; 2433 const author = (try self.session.eventAuthor()) orelse return; 2434 const form = self.threadForm() orelse return; 2435 const body_input = &form.children.values()[comment_body_field_index].widget.text_input; 2436 const body = try body_input.text(allocator); 2437 defer allocator.free(body); 2438 if (!evt.Comment.fieldsValid(body)) return; 2439 2440 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2441 @memcpy(&thread_id, self.data.selected_id); 2442 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2443 var parent: [evt.event_id_size * 2]u8 = undefined; 2444 @memcpy(&parent, self.data.comment_id); 2445 break :blk parent; 2446 }; 2447 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2448 const editing = self.data.view == .edit_comment; 2449 switch (src.repo_kind) { 2450 inline else => |repo_kind| { 2451 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2452 defer any_repo.deinit(io, allocator); 2453 switch (any_repo) { 2454 inline else => |*repo| if (editing) { 2455 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2456 try evt.Comment.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &comment_id, body, author); 2457 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 2458 } else { 2459 event_id_hex = try evt.Comment.create(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &parent_id, body, author); 2460 }, 2461 } 2462 }, 2463 } 2464 2465 body_input.clear(allocator); 2466 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2467 try self.session.navigate(route); 2468 } 2469 2470 // the terminal submit paths: the new form commits a new thread event, the 2471 // edit form re-emits the selected thread's event with the form's content. 2472 // the web posts the forms to their routes instead. 2473 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 2474 if (self.data.view == .edit) { 2475 try self.submitEditedThread(allocator); 2476 } else { 2477 try self.submitNewThread(allocator); 2478 } 2479 } 2480 2481 // commit the new thread to the repo's events branch and navigate to it. 2482 // this is the terminal path; the web posts the form to the thread route, 2483 // so the wasm side never runs (or compiles) the repo access below. 2484 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 2485 if (comptime wasm) return; 2486 const io = self.session.io orelse return; 2487 const src = self.data.repo_source orelse return; 2488 const author = (try self.session.eventAuthor()) orelse return; 2489 2490 const form = self.threadForm() orelse return; 2491 const title_input = &form.children.values()[title_field_index].widget.text_input; 2492 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2493 const description_input = &form.children.values()[description_field_index].widget.text_input; 2494 2495 const title = try title_input.text(allocator); 2496 defer allocator.free(title); 2497 const tags = try tags_input.text(allocator); 2498 defer allocator.free(tags); 2499 const description = try description_input.text(allocator); 2500 defer allocator.free(description); 2501 const target_branch = if (comptime supports_drafts) try form.children.values()[target_branch_field_index].widget.text_input.text(allocator) else ""; 2502 defer if (comptime supports_drafts) allocator.free(target_branch); 2503 2504 if (!Event.fieldsValid(title, tags)) { 2505 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, tags, description, target_branch); 2506 return; 2507 } 2508 2509 if (comptime supports_drafts) { 2510 const event_id_hex = Data.createDraft(self.data, self.session, allocator, title, tags, description, target_branch) catch |err| switch (err) { 2511 error.InvalidFields => { 2512 try self.rememberThreadFeedback(.invalid_target_branch, title, tags, description, target_branch); 2513 return; 2514 }, 2515 else => |other| return other, 2516 }; 2517 title_input.clear(allocator); 2518 tags_input.clear(allocator); 2519 description_input.clear(allocator); 2520 form.children.values()[target_branch_field_index].widget.text_input.clear(allocator); 2521 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 2522 try self.session.navigate(route); 2523 return; 2524 } 2525 2526 var id_bytes: [evt.event_id_size]u8 = undefined; 2527 io.random(&id_bytes); 2528 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2529 2530 const event = evt.EventWithId{ 2531 .id = event_id_hex, 2532 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2533 .author = author, 2534 .event = @unionInit(evt.Event, @tagName(kind), Event{ 2535 .title = title, 2536 .description = description, 2537 .tags = tags, 2538 }), 2539 }; 2540 2541 switch (src.repo_kind) { 2542 inline else => |repo_kind| { 2543 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2544 defer any_repo.deinit(io, allocator); 2545 switch (any_repo) { 2546 inline else => |*repo| try evt.consume(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2547 } 2548 }, 2549 } 2550 2551 // wipe the form so a return visit starts fresh 2552 title_input.clear(allocator); 2553 tags_input.clear(allocator); 2554 description_input.clear(allocator); 2555 2556 const route = listRoute(self.data.identity, @enumFromInt(0), "", &event_id_hex) orelse return; 2557 try self.session.navigate(route); 2558 } 2559 2560 // re-emit the selected thread's event with the form's content (status 2561 // preserved), then reload the thread's page. this is the terminal path; 2562 // the web posts the form to the edit route. 2563 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 2564 if (comptime wasm) return; 2565 const io = self.session.io orelse return; 2566 const entry = self.data.selectedThread() orelse return; 2567 2568 const form = self.threadForm() orelse return; 2569 const title_input = &form.children.values()[title_field_index].widget.text_input; 2570 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2571 const description_input = &form.children.values()[description_field_index].widget.text_input; 2572 2573 const title = try title_input.text(allocator); 2574 defer allocator.free(title); 2575 const tags = try tags_input.text(allocator); 2576 defer allocator.free(tags); 2577 const description = try description_input.text(allocator); 2578 defer allocator.free(description); 2579 const target_branch = if (comptime supports_drafts) try form.children.values()[target_branch_field_index].widget.text_input.text(allocator) else ""; 2580 defer if (comptime supports_drafts) allocator.free(target_branch); 2581 2582 if (!Event.fieldsValid(title, tags)) { 2583 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, tags, description, target_branch); 2584 return; 2585 } 2586 const src = self.data.repo_source orelse return; 2587 2588 if (comptime supports_drafts) { 2589 if (entryDraft(entry.*)) { 2590 self.data.editDraft(self.session, allocator, entry.id, title, tags, description, target_branch) catch |err| switch (err) { 2591 error.InvalidFields => { 2592 try self.rememberThreadFeedback(.invalid_target_branch, title, tags, description, target_branch); 2593 return; 2594 }, 2595 else => |other| return other, 2596 }; 2597 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2598 try self.session.navigate(route); 2599 return; 2600 } 2601 } 2602 2603 const id_bytes = try evt.parseEventId(entry.id); 2604 const author = (try self.session.eventAuthor()) orelse return; 2605 2606 switch (src.repo_kind) { 2607 inline else => |repo_kind| { 2608 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2609 defer any_repo.deinit(io, allocator); 2610 switch (any_repo) { 2611 inline else => |*repo| if (supports_drafts) { 2612 Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2613 .title = title, 2614 .tags = tags, 2615 .description = description, 2616 .target_branch = target_branch, 2617 } }, author) catch |err| switch (err) { 2618 error.InvalidFields => { 2619 try self.rememberThreadFeedback(.invalid_target_branch, title, tags, description, target_branch); 2620 return; 2621 }, 2622 else => |other| return other, 2623 }; 2624 } else if (has_status) 2625 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2626 .title = title, 2627 .tags = tags, 2628 .description = description, 2629 } }, author) 2630 else 2631 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, tags, description, author), 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 fn rememberThreadFeedback(self: *This, failure: FeedbackFailure, title: []const u8, tags: []const u8, description: []const u8, target_branch: []const u8) !void { 2641 const aa = self.session.arena.allocator(); 2642 self.session.data.form_feedback = @unionInit(ui.Session.FormFeedback, @tagName(feedback_tag), .{ 2643 .failure = failure, 2644 .fields = if (comptime supports_drafts) .{ 2645 .title = try aa.dupe(u8, title), 2646 .tags = try aa.dupe(u8, tags), 2647 .description = try aa.dupe(u8, description), 2648 .target_branch = try aa.dupe(u8, target_branch), 2649 } else .{ 2650 .title = try aa.dupe(u8, title), 2651 .tags = try aa.dupe(u8, tags), 2652 .description = try aa.dupe(u8, description), 2653 }, 2654 }); 2655 } 2656 2657 fn submitConfirmation(self: *This, allocator: std.mem.Allocator, button_index: usize) !void { 2658 switch (confirmation(self.data.view) orelse return) { 2659 .publish => if (comptime supports_drafts) { 2660 if (comptime wasm) return; 2661 const entry = self.data.selectedThread() orelse return; 2662 if (!entryDraft(entry.*)) return; 2663 try self.data.publishDraft(self.session, allocator, entry.id); 2664 const route = listRoute(self.data.identity, @enumFromInt(0), "", entry.id) orelse return; 2665 try self.session.navigate(route); 2666 } else unreachable, 2667 .merge => if (comptime supports_merge) { 2668 if (comptime wasm) return; 2669 const entry = self.data.selectedThread() orelse return; 2670 try self.data.mergePatch(self.session, allocator, entry.id, button_index > 1); 2671 const merged = std.meta.stringToEnum(Status, "merged") orelse return; 2672 const route = listRoute(self.data.identity, merged, "", entry.id) orelse return; 2673 try self.session.navigate(route); 2674 } else unreachable, 2675 .remove => try self.submitRemove(allocator), 2676 } 2677 } 2678 2679 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 2680 const comment = self.data.comment_id.len != 0; 2681 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 2682 const id = evt.parseEventId(event_id) catch return; 2683 try self.removeEvent(allocator, if (comment) .comment else kind, id); 2684 } 2685 2686 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 2687 if (comptime wasm) return; 2688 2689 if (comptime supports_drafts) { 2690 const selected = self.data.selectedThread() orelse return; 2691 if (event_kind == kind and entryDraft(selected.*)) { 2692 try Data.removeDraft(self.session, allocator, &id); 2693 const route = draftsRoute(self.data.identity) orelse return; 2694 try self.session.navigate(route); 2695 return; 2696 } 2697 } 2698 2699 const io = self.session.io orelse return; 2700 const src = self.data.repo_source orelse return; 2701 const author = (try self.session.eventAuthor()) orelse return; 2702 2703 switch (src.repo_kind) { 2704 inline else => |repo_kind| { 2705 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2706 defer any_repo.deinit(io, allocator); 2707 switch (any_repo) { 2708 inline else => |*repo| try evt.remove(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, &id, event_kind, author), 2709 } 2710 }, 2711 } 2712 2713 const id_hex = std.fmt.bytesToHex(id, .lower); 2714 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex) orelse return; 2715 try self.session.navigate(route); 2716 } 2717 2718 // return to the list. 2719 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 2720 root_focus.setFocus(self.listScroll(index).getFocus().id); 2721 } 2722 2723 // cross to the header tabs above the stack. 2724 fn focusHeader(self: *This, root_focus: *Focus) void { 2725 root_focus.setFocus(self.box.children.keys()[header_index]); 2726 } 2727 2728 pub fn clearGrid(self: *This) void { 2729 self.box.clearGrid(); 2730 } 2731 2732 pub fn getGrid(self: This) ?Grid { 2733 return self.box.getGrid(); 2734 } 2735 2736 pub fn getFocus(self: *This) *Focus { 2737 return self.box.getFocus(); 2738 } 2739 2740 // for the parent's "scroll up at the top jumps to the header" check: at the 2741 // top only while the header tabs hold focus, so up from the split 2742 // crosses into the tabs first. 2743 pub fn getSelectedIndex(self: *This) ?usize { 2744 return if (self.headerActive()) 0 else 1; 2745 } 2746 }; 2747 } 2748