mirror of
https://github.com/NanmiCoder/cc-haha
synced 2026-07-18 13:23:33 +08:00
Complete the Electron replacement boundary before merging by removing the renderer-side Tauri host fallback, tightening H5/browser access so only desktop navigation is tokenless, and moving desktop release publication to a tag-driven GitHub Actions matrix with a single final publish job. Constraint: H5/browser capability access must not gain tokenless access through localhost or retired Tauri origins Constraint: Desktop release artifacts must be built by GitHub Actions from version tags, not treated as local build outputs Rejected: Keep localhost browser origins trusted for convenience | local browser contexts can access loopback services and must use the H5 token path Rejected: Publish from each matrix job | partial releases can be created before all platforms finish Confidence: high Scope-risk: broad Directive: Do not reintroduce Tauri origins or localhost browser origins into the trusted desktop origin set without a reviewed security design Tested: bun test src/server/__tests__/h5-access-policy.test.ts src/server/__tests__/h5-access-auth.test.ts src/server/__tests__/diagnostics-service.test.ts src/server/middleware/cors.test.ts Tested: bun test scripts/pr/release-workflow.test.ts scripts/release-update-metadata.test.ts Tested: bun run check:desktop Tested: bun run check:native Tested: git diff --check Not-tested: bun run check:server is blocked by expired quarantine entries server:cron-scheduler, server:providers-real, server:tasks, server:e2e:business-flow, server:e2e:full-flow
163 lines
4.4 KiB
TypeScript
163 lines
4.4 KiB
TypeScript
import { useCallback, useEffect, useRef, useState, type DragEvent, type RefObject } from 'react'
|
|
import { getDesktopHost } from '../../lib/desktopHost'
|
|
import {
|
|
dataTransferHasFiles,
|
|
dataTransferToComposerAttachments,
|
|
pathsToComposerAttachments,
|
|
type ComposerAttachment,
|
|
} from '../../lib/composerAttachments'
|
|
|
|
type DesktopDropPosition = {
|
|
x: number
|
|
y: number
|
|
}
|
|
|
|
type DesktopDragDropPayload =
|
|
| { type: 'enter'; paths: string[]; position: DesktopDropPosition }
|
|
| { type: 'over'; position: DesktopDropPosition }
|
|
| { type: 'drop'; paths: string[]; position: DesktopDropPosition }
|
|
| { type: 'leave' }
|
|
| { type: 'cancel' }
|
|
|
|
type DesktopDragDropEvent = {
|
|
payload: DesktopDragDropPayload
|
|
}
|
|
|
|
type UseComposerFileDropOptions = {
|
|
disabled?: boolean
|
|
panelRef: RefObject<HTMLElement | null>
|
|
onAttachments: (attachments: ComposerAttachment[]) => void
|
|
onError?: (error: unknown) => void
|
|
}
|
|
|
|
function isPointInsideElement(element: HTMLElement | null, position: DesktopDropPosition): boolean {
|
|
if (!element) return false
|
|
const rect = element.getBoundingClientRect()
|
|
return (
|
|
position.x >= rect.left &&
|
|
position.x <= rect.right &&
|
|
position.y >= rect.top &&
|
|
position.y <= rect.bottom
|
|
)
|
|
}
|
|
|
|
export function useComposerFileDrop({
|
|
disabled = false,
|
|
panelRef,
|
|
onAttachments,
|
|
onError,
|
|
}: UseComposerFileDropOptions) {
|
|
const [isDragActive, setIsDragActive] = useState(false)
|
|
const dragDepthRef = useRef(0)
|
|
const disabledRef = useRef(disabled)
|
|
const onAttachmentsRef = useRef(onAttachments)
|
|
const onErrorRef = useRef(onError)
|
|
|
|
useEffect(() => {
|
|
disabledRef.current = disabled
|
|
}, [disabled])
|
|
|
|
useEffect(() => {
|
|
onAttachmentsRef.current = onAttachments
|
|
}, [onAttachments])
|
|
|
|
useEffect(() => {
|
|
onErrorRef.current = onError
|
|
}, [onError])
|
|
|
|
useEffect(() => {
|
|
const host = getDesktopHost()
|
|
if (!host.isDesktop) return
|
|
|
|
let disposed = false
|
|
let unlisten: (() => void) | undefined
|
|
|
|
void host.webview
|
|
.onDragDropEvent((event) => {
|
|
if (disposed) return
|
|
|
|
const payload = (event as DesktopDragDropEvent).payload
|
|
if (payload.type === 'cancel' || payload.type === 'leave') {
|
|
dragDepthRef.current = 0
|
|
setIsDragActive(false)
|
|
return
|
|
}
|
|
|
|
const isInside = isPointInsideElement(panelRef.current, payload.position)
|
|
if (payload.type === 'enter' || payload.type === 'over') {
|
|
setIsDragActive(!disabledRef.current && isInside)
|
|
return
|
|
}
|
|
|
|
dragDepthRef.current = 0
|
|
setIsDragActive(false)
|
|
if (disabledRef.current || !isInside) return
|
|
|
|
const attachments = pathsToComposerAttachments(payload.paths)
|
|
if (attachments.length > 0) onAttachmentsRef.current(attachments)
|
|
})
|
|
.then((nextUnlisten) => {
|
|
if (disposed) {
|
|
nextUnlisten()
|
|
return
|
|
}
|
|
unlisten = nextUnlisten
|
|
})
|
|
.catch((error) => {
|
|
onErrorRef.current?.(error)
|
|
})
|
|
|
|
return () => {
|
|
disposed = true
|
|
unlisten?.()
|
|
}
|
|
}, [panelRef])
|
|
|
|
const onDragEnter = useCallback((event: DragEvent) => {
|
|
if (disabled || !dataTransferHasFiles(event.dataTransfer)) return
|
|
event.preventDefault()
|
|
event.dataTransfer.dropEffect = 'copy'
|
|
dragDepthRef.current += 1
|
|
setIsDragActive(true)
|
|
}, [disabled])
|
|
|
|
const onDragOver = useCallback((event: DragEvent) => {
|
|
if (disabled || !dataTransferHasFiles(event.dataTransfer)) return
|
|
event.preventDefault()
|
|
event.dataTransfer.dropEffect = 'copy'
|
|
setIsDragActive(true)
|
|
}, [disabled])
|
|
|
|
const onDragLeave = useCallback((event: DragEvent) => {
|
|
if (disabled || !dataTransferHasFiles(event.dataTransfer)) return
|
|
event.preventDefault()
|
|
dragDepthRef.current = Math.max(0, dragDepthRef.current - 1)
|
|
if (dragDepthRef.current === 0) setIsDragActive(false)
|
|
}, [disabled])
|
|
|
|
const onDrop = useCallback((event: DragEvent) => {
|
|
if (disabled || !dataTransferHasFiles(event.dataTransfer)) return
|
|
event.preventDefault()
|
|
dragDepthRef.current = 0
|
|
setIsDragActive(false)
|
|
|
|
void dataTransferToComposerAttachments(event.dataTransfer)
|
|
.then((attachments) => {
|
|
if (attachments.length > 0) onAttachments(attachments)
|
|
})
|
|
.catch((error) => {
|
|
onError?.(error)
|
|
})
|
|
}, [disabled, onAttachments, onError])
|
|
|
|
return {
|
|
isDragActive,
|
|
dragHandlers: {
|
|
onDragEnter,
|
|
onDragOver,
|
|
onDragLeave,
|
|
onDrop,
|
|
},
|
|
}
|
|
}
|