66 lines
1.7 KiB
JavaScript
66 lines
1.7 KiB
JavaScript
import { useCallback } from 'react';
|
|
|
|
export const isPointerModifierEvent = (event) =>
|
|
Boolean(event && (event.shiftKey || event.metaKey || event.ctrlKey || event.altKey));
|
|
|
|
export const isPrimaryPointerEvent = (event) => {
|
|
if (!event) {
|
|
return true;
|
|
}
|
|
if (typeof event.button === 'number' && event.button !== 0) {
|
|
return false;
|
|
}
|
|
const type = typeof event.type === 'string' ? event.type.toLowerCase() : '';
|
|
return type === 'click' || type === 'pointerdown' || type === 'pointerup';
|
|
};
|
|
|
|
const EntryType = Object.freeze({
|
|
document: 'document',
|
|
folder: 'folder',
|
|
});
|
|
|
|
export const useEntryPointer = ({
|
|
resolveDocumentRowKey,
|
|
resolveFolderRowKey,
|
|
onSelectEntry,
|
|
onInspectDocument,
|
|
}) =>
|
|
useCallback(
|
|
(entry, event) => {
|
|
if (!entry || !entry.id) {
|
|
return;
|
|
}
|
|
|
|
const { type, id } = entry;
|
|
if (type !== EntryType.document && type !== EntryType.folder) {
|
|
return;
|
|
}
|
|
|
|
const rowKey = entry.key
|
|
|| (type === EntryType.document ? resolveDocumentRowKey?.(id) : resolveFolderRowKey?.(id));
|
|
if (!rowKey) {
|
|
return;
|
|
}
|
|
|
|
const modifierClick = isPointerModifierEvent(event);
|
|
const primaryClick = isPrimaryPointerEvent(event);
|
|
const metadata = { modifierClick, primaryClick, rowKey, type, id };
|
|
|
|
if (typeof onSelectEntry === 'function') {
|
|
onSelectEntry(entry, event, metadata);
|
|
}
|
|
|
|
if (
|
|
type === EntryType.document
|
|
&& !modifierClick
|
|
&& primaryClick
|
|
&& typeof onInspectDocument === 'function'
|
|
) {
|
|
onInspectDocument(id, metadata);
|
|
}
|
|
},
|
|
[resolveDocumentRowKey, resolveFolderRowKey, onSelectEntry, onInspectDocument],
|
|
);
|
|
|
|
export default useEntryPointer;
|