Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert base redux to TS #276

Merged
merged 2 commits into from
Nov 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 3 additions & 19 deletions packages/dashboard-core-plugins/src/linker/Linker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
getActiveTool,
getTimeZone,
setActiveTool as setActiveToolAction,
RootState,
} from '@deephaven/redux';
import {
getIsolatedLinkerPanelIdForDashboard,
Expand Down Expand Up @@ -55,7 +56,7 @@ interface OwnProps {
localDashboardId: string;
}

const mapState = (state: LinkerState, ownProps: OwnProps): StateProps => ({
const mapState = (state: RootState, ownProps: OwnProps): StateProps => ({
activeTool: getActiveTool(state),
isolatedLinkerPanelId: getIsolatedLinkerPanelIdForDashboard(
state,
Expand All @@ -65,24 +66,7 @@ const mapState = (state: LinkerState, ownProps: OwnProps): StateProps => ({
timeZone: getTimeZone(state),
});

type DispatchProps = {
setActiveTool: (activeTool: string) => void;
setDashboardLinks: (dashboardId: string, links: Link[]) => void;
addDashboardLinks: (dashboardId: string, links: Link[]) => void;
deleteDashboardLinks: (dashboardId: string, linkIds: string[]) => void;
setDashboardIsolatedLinkerPanelId: (
dashboardId: string,
panelId: string | undefined
) => void;
setDashboardColumnSelectionValidator: (
dashboardId: string,
columnValidator:
| ((panel: PanelComponent, column?: LinkColumn) => boolean)
| undefined
) => void;
};

const connector = connect<StateProps, DispatchProps, OwnProps>(mapState, {
const connector = connect(mapState, {
setActiveTool: setActiveToolAction,
setDashboardLinks: setDashboardLinksAction,
addDashboardLinks: addDashboardLinksAction,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Log from '@deephaven/log';
import { getFileStorage } from '@deephaven/redux';
import { getFileStorage, RootState } from '@deephaven/redux';
import FileExplorer, {
FileExplorerToolbar,
FileStorage,
Expand Down Expand Up @@ -244,7 +244,7 @@ export class FileExplorerPanel extends React.Component<
}

const mapStateToProps = (
state: unknown,
state: RootState,
ownProps: { localDashboardId: string }
) => {
const fileStorage = getFileStorage(state);
Expand Down
3 changes: 3 additions & 0 deletions packages/redux/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@
},
"devDependencies": {
"@babel/cli": "^7.14.8",
"@deephaven/console": "^0.6.0",
"@deephaven/file-explorer": "^0.6.0",
"@deephaven/log": "^0.6.0",
"@deephaven/tsconfig": "^0.6.0",
"@types/deep-equal": "^1.0.1",
"cross-env": "^7.0.2",
"npm-run-all": "^4.1.5",
"redux": "^4.0.5",
Expand Down
File renamed without changes.
74 changes: 0 additions & 74 deletions packages/redux/src/actions.js

This file was deleted.

112 changes: 112 additions & 0 deletions packages/redux/src/actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import type { Action } from 'redux';
import type { ThunkAction } from 'redux-thunk';
import type { CommandHistoryStorage } from '@deephaven/console';
import type { FileStorage } from '@deephaven/file-explorer';
import {
SET_USER,
SET_WORKSPACE,
SET_COMMAND_HISTORY_STORAGE,
SET_WORKSPACE_STORAGE,
SET_ACTIVE_TOOL,
SET_FILE_STORAGE,
} from './actionTypes';
import type {
RootState,
User,
Workspace,
WorkspaceData,
WorkspaceSettings,
WorkspaceStorage,
} from './store';

export interface PayloadAction<P = unknown> extends Action<string> {
payload: P;
}

export type PayloadActionCreator<P> = (payload: P) => PayloadAction<P>;

export const setUser: PayloadActionCreator<User> = user => ({
type: SET_USER,
payload: user,
});

export const setWorkspace: PayloadActionCreator<Workspace> = workspace => ({
type: SET_WORKSPACE,
payload: workspace,
});

export const setWorkspaceStorage: PayloadActionCreator<WorkspaceStorage> = workspaceStorage => ({
type: SET_WORKSPACE_STORAGE,
payload: workspaceStorage,
});

export const setCommandHistoryStorage: PayloadActionCreator<CommandHistoryStorage> = commandHistoryStorage => ({
type: SET_COMMAND_HISTORY_STORAGE,
payload: commandHistoryStorage,
});

export const setFileStorage: PayloadActionCreator<FileStorage> = fileStorage => ({
type: SET_FILE_STORAGE,
payload: fileStorage,
});

/**
* Sets the specified workspace locally and saves it remotely
* @param workspace The workspace to save
*/
export const saveWorkspace = (
workspace: Workspace
): ThunkAction<
Promise<Workspace>,
RootState,
never,
PayloadAction<unknown>
> => (dispatch, getState) => {
dispatch(setWorkspace(workspace));

const { storage } = getState();
const { workspaceStorage } = storage;
return workspaceStorage.save(workspace);
};

/**
* Update part of the workspace data and save it
* @param workspaceData The properties to update in workspace data
*/
export const updateWorkspaceData = (
workspaceData: Partial<WorkspaceData>
): ThunkAction<
Promise<Workspace>,
RootState,
never,
PayloadAction<unknown>
> => (dispatch, getState) => {
const { workspace } = getState();
const { data } = workspace;
const newWorkspace = {
...workspace,
data: {
...data,
...workspaceData,
},
};
return dispatch(saveWorkspace(newWorkspace));
};

/**
* Sets the specified settings locally and saves them remotely
* @param {Object} settings The settings to save
*/
export const saveSettings = (
settings: WorkspaceSettings
): ThunkAction<
Promise<Workspace>,
RootState,
never,
PayloadAction<unknown>
> => dispatch => dispatch(updateWorkspaceData({ settings }));

export const setActiveTool: PayloadActionCreator<string> = payload => ({
type: SET_ACTIVE_TOOL,
payload,
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export * from './actions';
export * from './reducers/common';
export { default as reducers } from './reducers';
export { default as reducerRegistry } from './reducerRegistry';
export * from './store';
export { default as store } from './store';
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { Middleware } from 'redux';
import Log from '@deephaven/log';

const log = Log.module('redux-crashReporter');

const crashReporter = store => next => action => {
const crashReporter: Middleware = store => next => action => {
try {
return next(action);
} catch (err) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { Middleware } from 'redux';
import Log from '@deephaven/log';

const log = Log.module('redux-logger');

const logger = store => next => action => {
const logger: Middleware = store => next => action => {
log.debug('dispatching', action);
const result = next(action);
log.debug('next state', store.getState());
Expand Down
27 changes: 0 additions & 27 deletions packages/redux/src/reducers/common/mergeReducer.js

This file was deleted.

34 changes: 34 additions & 0 deletions packages/redux/src/reducers/common/mergeReducer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import type { Reducer } from 'redux';

/**
* Setup a merge reducer for a specific action type.
* Will take the payload passed in and merge it with the previous state to update.
* @param type The action type
* @param initialState The initial state
*/
export default function mergeReducer<S>(
type: string,
initialState: S
): Reducer<S> {
return (state = initialState, action) => {
switch (action.type) {
case type: {
const newState = action.payload;
if (newState == null) {
return null;
}

if (state != null) {
return {
...state,
...newState,
};
}

return { ...newState };
}
default:
return state;
}
};
}
26 changes: 0 additions & 26 deletions packages/redux/src/reducers/common/replaceByIdReducer.js

This file was deleted.

Loading