Skip to content

Commit

Permalink
Merge pull request #67 from ar-io/react-example
Browse files Browse the repository at this point in the history
chore: add react typescript example
  • Loading branch information
dtfiedler authored May 1, 2024
2 parents 3b30abb + 1537b99 commit c69aa1a
Show file tree
Hide file tree
Showing 32 changed files with 14,067 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
CHANGELOG.md
examples/react/static
examples/react/build
examples/react/node_modules
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ const arIO = ArIO.init();
const gateways = await arIO.getGateways();
```

Note: polyfills are only provided when using the named `@ar.io/sdk/web` export (which requires `moduleResolution: nodenext` in `tsconfig.json`). If you are using the default export within a Typescript project (e.g. `moduleResolution: node`), you will need to provide your own polyfills - specifically `crypto`, and `fs`. An example React/Typescript application using default exports can be found at [examples/webpack] and [examples/vite]. For other project configurations, refer to your bundler's documentation for more information on how to provide the necessary polyfills.

#### Browser

```html
Expand Down Expand Up @@ -1309,5 +1311,7 @@ For more information on how to contribute, please see [CONTRIBUTING.md].
[ar.io]: https://ar.io
[package.json]: ./package.json
[examples]: ./examples
[examples/webpack]: ./examples/webpack
[examples/vite]: ./examples/vite
[arns-service]: https://github.com/ar-io/arns-service
[CONTRIBUTING.md]: ./CONTRIBUTING.md
23 changes: 23 additions & 0 deletions examples/vite/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
50 changes: 50 additions & 0 deletions examples/vite/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Typescript React Example - Vite

This example shows how to use the `@ar.io/sdk` within a Typescript/React project using [Vite].

## Getting Started

1. Install the dependencies:

```bash
yarn
```

2. Start the development server:

```bash
yarn start
```

3. Open your browser and navigate to `http://localhost:3000`. You should see:

![screenshot](./public/screenshot.png)

## Polyfills

The `@ar.io/sdk` uses some modern browser features that may not be available in all browsers. To ensure compatibility, you may need to include polyfills when your `tsconfig.json` uses `moduleResolution: "node"`.

The [tsconfig.json](./tsconfig.json) includes the following compiler options:

```json
{
"compilerOptions": {
"moduleResolution": "node",
"lib": ["es2015", "dom"]
}
}
```

The [vite.config.js](./vite.config.js) file includes the following polyfills required for the `@ar.io/sdk`:

```javascript
{
build: {},
plugins: [
react(),
nodePolyfills(),
],
}
```

If you're project is using `moduleResolution: "nodenext"`, you can remove the polyfills from the webpack configuration and use named export for web - `@ar.io/sdk/web` - which includes the necessary polyfills.
18 changes: 18 additions & 0 deletions examples/vite/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<script type="module" src="/src/index.tsx"></script>
</body>
</html>
35 changes: 35 additions & 0 deletions examples/vite/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "react-typescript-vite",
"version": "0.1.0",
"private": true,
"dependencies": {
"@ar.io/sdk": "^1.0.5-alpha.1",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-markdown": "^9.0.1",
"remark-gfm": "^4.0.0",
"warp-contracts": "^1.4.41"
},
"devDependencies": {
"@babel/core": "^7.24.5",
"@babel/preset-env": "^7.24.5",
"@babel/preset-react": "^7.24.1",
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"@types/jest": "^27.5.2",
"@types/node": "^16.18.96",
"@types/react": "^18.3.1",
"@types/react-dom": "^18.3.0",
"@vitejs/plugin-react": "^4.2.1",
"babel-loader": "^9.1.3",
"typescript": "^4.9.5",
"vite": "^5.2.10",
"vite-plugin-html": "^3.2.2",
"vite-plugin-node-polyfills": "^0.21.0"
},
"scripts": {
"start": "vite --config vite.config.js",
"build": "vite build --config vite.config.js"
}
}
Binary file added examples/vite/public/favicon.ico
Binary file not shown.
Binary file added examples/vite/public/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions examples/vite/src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.App {
text-align: center;
background-color: #282c34;
}

.markdown {
text-align: left;
margin: auto;
padding: 50px;
color: white;
}
9 changes: 9 additions & 0 deletions examples/vite/src/App.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { render, screen } from '@testing-library/react';

import App from './App';

test('renders learn react link', () => {
render(<App />);
const linkElement = screen.getByText(/learn react/i);
expect(linkElement).toBeInTheDocument();
});
38 changes: 38 additions & 0 deletions examples/vite/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { WarpFactory } from 'warp-contracts';
import { useEffect, useState } from 'react';
import Markdown from 'react-markdown';
import remarkGfm from 'remark-gfm';

import './App.css';

const contractTxId = 'ilwT4ObFQ7cGPbW-8z-h7mvvWGt_yhWNlqxNjSUgiYY';
const warp = WarpFactory.forMainnet();
const antContract = warp.contract(contractTxId);

function App() {
const [contract, setContract] = useState<string>('Loading...');

// NOTE: there is a bug in warp-contracts causing this to fail on `AbortError` import missing
useEffect(() => {
antContract.syncState(`https://api.arns.app/v1/contract/${contractTxId}`, {
validity: true
}).then(async (syncContract) => {
const { cachedValue } = await syncContract.readState();
setContract(`\`\`\`json\n${JSON.stringify(cachedValue.state, null, 2)}`);
})
.catch((error) => {
console.error(error);
setContract('Error loading contract state');
});
}, []);

return (
<div className="App">
<Markdown className="markdown" remarkPlugins={[remarkGfm]}>
{contract}
</Markdown>
</div>
);
}

export default App;
14 changes: 14 additions & 0 deletions examples/vite/src/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
body {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
background-color: #282c34;
}

code {
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
monospace;
}
14 changes: 14 additions & 0 deletions examples/vite/src/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from 'react';
import ReactDOM from 'react-dom/client';

import App from './App';
import './index.css';

const root = ReactDOM.createRoot(
document.getElementById('root') as HTMLElement,
);
root.render(
<React.StrictMode>
<App />
</React.StrictMode>,
);
18 changes: 18 additions & 0 deletions examples/vite/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"compilerOptions": {
"target": "ESNext",
"useDefineForClassFields": true,
"lib": ["DOM", "DOM.Iterable", "ESNext"],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "ESNext",
"moduleResolution": "node",
"isolatedModules": true,
"jsx": "react-jsx"
},
"include": ["src"]
}
9 changes: 9 additions & 0 deletions examples/vite/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import react from '@vitejs/plugin-react';
import { defineConfig } from 'vite';
import { nodePolyfills } from 'vite-plugin-node-polyfills';

export default defineConfig({
build: {},
base: '/',
plugins: [react(), nodePolyfills()],
});
Loading

0 comments on commit c69aa1a

Please sign in to comment.