weechatRN

Weechat relay client for iOS using websockets https://github.com/mhoran/weechatRN
git clone http://git.hanabi.in/repos/weechatRN.git
Log | Files | Refs | README | LICENSE

Root.tsx (3157B)


      1 import * as React from 'react';
      2 import { StatusBar } from 'react-native';
      3 import { Provider } from 'react-redux';
      4 import { PersistGate } from 'redux-persist/integration/react';
      5 
      6 import WeechatConnection from '../lib/weechat/connection';
      7 import { store, persistor } from '../store';
      8 
      9 import App from './App';
     10 import ConnectionGate from './ConnectionGate';
     11 import { getPushNotificationStatusAsync } from '../lib/helpers/push-notifications';
     12 
     13 interface State {
     14   connecting: boolean;
     15 }
     16 
     17 export default class WeechatNative extends React.Component<null, State> {
     18   state: State = {
     19     connecting: false
     20   };
     21 
     22   connection: WeechatConnection;
     23 
     24   constructor(props: null) {
     25     super(props);
     26     this.connection = new WeechatConnection(store.dispatch);
     27   }
     28 
     29   setNotificationToken = async (): Promise<void> => {
     30     const token = await getPushNotificationStatusAsync();
     31     if (token) this.sendMessageToBuffer('core.weechat', '/weechatrn ' + token);
     32   };
     33 
     34   onConnectionSuccess = (connection: WeechatConnection): void => {
     35     this.setState({ connecting: false });
     36     connection.send('(hotlist) hdata hotlist:gui_hotlist(*)');
     37     connection.send(
     38       '(buffers) hdata buffer:gui_buffers(*) local_variables,notify,number,full_name,short_name,title,hidden,type'
     39     );
     40     // connection.send("(nicklist) nicklist");
     41     connection.send('sync');
     42     this.setNotificationToken();
     43   };
     44 
     45   onConnectionError = (reconnect: boolean): void => {
     46     this.setState({ connecting: reconnect });
     47   };
     48 
     49   disconnect = (): void => {
     50     this.connection.close();
     51   };
     52 
     53   onConnect = (hostname: string, password: string, ssl: boolean): void => {
     54     this.setState({ connecting: true });
     55     this.connection.connect(
     56       hostname,
     57       password,
     58       ssl,
     59       this.onConnectionSuccess,
     60       this.onConnectionError
     61     );
     62   };
     63 
     64   fetchBufferInfo = (bufferId: string, numLines = 50): void => {
     65     if (this.connection) {
     66       this.connection.send(
     67         `(lines) hdata buffer:0x${bufferId}/own_lines/last_line(-${numLines})/data`
     68       );
     69       this.connection.send(`(nicklist) nicklist 0x${bufferId}`);
     70     }
     71   };
     72 
     73   sendMessageToBuffer = (fullBufferName: string, message: string): void => {
     74     this.connection &&
     75       this.connection.send(`(input) input ${fullBufferName} ${message}`);
     76   };
     77 
     78   clearHotlistForBuffer = (fullBufferName: string): void => {
     79     this.sendMessageToBuffer(fullBufferName, '/buffer set hotlist -1');
     80     this.sendMessageToBuffer(
     81       fullBufferName,
     82       '/input set_unread_current_buffer'
     83     );
     84   };
     85 
     86   render(): JSX.Element {
     87     const { connecting } = this.state;
     88 
     89     return (
     90       <Provider store={store}>
     91         <PersistGate loading={null} persistor={persistor}>
     92           <ConnectionGate connecting={connecting} onConnect={this.onConnect}>
     93             <StatusBar barStyle="light-content" />
     94             <App
     95               disconnect={this.disconnect}
     96               clearHotlistForBuffer={this.clearHotlistForBuffer}
     97               sendMessageToBuffer={this.sendMessageToBuffer}
     98               fetchBufferInfo={this.fetchBufferInfo}
     99             />
    100           </ConnectionGate>
    101         </PersistGate>
    102       </Provider>
    103     );
    104   }
    105 }