infra: add linting and formatting for js projects (#230)
* wip * wip * wip * format * wip(mobile): lint and format * cleanup * nits * nits * idiomatic react * nit * format all root files
This commit was merged in pull request #230.
This commit is contained in:
@@ -1,29 +1,35 @@
|
||||
import { useCallback, useEffect, useRef, useState } from "react";
|
||||
import { useCallback, useEffect, useRef, useState } from 'react';
|
||||
|
||||
interface UseFileInputOptions {
|
||||
onFilesSelected: (files: File[]) => void;
|
||||
enabled: boolean;
|
||||
}
|
||||
|
||||
export function useFileInput({ onFilesSelected, enabled }: UseFileInputOptions) {
|
||||
export function useFileInput({
|
||||
onFilesSelected,
|
||||
enabled,
|
||||
}: UseFileInputOptions) {
|
||||
const [isDragging, setIsDragging] = useState(false);
|
||||
const inputRef = useRef<HTMLInputElement | null>(null);
|
||||
const dragCountRef = useRef(0);
|
||||
|
||||
// Stable ref for the callback to avoid re-registering effects
|
||||
// Latest callback in a ref, so the effects below don't re-register when the
|
||||
// caller passes a new function each render.
|
||||
const onFilesRef = useRef(onFilesSelected);
|
||||
onFilesRef.current = onFilesSelected;
|
||||
useEffect(() => {
|
||||
onFilesRef.current = onFilesSelected;
|
||||
}, [onFilesSelected]);
|
||||
|
||||
// Hidden file input element
|
||||
useEffect(() => {
|
||||
const input = document.createElement("input");
|
||||
input.type = "file";
|
||||
const input = document.createElement('input');
|
||||
input.type = 'file';
|
||||
input.multiple = true;
|
||||
input.style.display = "none";
|
||||
input.addEventListener("change", () => {
|
||||
input.style.display = 'none';
|
||||
input.addEventListener('change', () => {
|
||||
if (input.files?.length) {
|
||||
onFilesRef.current(Array.from(input.files));
|
||||
input.value = "";
|
||||
input.value = '';
|
||||
}
|
||||
});
|
||||
document.body.appendChild(input);
|
||||
@@ -50,8 +56,8 @@ export function useFileInput({ onFilesSelected, enabled }: UseFileInputOptions)
|
||||
}
|
||||
};
|
||||
|
||||
window.addEventListener("paste", handlePaste);
|
||||
return () => window.removeEventListener("paste", handlePaste);
|
||||
window.addEventListener('paste', handlePaste);
|
||||
return () => window.removeEventListener('paste', handlePaste);
|
||||
}, [enabled]);
|
||||
|
||||
// Drag and drop handlers
|
||||
|
||||
Reference in New Issue
Block a user