╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2152 // something to focus; other keys move the tabs. 2153 if (inp.vertDirection(key) == .down) { 2154 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 2155 .tag_flow => |*tf| tf.text_boxes.items.len > 0, 2156 // nothing focusable in the unauthorized view 2157 .unauthorized => false, 2158 else => true, 2159 } else false; 2160 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 2161 } else { 2162 try self.header().input(allocator, key, root_focus); 2163 } 2164 return; 2165 } 2166 if (self.tagsViewActive()) { 2167 self.tagsViewInput(key, root_focus); 2168 return; 2169 } 2170 if (self.formViewActive()) { 2171 if (confirmation(self.data.view) != null) { 2172 try self.confirmationFormInput(allocator, key, root_focus); 2173 } else if (supports_conflicts and self.data.view == .resolve) { 2174 try self.resolveInput(allocator, key, root_focus); 2175 } else if (self.data.view == .new_comment or self.data.view == .edit_comment) { 2176 try self.commentFormInput(allocator, key, root_focus); 2177 } else { 2178 try self.formInput(allocator, key, root_focus); 2179 } 2180 return; 2181 } 2182 const i = self.selectedSplitIndex() orelse return; 2183 if (self.detailActive(i)) { 2184 switch (try self.detail(i).inputWithExit(allocator, key, root_focus)) { 2185 .none => {}, 2186 .list => self.focusList(i, root_focus), 2187 .header => self.focusHeader(root_focus), 2188 } 2189 } else { 2190 self.listInput(i, key, root_focus); 2191 } 2192 } 2193 2194 // arrow keys move the tag selection; up from the top row crosses to the 2195 // header tabs. 2196 fn tagsViewInput(self: *This, key: Key, root_focus: *Focus) void { 2197 const tf = self.tagsView(); 2198 const cid = tf.focus.child_id orelse return; 2199 const cur = tf.indexOfFocusId(cid) orelse return; 2200 const count = tf.text_boxes.items.len; 2201 switch (key) { 2202 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 2203 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 2204 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 2205 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 2206 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 2207 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 2208 else => {}, 2209 } 2210 } 2211 2212 fn listInput(self: *This, index: usize, key: Key, root_focus: *Focus) void { 2213 // up/down (and the scroll wheel) move the selection a row; page up/down 2214 // jump a fixed amount. right/Enter cross into the detail pane. up from 2215 // the top row crosses into the header tabs. 2216 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 2217 const lb = self.listBox(index); 2218 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 2219 if (delta < 0 and at_top) return self.focusHeader(root_focus); 2220 moveRowFocus(lb, self.listScroll(index), root_focus, delta); 2221 return; 2222 } 2223 switch (key) { 2224 .enter, .arrow_right => _ = self.detail(index).focusFirst(root_focus), 2225 else => {}, 2226 } 2227 } 2228 2229 fn formInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2230 const form = self.threadForm() orelse return; 2231 const cid = form.getFocus().child_id orelse return; 2232 const cur = form.children.getIndex(cid) orelse return; 2233 2234 if (cur == description_field_index) { 2235 const child = &form.children.values()[cur]; 2236 if (try multilineKeepsKey(&child.widget.text_input, allocator, key)) 2237 return child.widget.input(allocator, key, root_focus); 2238 } 2239 2240 switch (key) { 2241 .arrow_up, .back_tab => if (cur > 0) 2242 root_focus.setFocus(form.children.keys()[cur - 1]) 2243 else 2244 self.focusHeader(root_focus), 2245 .arrow_down, .tab => if (cur < submit_field_index) { 2246 root_focus.setFocus(form.children.keys()[cur + 1]); 2247 }, 2248 .enter => if (cur == submit_field_index) try self.submitForm(allocator), 2249 .mouse => |mouse| if (cur == submit_field_index) { 2250 const submit = &form.children.values()[cur].widget.submit_button; 2251 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitForm(allocator); 2252 }, 2253 else => try form.children.values()[cur].widget.input(allocator, key, root_focus), 2254 } 2255 } 2256 2257 fn commentFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2258 const form = self.formBox() orelse return; 2259 const cid = form.getFocus().child_id orelse return; 2260 const cur = form.children.getIndex(cid) orelse return; 2261 const child = &form.children.values()[cur]; 2262 2263 if (cur == comment_body_field_index and try multilineKeepsKey(&child.widget.text_input, allocator, key)) 2264 return child.widget.input(allocator, key, root_focus); 2265 2266 switch (key) { 2267 .arrow_up, .back_tab => if (cur > comment_author_field_index) 2268 root_focus.setFocus(form.children.keys()[cur - 1]) 2269 else 2270 self.focusHeader(root_focus), 2271 .arrow_down, .tab => if (cur < comment_submit_field_index) 2272 root_focus.setFocus(form.children.keys()[cur + 1]), 2273 .enter => if (cur == comment_submit_field_index) try self.submitComment(allocator), 2274 .mouse => |mouse| if (cur == comment_submit_field_index) { 2275 const submit = &child.widget.submit_button; 2276 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitComment(allocator); 2277 }, 2278 else => try child.widget.input(allocator, key, root_focus), 2279 } 2280 } 2281 2282 fn confirmationFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2283 const form = self.confirmationForm() orelse return; 2284 const child_id = form.getFocus().child_id orelse return; 2285 const index = form.children.getIndex(child_id) orelse return; 2286 switch (key) { 2287 .arrow_up, .back_tab => if (index > 1) 2288 root_focus.setFocus(form.children.keys()[index - 1]) 2289 else 2290 self.focusHeader(root_focus), 2291 .arrow_down, .tab => if (index + 1 < form.children.count()) root_focus.setFocus(form.children.keys()[index + 1]), 2292 .enter => try self.submitConfirmation(allocator, index), 2293 .mouse => |mouse| for (form.children.keys()[1..], 1..) |button_id, button_index| { 2294 if (inp.leftClickOn(root_focus, button_id, mouse)) { 2295 try self.submitConfirmation(allocator, button_index); 2296 break; 2297 } 2298 }, 2299 else => {}, 2300 } 2301 } 2302 2303 // the multiline hunk inputs keep enter and any up/down that has a row to 2304 // move to; enter on a "use this" link is a navigation the host follows 2305 // before this runs. 2306 fn resolveInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2307 const form = self.formBox() orelse return; 2308 const cid = form.getFocus().child_id orelse return; 2309 const cur = form.children.getIndex(cid) orelse return; 2310 const child = &form.children.values()[cur]; 2311 2312 if (child.widget == .text_input) { 2313 const ti = &child.widget.text_input; 2314 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 2315 return child.widget.input(allocator, key, root_focus); 2316 } 2317 2318 const on_submit = child.widget == .submit_button; 2319 2320 switch (key) { 2321 .arrow_up, .back_tab => if (resolveStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 2322 .arrow_down, .tab => if (resolveStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 2323 .arrow_left, .arrow_right => switch (child.widget) { 2324 .box => |*row| { 2325 const row_cid = row.getFocus().child_id orelse return; 2326 const row_index = row.children.getIndex(row_cid) orelse return; 2327 if (key == .arrow_left) { 2328 if (row_index > 0) root_focus.setFocus(row.children.keys()[row_index - 1]); 2329 } else if (row_index + 1 < row.children.count()) { 2330 root_focus.setFocus(row.children.keys()[row_index + 1]); 2331 } 2332 }, 2333 else => try child.widget.input(allocator, key, root_focus), 2334 }, 2335 .enter => if (on_submit) try self.submitResolution(allocator), 2336 .mouse => |mouse| if (on_submit) { 2337 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 2338 }, 2339 else => try child.widget.input(allocator, key, root_focus), 2340 } 2341 } 2342 2343 // a multiline input keeps enter and any up/down that has a row to move to 2344 fn multilineKeepsKey(text_input: *wgt.TextInput, allocator: std.mem.Allocator, key: Key) !bool { 2345 return switch (key) { 2346 .enter => true, 2347 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 2348 .arrow_down => !try text_input.cursorOnLastRow(allocator), 2349 else => false, 2350 }; 2351 } 2352 2353 // the neighboring focusable resolve-form child, skipping the spacer and 2354 // the blank gap rows 2355 fn resolveStep(form: *wgt.Box(Widget), cur: usize, down: bool) ?usize { 2356 var i = cur; 2357 while (true) { 2358 if (down) { 2359 i += 1; 2360 if (i >= form.children.count()) return null; 2361 } else { 2362 if (i == 0) return null; 2363 i -= 1; 2364 } 2365 switch (form.children.values()[i].widget) { 2366 .spacer, .text => continue, 2367 else => return i, 2368 } 2369 } 2370 } 2371 2372 // focus the resolve-form child at `i` (a version row focuses its selected 2373 // child) and keep it visible on the terminal 2374 fn focusResolveChild(self: *This, form: *wgt.Box(Widget), i: usize, root_focus: *Focus) void { 2375 const child = &form.children.values()[i]; 2376 switch (child.widget) { 2377 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2378 else => root_focus.setFocus(form.children.keys()[i]), 2379 } 2380 if (self.session.is_terminal) { 2381 if (self.resolveScroll()) |sc| { 2382 if (child.rect) |rect| sc.scrollToRect(rect); 2383 } 2384 } 2385 } 2386 2387 // re-emit the conflicted thread's event with the resolved content; any 2388 // event on the thread settles its conflict entry. this is the terminal 2389 // path; the web posts the form to the resolve route. 2390 fn submitResolution(self: *This, allocator: std.mem.Allocator) !void { 2391 if (comptime wasm) return; 2392 const io = self.session.io orelse return; 2393 const src = self.data.repo_source orelse return; 2394 const entry = self.data.selectedThread() orelse return; 2395 const author = (try self.session.eventAuthor()) orelse return; 2396 const form = self.formBox() orelse return; 2397 2398 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2399 var title: ?[]u8 = null; 2400 var tags: ?[]u8 = null; 2401 var hunks: std.ArrayList([]const u8) = .empty; 2402 defer { 2403 if (title) |t| allocator.free(t); 2404 if (tags) |t| allocator.free(t); 2405 for (hunks.items) |h| allocator.free(h); 2406 hunks.deinit(allocator); 2407 } 2408 for (form.children.values()) |*child| switch (child.widget) { 2409 .text_input => |*ti| { 2410 const text = try ti.text(allocator); 2411 errdefer allocator.free(text); 2412 if (std.mem.eql(u8, ti.options.name, "title")) { 2413 title = text; 2414 } else if (std.mem.eql(u8, ti.options.name, "tags")) { 2415 tags = text; 2416 } else { 2417 try hunks.append(allocator, text); 2418 } 2419 }, 2420 else => {}, 2421 }; 2422 2423 const id_bytes = try evt.parseEventId(entry.id); 2424 2425 switch (src.repo_kind) { 2426 inline else => |repo_kind| { 2427 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2428 defer any_repo.deinit(io, allocator); 2429 switch (any_repo) { 2430 inline else => |*repo| { 2431 var resolution: Event.Resolve = .{ 2432 .title = title, 2433 .tags = tags, 2434 .hunks = hunks.items, 2435 }; 2436 if (comptime @hasField(Event.Resolve, "theirs")) resolution.theirs = self.data.theirs_picks; 2437 const change = Event.Update{ .resolve = resolution }; 2438 Event.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, change, author) catch |err| switch (err) { 2439 // leave the form up for correction 2440 error.InvalidFields => return, 2441 else => |e| return e, 2442 }; 2443 }, 2444 } 2445 }, 2446 } 2447 2448 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2449 try self.session.navigate(route); 2450 } 2451 2452 // commit a new or edited comment and navigate to its permalink. this is the 2453 // terminal path; the web posts the form to its comment route. 2454 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 2455 if (comptime wasm) return; 2456 const io = self.session.io orelse return; 2457 const src = self.data.repo_source orelse return; 2458 const author = (try self.session.eventAuthor()) orelse return; 2459 const form = self.formBox() orelse return; 2460 const body_input = &form.children.values()[comment_body_field_index].widget.text_input; 2461 const body = try body_input.text(allocator); 2462 defer allocator.free(body); 2463 if (!evt.Comment.fieldsValid(body)) return; 2464 2465 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2466 @memcpy(&thread_id, self.data.selected_id); 2467 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2468 var parent: [evt.event_id_size * 2]u8 = undefined; 2469 @memcpy(&parent, self.data.comment_id); 2470 break :blk parent; 2471 }; 2472 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2473 const editing = self.data.view == .edit_comment; 2474 switch (src.repo_kind) { 2475 inline else => |repo_kind| { 2476 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2477 defer any_repo.deinit(io, allocator); 2478 switch (any_repo) { 2479 inline else => |*repo| if (editing) { 2480 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2481 try evt.Comment.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &comment_id, body, author); 2482 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 2483 } else { 2484 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); 2485 }, 2486 } 2487 }, 2488 } 2489 2490 body_input.clear(allocator); 2491 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2492 try self.session.navigate(route); 2493 } 2494 2495 // the terminal submit paths: the new form commits a new thread event, the 2496 // edit form re-emits the selected thread's event with the form's content. 2497 // the web posts the forms to their routes instead. 2498 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 2499 if (self.data.view == .edit) { 2500 try self.submitEditedThread(allocator); 2501 } else { 2502 try self.submitNewThread(allocator); 2503 } 2504 } 2505 2506 // commit the new thread to the repo's events branch and navigate to it. 2507 // this is the terminal path; the web posts the form to the thread route, 2508 // so the wasm side never runs (or compiles) the repo access below. 2509 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 2510 if (comptime wasm) return; 2511 const io = self.session.io orelse return; 2512 const src = self.data.repo_source orelse return; 2513 const author = (try self.session.eventAuthor()) orelse return; 2514 2515 const form = self.threadForm() orelse return; 2516 const title_input = &form.children.values()[title_field_index].widget.text_input; 2517 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2518 const description_input = &form.children.values()[description_field_index].widget.text_input; 2519 2520 const title = try title_input.text(allocator); 2521 defer allocator.free(title); 2522 const tags = try tags_input.text(allocator); 2523 defer allocator.free(tags); 2524 const description = try description_input.text(allocator); 2525 defer allocator.free(description); 2526 const target_branch = if (comptime supports_drafts) try form.children.values()[target_branch_field_index].widget.text_input.text(allocator) else ""; 2527 defer if (comptime supports_drafts) allocator.free(target_branch); 2528 2529 if (!Event.fieldsValid(title, tags)) { 2530 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, tags, description, target_branch); 2531 return; 2532 } 2533 2534 if (comptime supports_drafts) { 2535 const event_id_hex = Data.createDraft(self.data, self.session, allocator, title, tags, description, target_branch) catch |err| switch (err) { 2536 error.InvalidFields => { 2537 try self.rememberThreadFeedback(.invalid_target_branch, title, tags, description, target_branch); 2538 return; 2539 }, 2540 else => |other| return other, 2541 }; 2542 title_input.clear(allocator); 2543 tags_input.clear(allocator); 2544 description_input.clear(allocator); 2545 form.children.values()[target_branch_field_index].widget.text_input.clear(allocator); 2546 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 2547 try self.session.navigate(route); 2548 return; 2549 } 2550 2551 var id_bytes: [evt.event_id_size]u8 = undefined; 2552 io.random(&id_bytes); 2553 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2554 2555 const event = evt.EventWithId{ 2556 .id = event_id_hex, 2557 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2558 .author = author, 2559 .event = @unionInit(evt.Event, @tagName(kind), Event{ 2560 .title = title, 2561 .description = description, 2562 .tags = tags, 2563 }), 2564 }; 2565 2566 switch (src.repo_kind) { 2567 inline else => |repo_kind| { 2568 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2569 defer any_repo.deinit(io, allocator); 2570 switch (any_repo) { 2571 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}), 2572 } 2573 }, 2574 } 2575 2576 // wipe the form so a return visit starts fresh 2577 title_input.clear(allocator); 2578 tags_input.clear(allocator); 2579 description_input.clear(allocator); 2580 2581 const route = listRoute(self.data.identity, @enumFromInt(0), "", &event_id_hex) orelse return; 2582 try self.session.navigate(route); 2583 } 2584 2585 // re-emit the selected thread's event with the form's content (status 2586 // preserved), then reload the thread's page. this is the terminal path; 2587 // the web posts the form to the edit route. 2588 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 2589 if (comptime wasm) return; 2590 const io = self.session.io orelse return; 2591 const entry = self.data.selectedThread() orelse return; 2592 2593 const form = self.threadForm() orelse return; 2594 const title_input = &form.children.values()[title_field_index].widget.text_input; 2595 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2596 const description_input = &form.children.values()[description_field_index].widget.text_input; 2597 2598 const title = try title_input.text(allocator); 2599 defer allocator.free(title); 2600 const tags = try tags_input.text(allocator); 2601 defer allocator.free(tags); 2602 const description = try description_input.text(allocator); 2603 defer allocator.free(description); 2604 const target_branch = if (comptime supports_drafts) try form.children.values()[target_branch_field_index].widget.text_input.text(allocator) else ""; 2605 defer if (comptime supports_drafts) allocator.free(target_branch); 2606 2607 if (!Event.fieldsValid(title, tags)) { 2608 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, tags, description, target_branch); 2609 return; 2610 } 2611 const src = self.data.repo_source orelse return; 2612 2613 if (comptime supports_drafts) { 2614 if (entryDraft(entry.*)) { 2615 self.data.editDraft(self.session, allocator, entry.id, title, tags, description, target_branch) catch |err| switch (err) { 2616 error.InvalidFields => { 2617 try self.rememberThreadFeedback(.invalid_target_branch, title, tags, description, target_branch); 2618 return; 2619 }, 2620 else => |other| return other, 2621 }; 2622 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2623 try self.session.navigate(route); 2624 return; 2625 } 2626 } 2627 2628 const id_bytes = try evt.parseEventId(entry.id); 2629 const author = (try self.session.eventAuthor()) orelse return; 2630 2631 switch (src.repo_kind) { 2632 inline else => |repo_kind| { 2633 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2634 defer any_repo.deinit(io, allocator); 2635 switch (any_repo) { 2636 inline else => |*repo| if (supports_drafts) { 2637 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 .target_branch = target_branch, 2642 } }, author) catch |err| switch (err) { 2643 error.InvalidFields => { 2644 try self.rememberThreadFeedback(.invalid_target_branch, title, tags, description, target_branch); 2645 return; 2646 }, 2647 else => |other| return other, 2648 }; 2649 } else if (has_status) 2650 try Event.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2651 .title = title, 2652 .tags = tags, 2653 .description = description, 2654 } }, author) 2655 else 2656 try Event.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, tags, description, author), 2657 } 2658 }, 2659 } 2660 2661 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2662 try self.session.navigate(route); 2663 } 2664 2665 fn rememberThreadFeedback(self: *This, failure: FeedbackFailure, title: []const u8, tags: []const u8, description: []const u8, target_branch: []const u8) !void { 2666 const aa = self.session.arena.allocator(); 2667 self.session.data.form_feedback = @unionInit(ui.Session.FormFeedback, @tagName(feedback_tag), .{ 2668 .failure = failure, 2669 .fields = if (comptime supports_drafts) .{ 2670 .title = try aa.dupe(u8, title), 2671 .tags = try aa.dupe(u8, tags), 2672 .description = try aa.dupe(u8, description), 2673 .target_branch = try aa.dupe(u8, target_branch), 2674 } else .{ 2675 .title = try aa.dupe(u8, title), 2676 .tags = try aa.dupe(u8, tags), 2677 .description = try aa.dupe(u8, description), 2678 }, 2679 }); 2680 } 2681 2682 fn submitConfirmation(self: *This, allocator: std.mem.Allocator, button_index: usize) !void { 2683 switch (confirmation(self.data.view) orelse return) { 2684 .publish => if (comptime supports_drafts) { 2685 if (comptime wasm) return; 2686 const entry = self.data.selectedThread() orelse return; 2687 if (!entryDraft(entry.*)) return; 2688 try self.data.publishDraft(self.session, allocator, entry.id); 2689 const route = listRoute(self.data.identity, @enumFromInt(0), "", entry.id) orelse return; 2690 try self.session.navigate(route); 2691 } else unreachable, 2692 .merge => if (comptime supports_merge) { 2693 if (comptime wasm) return; 2694 const entry = self.data.selectedThread() orelse return; 2695 const revision: pch.MergeRevision = if (button_index > 1) .squash else .source; 2696 if (!Data.canMerge(entry.*, self.session) or entry.mergeability.get(revision) != .clean) return; 2697 self.data.mergePatch(self.session, allocator, entry.id, revision) catch |err| switch (err) { 2698 error.MergeConflict, error.MergeCheckUnavailable, error.PatchOutOfDate, error.PatchDataUnavailable => { 2699 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2700 try self.session.navigate(route); 2701 return; 2702 }, 2703 else => return err, 2704 }; 2705 const merged = std.meta.stringToEnum(Status, "merged") orelse return; 2706 const route = listRoute(self.data.identity, merged, "", entry.id) orelse return; 2707 try self.session.navigate(route); 2708 } else unreachable, 2709 .remove => try self.submitRemove(allocator), 2710 } 2711 } 2712 2713 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 2714 const comment = self.data.comment_id.len != 0; 2715 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 2716 const id = evt.parseEventId(event_id) catch return; 2717 try self.removeEvent(allocator, if (comment) .comment else kind, id); 2718 } 2719 2720 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 2721 if (comptime wasm) return; 2722 2723 if (comptime supports_drafts) { 2724 const selected = self.data.selectedThread() orelse return; 2725 if (event_kind == kind and entryDraft(selected.*)) { 2726 try Data.removeDraft(self.session, allocator, &id); 2727 const route = draftsRoute(self.data.identity) orelse return; 2728 try self.session.navigate(route); 2729 return; 2730 } 2731 } 2732 2733 const io = self.session.io orelse return; 2734 const src = self.data.repo_source orelse return; 2735 const author = (try self.session.eventAuthor()) orelse return; 2736 2737 switch (src.repo_kind) { 2738 inline else => |repo_kind| { 2739 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2740 defer any_repo.deinit(io, allocator); 2741 switch (any_repo) { 2742 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), 2743 } 2744 }, 2745 } 2746 2747 const id_hex = std.fmt.bytesToHex(id, .lower); 2748 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex) orelse return; 2749 try self.session.navigate(route); 2750 } 2751 2752 // return to the list. 2753 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 2754 root_focus.setFocus(self.listScroll(index).getFocus().id); 2755 } 2756 2757 // cross to the header tabs above the stack. 2758 fn focusHeader(self: *This, root_focus: *Focus) void { 2759 root_focus.setFocus(self.box.children.keys()[header_index]); 2760 } 2761 2762 pub fn clearGrid(self: *This) void { 2763 self.box.clearGrid(); 2764 } 2765 2766 pub fn getGrid(self: This) ?Grid { 2767 return self.box.getGrid(); 2768 } 2769 2770 pub fn getFocus(self: *This) *Focus { 2771 return self.box.getFocus(); 2772 } 2773 2774 // for the parent's "scroll up at the top jumps to the header" check: at the 2775 // top only while the header tabs hold focus, so up from the split 2776 // crosses into the tabs first. 2777 pub fn atTop(self: *This) bool { 2778 return self.headerActive(); 2779 } 2780 }; 2781 } 2782