okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-0243-guifix into f-0243-rel9caf39495

main
2.9 KiB46 linesraw
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() {
14    const guard = (e: DragEvent) => { if (!e.defaultPrevented && dragHasFiles(e.dataTransfer?.types)) e.preventDefault(); };
15    window.addEventListener('dragover', guard);
16    window.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 }) {
21  const [over, setOver] = useState(false);
22  const depth = useRef(0);
23  // Linux and macOS: the app shell reports the drag and the drop (the page is not given the files there).
24  useEffect(() => {
25    const drag = (e: Event) => setOver((e as CustomEvent).detail === 'enter');
26    const drop = (e: Event) => {
27      setOver(false); depth.current = 0;
28      const refs = readDroppedRefs((e as CustomEvent).detail);
29      if (!refs.length) return;
30      void fetchDropped(refs).then(files => {
31        if (files.length) window.dispatchEvent(new CustomEvent(DROP_EVENT, { detail: files }));
32        else pushToast('error', tr('The file was not added'), tr('Try dropping it again.'));
33      });
34    };
35    window.addEventListener(NATIVE_DRAG_EVENT, drag); window.addEventListener(NATIVE_DROP_EVENT, drop);
36    return () => { window.removeEventListener(NATIVE_DRAG_EVENT, drag); window.removeEventListener(NATIVE_DROP_EVENT, drop); };
37  }, []);
38  return <div className={className} data-dropping={over || undefined}
39    onDragEnter={e => { if (!hasFiles(e)) return; e.preventDefault(); depth.current++; setOver(true); }}
40    onDragOver={e => { if (!hasFiles(e)) return; e.preventDefault(); e.dataTransfer.dropEffect = 'copy'; }}
41    onDragLeave={e => { if (!hasFiles(e)) return; depth.current = Math.max(0, depth.current - 1); if (!depth.current) setOver(false); }}
42    onDrop={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}