The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { ContextMenuItem } from "@t3tools/contracts"; |
| 2 | import type { SnoozePreset } from "@t3tools/client-runtime/state/thread-settled"; |
| 3 | import { msg } from "gt-react"; |
| 4 | |
| 5 | import { m, type MessageResolver } from "../i18n/messages"; |
| 6 | |
| 7 | export type ThreadActionMenuId = |
| 8 | | "find" |
| 9 | | "new-thread-on-branch" |
| 10 | | "filter-by-project" |
| 11 | | "turn-into-project" |
| 12 | | "project-settings" |
| 13 | | "pin" |
| 14 | | "unpin" |
| 15 | | "settle" |
| 16 | | "unsettle" |
| 17 | | "auto-settle" |
| 18 | | "auto-settle:enabled" |
| 19 | | "auto-settle:disabled" |
| 20 | | "snooze" |
| 21 | | `snooze:${string}` |
| 22 | | "unsnooze" |
| 23 | | "rename" |
| 24 | | "regenerate-title" |
| 25 | | "mark-unread" |
| 26 | | "share-guest" |
| 27 | | "copy" |
| 28 | | "copy-path" |
| 29 | | "copy-branch" |
| 30 | | "copy-thread" |
| 31 | | "copy-thread-id" |
| 32 | | "archive" |
| 33 | | "delete"; |
| 34 | |
| 35 | export type DraftActionMenuId = |
| 36 | | "copy" |
| 37 | | "copy-path" |
| 38 | | "copy-branch" |
| 39 | | "project-settings" |
| 40 | | "discard"; |
| 41 | |
| 42 | export function buildDraftActionMenuItems( |
| 43 | options: { |
| 44 | readonly hasPath: boolean; |
| 45 | readonly hasBranch: boolean; |
| 46 | readonly hasProject: boolean; |
| 47 | }, |
| 48 | resolve: MessageResolver = m, |
| 49 | ): ReadonlyArray<ContextMenuItem<DraftActionMenuId>> { |
| 50 | return [ |
| 51 | { |
| 52 | id: "copy", |
| 53 | label: resolve(msg("Copy", { $context: "submenu of things to copy" })), |
| 54 | icon: "copy", |
| 55 | disabled: !options.hasPath && !options.hasBranch, |
| 56 | children: [ |
| 57 | ...(options.hasPath |
| 58 | ? [ |
| 59 | { |
| 60 | id: "copy-path" as const, |
| 61 | label: resolve(msg("Path", { $context: "copy the file path" })), |
| 62 | icon: "folder", |
| 63 | }, |
| 64 | ] |
| 65 | : []), |
| 66 | ...(options.hasBranch |
| 67 | ? [ |
| 68 | { |
| 69 | id: "copy-branch" as const, |
| 70 | label: resolve(msg("Branch", { $context: "copy the git branch name" })), |
| 71 | icon: "git-branch", |
| 72 | }, |
| 73 | ] |
| 74 | : []), |
| 75 | ], |
| 76 | }, |
| 77 | ...(options.hasProject |
| 78 | ? [ |
| 79 | { |
| 80 | id: "project-settings" as const, |
| 81 | label: resolve(msg("Project settings")), |
| 82 | icon: "settings", |
| 83 | }, |
| 84 | ] |
| 85 | : []), |
| 86 | { |
| 87 | id: "discard", |
| 88 | label: resolve(msg("Discard draft")), |
| 89 | icon: "trash", |
| 90 | destructive: true, |
| 91 | separatorBefore: true, |
| 92 | }, |
| 93 | ]; |
| 94 | } |
| 95 | |
| 96 | export interface ThreadActionMenuState { |
| 97 | readonly canOperate: boolean; |
| 98 | readonly canFind?: boolean; |
| 99 | /** False where no inline title editor is rendered, such as sidebar search results. */ |
| 100 | readonly canRename?: boolean; |
| 101 | readonly branch: string | null; |
| 102 | readonly projectFilter: { |
| 103 | readonly label: string; |
| 104 | readonly isActive: boolean; |
| 105 | } | null; |
| 106 | readonly isPinned: boolean; |
| 107 | readonly isSettled: boolean; |
| 108 | readonly autoSettleEnabled: boolean; |
| 109 | readonly isSnoozed: boolean; |
| 110 | readonly canSnoozeNow: boolean; |
| 111 | readonly isRegeneratingTitle: boolean; |
| 112 | /** Archive rejects attached providers, so disable it while running. */ |
| 113 | readonly isRunning: boolean; |
| 114 | readonly supports: { |
| 115 | readonly settlement: boolean; |
| 116 | readonly autoSettleOptOut: boolean; |
| 117 | readonly snooze: boolean; |
| 118 | readonly pinning: boolean; |
| 119 | readonly titleRegeneration: boolean; |
| 120 | }; |
| 121 | readonly snoozePresets: ReadonlyArray<SnoozePreset>; |
| 122 | /** Guest links need their own grant, so the caller checks it instead of `canOperate`. */ |
| 123 | readonly canShareWithGuest?: boolean; |
| 124 | /** A No project thread with its own folder, on a server that can promote it. */ |
| 125 | readonly canTurnIntoProject?: boolean; |
| 126 | } |
| 127 | |
| 128 | /** Local navigation, read markers, and copying remain available to read-only clients. */ |
| 129 | export function threadActionRequiresOperate(action: ThreadActionMenuId): boolean { |
| 130 | return ![ |
| 131 | "find", |
| 132 | "new-thread-on-branch", |
| 133 | "project-settings", |
| 134 | "mark-unread", |
| 135 | "share-guest", |
| 136 | "copy", |
| 137 | "copy-path", |
| 138 | "copy-branch", |
| 139 | "copy-thread", |
| 140 | "copy-thread-id", |
| 141 | ].includes(action); |
| 142 | } |
| 143 | |
| 144 | export function buildThreadActionMenuItems( |
| 145 | state: ThreadActionMenuState, |
| 146 | resolve: MessageResolver = m, |
| 147 | ): ReadonlyArray<ContextMenuItem<ThreadActionMenuId>> { |
| 148 | const items: ReadonlyArray<ContextMenuItem<ThreadActionMenuId>> = [ |
| 149 | ...(state.canFind |
| 150 | ? [{ id: "find" as const, label: resolve(msg("Find in thread")), icon: "text-search" }] |
| 151 | : []), |
| 152 | ...(state.branch |
| 153 | ? [ |
| 154 | { |
| 155 | id: "new-thread-on-branch" as const, |
| 156 | label: resolve(msg("New thread on {branch}", { branch: state.branch })), |
| 157 | icon: "message-square-plus", |
| 158 | }, |
| 159 | ] |
| 160 | : []), |
| 161 | ...(state.supports.pinning |
| 162 | ? [ |
| 163 | state.isPinned |
| 164 | ? { id: "unpin" as const, label: resolve(msg("Unpin thread")), icon: "pin-off" } |
| 165 | : { id: "pin" as const, label: resolve(msg("Pin thread")), icon: "pin" }, |
| 166 | ] |
| 167 | : []), |
| 168 | ...(state.supports.settlement |
| 169 | ? [ |
| 170 | state.isSettled |
| 171 | ? { |
| 172 | id: "unsettle" as const, |
| 173 | label: resolve(msg("Un-settle thread")), |
| 174 | icon: "circle-check", |
| 175 | } |
| 176 | : { id: "settle" as const, label: resolve(msg("Settle thread")), icon: "circle-check" }, |
| 177 | ] |
| 178 | : []), |
| 179 | ...(state.supports.snooze |
| 180 | ? [ |
| 181 | state.isSnoozed |
| 182 | ? { id: "unsnooze" as const, label: resolve(msg("Wake thread")), icon: "clock" } |
| 183 | : { |
| 184 | id: "snooze" as const, |
| 185 | label: resolve(msg("Snooze", { $context: "verb, snooze the thread" })), |
| 186 | icon: "clock", |
| 187 | disabled: !state.canSnoozeNow, |
| 188 | children: [ |
| 189 | ...state.snoozePresets.map((preset) => ({ |
| 190 | id: `snooze:${preset.id}` as const, |
| 191 | label: preset.label, |
| 192 | detail: preset.whenLabel, |
| 193 | })), |
| 194 | { |
| 195 | id: "snooze:custom" as const, |
| 196 | label: resolve(msg("Custom…")), |
| 197 | separatorBefore: state.snoozePresets.length > 0, |
| 198 | }, |
| 199 | ], |
| 200 | }, |
| 201 | ] |
| 202 | : []), |
| 203 | { |
| 204 | id: "rename", |
| 205 | label: resolve(msg("Rename thread")), |
| 206 | icon: "pencil", |
| 207 | disabled: state.canRename === false, |
| 208 | separatorBefore: true, |
| 209 | }, |
| 210 | ...(state.supports.titleRegeneration |
| 211 | ? [ |
| 212 | { |
| 213 | id: "regenerate-title" as const, |
| 214 | label: state.isRegeneratingTitle |
| 215 | ? resolve(msg("Regenerating…")) |
| 216 | : resolve(msg("Regenerate title")), |
| 217 | icon: "refresh-cw", |
| 218 | disabled: state.isRegeneratingTitle, |
| 219 | }, |
| 220 | ] |
| 221 | : []), |
| 222 | { id: "mark-unread", label: resolve(msg("Mark unread")), icon: "mail-open" }, |
| 223 | ...(state.canShareWithGuest |
| 224 | ? [{ id: "share-guest" as const, label: resolve(msg("Share with guest")), icon: "user-plus" }] |
| 225 | : []), |
| 226 | ...(state.projectFilter |
| 227 | ? [ |
| 228 | { |
| 229 | id: "filter-by-project" as const, |
| 230 | label: state.projectFilter.isActive |
| 231 | ? resolve(msg("Show all projects")) |
| 232 | : resolve(msg("Filter by {project}", { project: state.projectFilter.label })), |
| 233 | icon: "folder-tree", |
| 234 | }, |
| 235 | ] |
| 236 | : []), |
| 237 | ...(state.canTurnIntoProject |
| 238 | ? [ |
| 239 | { |
| 240 | id: "turn-into-project" as const, |
| 241 | label: resolve(msg("Turn into project")), |
| 242 | icon: "folder-plus", |
| 243 | disabled: state.isRunning, |
| 244 | }, |
| 245 | ] |
| 246 | : []), |
| 247 | ...(state.supports.autoSettleOptOut |
| 248 | ? [ |
| 249 | { |
| 250 | id: "auto-settle" as const, |
| 251 | label: resolve(msg("Auto-settle behavior")), |
| 252 | icon: "timer", |
| 253 | children: [ |
| 254 | { |
| 255 | id: "auto-settle:enabled" as const, |
| 256 | label: resolve(msg("Follow settings")), |
| 257 | checked: state.autoSettleEnabled, |
| 258 | }, |
| 259 | { |
| 260 | id: "auto-settle:disabled" as const, |
| 261 | label: resolve(msg("Never", { $context: "auto-settle never" })), |
| 262 | checked: !state.autoSettleEnabled, |
| 263 | }, |
| 264 | ], |
| 265 | }, |
| 266 | ] |
| 267 | : []), |
| 268 | { |
| 269 | id: "copy", |
| 270 | label: resolve(msg("Copy", { $context: "submenu of things to copy" })), |
| 271 | icon: "copy", |
| 272 | separatorBefore: true, |
| 273 | children: [ |
| 274 | { |
| 275 | id: "copy-path", |
| 276 | label: resolve(msg("Path", { $context: "copy the file path" })), |
| 277 | icon: "folder", |
| 278 | }, |
| 279 | ...(state.branch |
| 280 | ? [ |
| 281 | { |
| 282 | id: "copy-branch" as const, |
| 283 | label: resolve(msg("Branch", { $context: "copy the git branch name" })), |
| 284 | icon: "git-branch", |
| 285 | }, |
| 286 | ] |
| 287 | : []), |
| 288 | { |
| 289 | id: "copy-thread", |
| 290 | label: resolve(msg("Thread", { $context: "copy the thread contents" })), |
| 291 | icon: "message-square", |
| 292 | }, |
| 293 | { id: "copy-thread-id", label: resolve(msg("Thread ID")), icon: "hash" }, |
| 294 | ], |
| 295 | }, |
| 296 | { id: "project-settings", label: resolve(msg("Project settings")), icon: "settings" }, |
| 297 | { |
| 298 | id: "archive", |
| 299 | label: resolve(msg("Archive thread")), |
| 300 | icon: "archive", |
| 301 | disabled: state.isRunning, |
| 302 | separatorBefore: true, |
| 303 | }, |
| 304 | { |
| 305 | id: "delete", |
| 306 | label: resolve(msg("Delete", { $context: "verb, delete the thread" })), |
| 307 | destructive: true, |
| 308 | icon: "trash", |
| 309 | }, |
| 310 | ]; |
| 311 | return state.canOperate |
| 312 | ? items |
| 313 | : items.map((item) => |
| 314 | threadActionRequiresOperate(item.id) |
| 315 | ? { |
| 316 | ...item, |
| 317 | disabled: true, |
| 318 | ...(item.children |
| 319 | ? { children: item.children.map((child) => ({ ...child, disabled: true })) } |
| 320 | : {}), |
| 321 | } |
| 322 | : item, |
| 323 | ); |
| 324 | } |