╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2329 .enter => true, 2330 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 2331 .arrow_down => !try text_input.cursorOnLastRow(allocator), 2332 else => false, 2333 }; 2334 } 2335 2336 // the neighboring focusable resolve-form child, skipping the spacer and 2337 // the blank gap rows 2338 fn resolveStep(form: *wgt.Box(Widget), cur: usize, down: bool) ?usize { 2339 var i = cur; 2340 while (true) { 2341 if (down) { 2342 i += 1; 2343 if (i >= form.children.count()) return null; 2344 } else { 2345 if (i == 0) return null; 2346 i -= 1; 2347 } 2348 switch (form.children.values()[i].widget) { 2349 .spacer, .text => continue, 2350 else => return i, 2351 } 2352 } 2353 } 2354 2355 // focus the resolve-form child at `i` (a version row focuses its selected 2356 // child) and keep it visible on the terminal 2357 fn focusResolveChild(self: *This, form: *wgt.Box(Widget), i: usize, root_focus: *Focus) void { 2358 const child = &form.children.values()[i]; 2359 switch (child.widget) { 2360 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2361 else => root_focus.setFocus(form.children.keys()[i]), 2362 } 2363 if (self.session.is_terminal) { 2364 if (self.resolveScroll()) |sc| { 2365 if (child.rect) |rect| sc.scrollToRect(rect); 2366 } 2367 } 2368 } 2369 2370 // re-emit the conflicted thread's event with the resolved content; any 2371 // event on the thread settles its conflict entry. this is the terminal 2372 // path; the web posts the form to the resolve route. 2373 fn submitResolution(self: *This, allocator: std.mem.Allocator) !void { 2374 if (comptime wasm) return; 2375 const io = self.session.io orelse return; 2376 const src = self.data.repo_source orelse return; 2377 const entry = self.data.selectedThread() orelse return; 2378 const author = (try self.session.eventAuthor()) orelse return; 2379 const form = self.formBox() orelse return; 2380 2381 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2382 var title: ?[]u8 = null; 2383 var tags: ?[]u8 = null; 2384 var hunks: std.ArrayList([]const u8) = .empty; 2385 defer { 2386 if (title) |t| allocator.free(t); 2387 if (tags) |t| allocator.free(t); 2388 for (hunks.items) |h| allocator.free(h); 2389 hunks.deinit(allocator); 2390 } 2391 for (form.children.values()) |*child| switch (child.widget) { 2392 .text_input => |*ti| { 2393 const text = try ti.text(allocator); 2394 errdefer allocator.free(text); 2395 if (std.mem.eql(u8, ti.options.name, "title")) { 2396 title = text; 2397 } else if (std.mem.eql(u8, ti.options.name, "tags")) { 2398 tags = text; 2399 } else { 2400 try hunks.append(allocator, text); 2401 } 2402 }, 2403 else => {}, 2404 }; 2405 2406 const id_bytes = try evt.parseEventId(entry.id); 2407 2408 switch (src.repo_kind) { 2409 inline else => |repo_kind| { 2410 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2411 defer any_repo.deinit(io, allocator); 2412 switch (any_repo) { 2413 inline else => |*repo| { 2414 var resolution: Event.Resolve = .{ 2415 .title = title, 2416 .tags = tags, 2417 .hunks = hunks.items, 2418 }; 2419 if (comptime @hasField(Event.Resolve, "theirs")) resolution.theirs = self.data.theirs_picks; 2420 const change = Event.Update{ .resolve = resolution }; 2421 Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, change, author) catch |err| switch (err) { 2422 // leave the form up for correction 2423 error.InvalidFields => return, 2424 else => |e| return e, 2425 }; 2426 }, 2427 } 2428 }, 2429 } 2430 2431 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2432 try self.session.navigate(route); 2433 } 2434 2435 // commit a new or edited comment and navigate to its permalink. this is the 2436 // terminal path; the web posts the form to its comment route. 2437 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 2438 if (comptime wasm) return; 2439 const io = self.session.io orelse return; 2440 const src = self.data.repo_source orelse return; 2441 const author = (try self.session.eventAuthor()) orelse return; 2442 const form = self.formBox() orelse return; 2443 const body_input = &form.children.values()[comment_body_field_index].widget.text_input; 2444 const body = try body_input.text(allocator); 2445 defer allocator.free(body); 2446 if (!evt.Comment.fieldsValid(body)) return; 2447 2448 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2449 @memcpy(&thread_id, self.data.selected_id); 2450 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2451 var parent: [evt.event_id_size * 2]u8 = undefined; 2452 @memcpy(&parent, self.data.comment_id); 2453 break :blk parent; 2454 }; 2455 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2456 const editing = self.data.view == .edit_comment; 2457 switch (src.repo_kind) { 2458 inline else => |repo_kind| { 2459 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2460 defer any_repo.deinit(io, allocator); 2461 switch (any_repo) { 2462 inline else => |*repo| if (editing) { 2463 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2464 try evt.Comment.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &comment_id, body, author); 2465 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 2466 } else { 2467 event_id_hex = try evt.Comment.create(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &parent_id, body, author); 2468 }, 2469 } 2470 }, 2471 } 2472 2473 body_input.clear(allocator); 2474 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2475 try self.session.navigate(route); 2476 } 2477 2478 // the terminal submit paths: the new form commits a new thread event, the 2479 // edit form re-emits the selected thread's event with the form's content. 2480 // the web posts the forms to their routes instead. 2481 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 2482 if (self.data.view == .edit) { 2483 try self.submitEditedThread(allocator); 2484 } else { 2485 try self.submitNewThread(allocator); 2486 } 2487 } 2488 2489 // commit the new thread to the repo's events branch and navigate to it. 2490 // this is the terminal path; the web posts the form to the thread route, 2491 // so the wasm side never runs (or compiles) the repo access below. 2492 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 2493 if (comptime wasm) return; 2494 const io = self.session.io orelse return; 2495 const src = self.data.repo_source orelse return; 2496 const author = (try self.session.eventAuthor()) orelse return; 2497 2498 const form = self.threadForm() orelse return; 2499 const title_input = &form.children.values()[title_field_index].widget.text_input; 2500 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2501 const description_input = &form.children.values()[description_field_index].widget.text_input; 2502 2503 const title = try title_input.text(allocator); 2504 defer allocator.free(title); 2505 const tags = try tags_input.text(allocator); 2506 defer allocator.free(tags); 2507 const description = try description_input.text(allocator); 2508 defer allocator.free(description); 2509 const target_branch = if (comptime supports_drafts) try form.children.values()[target_branch_field_index].widget.text_input.text(allocator) else ""; 2510 defer if (comptime supports_drafts) allocator.free(target_branch); 2511 2512 if (!Event.fieldsValid(title, tags)) { 2513 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, tags, description, target_branch); 2514 return; 2515 } 2516 2517 if (comptime supports_drafts) { 2518 const event_id_hex = Data.createDraft(self.data, self.session, allocator, title, tags, description, target_branch) catch |err| switch (err) { 2519 error.InvalidFields => { 2520 try self.rememberThreadFeedback(.invalid_target_branch, title, tags, description, target_branch); 2521 return; 2522 }, 2523 else => |other| return other, 2524 }; 2525 title_input.clear(allocator); 2526 tags_input.clear(allocator); 2527 description_input.clear(allocator); 2528 form.children.values()[target_branch_field_index].widget.text_input.clear(allocator); 2529 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 2530 try self.session.navigate(route); 2531 return; 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