|
| 1 | +/* eslint-disable jsx-a11y/anchor-is-valid */ |
| 2 | +// @ts-nocheck |
| 3 | +import React, { useState, FunctionComponent } from 'react'; |
| 4 | +import { Switch, Route } from 'reactant-web'; |
| 5 | +import { |
| 6 | + ViewModule, |
| 7 | + injectable, |
| 8 | + useConnector, |
| 9 | + Router, |
| 10 | + PortDetector, |
| 11 | + ClientTransport as IClientTransport, |
| 12 | + ServerTransport as IServerTransport, |
| 13 | + Transport, |
| 14 | +} from 'reactant-share'; |
| 15 | +import { TodoListView } from './todoList.view'; |
| 16 | +import { CounterView } from './counter.view'; |
| 17 | +// @ts-ignore |
| 18 | +import style from './style.css'; |
| 19 | + |
| 20 | +const Link: FunctionComponent<{ active: boolean; onClick: () => any }> = ({ |
| 21 | + active, |
| 22 | + children, |
| 23 | + onClick, |
| 24 | +}) => { |
| 25 | + return ( |
| 26 | + <div onClick={onClick} style={{ color: active ? 'red' : 'black' }}> |
| 27 | + {children} |
| 28 | + </div> |
| 29 | + ); |
| 30 | +}; |
| 31 | + |
| 32 | +interface ClientTransport extends IClientTransport { |
| 33 | + test(n: number): Promise<string>; |
| 34 | +} |
| 35 | + |
| 36 | +@injectable({ |
| 37 | + name: 'appView', |
| 38 | +}) |
| 39 | +export class AppView extends ViewModule { |
| 40 | + type = ''; |
| 41 | + |
| 42 | + push?: (path: string) => void; |
| 43 | + |
| 44 | + setType?: React.Dispatch<React.SetStateAction<string>>; |
| 45 | + |
| 46 | + constructor( |
| 47 | + private todoListView: TodoListView, |
| 48 | + private counterView: CounterView, |
| 49 | + private portDetector: PortDetector, |
| 50 | + private router: Router |
| 51 | + ) { |
| 52 | + super(); |
| 53 | + |
| 54 | + this.portDetector.onServer( |
| 55 | + (transport: Transport<IServerTransport, ClientTransport>) => { |
| 56 | + transport.listen( |
| 57 | + 'test', |
| 58 | + async (n) => `response '${n}' from server port` |
| 59 | + ); |
| 60 | + } |
| 61 | + ); |
| 62 | + this.portDetector.onClient( |
| 63 | + (transport: Transport<ClientTransport, IServerTransport>) => { |
| 64 | + this.type = 'Client'; |
| 65 | + transport.emit('test', 42).then((response) => { |
| 66 | + console.log(response); |
| 67 | + }); |
| 68 | + } |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + load() { |
| 73 | + import(/* webpackChunkName: "lazyModule" */ './lazyModule').then( |
| 74 | + ({ fn }) => { |
| 75 | + fn(); |
| 76 | + } |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + component() { |
| 81 | + const [type, setType] = useState(this.type); |
| 82 | + const currentPath = useConnector(() => this.router?.currentPath); |
| 83 | + this.setType = setType; |
| 84 | + return ( |
| 85 | + <> |
| 86 | + <ul> |
| 87 | + <li> |
| 88 | + <Link |
| 89 | + active={currentPath === '/'} |
| 90 | + onClick={() => this.router.push('/')} |
| 91 | + > |
| 92 | + Home |
| 93 | + </Link> |
| 94 | + </li> |
| 95 | + <li> |
| 96 | + <Link |
| 97 | + active={currentPath === this.counterView.path} |
| 98 | + onClick={() => this.router.push(this.counterView.path)} |
| 99 | + > |
| 100 | + {this.counterView.name} |
| 101 | + </Link> |
| 102 | + </li> |
| 103 | + <li> |
| 104 | + <Link |
| 105 | + active={currentPath === this.todoListView.path} |
| 106 | + onClick={() => this.router.push(this.todoListView.path)} |
| 107 | + > |
| 108 | + {this.todoListView.name} |
| 109 | + </Link> |
| 110 | + </li> |
| 111 | + <li> |
| 112 | + <Link |
| 113 | + active={currentPath === '/iframe'} |
| 114 | + onClick={() => this.router.push('/iframe')} |
| 115 | + > |
| 116 | + iFrame mode |
| 117 | + </Link> |
| 118 | + </li> |
| 119 | + </ul> |
| 120 | + |
| 121 | + <Switch> |
| 122 | + <Route exact path="/"> |
| 123 | + <h2 |
| 124 | + style={{ color: type === 'Server' ? 'red' : 'green' }} |
| 125 | + className={style.use().locals.button} |
| 126 | + > |
| 127 | + {`This app is a ${type}`} |
| 128 | + </h2> |
| 129 | + </Route> |
| 130 | + <Route |
| 131 | + path={this.counterView.path} |
| 132 | + component={this.counterView.component} |
| 133 | + /> |
| 134 | + <Route |
| 135 | + path={this.todoListView.path} |
| 136 | + component={this.todoListView.component} |
| 137 | + /> |
| 138 | + <Route path="/iframe"> |
| 139 | + <iframe title="iFrame mode" src="./iframe.html" /> |
| 140 | + </Route> |
| 141 | + </Switch> |
| 142 | + </> |
| 143 | + ); |
| 144 | + } |
| 145 | +} |
0 commit comments