UNPKG

iobroker.telegram

Version:

The adapter allows to send and receive telegram messages from ioBroker and to be a broker.

2 lines (1 loc) 3.82 kB
import{j as s}from"./jsx-runtime-DscBPJdh.js";import{C as i,a as m}from"./ConfigCustomTelegramSet__loadShare___mf_0_mui_mf_1_icons_mf_2_material__loadShare__-DsvMV1W3.js";import{C as o}from"./ConfigCustomTelegramSet__loadShare___mf_0_iobroker_mf_1_adapter_mf_2_react_mf_2_v5__loadShare__-Ci6Xy-GH.js";import{C as d,i as c}from"./ConfigCustomTelegramSet__mf_v__runtimeInit__mf_v__-J6gnGK7T.js";const{loadShare:f}=c,{initPromise:p}=d,u=p.then(l=>f("@iobroker/json-config",{customShareInfo:{shareConfig:{singleton:!0,strictVersion:!1,requiredVersion:"*"}}})),C=await u.then(l=>l());var g=C;class j extends g.ConfigGeneric{constructor(e){super(e),this.state={...this.state,alive:!1,initialized:!1,users:[],confirm:null}}componentDidMount(){super.componentDidMount(),this.props.oContext.socket.getState(`system.adapter.telegram.${this.props.oContext.instance}.alive`).then(async e=>{e!=null&&e.val?this.setState({alive:!0},()=>this.readData()):this.setState({alive:!1}),await this.props.oContext.socket.subscribeState(`system.adapter.telegram.${this.props.oContext.instance}.alive`,this.onAliveChanged)})}readData(){this.props.oContext.socket.sendTo(`telegram.${this.props.oContext.instance}`,"adminuser",null).then(e=>{const a=[];for(const t in e){const n=[];e[t].userName&&n.push(e[t].userName),e[t].firstName&&n.push(e[t].firstName),a.push({id:t,names:n.join(" / "),sysMessages:e[t].sysMessages})}this.setState({users:a,initialized:!0})})}componentWillUnmount(){this.props.oContext.socket.unsubscribeState(`system.adapter.telegram.${this.props.oContext.instance}.alive`,this.onAliveChanged)}onAliveChanged=(e,a)=>{const t=a?a.val:!1;t!==this.state.alive&&this.setState({alive:t},()=>{t&&!this.state.initialized&&this.readData()})};onSysMessageChange(e){const a=this.state.users.findIndex(t=>t.id===e);if(a!==-1){const t=!this.state.users[a].sysMessages;this.props.oContext.socket.sendTo(`telegram.${this.props.oContext.instance}`,"systemMessages",{itemId:e,checked:t}).then(n=>{if(n===e){const r=JSON.parse(JSON.stringify(this.state.users)),h=r.findIndex(_=>_.id===e);h!==-1&&(r[h].sysMessages=t,this.setState({users:r}))}})}}onDelete(e){this.props.oContext.socket.sendTo(`telegram.${this.props.oContext.instance}`,"delUser",e).then(a=>{if(a===e){const t=JSON.parse(JSON.stringify(this.state.users)),n=t.findIndex(r=>r.id===e);n!==-1&&(t.splice(n,1),this.setState({users:t}))}})}renderConfirmDialog(){return this.state.confirm?s.jsx(o.Confirm,{onClose:e=>{const a=this.state.confirm;this.setState({confirm:null},()=>e&&this.onDelete(a))}}):null}renderItem(){return!this.state.alive&&!this.state.initialized?s.jsx("div",{children:o.I18n.t("custom_telegram_not_alive")}):this.state.initialized?s.jsxs("div",{style:{width:"100%"},children:[s.jsx("h4",{children:o.I18n.t("custom_telegram_title")}),s.jsx(i.TableContainer,{component:i.Paper,style:{width:"100%"},children:s.jsxs(i.Table,{style:{width:"100%"},size:"small",children:[s.jsx(i.TableHead,{children:s.jsxs(i.TableRow,{children:[s.jsx(i.TableCell,{children:o.I18n.t("custom_telegram_id")}),s.jsx(i.TableCell,{children:o.I18n.t("custom_telegram_name")}),s.jsx(i.TableCell,{children:o.I18n.t("custom_telegram_sys_messages")}),s.jsx(i.TableCell,{})]})}),s.jsx(i.TableBody,{children:this.state.users.map(e=>s.jsxs(i.TableRow,{sx:{"&:last-child td, &:last-child th":{border:0}},children:[s.jsx(i.TableCell,{component:"th",scope:"row",children:e.id}),s.jsx(i.TableCell,{children:e.names}),s.jsx(i.TableCell,{children:s.jsx(i.Checkbox,{disabled:!this.state.alive,checked:!!e.sysMessages,onClick:()=>this.onSysMessageChange(e.id)})}),s.jsx(i.TableCell,{children:s.jsx(i.IconButton,{disabled:!this.state.alive,onClick:()=>this.setState({confirm:e.id}),children:s.jsx(m.Delete,{})})})]},e.id))})]})}),this.renderConfirmDialog()]}):s.jsx(i.LinearProgress,{})}}export{j as T};