Skip to content

Commit 36ce681

Browse files
committed
Convert React to TypeScript
1 parent 9d75bd5 commit 36ce681

24 files changed

+1059
-989
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
"test": "npm run test:declarations && npm run test:javascript",
9191
"translate": "translate-adapter",
9292
"//postinstall": "node ./install/installTypings.js",
93-
"build": "node tasks",
93+
"build": "node tasks && npm run tsc-backend",
9494
"tsc-backend": "tsc -p tsconfig.build.json",
9595
"release": "release-script --noPush -y --all",
9696
"update-packages": "ncu --upgrade && cd src && ncu --upgrade && cd ../src-admin && ncu --upgrade",

src-editor/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@
4444
"scripts": {
4545
"start": "craco start",
4646
"lint": "eslint -c eslint.config.mjs",
47-
"build": "craco build"
47+
"build": "craco build",
48+
"tsc": "tsc -p tsconfig.json"
4849
},
4950
"eslintConfig": {
5051
"extends": "react-app"
@@ -55,4 +56,4 @@
5556
"not ie <= 11",
5657
"not op_mini all"
5758
]
58-
}
59+
}

src-editor/src/Components/RulesEditor/components/Blocks/TriggerSchedule.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
Schedule,
77
I18n,
88
} from '@iobroker/adapter-react-v5';
9-
import convertCronToText from '@iobroker/adapter-react-v5/Components/SimpleCron/cronText';
9+
import convertCronToText from '@iobroker/adapter-react-v5/build/Components/SimpleCron/cronText';
1010

1111
import GenericBlock from '../GenericBlock';
1212
import Compile from '../../helpers/Compile';

src-editor/src/Components/RulesEditor/components/GenericBlock/index.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import {
88
} from '@mui/material';
99
import { HelpOutline as IconHelp } from '@mui/icons-material';
1010

11-
import { getSelectIdIcon } from '@iobroker/adapter-react-v5/Components/Icon';
1211
import {
12+
getSelectIdIcon,
1313
I18n, Utils,
1414
SelectID as DialogSelectID,
1515
Error as DialogError,

src-editor/src/Components/RulesEditor/index.jsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,25 +160,21 @@ const RulesEditor = ({
160160
allBlocks={allBlocks}
161161
socket={socket}
162162
/>
163-
{importExport === 'export' ? (
163+
{modal ? (importExport === 'export' ? (
164164
<DialogExport
165-
key="dialogExport"
166165
onClose={() => setModal(false)}
167-
open={modal}
168166
text={JSON.stringify(userRules, null, 2)}
169167
/>
170168
) : (
171169
<DialogImport
172-
open={modal}
173-
key="dialogImport"
174170
onClose={text => {
175171
setModal(false);
176172
if (text) {
177173
onChangeBlocks(JSON.parse(text));
178174
}
179175
}}
180176
/>
181-
)}
177+
)) : null}
182178
{
183179
<div className={Utils.clsx(cls.rootWrapper, addClass[835] && cls.addClass)}>
184180
<Menu

src-editor/src/Dialogs/AdapterDebug.jsx renamed to src-editor/src/Dialogs/AdapterDebug.tsx

Lines changed: 63 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React from 'react';
2-
import PropTypes from 'prop-types';
32

43
import {
54
Button,
@@ -9,19 +8,19 @@ import {
98
Dialog,
109
ListItemIcon,
1110
List,
12-
ListItem,
1311
Grid2,
1412
ListItemText,
1513
Input,
1614
InputAdornment,
1715
IconButton,
16+
ListItemButton,
1817
} from '@mui/material';
1918

2019
import { Check as IconOk, Cancel as IconCancel, Close as IconClose } from '@mui/icons-material';
2120

22-
import { I18n } from '@iobroker/adapter-react-v5';
21+
import { type AdminConnection, I18n } from '@iobroker/adapter-react-v5';
2322

24-
const styles = {
23+
const styles: Record<string, React.CSSProperties> = {
2524
buttonIcon: {
2625
marginRight: 8,
2726
},
@@ -41,8 +40,28 @@ const styles = {
4140
},
4241
};
4342

44-
class DialogAdapterDebug extends React.Component {
45-
constructor(props) {
43+
interface DialogAdapterDebugProps {
44+
socket: AdminConnection;
45+
onDebug: (instance: string, adapterToDebug: string) => void;
46+
onClose: () => void;
47+
title?: string;
48+
}
49+
interface DialogAdapterDebugState {
50+
instances: {
51+
id: string;
52+
enabled: boolean;
53+
host: string;
54+
icon: string;
55+
}[];
56+
jsInstance: string;
57+
filter: string;
58+
showAskForStop: boolean;
59+
jsInstanceHost: string;
60+
adapterToDebug: string;
61+
}
62+
63+
class DialogAdapterDebug extends React.Component<DialogAdapterDebugProps, DialogAdapterDebugState> {
64+
constructor(props: DialogAdapterDebugProps) {
4665
super(props);
4766
this.state = {
4867
instances: [],
@@ -54,9 +73,14 @@ class DialogAdapterDebug extends React.Component {
5473
};
5574
}
5675

57-
componentDidMount() {
58-
this.props.socket.getAdapterInstances().then(instances => {
59-
instances = instances
76+
componentDidMount(): void {
77+
void this.props.socket.getAdapterInstances().then(oInstances => {
78+
const instances: {
79+
id: string;
80+
enabled: boolean;
81+
host: string;
82+
icon: string;
83+
}[] = oInstances
6084
.filter(i => i && !i.common?.onlyWWW)
6185
.map(item => {
6286
const name = item._id.replace(/^system\.adapter\./, '');
@@ -69,17 +93,19 @@ class DialogAdapterDebug extends React.Component {
6993
};
7094
});
7195
instances.sort((a, b) => (a.id > b.id ? 1 : a.id < b.id ? -1 : 0));
72-
let jsInstance = this.state.jsInstance || '';
73-
let jsInstanceObj = this.state.jsInstance && instances.find(item => item.id === this.state.jsInstance);
74-
let jsInstanceHost;
96+
let jsInstance: string = this.state.jsInstance || '';
97+
const jsInstanceObj = this.state.jsInstance
98+
? instances.find(item => item.id === this.state.jsInstance)
99+
: null;
100+
let jsInstanceHost: string;
75101

76102
// check if selected instance is in the list
77103
if (!this.state.jsInstance || !jsInstanceObj) {
78-
jsInstance = instances.find(item => item.id.startsWith('javascript.')); // take the first one
79-
jsInstanceHost = jsInstance ? jsInstance.host : '';
80-
jsInstance = jsInstance ? jsInstance.id : '';
104+
const oJsInstance = instances.find(item => item.id.startsWith('javascript.')); // take the first one
105+
jsInstanceHost = oJsInstance?.host || '';
106+
jsInstance = oJsInstance?.id || '';
81107
} else {
82-
jsInstanceHost = jsInstanceObj ? jsInstanceObj.host : '';
108+
jsInstanceHost = jsInstanceObj?.host || '';
83109
}
84110

85111
let adapterToDebug = this.state.adapterToDebug || '';
@@ -91,21 +117,23 @@ class DialogAdapterDebug extends React.Component {
91117
});
92118
}
93119

94-
handleOk = () => {
120+
handleOk = (): void => {
95121
// TODO
96-
if (this.state.instances.find(item => item.id === this.state.adapterToDebug).enabled) {
97-
return this.props.socket.getObject(`system.adapter.${this.state.adapterToDebug}`).then(obj => {
98-
obj.common.enabled = false;
99-
this.props.socket
100-
.setObject(obj._id, obj)
101-
.then(() => this.props.onDebug(this.state.jsInstance, this.state.adapterToDebug));
122+
if (this.state.instances.find(item => item.id === this.state.adapterToDebug)?.enabled) {
123+
void this.props.socket.getObject(`system.adapter.${this.state.adapterToDebug}`).then(obj => {
124+
if (obj) {
125+
obj.common.enabled = false;
126+
void this.props.socket
127+
.setObject(obj._id, obj)
128+
.then(() => this.props.onDebug(this.state.jsInstance, this.state.adapterToDebug));
129+
}
102130
});
103-
} else {
104-
this.props.onDebug(this.state.jsInstance, this.state.adapterToDebug);
131+
return;
105132
}
133+
this.props.onDebug(this.state.jsInstance, this.state.adapterToDebug);
106134
};
107135

108-
renderJavascriptList() {
136+
renderJavascriptList(): React.JSX.Element | null {
109137
const js = this.state.instances.filter(item => item.id.startsWith('javascript.'));
110138
if (js.length < 2) {
111139
return null;
@@ -115,8 +143,9 @@ class DialogAdapterDebug extends React.Component {
115143
<div style={styles.title}>{I18n.t('Host')}</div>
116144
<List component="nav">
117145
{js.map(item => (
118-
<ListItem
119-
button
146+
<ListItemButton
147+
component="div"
148+
key={item.id}
120149
selected={this.state.jsInstance === item.id}
121150
onClick={() => this.setState({ jsInstance: item.id, jsInstanceHost: item.host })}
122151
>
@@ -128,14 +157,14 @@ class DialogAdapterDebug extends React.Component {
128157
/>
129158
</ListItemIcon>
130159
<ListItemText primary={item.id} />
131-
</ListItem>
160+
</ListItemButton>
132161
))}
133162
</List>
134163
</Grid2>
135164
);
136165
}
137166

138-
renderInstances() {
167+
renderInstances(): React.JSX.Element {
139168
if (!this.state.jsInstance) {
140169
return <Grid2 />;
141170
}
@@ -151,8 +180,8 @@ class DialogAdapterDebug extends React.Component {
151180
<div style={styles.title}>{I18n.t('Instances')}</div>
152181
<List component="nav">
153182
{instances.map(item => (
154-
<ListItem
155-
button
183+
<ListItemButton
184+
key={item.id}
156185
selected={this.state.adapterToDebug === item.id}
157186
onDoubleClick={() => this.setState({ adapterToDebug: item.id }, () => this.handleOk())}
158187
onClick={() => this.setState({ adapterToDebug: item.id })}
@@ -165,14 +194,14 @@ class DialogAdapterDebug extends React.Component {
165194
/>
166195
</ListItemIcon>
167196
<ListItemText primary={item.id} />
168-
</ListItem>
197+
</ListItemButton>
169198
))}
170199
</List>
171200
</Grid2>
172201
);
173202
}
174203

175-
render() {
204+
render(): React.JSX.Element {
176205
return (
177206
<Dialog
178207
maxWidth="md"
@@ -245,10 +274,4 @@ class DialogAdapterDebug extends React.Component {
245274
}
246275
}
247276

248-
DialogAdapterDebug.propTypes = {
249-
socket: PropTypes.object.isRequired,
250-
onClose: PropTypes.func.isRequired,
251-
onDebug: PropTypes.func.isRequired,
252-
};
253-
254277
export default DialogAdapterDebug;

0 commit comments

Comments
 (0)