refactor search redux actions

explicit open and close instead of toggle and close
This commit is contained in:
Labhansh Agrawal 2022-01-09 11:05:57 +05:30
parent bd764b078a
commit 089b90fff1
9 changed files with 36 additions and 28 deletions

View file

@ -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) {

View file

@ -95,7 +95,8 @@ class TermGroup_ extends React.PureComponent<TermGroupProps> {
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,

View file

@ -309,7 +309,7 @@ export default class Term extends React.PureComponent<TermProps> {
};
closeSearchBox = () => {
this.props.toggleSearch();
this.props.onCloseSearch();
};
resize(cols: number, rows: number) {

View file

@ -109,7 +109,8 @@ export default class Terms extends React.Component<TermsProps> {
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,

View file

@ -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;

View file

@ -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) {

6
lib/hyper.d.ts vendored
View file

@ -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;

View file

@ -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', () => {

View file

@ -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(