-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from ar-io/react-example
chore: add react typescript example
- Loading branch information
Showing
32 changed files
with
14,067 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()], | ||
}); |
Oops, something went wrong.