okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
9caf39495
main
1import { useEffect , useRef , useState , type ReactNode } from 'react' ; 2import { NATIVE_DRAG_EVENT , NATIVE_DROP_EVENT , dragHasFiles , fetchDropped , readDroppedRefs } from '../lib/native-drop' ; 3import { pushToast } from '../lib/store' ; 4import { tr } from '../lib/i18n' ; 5import { Icon } from './ui' ; 6 7/** Files dropped on the chat go where the attach button would put them: the composer reads the 'strata:drop-files' event. */ 8export const DROP_EVENT = 'strata:drop-files' ; 9export { pastedFiles } from '../lib/pasted-files' ; 10const hasFiles = ( e :React . DragEvent ) => Array . from ( e . dataTransfer ?. types ?? []). includes ( 'Files' ); 11 12/** A file dropped outside the chat must not take the window to that file: the page ignores it (called once at start). */ 13export function installDropGuard () { 14const guard = ( e :DragEvent ) => { if ( ! e . defaultPrevented && dragHasFiles ( e . dataTransfer ?. types )) e . preventDefault (); }; 15window . addEventListener ( 'dragover' , guard ); 16window . addEventListener ( 'drop' , guard ); 17} 18 19/** The conversation column, with a calm highlight while something is dragged over it. */ 20export function DropZone ({ children, className} :{ children :ReactNode ; className ?:string }) { 21const [ over , setOver ] = useState ( false ); 22const depth = useRef ( 0 ); 23// Linux and macOS: the app shell reports the drag and the drop (the page is not given the files there). 24useEffect (() => { 25const drag = ( e :Event ) => setOver (( e as CustomEvent ). detail === 'enter' ); 26const drop = ( e :Event ) => { 27setOver ( false ); depth . current = 0 ; 28const refs = readDroppedRefs (( e as CustomEvent ). detail ); 29if ( ! refs . length ) return ; 30void fetchDropped ( refs ). then ( files => { 31if ( files . length ) window . dispatchEvent ( new CustomEvent ( DROP_EVENT , { detail :files })); 32else pushToast ( 'error' , tr ( 'The file was not added' ), tr ( 'Try dropping it again.' )); 33}); 34}; 35window . addEventListener ( NATIVE_DRAG_EVENT , drag ); window . addEventListener ( NATIVE_DROP_EVENT , drop ); 36return () => { window . removeEventListener ( NATIVE_DRAG_EVENT , drag ); window . removeEventListener ( NATIVE_DROP_EVENT , drop ); }; 37}, []); 38return < div className = { className } data-dropping = { over || undefined } 39onDragEnter = { e => { if ( ! hasFiles ( e )) return ; e . preventDefault (); depth . current ++ ; setOver ( true ); }} 40onDragOver = { e => { if ( ! hasFiles ( e )) return ; e . preventDefault (); e . dataTransfer . dropEffect = 'copy' ; }} 41onDragLeave = { e => { if ( ! hasFiles ( e )) return ; depth . current = Math . max ( 0 , depth . current - 1 ); if ( ! depth . current ) setOver ( false ); }} 42onDrop = { e => { if ( ! hasFiles ( e )) return ; e . preventDefault (); depth . current = 0 ; setOver ( false ); const files = Array . from ( e . dataTransfer . files ); if ( files . length ) window . dispatchEvent ( new CustomEvent ( DROP_EVENT , { detail :files })); }}> 43{ children } 44{ over ?< div className = "dropzone" aria-hidden = "true" >< Icon name = "attach" size = { 22 }/>< strong >{ tr ( "Drop files to add them" )}</ strong >< span >{ tr ( "Pictures and text files" )}</ span ></ div > :null } 45</ div >; 46}