From 089b90fff16830a162587247f521233b3e010239 Mon Sep 17 00:00:00 2001 From: Labhansh Agrawal Date: Sun, 9 Jan 2022 11:05:57 +0530 Subject: [PATCH] refactor search redux actions explicit open and close instead of toggle and close --- lib/actions/sessions.ts | 13 +++++++------ lib/components/term-group.tsx | 3 ++- lib/components/term.tsx | 2 +- lib/components/terms.tsx | 3 ++- lib/constants/sessions.ts | 9 ++------- lib/containers/terms.ts | 18 +++++++++++++++--- lib/hyper.d.ts | 6 ++++-- lib/index.tsx | 2 +- lib/reducers/sessions.ts | 8 ++------ 9 files changed, 36 insertions(+), 28 deletions(-) diff --git a/lib/actions/sessions.ts b/lib/actions/sessions.ts index af77f765..92b07eb1 100644 --- a/lib/actions/sessions.ts +++ b/lib/actions/sessions.ts @@ -13,8 +13,7 @@ import { SESSION_CLEAR_ACTIVE, SESSION_USER_DATA, SESSION_SET_XTERM_TITLE, - SESSION_SEARCH, - SESSION_SEARCH_CLOSE + SESSION_SEARCH } from '../constants/sessions'; import {HyperState, session, HyperDispatch, HyperActions} from '../hyper'; @@ -135,12 +134,13 @@ export function resizeSession(uid: string, cols: number, rows: number) { }; } -export function onSearch(uid?: string) { +export function openSearch(uid?: string) { return (dispatch: HyperDispatch, getState: () => HyperState) => { const targetUid = uid || getState().sessions.activeUid!; dispatch({ type: SESSION_SEARCH, - uid: targetUid + uid: targetUid, + value: true }); }; } @@ -150,8 +150,9 @@ export function closeSearch(uid?: string, keyEvent?: any) { const targetUid = uid || getState().sessions.activeUid!; if (getState().sessions.sessions[targetUid]?.search) { dispatch({ - type: SESSION_SEARCH_CLOSE, - uid: targetUid + type: SESSION_SEARCH, + uid: targetUid, + value: false }); } else { if (keyEvent) { diff --git a/lib/components/term-group.tsx b/lib/components/term-group.tsx index a0a0f691..de13b0a8 100644 --- a/lib/components/term-group.tsx +++ b/lib/components/term-group.tsx @@ -95,7 +95,8 @@ class TermGroup_ extends React.PureComponent { onResize: this.bind(this.props.onResize, null, uid), onTitle: this.bind(this.props.onTitle, null, uid), onData: this.bind(this.props.onData, null, uid), - toggleSearch: this.bind(this.props.toggleSearch, null, uid), + onOpenSearch: this.bind(this.props.onOpenSearch, null, uid), + onCloseSearch: this.bind(this.props.onCloseSearch, null, uid), onContextMenu: this.bind(this.props.onContextMenu, null, uid), borderColor: this.props.borderColor, selectionColor: this.props.selectionColor, diff --git a/lib/components/term.tsx b/lib/components/term.tsx index 352f7026..81413188 100644 --- a/lib/components/term.tsx +++ b/lib/components/term.tsx @@ -309,7 +309,7 @@ export default class Term extends React.PureComponent { }; closeSearchBox = () => { - this.props.toggleSearch(); + this.props.onCloseSearch(); }; resize(cols: number, rows: number) { diff --git a/lib/components/terms.tsx b/lib/components/terms.tsx index 1ba2040c..40c13c9f 100644 --- a/lib/components/terms.tsx +++ b/lib/components/terms.tsx @@ -109,7 +109,8 @@ export default class Terms extends React.Component { onResize: this.props.onResize, onTitle: this.props.onTitle, onData: this.props.onData, - toggleSearch: this.props.toggleSearch, + onOpenSearch: this.props.onOpenSearch, + onCloseSearch: this.props.onCloseSearch, onContextMenu: this.props.onContextMenu, quickEdit: this.props.quickEdit, webGLRenderer: this.props.webGLRenderer, diff --git a/lib/constants/sessions.ts b/lib/constants/sessions.ts index 0ef2f5d9..df96b59e 100644 --- a/lib/constants/sessions.ts +++ b/lib/constants/sessions.ts @@ -13,7 +13,6 @@ export const SESSION_URL_UNSET = 'SESSION_URL_UNSET'; export const SESSION_SET_XTERM_TITLE = 'SESSION_SET_XTERM_TITLE'; export const SESSION_SET_CWD = 'SESSION_SET_CWD'; export const SESSION_SEARCH = 'SESSION_SEARCH'; -export const SESSION_SEARCH_CLOSE = 'SESSION_SEARCH_CLOSE'; export interface SessionAddAction { type: typeof SESSION_ADD; @@ -83,10 +82,7 @@ export interface SessionSetCwdAction { export interface SessionSearchAction { type: typeof SESSION_SEARCH; uid: string; -} -export interface SessionSearchCloseAction { - type: typeof SESSION_SEARCH_CLOSE; - uid: string; + value: boolean; } export type SessionActions = @@ -104,5 +100,4 @@ export type SessionActions = | SessionUrlUnsetAction | SessionSetXtermTitleAction | SessionSetCwdAction - | SessionSearchAction - | SessionSearchCloseAction; + | SessionSearchAction; diff --git a/lib/containers/terms.ts b/lib/containers/terms.ts index 346545ff..c6ca776e 100644 --- a/lib/containers/terms.ts +++ b/lib/containers/terms.ts @@ -1,6 +1,13 @@ import Terms from '../components/terms'; import {connect} from '../utils/plugins'; -import {resizeSession, sendSessionData, setSessionXtermTitle, setActiveSession, onSearch} from '../actions/sessions'; +import { + resizeSession, + sendSessionData, + setSessionXtermTitle, + setActiveSession, + openSearch, + closeSearch +} from '../actions/sessions'; import {openContextMenu} from '../actions/ui'; import {getRootGroups} from '../selectors'; @@ -67,8 +74,13 @@ const mapDispatchToProps = (dispatch: HyperDispatch) => { onActive(uid: string) { dispatch(setActiveSession(uid)); }, - toggleSearch(uid: string) { - dispatch(onSearch(uid)); + + onOpenSearch(uid: string) { + dispatch(openSearch(uid)); + }, + + onCloseSearch(uid: string) { + dispatch(closeSearch(uid)); }, onContextMenu(uid: string, selection: any) { diff --git a/lib/hyper.d.ts b/lib/hyper.d.ts index d2941ad7..b6509d81 100644 --- a/lib/hyper.d.ts +++ b/lib/hyper.d.ts @@ -283,7 +283,9 @@ export type TermGroupOwnProps = { | 'modifierKeys' | 'onActive' | 'onContextMenu' + | 'onCloseSearch' | 'onData' + | 'onOpenSearch' | 'onResize' | 'onTitle' | 'padding' @@ -292,7 +294,6 @@ export type TermGroupOwnProps = { | 'scrollback' | 'selectionColor' | 'sessions' - | 'toggleSearch' | 'uiFontFamily' | 'webGLRenderer' | 'webLinksActivationKey' @@ -338,9 +339,11 @@ export type TermProps = { macOptionSelectionMode: string; modifierKeys: Immutable<{altIsMeta: boolean; cmdIsMeta: boolean}>; onActive: () => void; + onCloseSearch: () => void; onContextMenu: (selection: any) => void; onCursorMove?: (cursorFrame: {x: number; y: number; width: number; height: number; col: number; row: number}) => void; onData: (data: string) => void; + onOpenSearch: () => void; onResize: (cols: number, rows: number) => void; onTitle: (title: string) => void; padding: string; @@ -352,7 +355,6 @@ export type TermProps = { searchAddon: SearchAddon | null; selectionColor: string; term: Terminal | null; - toggleSearch: () => void; uid: string; uiFontFamily: string; url: string | null; diff --git a/lib/index.tsx b/lib/index.tsx index 41296004..944d143c 100644 --- a/lib/index.tsx +++ b/lib/index.tsx @@ -148,7 +148,7 @@ rpc.on('session tmux req', () => { }); rpc.on('session search', () => { - store_.dispatch(sessionActions.onSearch()); + store_.dispatch(sessionActions.openSearch()); }); rpc.on('session search close', () => { diff --git a/lib/reducers/sessions.ts b/lib/reducers/sessions.ts index 1850df2f..ade6a2bc 100644 --- a/lib/reducers/sessions.ts +++ b/lib/reducers/sessions.ts @@ -10,8 +10,7 @@ import { SESSION_RESIZE, SESSION_SET_XTERM_TITLE, SESSION_SET_CWD, - SESSION_SEARCH, - SESSION_SEARCH_CLOSE + SESSION_SEARCH } from '../constants/sessions'; import {sessionState, session, Mutable, ISessionReducer} from '../hyper'; @@ -61,10 +60,7 @@ const reducer: ISessionReducer = (state = initialState, action) => { return state.set('activeUid', action.uid); case SESSION_SEARCH: - return state.setIn(['sessions', action.uid, 'search'], !state.sessions[action.uid].search); - - case SESSION_SEARCH_CLOSE: - return state.setIn(['sessions', action.uid, 'search'], false); + return state.setIn(['sessions', action.uid, 'search'], action.value); case SESSION_CLEAR_ACTIVE: return state.merge(