╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2490 } 2491 2492 var id_bytes: [evt.event_id_size]u8 = undefined; 2493 io.random(&id_bytes); 2494 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2495 2496 const event = evt.EventWithId{ 2497 .id = event_id_hex, 2498 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2499 .author = author, 2500 .event = @unionInit(evt.Event, @tagName(kind), Event{ 2501 .title = title, 2502 .description = description, 2503 .tags = tags, 2504 }), 2505 }; 2506 2507 switch (src.repo_kind) { 2508 inline else => |repo_kind| { 2509 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2510 defer any_repo.deinit(io, allocator); 2511 switch (any_repo) { 2512 inline else => |*repo| try evt.consume(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2513 } 2514 }, 2515 } 2516 2517 // wipe the form so a return visit starts fresh 2518 title_input.clear(allocator); 2519 tags_input.clear(allocator); 2520 description_input.clear(allocator); 2521 2522 const route = listRoute(self.data.identity, @enumFromInt(0), "", &event_id_hex) orelse return; 2523 try self.session.navigate(route); 2524 } 2525 2526 // re-emit the selected thread's event with the form's content (status 2527 // preserved), then reload the thread's page. this is the terminal path; 2528 // the web posts the form to the edit route. 2529 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 2530 if (comptime wasm) return; 2531 const io = self.session.io orelse return; 2532 const entry = self.data.selectedThread() orelse return; 2533 2534 const form = self.threadForm() orelse return; 2535 const title_input = &form.children.values()[title_field_index].widget.text_input; 2536 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2537 const description_input = &form.children.values()[description_field_index].widget.text_input; 2538 2539 const title = try title_input.text(allocator); 2540 defer allocator.free(title); 2541 const tags = try tags_input.text(allocator); 2542 defer allocator.free(tags); 2543 const description = try description_input.text(allocator); 2544 defer allocator.free(description); 2545 const target_branch = if (comptime supports_drafts) try form.children.values()[target_branch_field_index].widget.text_input.text(allocator) else ""; 2546 defer if (comptime supports_drafts) allocator.free(target_branch); 2547 2548 if (!Event.fieldsValid(title, tags)) return; 2549 2550 if (comptime supports_drafts) { 2551 if (entryDraft(entry.*)) { 2552 self.data.editDraft(self.session, allocator, entry.id, title, tags, description, target_branch) catch return; 2553 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2554 try self.session.navigate(route); 2555 return; 2556 } 2557 } 2558 2559 const id_bytes = try evt.parseEventId(entry.id); 2560 const src = self.data.repo_source orelse return; 2561 const author = (try self.session.eventAuthor()) orelse return; 2562 2563 switch (src.repo_kind) { 2564 inline else => |repo_kind| { 2565 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2566 defer any_repo.deinit(io, allocator); 2567 switch (any_repo) { 2568 inline else => |*repo| if (supports_drafts) { 2569 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2570 .title = title, 2571 .tags = tags, 2572 .description = description, 2573 .target_branch = target_branch, 2574 } }, author); 2575 } else if (has_status) 2576 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2577 .title = title, 2578 .tags = tags, 2579 .description = description, 2580 } }, author) 2581 else 2582 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, tags, description, author), 2583 } 2584 }, 2585 } 2586 2587 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2588 try self.session.navigate(route); 2589 } 2590 2591 fn submitConfirmation(self: *This, allocator: std.mem.Allocator, button_index: usize) !void { 2592 switch (confirmation(self.data.view) orelse return) { 2593 .publish => if (comptime supports_drafts) { 2594 if (comptime wasm) return; 2595 const entry = self.data.selectedThread() orelse return; 2596 if (!entryDraft(entry.*)) return; 2597 try self.data.publishDraft(self.session, allocator, entry.id); 2598 const route = listRoute(self.data.identity, @enumFromInt(0), "", entry.id) orelse return; 2599 try self.session.navigate(route); 2600 } else unreachable, 2601 .merge => if (comptime supports_merge) { 2602 if (comptime wasm) return; 2603 const entry = self.data.selectedThread() orelse return; 2604 try self.data.mergePatch(self.session, allocator, entry.id, button_index > 1); 2605 const merged = std.meta.stringToEnum(Status, "merged") orelse return; 2606 const route = listRoute(self.data.identity, merged, "", entry.id) orelse return; 2607 try self.session.navigate(route); 2608 } else unreachable, 2609 .remove => try self.submitRemove(allocator), 2610 } 2611 } 2612 2613 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 2614 const comment = self.data.comment_id.len != 0; 2615 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 2616 const id = evt.parseEventId(event_id) catch return; 2617 try self.removeEvent(allocator, if (comment) .comment else kind, id); 2618 } 2619 2620 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 2621 if (comptime wasm) return; 2622 2623 if (comptime supports_drafts) { 2624 const selected = self.data.selectedThread() orelse return; 2625 if (event_kind == kind and entryDraft(selected.*)) { 2626 try Data.removeDraft(self.session, allocator, &id); 2627 const route = draftsRoute(self.data.identity) orelse return; 2628 try self.session.navigate(route); 2629 return; 2630 } 2631 } 2632 2633 const io = self.session.io orelse return; 2634 const src = self.data.repo_source orelse return; 2635 const author = (try self.session.eventAuthor()) orelse return; 2636 2637 switch (src.repo_kind) { 2638 inline else => |repo_kind| { 2639 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2640 defer any_repo.deinit(io, allocator); 2641 switch (any_repo) { 2642 inline else => |*repo| try evt.remove(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, &id, event_kind, author), 2643 } 2644 }, 2645 } 2646 2647 const id_hex = std.fmt.bytesToHex(id, .lower); 2648 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex) orelse return; 2649 try self.session.navigate(route); 2650 } 2651 2652 // return to the list. 2653 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 2654 root_focus.setFocus(self.listScroll(index).getFocus().id); 2655 } 2656 2657 // cross to the header tabs above the stack. 2658 fn focusHeader(self: *This, root_focus: *Focus) void { 2659 root_focus.setFocus(self.box.children.keys()[header_index]); 2660 } 2661 2662 pub fn clearGrid(self: *This) void { 2663 self.box.clearGrid(); 2664 } 2665 2666 pub fn getGrid(self: This) ?Grid { 2667 return self.box.getGrid(); 2668 } 2669 2670 pub fn getFocus(self: *This) *Focus { 2671 return self.box.getFocus(); 2672 } 2673 2674 // for the parent's "scroll up at the top jumps to the header" check: at the 2675 // top only while the header tabs hold focus, so up from the split 2676 // crosses into the tabs first. 2677 pub fn getSelectedIndex(self: *This) ?usize { 2678 return if (self.headerActive()) 0 else 1; 2679 } 2680 }; 2681 } 2682