2022-03-22 13:48:20 +01:00
|
|
|
import React, { useState, useEffect } from 'react';
|
2022-03-18 14:08:49 +01:00
|
|
|
import find from 'lodash/find';
|
2022-01-24 19:27:30 +01:00
|
|
|
import Mousetrap from 'mousetrap';
|
2022-03-18 14:08:49 +01:00
|
|
|
import { useSelector, useDispatch } from 'react-redux';
|
2022-03-22 13:48:20 +01:00
|
|
|
import SaveRequest from 'components/RequestPane/SaveRequest';
|
2022-03-18 18:05:12 +01:00
|
|
|
import { saveRequest, sendRequest } from 'providers/ReduxStore/slices/collections';
|
|
|
|
import { findCollectionByUid, findItemInCollection } from 'utils/collections';
|
2022-01-24 19:27:30 +01:00
|
|
|
|
|
|
|
export const HotkeysContext = React.createContext();
|
|
|
|
|
|
|
|
export const HotkeysProvider = props => {
|
2022-03-18 14:08:49 +01:00
|
|
|
const dispatch = useDispatch();
|
|
|
|
const tabs = useSelector((state) => state.tabs.tabs);
|
2022-03-18 18:05:12 +01:00
|
|
|
const collections = useSelector((state) => state.collections.collections);
|
2022-03-18 14:08:49 +01:00
|
|
|
const activeTabUid = useSelector((state) => state.tabs.activeTabUid);
|
2022-03-22 13:48:20 +01:00
|
|
|
const [showSaveRequestModal, setShowSaveRequestModal] = useState(false);
|
|
|
|
|
|
|
|
const getCurrentCollectionItems = () => {
|
|
|
|
const activeTab = find(tabs, (t) => t.uid === activeTabUid);
|
|
|
|
if(activeTab) {
|
|
|
|
const collection = findCollectionByUid(collections, activeTab.collectionUid);
|
|
|
|
|
|
|
|
return collection ? collection.items : [];
|
|
|
|
};
|
|
|
|
};
|
2022-03-15 20:30:35 +01:00
|
|
|
|
2022-03-18 18:05:12 +01:00
|
|
|
// save hotkey
|
2022-01-24 19:27:30 +01:00
|
|
|
useEffect(() => {
|
|
|
|
Mousetrap.bind(['command+s', 'ctrl+s'], (e) => {
|
2022-03-22 13:48:20 +01:00
|
|
|
const activeTab = find(tabs, (t) => t.uid === activeTabUid);
|
|
|
|
if(activeTab) {
|
|
|
|
const collection = findCollectionByUid(collections, activeTab.collectionUid);
|
|
|
|
if(collection) {
|
|
|
|
const item = findItemInCollection(collection, activeTab.uid);
|
|
|
|
if(item && item.uid) {
|
|
|
|
dispatch(saveRequest(activeTab.uid, activeTab.collectionUid))
|
|
|
|
} else {
|
|
|
|
setShowSaveRequestModal(true);
|
|
|
|
}
|
2022-03-18 14:08:49 +01:00
|
|
|
}
|
|
|
|
}
|
2022-03-15 20:30:35 +01:00
|
|
|
|
2022-01-24 19:27:30 +01:00
|
|
|
return false; // this stops the event bubbling
|
|
|
|
});
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
Mousetrap.unbind(['command+s', 'ctrl+s']);
|
|
|
|
};
|
2022-03-22 13:48:20 +01:00
|
|
|
}, [activeTabUid, tabs, saveRequest, collections]);
|
2022-01-24 19:27:30 +01:00
|
|
|
|
2022-03-18 18:05:12 +01:00
|
|
|
// send request (ctrl/cmd + enter)
|
|
|
|
useEffect(() => {
|
2022-03-22 13:48:20 +01:00
|
|
|
Mousetrap.bind(['command+enter', 'ctrl+enter'], (e) => {
|
|
|
|
const activeTab = find(tabs, (t) => t.uid === activeTabUid);
|
|
|
|
if(activeTab) {
|
|
|
|
const collection = findCollectionByUid(collections, activeTab.collectionUid);
|
2022-03-18 18:05:12 +01:00
|
|
|
|
2022-03-22 13:48:20 +01:00
|
|
|
if(collection) {
|
|
|
|
const item = findItemInCollection(collection, activeTab.uid);
|
|
|
|
if(item) {
|
|
|
|
dispatch(sendRequest(item, collection.uid));
|
2022-03-18 18:05:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false; // this stops the event bubbling
|
|
|
|
});
|
|
|
|
|
|
|
|
return () => {
|
2022-03-22 13:48:20 +01:00
|
|
|
Mousetrap.unbind(['command+enter', 'ctrl+enter']);
|
2022-03-18 18:05:12 +01:00
|
|
|
};
|
2022-03-22 13:48:20 +01:00
|
|
|
}, [activeTabUid, tabs, saveRequest, collections]);
|
2022-03-18 18:05:12 +01:00
|
|
|
|
2022-01-24 19:27:30 +01:00
|
|
|
return (
|
2022-02-03 18:55:38 +01:00
|
|
|
<HotkeysContext.Provider {...props} value='hotkey'>
|
2022-03-22 13:48:20 +01:00
|
|
|
{showSaveRequestModal && <SaveRequest items={getCurrentCollectionItems()} onClose={() => setShowSaveRequestModal(false)}/>}
|
|
|
|
<div>
|
|
|
|
{props.children}
|
|
|
|
</div>
|
2022-01-24 19:27:30 +01:00
|
|
|
</HotkeysContext.Provider>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useHotkeys = () => {
|
|
|
|
const context = React.useContext(HotkeysContext);
|
|
|
|
|
|
|
|
if (!context) {
|
|
|
|
throw new Error(`useHotkeys must be used within a HotkeysProvider`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default HotkeysProvider;
|