From ce029060702c1c271b086e21ff31ffa2f07330da Mon Sep 17 00:00:00 2001 From: Anoop M D Date: Fri, 18 Mar 2022 22:35:12 +0530 Subject: [PATCH] feat: ctrl+enter triggers send --- renderer/providers/Hotkeys/index.js | 34 +++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/renderer/providers/Hotkeys/index.js b/renderer/providers/Hotkeys/index.js index eec833b44..1adcfdff2 100644 --- a/renderer/providers/Hotkeys/index.js +++ b/renderer/providers/Hotkeys/index.js @@ -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 ( {props.children}