╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2343 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2344 @memcpy(&thread_id, self.data.selected_id); 2345 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2346 var parent: [evt.event_id_size * 2]u8 = undefined; 2347 @memcpy(&parent, self.data.comment_id); 2348 break :blk parent; 2349 }; 2350 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2351 const editing = self.data.view == .edit_comment; 2352 switch (src.repo_kind) { 2353 inline else => |repo_kind| { 2354 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2355 defer any_repo.deinit(io, allocator); 2356 switch (any_repo) { 2357 inline else => |*repo| if (editing) { 2358 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2359 try evt.Comment.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &comment_id, body, author); 2360 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 2361 } else { 2362 event_id_hex = try evt.Comment.create(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &parent_id, body, author); 2363 }, 2364 } 2365 }, 2366 } 2367 2368 body_input.clear(allocator); 2369 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2370 try self.session.navigate(route); 2371 } 2372 2373 // the terminal submit paths: the new form commits a new thread event, the 2374 // edit form re-emits the selected thread's event with the form's content. 2375 // the web posts the forms to their routes instead. 2376 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 2377 if (self.data.view == .edit) { 2378 try self.submitEditedThread(allocator); 2379 } else { 2380 try self.submitNewThread(allocator); 2381 } 2382 } 2383 2384 // commit the new thread to the repo's events branch and navigate to it. 2385 // this is the terminal path; the web posts the form to the thread route, 2386 // so the wasm side never runs (or compiles) the repo access below. 2387 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 2388 if (comptime wasm) return; 2389 const io = self.session.io orelse return; 2390 const src = self.data.repo_source orelse return; 2391 const author = (try self.session.eventAuthor()) orelse return; 2392 2393 const form = self.threadForm() orelse return; 2394 const title_input = &form.children.values()[title_field_index].widget.text_input; 2395 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2396 const description_input = &form.children.values()[description_field_index].widget.text_input; 2397 2398 const title = try title_input.text(allocator); 2399 defer allocator.free(title); 2400 const tags = try tags_input.text(allocator); 2401 defer allocator.free(tags); 2402 const description = try description_input.text(allocator); 2403 defer allocator.free(description); 2404 2405 if (!Event.fieldsValid(title, tags)) return; 2406 2407 if (comptime supports_drafts) { 2408 const event_id_hex = try Data.createDraft(self.data, self.session, allocator, title, tags, description); 2409 title_input.clear(allocator); 2410 tags_input.clear(allocator); 2411 description_input.clear(allocator); 2412 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 2413 try self.session.navigate(route); 2414 return; 2415 } 2416 2417 var id_bytes: [evt.event_id_size]u8 = undefined; 2418 io.random(&id_bytes); 2419 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2420 2421 const event = evt.EventWithId{ 2422 .id = event_id_hex, 2423 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2424 .author = author, 2425 .event = @unionInit(evt.Event, @tagName(kind), Event{ 2426 .title = title, 2427 .description = description, 2428 .tags = tags, 2429 }), 2430 }; 2431 2432 switch (src.repo_kind) { 2433 inline else => |repo_kind| { 2434 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2435 defer any_repo.deinit(io, allocator); 2436 switch (any_repo) { 2437 inline else => |*repo| try evt.consume(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2438 } 2439 }, 2440 } 2441 2442 // wipe the form so a return visit starts fresh 2443 title_input.clear(allocator); 2444 tags_input.clear(allocator); 2445 description_input.clear(allocator); 2446 2447 const route = listRoute(self.data.identity, @enumFromInt(0), "", &event_id_hex) orelse return; 2448 try self.session.navigate(route); 2449 } 2450 2451 // re-emit the selected thread's event with the form's content (status 2452 // preserved), then reload the thread's page. this is the terminal path; 2453 // the web posts the form to the edit route. 2454 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 2455 if (comptime wasm) return; 2456 const io = self.session.io orelse return; 2457 const entry = self.data.selectedThread() orelse return; 2458 2459 const form = self.threadForm() orelse return; 2460 const title_input = &form.children.values()[title_field_index].widget.text_input; 2461 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2462 const description_input = &form.children.values()[description_field_index].widget.text_input; 2463 2464 const title = try title_input.text(allocator); 2465 defer allocator.free(title); 2466 const tags = try tags_input.text(allocator); 2467 defer allocator.free(tags); 2468 const description = try description_input.text(allocator); 2469 defer allocator.free(description); 2470 2471 if (!Event.fieldsValid(title, tags)) return; 2472 2473 if (comptime supports_drafts) { 2474 if (entryDraft(entry.*)) { 2475 try self.data.editDraft(self.session, allocator, entry.id, title, tags, description); 2476 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2477 try self.session.navigate(route); 2478 return; 2479 } 2480 } 2481 2482 const id_bytes = try evt.parseEventId(entry.id); 2483 const src = self.data.repo_source orelse return; 2484 const author = (try self.session.eventAuthor()) orelse return; 2485 2486 switch (src.repo_kind) { 2487 inline else => |repo_kind| { 2488 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2489 defer any_repo.deinit(io, allocator); 2490 switch (any_repo) { 2491 inline else => |*repo| if (has_status) 2492 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2493 .title = title, 2494 .tags = tags, 2495 .description = description, 2496 } }, author) 2497 else 2498 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, tags, description, author), 2499 } 2500 }, 2501 } 2502 2503 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2504 try self.session.navigate(route); 2505 } 2506 2507 fn submitConfirmation(self: *This, allocator: std.mem.Allocator) !void { 2508 switch (confirmation(self.data.view) orelse return) { 2509 .publish => if (comptime supports_drafts) { 2510 if (comptime wasm) return; 2511 const entry = self.data.selectedThread() orelse return; 2512 if (!entryDraft(entry.*)) return; 2513 try self.data.publishDraft(self.session, allocator, entry.id); 2514 const route = listRoute(self.data.identity, @enumFromInt(0), "", entry.id) orelse return; 2515 try self.session.navigate(route); 2516 } else unreachable, 2517 .remove => try self.submitRemove(allocator), 2518 } 2519 } 2520 2521 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 2522 const comment = self.data.comment_id.len != 0; 2523 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 2524 const id = evt.parseEventId(event_id) catch return; 2525 try self.removeEvent(allocator, if (comment) .comment else kind, id); 2526 } 2527 2528 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 2529 if (comptime wasm) return; 2530 2531 if (comptime supports_drafts) { 2532 const selected = self.data.selectedThread() orelse return; 2533 if (event_kind == kind and entryDraft(selected.*)) { 2534 try Data.removeDraft(self.session, allocator, &id); 2535 const route = draftsRoute(self.data.identity) orelse return; 2536 try self.session.navigate(route); 2537 return; 2538 } 2539 } 2540 2541 const io = self.session.io orelse return; 2542 const src = self.data.repo_source orelse return; 2543 const author = (try self.session.eventAuthor()) orelse return; 2544 2545 switch (src.repo_kind) { 2546 inline else => |repo_kind| { 2547 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2548 defer any_repo.deinit(io, allocator); 2549 switch (any_repo) { 2550 inline else => |*repo| try evt.remove(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, &id, event_kind, author), 2551 } 2552 }, 2553 } 2554 2555 const id_hex = std.fmt.bytesToHex(id, .lower); 2556 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex) orelse return; 2557 try self.session.navigate(route); 2558 } 2559 2560 // return to the list. 2561 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 2562 root_focus.setFocus(self.listScroll(index).getFocus().id); 2563 } 2564 2565 // cross to the header tabs above the stack. 2566 fn focusHeader(self: *This, root_focus: *Focus) void { 2567 root_focus.setFocus(self.box.children.keys()[header_index]); 2568 } 2569 2570 pub fn clearGrid(self: *This) void { 2571 self.box.clearGrid(); 2572 } 2573 2574 pub fn getGrid(self: This) ?Grid { 2575 return self.box.getGrid(); 2576 } 2577 2578 pub fn getFocus(self: *This) *Focus { 2579 return self.box.getFocus(); 2580 } 2581 2582 // for the parent's "scroll up at the top jumps to the header" check: at the 2583 // top only while the header tabs hold focus, so up from the split 2584 // crosses into the tabs first. 2585 pub fn getSelectedIndex(self: *This) ?usize { 2586 return if (self.headerActive()) 0 else 1; 2587 } 2588 }; 2589 } 2590