mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
Enhance permission request handling for multi-session support
This commit is contained in:
1 parent
7cec19f7cc
commit
adbc632f0e
4 files changed
+103
-24
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import { useState, useEffect, useCallback } from 'react'
|
||||
import { useState, useEffect, useCallback, useMemo } from 'react'
|
||||
import type { Permission } from '@/api/types'
|
||||
|
||||
type PermissionEventType = 'add' | 'remove'
|
||||
@@ -6,6 +6,7 @@ type PermissionEventType = 'add' | 'remove'
|
||||
interface PermissionEvent {
|
||||
type: PermissionEventType
|
||||
permission?: Permission
|
||||
sessionID?: string
|
||||
permissionID?: string
|
||||
}
|
||||
|
||||
@@ -25,37 +26,98 @@ export const permissionEvents = {
|
||||
}
|
||||
}
|
||||
|
||||
export function usePermissionRequests() {
|
||||
const [permissions, setPermissions] = useState<Permission[]>([])
|
||||
type PermissionsBySession = Record<string, Permission[]>
|
||||
|
||||
export function usePermissionRequests(currentSessionID?: string) {
|
||||
const [permissionsBySession, setPermissionsBySession] = useState<PermissionsBySession>({})
|
||||
|
||||
useEffect(() => {
|
||||
const unsubscribe = permissionEvents.subscribe((event) => {
|
||||
if (event.type === 'add' && event.permission) {
|
||||
setPermissions(prev => {
|
||||
const exists = prev.some(p => p.id === event.permission!.id)
|
||||
if (exists) return prev
|
||||
return [...prev, event.permission!]
|
||||
const permission = event.permission
|
||||
const sessionID = permission.sessionID
|
||||
|
||||
setPermissionsBySession(prev => {
|
||||
const sessionPermissions = prev[sessionID] ?? []
|
||||
const existingIndex = sessionPermissions.findIndex(p => p.id === permission.id)
|
||||
|
||||
if (existingIndex >= 0) {
|
||||
const updated = [...sessionPermissions]
|
||||
updated[existingIndex] = permission
|
||||
return { ...prev, [sessionID]: updated }
|
||||
}
|
||||
|
||||
return { ...prev, [sessionID]: [...sessionPermissions, permission] }
|
||||
})
|
||||
} else if (event.type === 'remove' && event.permissionID && event.sessionID) {
|
||||
const { sessionID, permissionID } = event
|
||||
|
||||
setPermissionsBySession(prev => {
|
||||
const sessionPermissions = prev[sessionID]
|
||||
if (!sessionPermissions) return prev
|
||||
|
||||
const filtered = sessionPermissions.filter(p => p.id !== permissionID)
|
||||
if (filtered.length === 0) {
|
||||
const { [sessionID]: _, ...rest } = prev
|
||||
return rest
|
||||
}
|
||||
return { ...prev, [sessionID]: filtered }
|
||||
})
|
||||
} else if (event.type === 'remove' && event.permissionID) {
|
||||
setPermissions(prev => prev.filter(p => p.id !== event.permissionID))
|
||||
}
|
||||
})
|
||||
return unsubscribe
|
||||
}, [])
|
||||
|
||||
const currentPermission = permissions[0] || null
|
||||
const allPermissions = useMemo(() => {
|
||||
return Object.values(permissionsBySession).flat()
|
||||
}, [permissionsBySession])
|
||||
|
||||
const dismissPermission = useCallback((permissionID: string) => {
|
||||
setPermissions(prev => prev.filter(p => p.id !== permissionID))
|
||||
const currentPermission = useMemo(() => {
|
||||
if (currentSessionID) {
|
||||
const sessionPerms = permissionsBySession[currentSessionID] ?? []
|
||||
if (sessionPerms.length > 0) return sessionPerms[0]
|
||||
}
|
||||
return allPermissions[0] ?? null
|
||||
}, [permissionsBySession, currentSessionID, allPermissions])
|
||||
|
||||
const isFromDifferentSession = useMemo(() => {
|
||||
if (!currentPermission || !currentSessionID) return false
|
||||
return currentPermission.sessionID !== currentSessionID
|
||||
}, [currentPermission, currentSessionID])
|
||||
|
||||
const dismissPermission = useCallback((permissionID: string, sessionID?: string) => {
|
||||
setPermissionsBySession(prev => {
|
||||
if (sessionID) {
|
||||
const sessionPermissions = prev[sessionID]
|
||||
if (!sessionPermissions) return prev
|
||||
|
||||
const filtered = sessionPermissions.filter(p => p.id !== permissionID)
|
||||
if (filtered.length === 0) {
|
||||
const { [sessionID]: _, ...rest } = prev
|
||||
return rest
|
||||
}
|
||||
return { ...prev, [sessionID]: filtered }
|
||||
}
|
||||
|
||||
const newState: PermissionsBySession = {}
|
||||
for (const [sid, perms] of Object.entries(prev)) {
|
||||
const filtered = perms.filter(p => p.id !== permissionID)
|
||||
if (filtered.length > 0) {
|
||||
newState[sid] = filtered
|
||||
}
|
||||
}
|
||||
return newState
|
||||
})
|
||||
}, [])
|
||||
|
||||
const clearAllPermissions = useCallback(() => {
|
||||
setPermissions([])
|
||||
setPermissionsBySession({})
|
||||
}, [])
|
||||
|
||||
return {
|
||||
currentPermission,
|
||||
pendingCount: permissions.length,
|
||||
pendingCount: allPermissions.length,
|
||||
isFromDifferentSession,
|
||||
dismissPermission,
|
||||
clearAllPermissions
|
||||
}
|
||||
|
||||
@@ -293,8 +293,12 @@ export const useSSE = (opcodeUrl: string | null | undefined, directory?: string)
|
||||
break
|
||||
|
||||
case 'permission.replied':
|
||||
if ('permissionID' in event.properties) {
|
||||
permissionEvents.emit({ type: 'remove', permissionID: event.properties.permissionID })
|
||||
if ('permissionID' in event.properties && 'sessionID' in event.properties) {
|
||||
permissionEvents.emit({
|
||||
type: 'remove',
|
||||
sessionID: event.properties.sessionID,
|
||||
permissionID: event.properties.permissionID
|
||||
})
|
||||
}
|
||||
break
|
||||
|
||||
|
||||
Reference in new issue
Block a user