Implement React example of WS integration with Flipper
Summary: Create an example of how one can use `js-flipper` in a browser to connect to Flipper over WS. Reviewed By: mweststrate Differential Revision: D31688114 fbshipit-source-id: 135f826daeddeda8dca5b3df6504cc2bdc04dd1b
This commit is contained in:
committed by
Facebook GitHub Bot
parent
25a6fc1ab1
commit
02115722b3
93
js/react-flipper-example/src/FlipperTicTacToe.tsx
Normal file
93
js/react-flipper-example/src/FlipperTicTacToe.tsx
Normal file
@@ -0,0 +1,93 @@
|
||||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*
|
||||
* @format
|
||||
*/
|
||||
|
||||
import {useState, useEffect, FC} from 'react';
|
||||
import type {FlipperPluginConnection, FlipperClient} from 'js-flipper';
|
||||
import './FlipperTicTacToe.css';
|
||||
|
||||
// We want to import and start flipper client only in development and test modes
|
||||
let flipperClientPromise: Promise<FlipperClient> | undefined;
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
flipperClientPromise = import('js-flipper').then(({flipperClient}) => {
|
||||
flipperClient.start('React Tic-Tac-Toe');
|
||||
return flipperClient;
|
||||
});
|
||||
}
|
||||
|
||||
interface GameState {
|
||||
cells: string[];
|
||||
turn: string;
|
||||
winner: string;
|
||||
}
|
||||
|
||||
const FlipperTicTacToe: FC = () => {
|
||||
const [status, setStatus] = useState('Waiting for Flipper Desktop Player...');
|
||||
const [gameState, setGameState] = useState<GameState>({
|
||||
cells: [],
|
||||
turn: ' ',
|
||||
winner: ' ',
|
||||
});
|
||||
const [connection, setConnection] = useState<FlipperPluginConnection>();
|
||||
|
||||
useEffect(() => {
|
||||
flipperClientPromise?.then(flipperClient => {
|
||||
flipperClient.addPlugin({
|
||||
getId() {
|
||||
return 'ReactNativeTicTacToe';
|
||||
},
|
||||
onConnect(connection) {
|
||||
setStatus('Desktop player present');
|
||||
setConnection(connection);
|
||||
|
||||
// listen to updates
|
||||
connection.receive('SetState', (gameState: GameState) => {
|
||||
if (gameState.winner !== ' ') {
|
||||
setStatus(
|
||||
`Winner is ${gameState.winner}! Waiting for a new game...`,
|
||||
);
|
||||
} else {
|
||||
setStatus(
|
||||
gameState.turn === 'X'
|
||||
? 'Your turn...'
|
||||
: 'Awaiting desktop players turn...',
|
||||
);
|
||||
}
|
||||
setGameState(gameState);
|
||||
});
|
||||
|
||||
// request initial state
|
||||
connection.send('GetState');
|
||||
},
|
||||
onDisconnect() {
|
||||
setConnection(undefined);
|
||||
setStatus('Desktop player gone...');
|
||||
},
|
||||
});
|
||||
});
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div>
|
||||
<p>{status}</p>
|
||||
<div className="grid">
|
||||
{gameState.cells.map((state, idx) => (
|
||||
<button
|
||||
key={idx}
|
||||
disabled={!connection || gameState.turn !== 'X' || state !== ' '}
|
||||
onClick={() => connection?.send('XMove', {move: idx})}
|
||||
className="cell">
|
||||
{state}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default FlipperTicTacToe;
|
||||
Reference in New Issue
Block a user