Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.
TypeScript adeagent-ideai-agentsautomationbotclaude-codecodexghosttygrok-bothermes-agentmobile-appmuseopenclawopencodeorchestrationpiruntimeterminalworktreesyc-backed
import { useCallback, useMemo, useRef, useState } from 'react'
import type { PRComment } from '../../../src/shared/github/comment-types'
import type { ConnectionState } from '../transport/types'
import type { RpcClient } from '../transport/rpc-client'
import type { GitHubPrRepoSlug } from './github-pr-rpc'
import type { RpcOperationSender } from '../transport/rpc-operation-sender'
import {
fetchAddIssueComment,
fetchAddPRReviewCommentReply,
fetchDeleteIssueComment,
fetchResolveReviewThread,
fetchUpdateIssueComment,
type GitHubPrMutationOutcome
} from './github-pr-mutations'
import { triggerError, triggerSuccess } from '../platform/haptics'
import { useMobileTranslation } from '../i18n/use-mobile-translation'
import {
buildAddRootCommentParams,
buildDeleteCommentParams,
buildEditCommentParams,
buildReplyParams,
buildResolveParams
} from './pr-comment-actions'
export type PrCommentMutations = {
reply: (args: {
prNumber: number
commentId: number
body: string
threadId?: string
path?: string
line?: number
prRepo?: GitHubPrRepoSlug | null
}) => Promise<GitHubPrMutationOutcome>
resolveThread: (args: {
threadId: string
resolve: boolean
prRepo?: GitHubPrRepoSlug | null
}) => Promise<GitHubPrMutationOutcome>
addRootComment: (args: {
prNumber: number
body: string
prRepo?: GitHubPrRepoSlug | null
}) => Promise<GitHubPrMutationOutcome>
editComment: (args: {
owner: string
repo: string
host?: string
commentId: number
body: string
}) => Promise<GitHubPrMutationOutcome>
deleteComment: (args: {
owner: string
repo: string
host?: string
commentId: number
}) => Promise<GitHubPrMutationOutcome>
}
export type PrCommentActionsInput = {
client: RpcClient | null
connState: ConnectionState
worktreeId: string
prNumber: number
prRepo?: GitHubPrRepoSlug | null
refetch: () => void | Promise<void>
mutations?: PrCommentMutations
}
function realMutations(client: RpcOperationSender, worktreeId: string): PrCommentMutations {
return {
reply: (args) => fetchAddPRReviewCommentReply(client, worktreeId, args),
resolveThread: (args) => fetchResolveReviewThread(client, worktreeId, args),
addRootComment: (args) => fetchAddIssueComment(client, worktreeId, args),
editComment: (args) => fetchUpdateIssueComment(client, args),
deleteComment: (args) => fetchDeleteIssueComment(client, args)
}
}
// Track busy state per comment so one write cannot disable unrelated reply/resolve controls.
function replyKey(commentId: number): string {
return `reply:${commentId}`
}
function resolveKey(threadId: string): string {
return `resolve:${threadId}`
}
function editKey(commentId: number): string {
return `edit:${commentId}`
}
function deleteKey(commentId: number): string {
return `delete:${commentId}`
}
const ROOT_KEY = 'root'
export function useMobilePrCommentActions(input: PrCommentActionsInput) {
const t = useMobileTranslation()
const { client, connState, worktreeId, prNumber, prRepo, refetch } = input
const [busyKeys, setBusyKeys] = useState<ReadonlySet<string>>(() => new Set())
const [error, setError] = useState<string | { kind: 'actionFailed' } | null>(null)
// Guard against overlapping fires of the same key (double-tap before refetch).
const inFlightRef = useRef<Set<string>>(new Set())
const mutations = useMemo(
() => input.mutations ?? (client ? realMutations(client, worktreeId) : null),
[input.mutations, client, worktreeId]
)
const ready = mutations !== null && (input.mutations !== undefined || connState === 'connected')
const setBusy = useCallback((key: string, busy: boolean) => {
setBusyKeys((prev) => {
const next = new Set(prev)
if (busy) {
next.add(key)
} else {
next.delete(key)
}
return next
})
}, [])
const run = useCallback(
async (key: string, mutate: () => Promise<GitHubPrMutationOutcome>): Promise<boolean> => {
if (!ready || inFlightRef.current.has(key)) {
return false
}
inFlightRef.current.add(key)
setBusy(key, true)
setError(null)
try {
const outcome = await mutate()
if (outcome.ok) {
triggerSuccess()
await refetch()
return true
}
triggerError()
setError(outcome.error)
return false
} catch (err) {
// Contain mutation/refetch throws as false with feedback; callers rely on the boolean contract.
triggerError()
setError(err instanceof Error ? err.message : { kind: 'actionFailed' })
return false
} finally {
inFlightRef.current.delete(key)
setBusy(key, false)
}
},
[ready, refetch, setBusy]
)
const reply = useCallback(
(comment: PRComment, body: string) => {
if (!mutations) {
return Promise.resolve(false)
}
const params = buildReplyParams(prNumber, comment, body)
return run(replyKey(comment.id), () => mutations.reply({ ...params, prRepo }))
},
[mutations, prNumber, prRepo, run]
)
const toggleResolve = useCallback(
(comment: PRComment) => {
const params = buildResolveParams(comment)
if (!mutations || !params) {
return Promise.resolve(false)
}
return run(resolveKey(params.threadId), () => mutations.resolveThread({ ...params, prRepo }))
},
[mutations, prRepo, run]
)
const addRootComment = useCallback(
(body: string) => {
if (!mutations) {
return Promise.resolve(false)
}
const params = buildAddRootCommentParams(prNumber, body)
return run(ROOT_KEY, () => mutations.addRootComment({ ...params, prRepo }))
},
[mutations, prNumber, prRepo, run]
)
const editComment = useCallback(
(commentId: number, body: string) => {
if (!mutations || !prRepo) {
return Promise.resolve(false)
}
const params = buildEditCommentParams(prRepo, commentId, body)
return run(editKey(commentId), () => mutations.editComment(params))
},
[mutations, prRepo, run]
)
const deleteComment = useCallback(
(commentId: number) => {
if (!mutations || !prRepo) {
return Promise.resolve(false)
}
const params = buildDeleteCommentParams(prRepo, commentId)
return run(deleteKey(commentId), () => mutations.deleteComment(params))
},
[mutations, prRepo, run]
)
return {
ready,
error:
typeof error === 'string' || error === null
? error
: t('session.usePrCommentActions.actionFailed', 'Comment action failed'),
clearError: useCallback(() => setError(null), []),
isReplyBusy: useCallback((commentId: number) => busyKeys.has(replyKey(commentId)), [busyKeys]),
isResolveBusy: useCallback(
(threadId: string) => busyKeys.has(resolveKey(threadId)),
[busyKeys]
),
isEditBusy: useCallback((commentId: number) => busyKeys.has(editKey(commentId)), [busyKeys]),
isDeleteBusy: useCallback(
(commentId: number) => busyKeys.has(deleteKey(commentId)),
[busyKeys]
),
isRootBusy: busyKeys.has(ROOT_KEY),
reply,
toggleResolve,
addRootComment,
editComment,
deleteComment
}
}
export type MobilePrCommentActions = ReturnType<typeof useMobilePrCommentActions>