-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
72 lines (68 loc) · 2.57 KB
/
App.tsx
File metadata and controls
72 lines (68 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { useEffect, useState, useRef } from "react";
import { IFluidContainer } from "@fluidframework/fluid-static";
import { SharedMap } from "@fluidframework/map";
// Not intended for use outside of a Codebox Live sandbox
import { CodeboxLive } from "@codeboxlive/extensions-core";
// In production, import AzureClient from "@fluidframework/azure-client"
import { CodeboxLiveHost } from "@codeboxlive/extensions-fluid";
import { LiveShareClient } from "@microsoft/live-share";
import Header from "./Header";
export default function App(): JSX.Element {
const counterMapRef = useRef<SharedMap | undefined>();
const initRef = useRef<boolean>(false);
const [counterValue, setCounterValue] = useState<number>(0);
const [started, setStarted] = useState<boolean>(false);
useEffect(() => {
if (initRef.current) return;
initRef.current = true;
// Join container on app load
async function start(): Promise<void> {
// Initialize the CodeboxLiveClient so that this sandbox app can communicate
// with the Codebox Live application using window post messages. This is used
// to authenticate a Fluid container when testing this app in a sandbox.
await CodeboxLive.initialize();
// Define container schema
const schema = {
initialObjects: {
counterMap: SharedMap,
},
};
// Define container callback for when container is first created
const onFirstInitialize = (container: IFluidContainer) => {
// Setup any initial state here
};
const host = new CodeboxLiveHost();
const client = new LiveShareClient(host);
const results = await client.joinContainer(schema, onFirstInitialize);
counterMapRef.current = results?.container.initialObjects
.counterMap as SharedMap;
// Listen for changes to the value
counterMapRef.current!.on("valueChanged", () => {
setCounterValue(counterMapRef.current!.get("count") ?? 0);
});
setStarted(true);
// Set initial value
setCounterValue(counterMapRef.current!.get("count") ?? 0);
}
start().catch((error: any) => console.error(error));
});
return (
<div>
{started && (
<>
<Header />
<p>{"Click the button to iterate the counter"}</p>
<button
onClick={() => {
counterMapRef.current!.set("count", counterValue + 1);
}}
>
{"+1"}
</button>
<h2 style={{ color: "red" }}>{counterValue}</h2>
</>
)}
{!started && <div>{"Loading..."}</div>}
</div>
);
}