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

Added info-box #2934

Merged
merged 6 commits into from
Feb 5, 2025
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ The icons may not be reused in other projects without the proper flaticon licens
<!--
### **WORK IN PROGRESS**
-->
### **WORK IN PROGRESS**
- (@GermanBluefox) Corrected the device manager in `hm-rpc` and other adapters

### 7.4.19 (2025-01-26)
- (@GermanBluefox) Corrected file upload in File Browser and in JSON Config

Expand Down
423 changes: 195 additions & 228 deletions package-lock.json

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions packages/adapter-react-v5/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@
"@iobroker/js-controller-common-db": "^7.0.6",
"@iobroker/socket-client": "^4.0.0",
"@iobroker/types": "^7.0.6",
"@mui/icons-material": "^6.4.1",
"@mui/material": "^6.4.1",
"@sentry/browser": "^8.51.0",
"cronstrue": "^2.53.0",
"@mui/icons-material": "^6.4.3",
"@mui/material": "^6.4.3",
"@sentry/browser": "^8.54.0",
"cronstrue": "^2.54.0",
"file-selector": "^2.1.2",
"react-color": "^2.19.3",
"react-colorful": "^5.6.1",
Expand All @@ -73,14 +73,14 @@
"react-inlinesvg": "^4.1.8"
},
"devDependencies": {
"@babel/core": "^7.26.0",
"@babel/core": "^7.26.7",
"@babel/plugin-proposal-class-properties": "^7.18.6",
"@babel/plugin-transform-runtime": "^7.25.9",
"@babel/preset-env": "^7.26.0",
"@babel/preset-env": "^7.26.7",
"@babel/preset-flow": "^7.25.9",
"@babel/preset-react": "^7.26.3",
"@iobroker/eslint-config": "^1.0.0",
"@types/node": "^22.10.10",
"@types/node": "^22.13.1",
"@types/react": "^18.3.18",
"@types/react-color": "^3.0.13",
"ajv": "^8.17.1",
Expand Down
167 changes: 167 additions & 0 deletions packages/adapter-react-v5/src/Components/InfoBox.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
import React from 'react';
import { Box, Typography } from '@mui/material';
import { Info, Warning, Close, Visibility, type SvgIconComponent, Check } from '@mui/icons-material';

interface InfoBoxProps {
/** Text to display in the info box */
children: string | (string | React.JSX.Element | null)[] | React.JSX.Element;
/** The type determines the color and symbol */
type: 'warning' | 'info' | 'error' | 'ok';
/** If the Box is closeable */
closeable?: boolean;
/** Use together with `closeable: true`. You can specify in which variable in local storage the state of this info box could be stored */
storeId?: string;
/** Use together with `closeable: true`, listener called if close button clicked */
onClose?: (desiredState: boolean) => void;
/** Custom style */
style?: React.CSSProperties;
/** Icon position */
iconPosition?: 'top' | 'middle';
/** Use together with `closeable: true`. If the box is closed or not. In this case, it will be controlled from outside */
closed?: boolean;
}

interface InfoBoxState {
closed: boolean;
}
/**
* This component can be used to show important information or warnings to the user
*/
export default class InfoBox extends React.Component<InfoBoxProps, InfoBoxState> {
private readonly refTypo: React.RefObject<HTMLDivElement>;
private height: number;
private width: number;

constructor(props: InfoBoxProps) {
super(props);
this.state = {
closed: this.props.storeId ? window.localStorage.getItem(this.props.storeId) === 'true' : false,
};
this.height = 0;
this.width = 0;

this.refTypo = React.createRef();
}

componentDidMount(): void {
this.detectHeight();
}

onClick(): void {
if (this.props.storeId && this.props.closed === undefined) {
if (this.state.closed) {
window.localStorage.removeItem(this.props.storeId);
} else {
window.localStorage.setItem(this.props.storeId, 'true');
}
}
if (this.props.closed === undefined) {
this.setState({ closed: !this.state.closed }, () => {
// Inform about state change
if (this.props.onClose) {
this.props.onClose(this.state.closed);
}
});
} else if (this.props.onClose) {
this.props.onClose(!this.props.closed);
}
}

detectHeight(): void {
const closed = this.props.closed !== undefined ? this.props.closed : this.state.closed;
// We must get the height of the element when it is open to make transition
if (this.props.closeable && !closed && this.refTypo.current) {
window.requestAnimationFrame(() => {
const closed = this.props.closed !== undefined ? this.props.closed : this.state.closed;
if (closed) {
return;
}
if (this.refTypo.current && (!this.height || this.width !== this.refTypo.current.clientWidth)) {
this.height = this.refTypo.current.clientHeight;
this.width = this.refTypo.current.clientWidth;
this.forceUpdate();
}
});
}
}

componentDidUpdate(): void {
this.detectHeight();
}

render(): React.ReactNode {
const closed = this.props.closed !== undefined ? this.props.closed : this.state.closed;

const Icon: SvgIconComponent = closed ? Visibility : Close;

return (
<Box
className="iom-info-box"
style={{
whiteSpace: 'preserve',
display: 'flex',
gap: 8,
alignItems: closed || this.props.iconPosition === 'top' ? 'flex-start' : 'center',
borderWidth: 1,
borderStyle: 'solid',
padding: 4,
borderRadius: 5,
marginBottom: 8,
maxWidth: '100%',
transition: 'height 0.5s',
height: this.props.closeable ? (closed ? 30 : this.height || undefined) : undefined,
overflow: this.props.closeable ? 'hidden' : undefined,
position: 'relative',
...this.props.style,
}}
sx={{
borderColor: theme =>
this.props.type === 'ok' ? theme.palette.info.main : theme.palette[this.props.type].main,
}}
>
{this.props.type === 'ok' ? (
<Check style={{ color: '#0F0' }} />
) : this.props.type === 'info' ? (
<Info color="primary" />
) : (
<Warning color={this.props.type} />
)}
<Typography ref={this.refTypo}>{this.props.children}</Typography>
{this.props.closeable ? (
<Icon
sx={{
color: theme => (theme.palette.mode === 'dark' ? 'lightgray' : 'gray'),
// alignSelf: 'flex-start',
cursor: 'pointer',
position: 'absolute',
top: 4,
right: 4,
}}
onClick={() => this.onClick()}
/>
) : null}
{/* Reserve place for button so it will not overlap the text */}
{this.props.closeable ? <div style={{ width: 22 }} /> : null}
{closed ? (
<Box
// This is a shadow box at the bottom of the InfoBox when it closed
component="div"
sx={theme => {
const color = theme.palette[this.props.type === 'ok' ? 'info' : this.props.type].main;
return {
background: `linear-gradient(${color}00 0%, ${color}10 60%, ${color}90 100%)`,
};
}}
style={{
bottom: 0,
position: 'absolute',
left: 0,
right: 0,
height: 10,
}}
/>
) : null}
</Box>
);
}
}
47 changes: 27 additions & 20 deletions packages/adapter-react-v5/src/GenericApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,22 @@ body {
}
`;

function isIFrame(): boolean {
try {
return window.self !== window.top;
} catch {
return true;
}
}

export class GenericApp<
TProps extends GenericAppProps = GenericAppProps,
TState extends GenericAppState = GenericAppState,
> extends Router<TProps, TState> {
protected socket: AdminConnection;

protected isIFrame = isIFrame();

protected readonly instance: number;

protected readonly adapterName: string;
Expand Down Expand Up @@ -686,12 +696,11 @@ export class GenericApp<
*/
onPrepareSave(settings: Record<string, any>): boolean {
// here you can encode values
this.encryptedFields &&
this.encryptedFields.forEach(attr => {
if (settings[attr]) {
settings[attr] = this.encrypt(settings[attr]);
}
});
this.encryptedFields?.forEach(attr => {
if (settings[attr]) {
settings[attr] = this.encrypt(settings[attr]);
}
});

return true;
}
Expand All @@ -705,20 +714,18 @@ export class GenericApp<
*/
onPrepareLoad(settings: Record<string, any>, encryptedNative?: string[]): void {
// here you can encode values
this.encryptedFields &&
this.encryptedFields.forEach(attr => {
if (settings[attr]) {
settings[attr] = this.decrypt(settings[attr]);
}
});
encryptedNative &&
encryptedNative.forEach(attr => {
this.encryptedFields = this.encryptedFields || [];
!this.encryptedFields.includes(attr) && this.encryptedFields.push(attr);
if (settings[attr]) {
settings[attr] = this.decrypt(settings[attr]);
}
});
this.encryptedFields?.forEach(attr => {
if (settings[attr]) {
settings[attr] = this.decrypt(settings[attr]);
}
});
encryptedNative?.forEach(attr => {
this.encryptedFields = this.encryptedFields || [];
!this.encryptedFields.includes(attr) && this.encryptedFields.push(attr);
if (settings[attr]) {
settings[attr] = this.decrypt(settings[attr]);
}
});
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/admin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@
"json5": "^2.2.3",
"mime": "^3.0.0",
"passport-local": "^1.0.0",
"semver": "^7.6.3"
"semver": "^7.7.1"
},
"devDependencies": {
"@iobroker/build-tools": "^2.0.14",
"@iobroker/build-tools": "^2.0.15",
"@iobroker/dm-gui-components": "file:../dm-gui-components",
"@iobroker/json-config": "file:../jsonConfig",
"@iobroker/legacy-testing": "^2.0.2",
Expand Down
16 changes: 8 additions & 8 deletions packages/admin/src-admin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@
"@types/ace": "^0.0.52",
"@types/crypto-js": "^4.2.2",
"@types/leaflet": "^1.9.16",
"@types/less": "^3.0.7",
"@types/lodash": "^4.17.14",
"@types/less": "^3.0.8",
"@types/lodash": "^4.17.15",
"@types/mocha": "^10.0.10",
"@types/node": "^22.10.10",
"@types/node": "^22.13.1",
"@types/react": "^18.3.18",
"@types/react-color": "^3.0.13",
"@types/react-dom": "^18.3.5",
Expand All @@ -71,18 +71,18 @@
"react-ace": "^13.0.0",
"react-dnd": "^16.0.1",
"react-dnd-html5-backend": "^16.0.1",
"react-dnd-multi-backend": "^8.1.2",
"react-dnd-preview": "^8.1.2",
"react-dnd-multi-backend": "^9.0.0",
"react-dnd-preview": "^9.0.0",
"react-dnd-touch-backend": "^16.0.1",
"react-icons": "^5.4.0",
"react-leaflet": "^4.2.1",
"react-markdown": "^9.0.3",
"react-monaco-editor": "^0.56.2",
"react-monaco-editor": "^0.58.0",
"react-qr-code": "^2.0.15",
"react-scripts": "^5.0.1",
"react-showdown": "^2.3.1",
"react-sortable-hoc": "^2.0.0",
"semver": "^7.6.3",
"semver": "^7.7.1",
"tsconfig-paths-webpack-plugin": "^4.2.0"
},
"resolutions": {
Expand All @@ -103,5 +103,5 @@
}
]
],
"version": "7.4.18"
"version": "7.4.19"
}
2 changes: 1 addition & 1 deletion packages/admin/src-admin/public/css/adapter.css

Large diffs are not rendered by default.

Loading
Loading