import React from "react";
import { connect } from "react-redux";
import classnames from "classnames";
import "../../../css/gen-window.css";
import { SET_FOCUSED_WINDOW } from "../../actionTypes";
import { setWindowSize, closeWindow } from "../../actionCreators";
import { getWindowPixelSize } from "../../selectors";
import ResizeTarget from "../ResizeTarget";
import { AppState, WindowId, Dispatch } from "../../types";
interface TextProps {
children: string;
}
const Text = ({ children }: TextProps) => {
const letters = children.split("");
return (
{letters.map((letter, i) => (
))}
);
};
const CHROME_WIDTH = 19;
const CHROME_HEIGHT = 34;
interface WindowSize {
width: number;
height: number;
}
interface OwnProps {
windowId: WindowId;
children: (windowSize: WindowSize) => React.ReactNode;
title: string;
}
interface DispatchProps {
setFocus: (windowId: WindowId) => void;
close: (windowId: WindowId) => void;
setGenWindowSize: (windowId: WindowId, size: [number, number]) => void;
}
interface StateProps {
windowSize: [number, number];
selected: boolean;
height: number;
width: number;
}
type Props = OwnProps & DispatchProps & StateProps;
// Named export for testing
export const GenWindow = ({
selected,
children,
close,
title,
setFocus,
windowId,
windowSize,
setGenWindowSize,
width,
height
}: Props) => {
return (
setFocus(windowId)}
style={{ width, height }}
>
{title}
{children({
width: width - CHROME_WIDTH,
height: height - CHROME_HEIGHT
})}
setGenWindowSize(windowId, size)}
id={"gen-resize-target"}
/>
);
};
const mapStateToProps = (state: AppState, ownProps: OwnProps): StateProps => {
const { width, height } = getWindowPixelSize(state)(ownProps.windowId);
return {
width,
height,
selected: state.windows.focused === ownProps.windowId,
windowSize: state.windows.genWindows[ownProps.windowId].size
};
};
const mapDispatchToProps = (dispatch: Dispatch): DispatchProps => {
return {
setFocus: (windowId: WindowId) =>
dispatch({ type: SET_FOCUSED_WINDOW, window: windowId }),
close: (windowId: WindowId) => dispatch(closeWindow(windowId)),
setGenWindowSize: (windowId: WindowId, size: [number, number]) =>
dispatch(setWindowSize(windowId, size))
};
};
export default connect(
mapStateToProps,
mapDispatchToProps
)(GenWindow);