├── src ├── App.css ├── index.jsx ├── AuthenticatedApp.jsx ├── components │ ├── ListHeader.jsx │ ├── ListDetailFooter.jsx │ ├── Footer.jsx │ ├── ShareModal.jsx │ ├── TodoList.jsx │ └── ListDetail.jsx ├── UnauthenticatedApp.jsx ├── App.jsx ├── utils │ └── shared-resource.js ├── views │ ├── ProfileView.jsx │ ├── MainView.jsx │ └── DetailView.jsx ├── logo.svg └── index.css ├── vite.config.js ├── .gitignore ├── index.html ├── LICENSE.md ├── server.js ├── package.json ├── README.md └── pnpm-lock.yaml /src/App.css: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /vite.config.js: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'vite'; 2 | import reactPlugin from '@vitejs/plugin-react'; 3 | 4 | // https://vitejs.dev/config/ 5 | export default defineConfig({ 6 | // This changes the out put dir from dist to build 7 | // comment this out if that isn't relevant for your project 8 | build: { 9 | outDir: 'build', 10 | }, 11 | plugins: [reactPlugin()], 12 | server: { 13 | port: 3000, 14 | }, 15 | }); 16 | -------------------------------------------------------------------------------- /src/index.jsx: -------------------------------------------------------------------------------- 1 | import { StrictMode } from 'react'; 2 | import { createRoot } from 'react-dom/client'; 3 | import './index.css'; 4 | import App from './App'; 5 | import { BrowserRouter as Router } from 'react-router-dom'; 6 | 7 | const container = document.getElementById('root'); 8 | const root = createRoot(container); 9 | 10 | root.render( 11 | 12 | 13 | 14 | 15 | 16 | ); 17 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | 21 | npm-debug.log* 22 | yarn-debug.log* 23 | yarn-error.log* 24 | 25 | ossl 26 | radata 27 | .vercel 28 | -------------------------------------------------------------------------------- /src/AuthenticatedApp.jsx: -------------------------------------------------------------------------------- 1 | import { MainView } from './views/MainView'; 2 | import { DetailView } from './views/DetailView'; 3 | import { ProfileView } from './views/ProfileView'; 4 | import { Routes, Route } from 'react-router-dom'; 5 | 6 | function AuthenticatedApp({ route }) { 7 | const child = route?.routes?.map((r) => ({ 8 | ...r, 9 | 10 | // Commment out following two lines it'll fail to render child routes 11 | path: `${route.path === '/' ? '' : route.path}${r.path}`, 12 | exact: r.path === '/', 13 | })); 14 | return ( 15 | 16 | } /> 17 | } /> 18 | } /> 19 | 20 | ); 21 | } 22 | 23 | export default AuthenticatedApp; 24 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 11 | 16 | 17 | 18 | 19 | 20 | TODOs App 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright 2020 Carlos Vega 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /src/components/ListHeader.jsx: -------------------------------------------------------------------------------- 1 | import { Fragment } from 'react'; 2 | 3 | export const ListHeader = ({ 4 | addTodo, 5 | setNewTodo, 6 | updateName, 7 | readOnly, 8 | newTodo, 9 | name, 10 | }) => { 11 | return !readOnly ? ( 12 | 13 |

{ 18 | const name = e.target.innerText; 19 | updateName(name); 20 | e.target.innerText = ''; 21 | }} 22 | > 23 | {name || '[Add new name]'} 24 |

25 | { 29 | setNewTodo(e.target.value); 30 | }} 31 | onBlur={(e) => { 32 | addTodo(newTodo); 33 | e.target.value = ''; 34 | }} 35 | type="text" 36 | placeholder="What needs to be done?" 37 | /> 38 |
39 | ) : ( 40 |

{name}

41 | ); 42 | }; 43 | -------------------------------------------------------------------------------- /src/UnauthenticatedApp.jsx: -------------------------------------------------------------------------------- 1 | import { useAuth } from '@altrx/gundb-react-hooks'; 2 | 3 | export default function LoginView() { 4 | const { login } = useAuth(); 5 | async function getApp(type, value) { 6 | try { 7 | let keys; 8 | 9 | if (type !== 'new') { 10 | if (typeof value === 'string') { 11 | keys = JSON.parse(value); 12 | } else { 13 | keys = value; 14 | } 15 | } 16 | login(keys); 17 | } catch (e) {} 18 | } 19 | 20 | return ( 21 |
22 |

TODOs App

23 | 31 |

Already have one?

32 | { 35 | const { target } = e; 36 | getApp('existing', target.value); 37 | }} 38 | placeholder="Paste keys here" 39 | /> 40 |
41 | ); 42 | } 43 | -------------------------------------------------------------------------------- /src/components/ListDetailFooter.jsx: -------------------------------------------------------------------------------- 1 | export const ListDetailFooter = ({ 2 | activeTodoCount = 0, 3 | nowShowing = 'all', 4 | setNowShowing, 5 | }) => { 6 | return ( 7 | 35 | ); 36 | }; 37 | -------------------------------------------------------------------------------- /server.js: -------------------------------------------------------------------------------- 1 | const express = require('express'); 2 | const app = express(); 3 | 4 | (function () { 5 | var cluster = require('cluster'); 6 | if (cluster.isMaster) { 7 | return ( 8 | cluster.fork() && 9 | cluster.on('exit', function () { 10 | cluster.fork(); 11 | }) 12 | ); 13 | } 14 | 15 | var fs = require('fs'); 16 | var config = { port: 8765 }; 17 | var Gun = require('gun'); 18 | 19 | if (process.env.HTTPS_KEY) { 20 | config.key = fs.readFileSync(process.env.HTTPS_KEY); 21 | config.cert = fs.readFileSync(process.env.HTTPS_CERT); 22 | config.server = require('https').createServer(config, Gun.serve(__dirname)); 23 | } else { 24 | config.server = require('http').createServer(Gun.serve(__dirname)); 25 | } 26 | 27 | var gun = Gun({ 28 | web: config.server.listen(8765), 29 | config, 30 | }); 31 | console.log('Relay peer started on port ' + 8765 + ' with /gun'); 32 | 33 | module.exports = gun; 34 | const listener = app.listen(process.env.PORT || 5151, function () { 35 | console.log('Your app is listening on port ' + listener.address().port); 36 | }); 37 | })(); 38 | -------------------------------------------------------------------------------- /src/components/Footer.jsx: -------------------------------------------------------------------------------- 1 | export const Footer = ({ 2 | activeListCount = 0, 3 | nowShowing = 'active', 4 | setNowShowing, 5 | }) => { 6 | return ( 7 | 38 | ); 39 | }; 40 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "my-gun-react-app", 3 | "version": "0.2.0", 4 | "homepage": "https://gun-react-todoapp.vercel.app/", 5 | "repository": "https://github.com/alterx/gun-react-todoapp", 6 | "private": true, 7 | "dependencies": { 8 | "@altrx/gundb-react-hooks": "1.0.0-rc3", 9 | "@gun-vue/gun-es": "^0.4.1240", 10 | "@reach/alert-dialog": "^0.18.0", 11 | "@reach/dialog": "^0.18.0", 12 | "clipboard": "^2.0.11", 13 | "gun": "^0.2020.1241", 14 | "react": "^19.1.1", 15 | "react-dom": "^19.1.1", 16 | "react-refresh": "^0.17.0", 17 | "react-router-dom": "^7.8.1" 18 | }, 19 | "scripts": { 20 | "start": "vite", 21 | "build": "vite build", 22 | "serve": "vite preview", 23 | "server": "node server.js" 24 | }, 25 | "eslintConfig": { 26 | "extends": "react-app" 27 | }, 28 | "browserslist": { 29 | "production": [ 30 | ">0.2%", 31 | "not dead", 32 | "not op_mini all" 33 | ], 34 | "development": [ 35 | "last 1 chrome version", 36 | "last 1 firefox version", 37 | "last 1 safari version" 38 | ] 39 | }, 40 | "devDependencies": { 41 | "@vitejs/plugin-react": "^5.0.1", 42 | "express": "^5.1.0", 43 | "vite": "^7.1.3" 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Gun TODO list 2 | 3 | This is a TODO list example inspired by https://github.com/thrownness/decentralized-todo-app and built with React + hooks. 4 | This project uses the official TODO MVC CSS [https://github.com/tastejs/todomvc-app-css] 5 | 6 | LIVE DEMO: https://gun-react-todoapp.vercel.app/ 7 | 8 | ## License 9 | 10 | Licensed under [MIT](https://github.com/alterx/gun-react-todoapp/blob/master/LICENSE.md). 11 | 12 | ## Available Scripts 13 | 14 | In the project directory, you can run: 15 | 16 | ### `yarn server` 17 | 18 | Runs a local Gun peer ([localhost:5151](http://localhost:5151)). 19 | 20 | ### `yarn start` 21 | 22 | Runs the app in the development mode.
23 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser. 24 | 25 | The page will reload if you make edits.
26 | You will also see any lint errors in the console. 27 | 28 | ### `yarn build` 29 | 30 | Builds the app for production to the `build` folder.
31 | It correctly bundles React in production mode and optimizes the build for the best performance. 32 | 33 | The build is minified and the filenames include the hashes.
34 | Your app is ready to be deployed! 35 | 36 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. 37 | -------------------------------------------------------------------------------- /src/App.jsx: -------------------------------------------------------------------------------- 1 | import { lazy, Suspense } from 'react'; 2 | import { useAuth, AuthProvider } from '@altrx/gundb-react-hooks'; 3 | import { Gun, SEA } from '@gun-vue/gun-es'; 4 | 5 | const AuthenticatedApp = lazy(() => 6 | import(/* webpackPrefetch: true */ './AuthenticatedApp') 7 | ); 8 | 9 | const AuthedApp = () => { 10 | const { user, isLoggedIn, login } = useAuth(); 11 | 12 | return ( 13 |
14 | {!isLoggedIn && ( 15 |
16 |

TODOs App

17 | 26 |
27 | )} 28 | {isLoggedIn && user && } 29 |
30 | ); 31 | }; 32 | 33 | const App = () => { 34 | return ( 35 |
36 | loading...

}> 37 | 46 | 47 | 48 |
49 |
50 | ); 51 | }; 52 | 53 | export default App; 54 | -------------------------------------------------------------------------------- /src/utils/shared-resource.js: -------------------------------------------------------------------------------- 1 | export const getHash = async ( 2 | data, 3 | sea, 4 | max = 12, 5 | name = 'SHA-1', 6 | salt = null 7 | ) => { 8 | const message = await sea.work(data, salt, null, { 9 | name, 10 | encode: 'hex', 11 | }); 12 | return message.slice(0, max); 13 | }; 14 | 15 | export const createSharedResource = async (newGunInstance, sea) => { 16 | const keys = await sea.pair(); 17 | const sharedKeyString = JSON.stringify(keys); 18 | let keyId = await getHash(sharedKeyString, sea); 19 | const nodeID = `${keyId}`; 20 | return new Promise(async (resolve) => { 21 | const newGun = newGunInstance({ 22 | peers: ['http://localhost:8765/gun'], 23 | localStorage: false, 24 | radisk: true, 25 | file: nodeID, 26 | }); 27 | 28 | newGun.on('auth', () => { 29 | // TODO: workaround while https://github.com/amark/gun/issues/937 is fixed 30 | // should not need a function 31 | const node = () => namespace.get(nodeID); 32 | 33 | resolve({ 34 | node, 35 | shareKeys: { 36 | keyId, 37 | sharedKeyString, 38 | nodeID, 39 | }, 40 | keys, 41 | }); 42 | }); 43 | 44 | const namespace = newGun.user(); 45 | namespace.auth(keys); 46 | }); 47 | }; 48 | 49 | export const openSharedResource = async (nodeID, newGunInstance, keys, pub) => { 50 | return new Promise((resolve) => { 51 | const newGun = newGunInstance({ 52 | peers: ['http://localhost:8765/gun'], 53 | localStorage: false, 54 | radisk: true, 55 | file: nodeID, 56 | }); 57 | let namespace; 58 | 59 | if (keys) { 60 | newGun.on('auth', () => { 61 | // TODO: workaround while https://github.com/amark/gun/issues/937 is fixed 62 | // should not need a function 63 | const node = () => namespace.get(nodeID); 64 | resolve(node); 65 | }); 66 | namespace = newGun.user(); 67 | namespace.auth(keys); 68 | } else { 69 | namespace = newGun.get(pub); 70 | const node = () => namespace.get(nodeID); 71 | resolve(node); 72 | } 73 | }); 74 | }; 75 | -------------------------------------------------------------------------------- /src/components/ShareModal.jsx: -------------------------------------------------------------------------------- 1 | import { useState, useRef } from 'react'; 2 | import { 3 | AlertDialog, 4 | AlertDialogLabel, 5 | AlertDialogDescription, 6 | } from '@reach/alert-dialog'; 7 | import '@reach/dialog/styles.css'; 8 | 9 | export const ShareModal = ({ showDialog, setShowDialog, onDismiss }) => { 10 | const [passphrase, setPassphrase] = useState(''); 11 | const [readOnly, setReadOnly] = useState('readonly'); 12 | const cancelRef = useRef(); 13 | 14 | const onClose = (result) => { 15 | if (result === 'success') { 16 | onDismiss(passphrase, readOnly === 'readonly'); 17 | } 18 | setShowDialog(false); 19 | }; 20 | 21 | return ( 22 |
23 | {showDialog && ( 24 | onClose('success')} 26 | leastDestructiveRef={cancelRef} 27 | > 28 | 29 |

Sharing

30 |
31 | 32 | Choose wheter you want to share this list as read-only or with fully 33 | fledged read/write privileges:{' '} 34 | 43 |
44 |
45 | Then enter a passphrase (and memorize it!):{' '} 46 | { 48 | setPassphrase(event.target.value); 49 | }} 50 | value={passphrase} 51 | type="text" 52 | /> 53 |
54 |
55 | Anyone with this link will also need to know this passphrase to open 56 | it. 57 |
58 |
59 |
60 | {' '} 61 | 64 |
65 |
66 | )} 67 |
68 | ); 69 | }; 70 | -------------------------------------------------------------------------------- /src/views/ProfileView.jsx: -------------------------------------------------------------------------------- 1 | import { useState, useEffect } from 'react'; 2 | import { Link, useNavigate } from 'react-router-dom'; 3 | import ClipboardJS from 'clipboard'; 4 | import { useAuth } from '@altrx/gundb-react-hooks'; 5 | import { useGunState } from '@altrx/gundb-react-hooks'; 6 | 7 | export const ProfileView = () => { 8 | const { appKeys, sea, user, logout } = useAuth(); 9 | const [clipboard, setClipboard] = useState(null); 10 | let navigate = useNavigate(); 11 | const appName = 'todomvc'; 12 | const { fields: profile, put } = useGunState( 13 | user.get(appName).get('profile'), 14 | { appKeys, sea } 15 | ); 16 | const { name = '' } = profile; 17 | const [nameValue, setNameValue] = useState(name); 18 | 19 | useEffect(() => { 20 | if (nameValue && nameValue !== name) { 21 | put({ name: nameValue }); 22 | } 23 | }, [nameValue]); 24 | 25 | useEffect(() => { 26 | if (name && nameValue !== name) { 27 | setNameValue(name); 28 | } 29 | }, [name]); 30 | 31 | useEffect(() => { 32 | if (!clipboard) { 33 | setClipboard(new ClipboardJS('.btn')); 34 | } 35 | return () => { 36 | if (clipboard) { 37 | clipboard.destroy(); 38 | } 39 | }; 40 | }, [clipboard]); 41 | 42 | return ( 43 |
44 | 45 | 46 | 47 |

My Profile

48 | { 51 | setNameValue(e.target.value); 52 | }} 53 | /> 54 |

55 | These are your private keys, you can use them to log into other devices,{' '} 56 | DO NOT SHARE THEM. 57 |

58 |