Skip to content

Commit

Permalink
docs: ✏️ prepares hooks docs to release (#5)
Browse files Browse the repository at this point in the history
* docs: ✏️ prepares hooks docs to release

* docs: ✏️ adding useStorage and useSession docs
  • Loading branch information
fredcido authored Dec 8, 2023
1 parent 31ae1f3 commit e4ccd18
Show file tree
Hide file tree
Showing 27 changed files with 1,288 additions and 34 deletions.
2 changes: 2 additions & 0 deletions global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// https://vitejs.dev/guide/features.html#typescript-compiler-options
/// <reference types="vite/client" />
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"typings": "lib/indeqx.d.ts",
"scripts": {
"start": "jest --watchAll --silent --coverage",
"app": "yarn build && vite",
"test": "jest --maxWorkers 2",
"test:ci": "jest --coverage",
"commit": "cz",
Expand Down Expand Up @@ -57,6 +58,7 @@
"@testing-library/react-hooks": "^8.0.1",
"@types/jest": "^29.5.10",
"@typescript-eslint/eslint-plugin": "^6.4.0",
"@vitejs/plugin-react": "^4.2.1",
"commit-and-tag-version": "^12.0.0",
"commitizen": "^4.3.0",
"concurrently": "^8.2.2",
Expand All @@ -77,10 +79,12 @@
"prettier": "^3.1.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.20.1",
"rimraf": "^5.0.5",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.1",
"typescript": "*"
"typescript": "*",
"vite": "^5.0.7"
},
"config": {
"commitizen": {
Expand Down
14 changes: 14 additions & 0 deletions src/app-example/app.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://miro.com/app/static/sdk/v2/miro.js"></script>
<title>Miro - React hooks app example</title>
</head>
<body>
<div id="root"></div>

<script type="module" src="/app.tsx"></script>
</body>
</html>
24 changes: 24 additions & 0 deletions src/app-example/app.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"use client";

import * as React from "react";
import { createRoot } from "react-dom/client";
import { createBrowserRouter, RouterProvider } from "react-router-dom";

import { MiroProvider } from "../../esm";
import { routes } from "./routes";

const router = createBrowserRouter(routes);

const App = () => {
return (
<React.StrictMode>
<MiroProvider>
<RouterProvider router={router} />
</MiroProvider>
</React.StrictMode>
);
};

const container = document.getElementById("root")!;
const root = createRoot(container);
root.render(<App />);
12 changes: 12 additions & 0 deletions src/app-example/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://miro.com/app/static/sdk/v2/miro.js"></script>
<title>Miro - React hooks app example</title>
</head>
<body>
<script type="module" src="./index.ts"></script>
</body>
</html>
7 changes: 7 additions & 0 deletions src/app-example/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export async function init() {
miro.board.ui.on("icon:click", async () => {
await miro.board.ui.openPanel({ url: "app.html" });
});
}

init();
21 changes: 21 additions & 0 deletions src/app-example/pages/Index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"use client";

import * as React from "react";
import { Link } from "react-router-dom";
import { routes } from "../routes";

const readable = (path: string) => path.split("/").pop();

export const Index: React.FC = () => {
return (
<main>
<ul>
{routes.map((route) => (
<li key={route.path}>
<Link to={route.path}>{readable(route.path)}</Link>
</li>
))}
</ul>
</main>
);
};
25 changes: 25 additions & 0 deletions src/app-example/pages/UseInfo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"use client";

import * as React from "react";
import { useInfo } from "../../../esm";

export const UseInfo: React.FC = () => {
const { status, error, result } = useInfo();

if (status === "loading") {
return <p>Fetching board info...</p>;
}

if (error) {
return <p>Something went wrong</p>;
}

if (status === "success") {
return (
<div>
<p>Current board info</p>
<pre>{JSON.stringify(result, null, 2)}</pre>
</div>
);
}
};
10 changes: 10 additions & 0 deletions src/app-example/pages/UseMiro.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"use client";

import * as React from "react";
import { useMiro } from "../../../esm";

export const Miro: React.FC = () => {
const miro = useMiro();

return <pre>{JSON.stringify(miro, null, 2)}</pre>;
};
31 changes: 31 additions & 0 deletions src/app-example/pages/UseOnlineUsers.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"use client";

import * as React from "react";
import { useOnlineUsers } from "../../../esm";

export const UseOnlineUsers: React.FC = () => {
const { status, result, error } = useOnlineUsers();

if (status === "loading") {
return <p>Fetching online users...</p>;
}

if (error) {
return <p>Something went wrong</p>;
}

if (status === "success") {
return (
<div>
<p>Online users</p>
<ul>
{result.map((user) => (
<li key={user.id}>
#{user.id} - {user.name}
</li>
))}
</ul>
</div>
);
}
};
31 changes: 31 additions & 0 deletions src/app-example/pages/UseSelectedItems.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"use client";

import * as React from "react";
import { useSelectedItems } from "../../../esm";

export const UseSelectedItems: React.FC = () => {
const { status, result, error } = useSelectedItems();

if (status === "loading") {
return <p>Fetching selected items...</p>;
}

if (error) {
return <p>Something went wrong</p>;
}

if (status === "success") {
return (
<div>
<p>Selected items:</p>
<ul>
{result.map((item) => (
<li key={item.id}>
#{item.id} - {item.type}
</li>
))}
</ul>
</div>
);
}
};
55 changes: 55 additions & 0 deletions src/app-example/pages/UseSession.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"use client";

import * as React from "react";
import { OnlineUserInfo } from "@mirohq/websdk-types";
import { useSession } from "../../../esm";

export const UseSession: React.FC = () => {
const { status, session, start, usersInSession, usersNotInSession } = useSession();

const handleStartSession = async () => {
await start({
name: "Test session",
});
};

const handleInviteUser = async (user: OnlineUserInfo) => {
await session?.invite(user);
};

return (
<div>
<h4>Session {status}</h4>
{session ? (
<div>
<p>
#{session.id} - {session.name}
</p>
<ul>
<li>
<strong>Users in session</strong>
</li>
{usersInSession.map((user) => (
<li key={user.id}>
#{user.id} - {user.name}
</li>
))}
</ul>
<ul>
<li>
<strong>Users not in session</strong>
</li>
{usersNotInSession.map((user) => (
<li key={user.id}>
#{user.id} - {user.name}
<button onClick={() => handleInviteUser(user)}>Invite</button>
</li>
))}
</ul>
</div>
) : (
<button onClick={handleStartSession}>Start session</button>
)}
</div>
);
};
37 changes: 37 additions & 0 deletions src/app-example/pages/UseStorage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"use client";

import * as React from "react";
import { useStorage } from "../../../esm";

export const UseStorage: React.FC = () => {
const { status, result, error, set, remove } = useStorage("react-hooks", "example");

if (status === "loading") {
return <p>Fetching values...</p>;
}

if (error) {
return <p>Something went wrong</p>;
}

const handleSetValue = async () => {
await set("Just changed from the app");
};

const handleRemoveValue = async () => {
await remove();
};

if (status === "success") {
return (
<div>
<p>Current value:</p>
<pre>{JSON.stringify(result, null, 2)}</pre>
<button onClick={handleSetValue}>Set value</button>
<button disabled={!result} onClick={handleRemoveValue}>
Remove value
</button>
</div>
);
}
};
64 changes: 64 additions & 0 deletions src/app-example/pages/UseViewport.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
"use client";

import * as React from "react";
import { useSelectedItems, useViewport } from "../../../esm";

export const UseViewport: React.FC = () => {
const { status, result, error, set, zoomTo } = useViewport();
const selected = useSelectedItems();

if (status === "loading") {
return <p>Fetching viewport...</p>;
}

if (error) {
return <p>Something went wrong</p>;
}

const handleSetViewport = async () => {
await set({
viewport: {
x: 200,
y: 100,
width: 1280,
height: 720,
},
padding: {
top: 100,
left: 200,
bottom: 50,
right: 20,
},
animationDurationInMs: 1000,
});
};

const handleZoomTo = async () => {
if (selected.result.length < 1) {
return;
}

await zoomTo(selected.result);
};

if (status === "success") {
return (
<div>
<p>Current viewport:</p>
<ul>
<li>
X and Y: {result?.x} x {result?.y}
</li>
<li>
Size (WxH): {result?.width} x {result?.height}
</li>
<li>Zoom level {result?.zoomLevel}</li>
</ul>
<button onClick={handleSetViewport}>Set viewport</button>
<button disabled={selected.result.length < 1} onClick={handleZoomTo}>
Zoom to selected
</button>
</div>
);
}
};
20 changes: 20 additions & 0 deletions src/app-example/pages/UserCurrentUser.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"use client";

import * as React from "react";
import { useCurrentUser } from "../../../esm";

export const UseCurrentUser: React.FC = () => {
const { status, result, error } = useCurrentUser();

if (status === "loading") {
return <p>Fetching user...</p>;
}

if (error) {
return <p>Something went wrong</p>;
}

if (status === "success") {
return <p>The current user is "{result?.name}"</p>;
}
};
Loading

0 comments on commit e4ccd18

Please sign in to comment.