feat: ctrl+enter triggers send

This commit is contained in:
Anoop M D 2022-03-18 22:35:12 +05:30
parent a79593f951
commit ce02906070

View File

@ -1,21 +1,22 @@
import React, { useEffect } from 'react';
import find from 'lodash/find';
import Mousetrap from 'mousetrap';
import { saveRequest } from 'providers/ReduxStore/slices/collections';
import { requestSaved } from 'providers/ReduxStore/slices/tabs';
import { useSelector, useDispatch } from 'react-redux';
import { saveRequest, sendRequest } from 'providers/ReduxStore/slices/collections';
import { requestSaved } from 'providers/ReduxStore/slices/tabs';
import { findCollectionByUid, findItemInCollection } from 'utils/collections';
export const HotkeysContext = React.createContext();
export const HotkeysProvider = props => {
const dispatch = useDispatch();
const tabs = useSelector((state) => state.tabs.tabs);
const collections = useSelector((state) => state.collections.collections);
const activeTabUid = useSelector((state) => state.tabs.activeTabUid);
// save hotkey
useEffect(() => {
Mousetrap.bind(['command+s', 'ctrl+s'], (e) => {
console.log("Save hotkey");
if(activeTabUid) {
const activeTab = find(tabs, (t) => t.uid === activeTabUid);
if(activeTab) {
@ -35,6 +36,31 @@ export const HotkeysProvider = props => {
};
}, [activeTabUid, tabs, saveRequest, requestSaved]);
// send request (ctrl/cmd + enter)
useEffect(() => {
Mousetrap.bind(['ctrl+command', 'ctrl+enter'], (e) => {
if(activeTabUid) {
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) {
dispatch(sendRequest(item, collection.uid));
}
}
}
}
return false; // this stops the event bubbling
});
return () => {
Mousetrap.unbind(['command+s', 'ctrl+s']);
};
}, [activeTabUid, tabs, saveRequest, requestSaved, collections]);
return (
<HotkeysContext.Provider {...props} value='hotkey'>
{props.children}