Improve containers typings and add prop types

This commit is contained in:
Labhansh Agrawal 2020-02-19 21:11:29 +05:30 committed by Benjamin Staneck
parent d0ce94ce8e
commit c337288efa
5 changed files with 249 additions and 250 deletions

View file

@ -5,8 +5,7 @@ import Header from '../components/header';
import {closeTab, changeTab, maximize, openHamburgerMenu, unmaximize, minimize, close} from '../actions/header'; import {closeTab, changeTab, maximize, openHamburgerMenu, unmaximize, minimize, close} from '../actions/header';
import {connect} from '../utils/plugins'; import {connect} from '../utils/plugins';
import {getRootGroups} from '../selectors'; import {getRootGroups} from '../selectors';
import {HyperState} from '../hyper'; import {HyperState, HyperDispatch} from '../hyper';
import {Dispatch} from 'redux';
const isMac = /Mac/.test(navigator.userAgent); const isMac = /Mac/.test(navigator.userAgent);
@ -29,8 +28,7 @@ const getTabs = createSelector(
}) })
); );
export const HeaderContainer = connect( const mapStateToProps = (state: HyperState) => {
(state: HyperState) => {
return { return {
// active is an index // active is an index
isMac, isMac,
@ -43,8 +41,9 @@ export const HeaderContainer = connect(
showHamburgerMenu: state.ui.showHamburgerMenu, showHamburgerMenu: state.ui.showHamburgerMenu,
showWindowControls: state.ui.showWindowControls showWindowControls: state.ui.showWindowControls
}; };
}, };
(dispatch: Dispatch<any>) => {
const mapDispatchToProps = (dispatch: HyperDispatch) => {
return { return {
onCloseTab: (i: string) => { onCloseTab: (i: string) => {
dispatch(closeTab(i)); dispatch(closeTab(i));
@ -74,6 +73,8 @@ export const HeaderContainer = connect(
dispatch(close()); dispatch(close());
} }
}; };
}, };
null
)(Header, 'Header'); export const HeaderContainer = connect(mapStateToProps, mapDispatchToProps, null)(Header, 'Header');
export type HeaderConnectedProps = ReturnType<typeof mapStateToProps> & ReturnType<typeof mapDispatchToProps>;

View file

@ -11,12 +11,11 @@ import stylis from 'stylis';
import {HeaderContainer} from './header'; import {HeaderContainer} from './header';
import TermsContainer from './terms'; import TermsContainer from './terms';
import NotificationsContainer from './notifications'; import NotificationsContainer from './notifications';
import {HyperState} from '../hyper'; import {HyperState, HyperProps, HyperDispatch} from '../hyper';
import {Dispatch} from 'redux';
const isMac = /Mac/.test(navigator.userAgent); const isMac = /Mac/.test(navigator.userAgent);
class Hyper extends React.PureComponent<any, any> { class Hyper extends React.PureComponent<HyperProps, {lastConfigUpdate: number}> {
mousetrap!: MousetrapInstance; mousetrap!: MousetrapInstance;
terms: any; terms: any;
constructor(props: any) { constructor(props: any) {
@ -26,7 +25,7 @@ class Hyper extends React.PureComponent<any, any> {
}; };
} }
//TODO: Remove usage of legacy and soon deprecated lifecycle methods //TODO: Remove usage of legacy and soon deprecated lifecycle methods
UNSAFE_componentWillReceiveProps(next: any) { UNSAFE_componentWillReceiveProps(next: HyperProps) {
if (this.props.backgroundColor !== next.backgroundColor) { if (this.props.backgroundColor !== next.backgroundColor) {
// this can be removed when `setBackgroundColor` in electron // this can be removed when `setBackgroundColor` in electron
// starts working again // starts working again
@ -90,9 +89,9 @@ class Hyper extends React.PureComponent<any, any> {
window.focusActiveTerm = this.handleFocusActive; window.focusActiveTerm = this.handleFocusActive;
}; };
componentDidUpdate(prev: any) { componentDidUpdate(prev: HyperProps) {
if (prev.activeSession !== this.props.activeSession) { if (prev.activeSession !== this.props.activeSession) {
this.handleFocusActive(this.props.activeSession); this.handleFocusActive(this.props.activeSession!);
} }
} }
@ -147,8 +146,7 @@ class Hyper extends React.PureComponent<any, any> {
} }
} }
const HyperContainer = connect( const mapStateToProps = (state: HyperState) => {
(state: HyperState) => {
return { return {
isMac, isMac,
customCSS: state.ui.css, customCSS: state.ui.css,
@ -160,16 +158,18 @@ const HyperContainer = connect(
fullScreen: state.ui.fullScreen, fullScreen: state.ui.fullScreen,
lastConfigUpdate: state.ui._lastUpdate lastConfigUpdate: state.ui._lastUpdate
}; };
}, };
(dispatch: Dispatch<any>) => {
const mapDispatchToProps = (dispatch: HyperDispatch) => {
return { return {
execCommand: (command: any, fn: any, e: any) => { execCommand: (command: any, fn: any, e: any) => {
dispatch(uiActions.execCommand(command, fn, e)); dispatch(uiActions.execCommand(command, fn, e));
} }
}; };
}, };
null,
{forwardRef: true} const HyperContainer = connect(mapStateToProps, mapDispatchToProps, null, {forwardRef: true})(Hyper, 'Hyper');
)(Hyper, 'Hyper');
export default HyperContainer; export default HyperContainer;
export type HyperConnectedProps = ReturnType<typeof mapStateToProps> & ReturnType<typeof mapDispatchToProps>;

View file

@ -2,56 +2,75 @@ import Notifications from '../components/notifications';
import {installUpdate} from '../actions/updater'; import {installUpdate} from '../actions/updater';
import {connect} from '../utils/plugins'; import {connect} from '../utils/plugins';
import {dismissNotification} from '../actions/notifications'; import {dismissNotification} from '../actions/notifications';
import {HyperState} from '../hyper'; import {HyperState, HyperDispatch} from '../hyper';
import {Dispatch} from 'redux';
const NotificationsContainer = connect( const mapStateToProps = (state: HyperState) => {
(state: HyperState) => {
const {ui} = state; const {ui} = state;
const {notifications} = ui; const {notifications} = ui;
const state_ = {}; let state_: Partial<{
fontShowing: boolean;
fontSize: number;
fontText: string;
resizeShowing: boolean;
cols: number | null;
rows: number | null;
updateShowing: boolean;
updateVersion: string | null;
updateNote: string | null;
updateReleaseUrl: string | null;
updateCanInstall: boolean | null;
messageShowing: boolean;
messageText: string | null;
messageURL: string | null;
messageDismissable: boolean | null;
}> = {};
if (notifications.font) { if (notifications.font) {
const fontSize = ui.fontSizeOverride || ui.fontSize; const fontSize = ui.fontSizeOverride || ui.fontSize;
Object.assign(state_, { state_ = {
...state_,
fontShowing: true, fontShowing: true,
fontSize, fontSize,
fontText: `${fontSize}px` fontText: `${fontSize}px`
}); };
} }
if (notifications.resize) { if (notifications.resize) {
const cols = ui.cols; const cols = ui.cols;
const rows = ui.rows; const rows = ui.rows;
Object.assign(state_, { state_ = {
...state_,
resizeShowing: true, resizeShowing: true,
cols, cols,
rows rows
}); };
} }
if (notifications.updates) { if (notifications.updates) {
Object.assign(state_, { state_ = {
...state_,
updateShowing: true, updateShowing: true,
updateVersion: ui.updateVersion, updateVersion: ui.updateVersion,
updateNote: ui.updateNotes!.split('\n')[0], updateNote: ui.updateNotes!.split('\n')[0],
updateReleaseUrl: ui.updateReleaseUrl, updateReleaseUrl: ui.updateReleaseUrl,
updateCanInstall: ui.updateCanInstall updateCanInstall: ui.updateCanInstall
}); };
} else if (notifications.message) { } else if (notifications.message) {
Object.assign(state_, { state_ = {
...state_,
messageShowing: true, messageShowing: true,
messageText: ui.messageText, messageText: ui.messageText,
messageURL: ui.messageURL, messageURL: ui.messageURL,
messageDismissable: ui.messageDismissable messageDismissable: ui.messageDismissable
}); };
} }
return state_; return state_;
}, };
(dispatch: Dispatch<any>) => {
const mapDispatchToProps = (dispatch: HyperDispatch) => {
return { return {
onDismissFont: () => { onDismissFont: () => {
dispatch(dismissNotification('font')); dispatch(dismissNotification('font'));
@ -69,8 +88,10 @@ const NotificationsContainer = connect(
dispatch(installUpdate()); dispatch(installUpdate());
} }
}; };
}, };
null
)(Notifications, 'Notifications'); const NotificationsContainer = connect(mapStateToProps, mapDispatchToProps, null)(Notifications, 'Notifications');
export default NotificationsContainer; export default NotificationsContainer;
export type NotificationsConnectedProps = ReturnType<typeof mapStateToProps> & ReturnType<typeof mapDispatchToProps>;

View file

@ -4,11 +4,9 @@ import {resizeSession, sendSessionData, setSessionXtermTitle, setActiveSession,
import {openContextMenu} from '../actions/ui'; import {openContextMenu} from '../actions/ui';
import {getRootGroups} from '../selectors'; import {getRootGroups} from '../selectors';
import {HyperState, TermsProps} from '../hyper'; import {HyperState, HyperDispatch} from '../hyper';
import {Dispatch} from 'redux';
const TermsContainer = connect( const mapStateToProps = (state: HyperState) => {
(state: HyperState): TermsProps => {
const {sessions} = state.sessions; const {sessions} = state.sessions;
return { return {
sessions, sessions,
@ -48,8 +46,9 @@ const TermsContainer = connect(
macOptionSelectionMode: state.ui.macOptionSelectionMode, macOptionSelectionMode: state.ui.macOptionSelectionMode,
disableLigatures: state.ui.disableLigatures disableLigatures: state.ui.disableLigatures
}; };
}, };
(dispatch: Dispatch<any>) => {
const mapDispatchToProps = (dispatch: HyperDispatch) => {
return { return {
onData(uid: string, data: any) { onData(uid: string, data: any) {
dispatch(sendSessionData(uid, data)); dispatch(sendSessionData(uid, data));
@ -75,9 +74,10 @@ const TermsContainer = connect(
dispatch(openContextMenu(uid, selection)); dispatch(openContextMenu(uid, selection));
} }
}; };
}, };
null,
{forwardRef: true} const TermsContainer = connect(mapStateToProps, mapDispatchToProps, null, {forwardRef: true})(Terms, 'Terms');
)(Terms, 'Terms');
export default TermsContainer; export default TermsContainer;
export type TermsConnectedProps = ReturnType<typeof mapStateToProps> & ReturnType<typeof mapDispatchToProps>;

57
lib/hyper.d.ts vendored
View file

@ -185,44 +185,21 @@ import configureStore from './store/configure-store';
export type HyperThunkDispatch = ThunkDispatch<HyperState, undefined, HyperActions>; export type HyperThunkDispatch = ThunkDispatch<HyperState, undefined, HyperActions>;
export type HyperDispatch = ReturnType<typeof configureStore>['dispatch']; export type HyperDispatch = ReturnType<typeof configureStore>['dispatch'];
export type TermsProps = { type extensionProps = Partial<{
activeRootGroup: string | null; customChildren: any;
activeSession: string | null; customChildrenBefore: any;
customCSS: string; customCSS: string;
fontSmoothing: string; customInnerChildren: any;
termGroups: Immutable<ITermGroup>[]; }>;
} & immutableRecord<
Pick< import {HeaderConnectedProps} from './containers/header';
uiState, export type HeaderProps = HeaderConnectedProps & extensionProps;
| 'backgroundColor'
| 'bell' import {HyperConnectedProps} from './containers/hyper';
| 'bellSound' export type HyperProps = HyperConnectedProps & extensionProps;
| 'bellSoundURL'
| 'borderColor' import {NotificationsConnectedProps} from './containers/notifications';
| 'colors' export type NotificationsProps = NotificationsConnectedProps & extensionProps;
| 'cols'
| 'copyOnSelect' import {TermsConnectedProps} from './containers/terms';
| 'cursorAccentColor' export type TermsProps = TermsConnectedProps & extensionProps & {ref_: any};
| 'cursorBlink'
| 'cursorColor'
| 'cursorShape'
| 'disableLigatures'
| 'fontFamily'
| 'fontSize'
| 'fontWeight'
| 'fontWeightBold'
| 'foregroundColor'
| 'letterSpacing'
| 'lineHeight'
| 'macOptionSelectionMode'
| 'modifierKeys'
| 'padding'
| 'quickEdit'
| 'rows'
| 'scrollback'
| 'selectionColor'
| 'uiFontFamily'
| 'webGLRenderer'
>
> &
immutableRecord<Pick<sessionState, 'sessions' | 'write'>>;