├── .gitignore ├── .prettierignore ├── .prettierrc.js ├── CODE_OF_CONDUCT.md ├── LICENSE ├── README.md ├── notes └── .gitkeep ├── package-lock.json ├── package.json ├── public ├── checkmark.svg ├── chevron-down.svg ├── chevron-up.svg ├── cross.svg ├── favicon.ico ├── index.html ├── logo.svg └── style.css ├── scripts ├── build.js └── seed.js ├── server ├── api.server.js └── package.json └── src ├── App.server.js ├── Cache.client.js ├── EditButton.client.js ├── LocationContext.client.js ├── Note.server.js ├── NoteEditor.client.js ├── NoteList.server.js ├── NoteListSkeleton.js ├── NotePreview.js ├── NoteSkeleton.js ├── Root.client.js ├── SearchField.client.js ├── SidebarNote.client.js ├── SidebarNote.js ├── Spinner.js ├── TextWithMarkdown.js ├── db.server.js └── index.client.js /.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 | /dist 14 | 15 | # notes 16 | notes/*.md 17 | 18 | # misc 19 | .DS_Store 20 | .eslintcache 21 | .env.local 22 | .env.development.local 23 | .env.test.local 24 | .env.production.local 25 | 26 | npm-debug.log* 27 | yarn-debug.log* 28 | yarn-error.log* 29 | 30 | # vscode 31 | .vscode -------------------------------------------------------------------------------- /.prettierignore: -------------------------------------------------------------------------------- 1 | # dependencies 2 | /node_modules 3 | /.pnp 4 | .pnp.js 5 | 6 | # testing 7 | /coverage 8 | 9 | # production 10 | /build 11 | /dist 12 | 13 | # misc 14 | .DS_Store 15 | .eslintcache 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 | *.html 26 | *.json 27 | *.md 28 | -------------------------------------------------------------------------------- /.prettierrc.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) Facebook, Inc. and its affiliates. 3 | * 4 | * This source code is licensed under the MIT license found in the 5 | * LICENSE file in the root directory of this source tree. 6 | * 7 | */ 8 | 9 | 'use strict'; 10 | 11 | module.exports = { 12 | arrowParens: 'always', 13 | bracketSpacing: false, 14 | singleQuote: true, 15 | jsxBracketSameLine: true, 16 | trailingComma: 'es5', 17 | printWidth: 80, 18 | }; 19 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to make participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, sex characteristics, gender identity and expression, 9 | level of experience, education, socio-economic status, nationality, personal 10 | appearance, race, religion, or sexual identity and orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies within all project spaces, and it also applies when 49 | an individual is representing the project or its community in public spaces. 50 | Examples of representing a project or community include using an official 51 | project e-mail address, posting via an official social media account, or acting 52 | as an appointed representative at an online or offline event. Representation of 53 | a project may be further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at . All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html 72 | 73 | [homepage]: https://www.contributor-covenant.org 74 | 75 | For answers to common questions about this code of conduct, see 76 | https://www.contributor-covenant.org/faq 77 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Facebook, Inc. and its affiliates. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # React Server Components Demo 2 | 3 | > This is a fork of the original demo without the Postgres dependency 4 | 5 | * [What is this?](#what-is-this) 6 | * [When will I be able to use this?](#when-will-i-be-able-to-use-this) 7 | * [Setup](#setup) 8 | * [DB Setup](#db-setup) 9 | + [Step 1. Create the Database](#step-1-create-the-database) 10 | + [Step 2. Connect to the Database](#step-2-connect-to-the-database) 11 | + [Step 3. Run the seed script](#step-3-run-the-seed-script) 12 | * [Notes about this app](#notes-about-this-app) 13 | + [Interesting things to try](#interesting-things-to-try) 14 | * [Built by (A-Z)](#built-by-a-z) 15 | * [Code of Conduct](#code-of-conduct) 16 | * [License](#license) 17 | 18 | ## What is this? 19 | 20 | This is a demo app built with Server Components, an experimental React feature. **We strongly recommend [watching our talk introducing Server Components](https://reactjs.org/server-components) before exploring this demo.** The talk includes a walkthrough of the demo code and highlights key points of how Server Components work and what features they provide. 21 | 22 | ## When will I be able to use this? 23 | 24 | Server Components are an experimental feature and **are not ready for adoption**. For now, we recommend experimenting w Server Components via this demo app. **Use this in your own projects at your own risk.** 25 | 26 | ## Setup 27 | 28 | ``` 29 | npm install 30 | npm start 31 | ``` 32 | 33 | (Or `npm run start:prod` for a production build.) 34 | 35 | Then open http://localhost:4000. 36 | 37 | The app won't work until you set up the database, as described below. 38 | 39 | ## DB Setup 40 | 41 | There is no database in this fork. 42 | 43 | I replaced it with an object in `./src/db.server.js`. 44 | 45 | ## Notes about this app 46 | 47 | The demo is a note taking app called **React Notes**. It consists of a few major parts: 48 | 49 | - It uses a Webpack plugin (not defined in this repo) that allows us to only include client components in build artifacts 50 | - An Express server that: 51 | - Serves API endpoints used in the app 52 | - Renders Server Components into a special format that we can read on the client 53 | - A React app containing Server and Client components used to build React Notes 54 | 55 | This demo is built on top of our Webpack plugin, but this is not how we envision using Server Components when they are stable. They are intended to be used in a framework that supports server rendering — for example, in Next.js. This is an early demo -- the real integration will be developed in the coming months. Learn more in the [announcement post](https://reactjs.org/server-components). 56 | 57 | ### Interesting things to try 58 | 59 | - Expand note(s) by hovering over the note in the sidebar, and clicking the expand/collapse toggle. Next create or delete a note. What happens to the expanded notes? 60 | - Change a note's title while editing, and notice how editing an existing item animates in the sidebar. What happens if you edit a note in the middle of the list? 61 | - Search for any title. With the search text still in the search input, create a new note with a title matching the search text. What happens? 62 | - Search while on Slow 3G, observe the inline loading indicator. 63 | - Switch between two notes back and forth. Observe we don't send new responses next time we switch them again. 64 | - Uncomment the `fetch('http://localhost:4000/sleep/....')` calls in `NoteServer.js` and/or `NoteList.server.js` to introduce an artificial delay and trigger Suspense. 65 | 66 | ## Built by (A-Z) 67 | 68 | - [Andrew Clark](https://twitter.com/acdlite) 69 | - [Dan Abramov](https://twitter.com/dan_abramov) 70 | - [Joe Savona](https://twitter.com/en_JS) 71 | - [Lauren Tan](https://twitter.com/sugarpirate_) 72 | - [Sebastian Markbåge](https://twitter.com/sebmarkbage) 73 | - [Tate Strickland](http://www.tatestrickland.com/) (Design) 74 | 75 | ## [Code of Conduct](https://engineering.fb.com/codeofconduct/) 76 | Facebook has adopted a Code of Conduct that we expect project participants to adhere to. Please read the [full text](https://engineering.fb.com/codeofconduct/) so that you can understand what actions will and will not be tolerated. 77 | 78 | ## License 79 | This demo is MIT licensed. 80 | -------------------------------------------------------------------------------- /notes/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pomber/server-components-demo/1f72a1a24ce07afd28462cf949028d9f3aa4be75/notes/.gitkeep -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-notes", 3 | "version": "0.1.0", 4 | "private": true, 5 | "engines": { 6 | "node": ">=14.9.0" 7 | }, 8 | "license": "MIT", 9 | "dependencies": { 10 | "@babel/core": "7.12.3", 11 | "@babel/register": "^7.12.1", 12 | "babel-loader": "8.1.0", 13 | "babel-preset-react-app": "10.0.0", 14 | "compression": "^1.7.4", 15 | "concurrently": "^5.3.0", 16 | "date-fns": "^2.16.1", 17 | "excerpts": "^0.0.3", 18 | "express": "^4.17.1", 19 | "html-webpack-plugin": "4.5.0", 20 | "marked": "^1.2.5", 21 | "nodemon": "^2.0.6", 22 | "react": "0.0.0-experimental-3310209d0", 23 | "react-dom": "0.0.0-experimental-3310209d0", 24 | "react-error-boundary": "^3.1.0", 25 | "react-fetch": "0.0.0-experimental-3310209d0", 26 | "react-fs": "0.0.0-experimental-3310209d0", 27 | "react-pg": "0.0.0-experimental-3310209d0", 28 | "react-server-dom-webpack": "0.0.0-experimental-3310209d0", 29 | "resolve": "1.12.0", 30 | "rimraf": "^3.0.2", 31 | "sanitize-html": "^2.2.0", 32 | "webpack": "4.44.2", 33 | "webpack-cli": "^4.2.0" 34 | }, 35 | "devDependencies": { 36 | "prettier": "1.19.1" 37 | }, 38 | "scripts": { 39 | "start": "concurrently \"npm run server:dev\" \"npm run bundler:dev\"", 40 | "start:prod": "concurrently \"npm run server:prod\" \"npm run bundler:prod\"", 41 | "server:dev": "NODE_ENV=development nodemon -- --conditions=react-server server", 42 | "server:prod": "NODE_ENV=production nodemon -- --conditions=react-server server", 43 | "bundler:dev": "NODE_ENV=development nodemon -- scripts/build.js", 44 | "bundler:prod": "NODE_ENV=production nodemon -- scripts/build.js", 45 | "prettier": "prettier --write **/*.js", 46 | "seed": "node ./scripts/seed.js" 47 | }, 48 | "babel": { 49 | "presets": [ 50 | [ 51 | "react-app", 52 | { 53 | "runtime": "automatic" 54 | } 55 | ] 56 | ] 57 | }, 58 | "nodemonConfig": { 59 | "ignore": [ 60 | "build/*" 61 | ] 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /public/checkmark.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /public/chevron-down.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /public/chevron-up.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /public/cross.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pomber/server-components-demo/1f72a1a24ce07afd28462cf949028d9f3aa4be75/public/favicon.ico -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | React Notes 9 | 10 | 11 |
12 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /public/logo.svg: -------------------------------------------------------------------------------- 1 | 2 | React Logo 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /public/style.css: -------------------------------------------------------------------------------- 1 | /* -------------------------------- CSSRESET --------------------------------*/ 2 | /* CSS Reset adapted from https://dev.to/hankchizljaw/a-modern-css-reset-6p3 */ 3 | /* Box sizing rules */ 4 | *, 5 | *::before, 6 | *::after { 7 | box-sizing: border-box; 8 | } 9 | 10 | /* Remove default padding */ 11 | ul[class], 12 | ol[class] { 13 | padding: 0; 14 | } 15 | 16 | /* Remove default margin */ 17 | body, 18 | h1, 19 | h2, 20 | h3, 21 | h4, 22 | p, 23 | ul[class], 24 | ol[class], 25 | li, 26 | figure, 27 | figcaption, 28 | blockquote, 29 | dl, 30 | dd { 31 | margin: 0; 32 | } 33 | 34 | /* Set core body defaults */ 35 | body { 36 | min-height: 100vh; 37 | scroll-behavior: smooth; 38 | text-rendering: optimizeSpeed; 39 | line-height: 1.5; 40 | } 41 | 42 | /* Remove list styles on ul, ol elements with a class attribute */ 43 | ul[class], 44 | ol[class] { 45 | list-style: none; 46 | } 47 | 48 | /* A elements that don't have a class get default styles */ 49 | a:not([class]) { 50 | text-decoration-skip-ink: auto; 51 | } 52 | 53 | /* Make images easier to work with */ 54 | img { 55 | max-width: 100%; 56 | display: block; 57 | } 58 | 59 | /* Natural flow and rhythm in articles by default */ 60 | article > * + * { 61 | margin-block-start: 1em; 62 | } 63 | 64 | /* Inherit fonts for inputs and buttons */ 65 | input, 66 | button, 67 | textarea, 68 | select { 69 | font: inherit; 70 | } 71 | 72 | /* Remove all animations and transitions for people that prefer not to see them */ 73 | @media (prefers-reduced-motion: reduce) { 74 | * { 75 | animation-duration: 0.01ms !important; 76 | animation-iteration-count: 1 !important; 77 | transition-duration: 0.01ms !important; 78 | scroll-behavior: auto !important; 79 | } 80 | } 81 | /* -------------------------------- /CSSRESET --------------------------------*/ 82 | 83 | :root { 84 | /* Colors */ 85 | --main-border-color: #ddd; 86 | --primary-border: #037dba; 87 | --gray-20: #404346; 88 | --gray-60: #8a8d91; 89 | --gray-70: #bcc0c4; 90 | --gray-80: #c9ccd1; 91 | --gray-90: #e4e6eb; 92 | --gray-95: #f0f2f5; 93 | --gray-100: #f5f7fa; 94 | --primary-blue: #037dba; 95 | --secondary-blue: #0396df; 96 | --tertiary-blue: #c6efff; 97 | --flash-blue: #4cf7ff; 98 | --outline-blue: rgba(4, 164, 244, 0.6); 99 | --navy-blue: #035e8c; 100 | --red-25: #bd0d2a; 101 | --secondary-text: #65676b; 102 | --white: #fff; 103 | --yellow: #fffae1; 104 | 105 | --outline-box-shadow: 0 0 0 2px var(--outline-blue); 106 | --outline-box-shadow-contrast: 0 0 0 2px var(--navy-blue); 107 | 108 | /* Fonts */ 109 | --sans-serif: -apple-system, system-ui, BlinkMacSystemFont, 'Segoe UI', Roboto, 110 | Ubuntu, Helvetica, sans-serif; 111 | --monospace: Menlo, Consolas, Monaco, Liberation Mono, Lucida Console, 112 | monospace; 113 | } 114 | 115 | html { 116 | font-size: 100%; 117 | } 118 | 119 | body { 120 | font-family: var(--sans-serif); 121 | background: var(--gray-100); 122 | font-weight: 400; 123 | line-height: 1.75; 124 | } 125 | 126 | h1, 127 | h2, 128 | h3, 129 | h4, 130 | h5 { 131 | margin: 0; 132 | font-weight: 700; 133 | line-height: 1.3; 134 | } 135 | 136 | h1 { 137 | font-size: 3.052rem; 138 | } 139 | h2 { 140 | font-size: 2.441rem; 141 | } 142 | h3 { 143 | font-size: 1.953rem; 144 | } 145 | h4 { 146 | font-size: 1.563rem; 147 | } 148 | h5 { 149 | font-size: 1.25rem; 150 | } 151 | small, 152 | .text_small { 153 | font-size: 0.8rem; 154 | } 155 | pre, 156 | code { 157 | font-family: var(--monospace); 158 | border-radius: 6px; 159 | } 160 | pre { 161 | background: var(--gray-95); 162 | padding: 12px; 163 | line-height: 1.5; 164 | } 165 | code { 166 | background: var(--yellow); 167 | padding: 0 3px; 168 | font-size: 0.94rem; 169 | word-break: break-word; 170 | } 171 | pre code { 172 | background: none; 173 | } 174 | a { 175 | color: var(--primary-blue); 176 | } 177 | 178 | .text-with-markdown h1, 179 | .text-with-markdown h2, 180 | .text-with-markdown h3, 181 | .text-with-markdown h4, 182 | .text-with-markdown h5 { 183 | margin-block: 2rem 0.7rem; 184 | margin-inline: 0; 185 | } 186 | 187 | .text-with-markdown blockquote { 188 | font-style: italic; 189 | color: var(--gray-20); 190 | border-left: 3px solid var(--gray-80); 191 | padding-left: 10px; 192 | } 193 | 194 | hr { 195 | border: 0; 196 | height: 0; 197 | border-top: 1px solid rgba(0, 0, 0, 0.1); 198 | border-bottom: 1px solid rgba(255, 255, 255, 0.3); 199 | } 200 | 201 | /* ---------------------------------------------------------------------------*/ 202 | .main { 203 | display: flex; 204 | height: 100vh; 205 | width: 100%; 206 | overflow: hidden; 207 | } 208 | 209 | .col { 210 | height: 100%; 211 | } 212 | .col:last-child { 213 | flex-grow: 1; 214 | } 215 | 216 | .logo { 217 | height: 20px; 218 | width: 22px; 219 | margin-inline-end: 10px; 220 | } 221 | 222 | .edit-button { 223 | border-radius: 100px; 224 | letter-spacing: 0.12em; 225 | text-transform: uppercase; 226 | padding-block: 6px 8px; 227 | padding-inline: 20px; 228 | cursor: pointer; 229 | font-weight: 700; 230 | outline-style: none; 231 | } 232 | .edit-button--solid { 233 | background: var(--primary-blue); 234 | color: var(--white); 235 | border: none; 236 | margin-inline-start: 6px; 237 | transition: all 0.2s ease-in-out; 238 | } 239 | .edit-button--solid:hover { 240 | background: var(--secondary-blue); 241 | } 242 | .edit-button--solid:focus { 243 | box-shadow: var(--outline-box-shadow-contrast); 244 | } 245 | .edit-button--outline { 246 | background: var(--white); 247 | color: var(--primary-blue); 248 | border: 1px solid var(--primary-blue); 249 | margin-inline-start: 12px; 250 | transition: all 0.1s ease-in-out; 251 | } 252 | .edit-button--outline:disabled { 253 | opacity: 0.5; 254 | } 255 | .edit-button--outline:hover:not([disabled]) { 256 | background: var(--primary-blue); 257 | color: var(--white); 258 | } 259 | .edit-button--outline:focus { 260 | box-shadow: var(--outline-box-shadow); 261 | } 262 | 263 | ul.notes-list { 264 | padding: 16px 0; 265 | } 266 | .notes-list > li { 267 | padding: 0 16px; 268 | } 269 | .notes-empty { 270 | padding: 16px; 271 | } 272 | 273 | .sidebar { 274 | background: var(--white); 275 | box-shadow: 0px 8px 24px rgba(0, 0, 0, 0.1), 0px 2px 2px rgba(0, 0, 0, 0.1); 276 | overflow-y: scroll; 277 | z-index: 1000; 278 | flex-shrink: 0; 279 | max-width: 350px; 280 | min-width: 250px; 281 | width: 30%; 282 | } 283 | .sidebar-header { 284 | letter-spacing: 0.15em; 285 | text-transform: uppercase; 286 | padding-block: 36px 16px; 287 | padding-inline: 16px; 288 | display: flex; 289 | align-items: center; 290 | } 291 | .sidebar-menu { 292 | padding-inline: 16px; 293 | padding-block: 0 16px; 294 | display: flex; 295 | justify-content: space-between; 296 | } 297 | .sidebar-menu > .search { 298 | position: relative; 299 | flex-grow: 1; 300 | } 301 | .sidebar-note-list-item { 302 | position: relative; 303 | margin-bottom: 12px; 304 | padding: 16px; 305 | width: 100%; 306 | display: flex; 307 | justify-content: space-between; 308 | align-items: flex-start; 309 | flex-wrap: wrap; 310 | max-height: 100px; 311 | transition: max-height 250ms ease-out; 312 | transform: scale(1); 313 | } 314 | .sidebar-note-list-item.note-expanded { 315 | max-height: 300px; 316 | transition: max-height 0.5s ease; 317 | } 318 | .sidebar-note-list-item.flash { 319 | animation-name: flash; 320 | animation-duration: 0.6s; 321 | } 322 | 323 | .sidebar-note-open { 324 | position: absolute; 325 | top: 0; 326 | left: 0; 327 | right: 0; 328 | bottom: 0; 329 | width: 100%; 330 | z-index: 0; 331 | border: none; 332 | border-radius: 6px; 333 | text-align: start; 334 | background: var(--gray-95); 335 | cursor: pointer; 336 | outline-style: none; 337 | color: transparent; 338 | font-size: 0px; 339 | } 340 | .sidebar-note-open:focus { 341 | box-shadow: var(--outline-box-shadow); 342 | } 343 | .sidebar-note-open:hover { 344 | background: var(--gray-90); 345 | } 346 | .sidebar-note-header { 347 | z-index: 1; 348 | max-width: 85%; 349 | pointer-events: none; 350 | } 351 | .sidebar-note-header > strong { 352 | display: block; 353 | font-size: 1.25rem; 354 | line-height: 1.2; 355 | white-space: nowrap; 356 | overflow: hidden; 357 | text-overflow: ellipsis; 358 | } 359 | .sidebar-note-toggle-expand { 360 | z-index: 2; 361 | border-radius: 50%; 362 | height: 24px; 363 | border: 1px solid var(--gray-60); 364 | cursor: pointer; 365 | flex-shrink: 0; 366 | visibility: hidden; 367 | opacity: 0; 368 | cursor: default; 369 | transition: visibility 0s linear 20ms, opacity 300ms; 370 | outline-style: none; 371 | } 372 | .sidebar-note-toggle-expand:focus { 373 | box-shadow: var(--outline-box-shadow); 374 | } 375 | .sidebar-note-open:hover + .sidebar-note-toggle-expand, 376 | .sidebar-note-open:focus + .sidebar-note-toggle-expand, 377 | .sidebar-note-toggle-expand:hover, 378 | .sidebar-note-toggle-expand:focus { 379 | visibility: visible; 380 | opacity: 1; 381 | transition: visibility 0s linear 0s, opacity 300ms; 382 | } 383 | .sidebar-note-toggle-expand img { 384 | width: 10px; 385 | height: 10px; 386 | } 387 | 388 | .sidebar-note-excerpt { 389 | pointer-events: none; 390 | z-index: 2; 391 | flex: 1 1 250px; 392 | color: var(--secondary-text); 393 | position: relative; 394 | animation: slideIn 100ms; 395 | } 396 | 397 | .search input { 398 | padding: 0 16px; 399 | border-radius: 100px; 400 | border: 1px solid var(--gray-90); 401 | width: 100%; 402 | height: 100%; 403 | outline-style: none; 404 | } 405 | .search input:focus { 406 | box-shadow: var(--outline-box-shadow); 407 | } 408 | .search .spinner { 409 | position: absolute; 410 | right: 10px; 411 | top: 10px; 412 | } 413 | 414 | .note-viewer { 415 | display: flex; 416 | align-items: center; 417 | justify-content: center; 418 | } 419 | .note { 420 | background: var(--white); 421 | box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.1), 0px 0px 1px rgba(0, 0, 0, 0.1); 422 | border-radius: 8px; 423 | height: 95%; 424 | width: 95%; 425 | min-width: 400px; 426 | padding: 8%; 427 | overflow-y: auto; 428 | } 429 | .note--empty-state { 430 | margin-inline: 20px 20px; 431 | } 432 | .note-text--empty-state { 433 | font-size: 1.5rem; 434 | } 435 | .note-header { 436 | display: flex; 437 | justify-content: space-between; 438 | align-items: center; 439 | flex-wrap: wrap-reverse; 440 | margin-inline-start: -12px; 441 | } 442 | .note-menu { 443 | display: flex; 444 | justify-content: space-between; 445 | align-items: center; 446 | flex-grow: 1; 447 | } 448 | .note-title { 449 | line-height: 1.3; 450 | flex-grow: 1; 451 | overflow-wrap: break-word; 452 | margin-inline-start: 12px; 453 | } 454 | .note-updated-at { 455 | color: var(--secondary-text); 456 | white-space: nowrap; 457 | margin-inline-start: 12px; 458 | } 459 | .note-preview { 460 | margin-block-start: 50px; 461 | } 462 | 463 | .note-editor { 464 | background: var(--white); 465 | display: flex; 466 | height: 100%; 467 | width: 100%; 468 | padding: 58px; 469 | overflow-y: auto; 470 | } 471 | .note-editor .label { 472 | margin-bottom: 20px; 473 | } 474 | .note-editor-form { 475 | display: flex; 476 | flex-direction: column; 477 | width: 400px; 478 | flex-shrink: 0; 479 | position: sticky; 480 | top: 0; 481 | } 482 | .note-editor-form input, 483 | .note-editor-form textarea { 484 | background: none; 485 | border: 1px solid var(--gray-70); 486 | border-radius: 2px; 487 | font-family: var(--monospace); 488 | font-size: 0.8rem; 489 | padding: 12px; 490 | outline-style: none; 491 | } 492 | .note-editor-form input:focus, 493 | .note-editor-form textarea:focus { 494 | box-shadow: var(--outline-box-shadow); 495 | } 496 | .note-editor-form input { 497 | height: 44px; 498 | margin-bottom: 16px; 499 | } 500 | .note-editor-form textarea { 501 | height: 100%; 502 | max-width: 400px; 503 | } 504 | .note-editor-menu { 505 | display: flex; 506 | justify-content: flex-end; 507 | align-items: center; 508 | margin-bottom: 12px; 509 | } 510 | .note-editor-preview { 511 | margin-inline-start: 40px; 512 | width: 100%; 513 | } 514 | .note-editor-done, 515 | .note-editor-delete { 516 | display: flex; 517 | justify-content: space-between; 518 | align-items: center; 519 | border-radius: 100px; 520 | letter-spacing: 0.12em; 521 | text-transform: uppercase; 522 | padding-block: 6px 8px; 523 | padding-inline: 20px; 524 | cursor: pointer; 525 | font-weight: 700; 526 | margin-inline-start: 12px; 527 | outline-style: none; 528 | transition: all 0.2s ease-in-out; 529 | } 530 | .note-editor-done:disabled, 531 | .note-editor-delete:disabled { 532 | opacity: 0.5; 533 | } 534 | .note-editor-done { 535 | border: none; 536 | background: var(--primary-blue); 537 | color: var(--white); 538 | } 539 | .note-editor-done:focus { 540 | box-shadow: var(--outline-box-shadow-contrast); 541 | } 542 | .note-editor-done:hover:not([disabled]) { 543 | background: var(--secondary-blue); 544 | } 545 | .note-editor-delete { 546 | border: 1px solid var(--red-25); 547 | background: var(--white); 548 | color: var(--red-25); 549 | } 550 | .note-editor-delete:focus { 551 | box-shadow: var(--outline-box-shadow); 552 | } 553 | .note-editor-delete:hover:not([disabled]) { 554 | background: var(--red-25); 555 | color: var(--white); 556 | } 557 | /* Hack to color our svg */ 558 | .note-editor-delete:hover:not([disabled]) img { 559 | filter: grayscale(1) invert(1) brightness(2); 560 | } 561 | .note-editor-done > img { 562 | width: 14px; 563 | } 564 | .note-editor-delete > img { 565 | width: 10px; 566 | } 567 | .note-editor-done > img, 568 | .note-editor-delete > img { 569 | margin-inline-end: 12px; 570 | } 571 | .note-editor-done[disabled], 572 | .note-editor-delete[disabled] { 573 | opacity: 0.5; 574 | } 575 | 576 | .label { 577 | display: inline-block; 578 | border-radius: 100px; 579 | letter-spacing: 0.05em; 580 | text-transform: uppercase; 581 | font-weight: 700; 582 | padding: 4px 14px; 583 | } 584 | .label--preview { 585 | background: rgba(38, 183, 255, 0.15); 586 | color: var(--primary-blue); 587 | } 588 | 589 | .text-with-markdown p { 590 | margin-bottom: 16px; 591 | } 592 | .text-with-markdown img { 593 | width: 100%; 594 | } 595 | 596 | /* https://codepen.io/mandelid/pen/vwKoe */ 597 | .spinner { 598 | display: inline-block; 599 | transition: opacity linear 0.1s; 600 | width: 20px; 601 | height: 20px; 602 | border: 3px solid rgba(80, 80, 80, 0.5); 603 | border-radius: 50%; 604 | border-top-color: #fff; 605 | animation: spin 1s ease-in-out infinite; 606 | opacity: 0; 607 | } 608 | .spinner--active { 609 | opacity: 1; 610 | } 611 | 612 | .skeleton::after { 613 | content: 'Loading...'; 614 | } 615 | .skeleton { 616 | height: 100%; 617 | background-color: #eee; 618 | background-image: linear-gradient(90deg, #eee, #f5f5f5, #eee); 619 | background-size: 200px 100%; 620 | background-repeat: no-repeat; 621 | border-radius: 4px; 622 | display: block; 623 | line-height: 1; 624 | width: 100%; 625 | animation: shimmer 1.2s ease-in-out infinite; 626 | color: transparent; 627 | } 628 | .skeleton:first-of-type { 629 | margin: 0; 630 | } 631 | .skeleton--button { 632 | border-radius: 100px; 633 | padding-block: 6px 8px; 634 | padding-inline: 20px; 635 | width: auto; 636 | } 637 | .v-stack + .v-stack { 638 | margin-block-start: 0.8em; 639 | } 640 | 641 | .offscreen { 642 | border: 0; 643 | clip: rect(0, 0, 0, 0); 644 | height: 1px; 645 | margin: -1px; 646 | overflow: hidden; 647 | padding: 0; 648 | width: 1px; 649 | position: absolute; 650 | } 651 | 652 | /* ---------------------------------------------------------------------------*/ 653 | @keyframes spin { 654 | to { 655 | transform: rotate(360deg); 656 | } 657 | } 658 | @keyframes spin { 659 | to { 660 | transform: rotate(360deg); 661 | } 662 | } 663 | 664 | @keyframes shimmer { 665 | 0% { 666 | background-position: -200px 0; 667 | } 668 | 100% { 669 | background-position: calc(200px + 100%) 0; 670 | } 671 | } 672 | 673 | @keyframes slideIn { 674 | 0% { 675 | top: -10px; 676 | opacity: 0; 677 | } 678 | 100% { 679 | top: 0; 680 | opacity: 1; 681 | } 682 | } 683 | 684 | @keyframes flash { 685 | 0% { 686 | transform: scale(1); 687 | opacity: 1; 688 | } 689 | 50% { 690 | transform: scale(1.05); 691 | opacity: 0.9; 692 | } 693 | 100% { 694 | transform: scale(1); 695 | opacity: 1; 696 | } 697 | } -------------------------------------------------------------------------------- /scripts/build.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) Facebook, Inc. and its affiliates. 3 | * 4 | * This source code is licensed under the MIT license found in the 5 | * LICENSE file in the root directory of this source tree. 6 | * 7 | */ 8 | 9 | 'use strict'; 10 | 11 | const path = require('path'); 12 | const rimraf = require('rimraf'); 13 | const webpack = require('webpack'); 14 | const HtmlWebpackPlugin = require('html-webpack-plugin'); 15 | const ReactServerWebpackPlugin = require('react-server-dom-webpack/plugin'); 16 | 17 | const isProduction = process.env.NODE_ENV === 'production'; 18 | rimraf.sync(path.resolve(__dirname, '../build')); 19 | webpack( 20 | { 21 | mode: isProduction ? 'production' : 'development', 22 | devtool: isProduction ? 'source-map' : 'cheap-module-source-map', 23 | entry: [path.resolve(__dirname, '../src/index.client.js')], 24 | output: { 25 | path: path.resolve(__dirname, '../build'), 26 | filename: 'main.js', 27 | }, 28 | module: { 29 | rules: [ 30 | { 31 | test: /\.js$/, 32 | use: 'babel-loader', 33 | exclude: /node_modules/, 34 | }, 35 | ], 36 | }, 37 | plugins: [ 38 | new HtmlWebpackPlugin({ 39 | inject: true, 40 | template: path.resolve(__dirname, '../public/index.html'), 41 | }), 42 | new ReactServerWebpackPlugin({isServer: false}), 43 | ], 44 | }, 45 | (err, stats) => { 46 | if (err) { 47 | console.error(err.stack || err); 48 | if (err.details) { 49 | console.error(err.details); 50 | } 51 | process.exit(1); 52 | return; 53 | } 54 | const info = stats.toJson(); 55 | if (stats.hasErrors()) { 56 | console.log('Finished running webpack with errors.'); 57 | info.errors.forEach((e) => console.error(e)); 58 | process.exit(1); 59 | } else { 60 | console.log('Finished running webpack.'); 61 | } 62 | } 63 | ); 64 | -------------------------------------------------------------------------------- /scripts/seed.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) Facebook, Inc. and its affiliates. 3 | * 4 | * This source code is licensed under the MIT license found in the 5 | * LICENSE file in the root directory of this source tree. 6 | * 7 | */ 8 | 9 | 'use strict'; 10 | 11 | const fs = require('fs'); 12 | const path = require('path'); 13 | const {readdir, unlink, writeFile} = require('fs/promises'); 14 | const {db} = require('../src/db.server'); 15 | 16 | const NOTES_PATH = './notes'; 17 | 18 | async function seed() { 19 | const oldNotes = await readdir(path.resolve(NOTES_PATH)); 20 | await Promise.all( 21 | oldNotes 22 | .filter((filename) => filename.endsWith('.md')) 23 | .map((filename) => unlink(path.resolve(NOTES_PATH, filename))) 24 | ); 25 | 26 | await Promise.all( 27 | db.map((note) => { 28 | const id = note.id; 29 | const content = note.body; 30 | const data = new Uint8Array(Buffer.from(content)); 31 | return writeFile(path.resolve(NOTES_PATH, `${id}.md`), data, (err) => { 32 | if (err) { 33 | throw err; 34 | } 35 | }); 36 | }) 37 | ); 38 | } 39 | 40 | seed(); 41 | -------------------------------------------------------------------------------- /server/api.server.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) Facebook, Inc. and its affiliates. 3 | * 4 | * This source code is licensed under the MIT license found in the 5 | * LICENSE file in the root directory of this source tree. 6 | * 7 | */ 8 | 9 | 'use strict'; 10 | 11 | const register = require('react-server-dom-webpack/node-register'); 12 | register(); 13 | const babelRegister = require('@babel/register'); 14 | 15 | babelRegister({ 16 | ignore: [/\/(build|server|node_modules)\//], 17 | presets: [['react-app', {runtime: 'automatic'}]], 18 | plugins: ['@babel/transform-modules-commonjs'], 19 | }); 20 | 21 | const express = require('express'); 22 | const compress = require('compression'); 23 | const {readFileSync} = require('fs'); 24 | const {unlink, writeFile} = require('fs/promises'); 25 | const {pipeToNodeWritable} = require('react-server-dom-webpack/writer'); 26 | const path = require('path'); 27 | const React = require('react'); 28 | const ReactApp = require('../src/App.server').default; 29 | const { 30 | db, 31 | findNote, 32 | insertNote, 33 | editNote, 34 | deleteNote, 35 | } = require('../src/db.server'); 36 | 37 | const PORT = 4000; 38 | const app = express(); 39 | 40 | app.use(compress()); 41 | app.use(express.json()); 42 | 43 | app.listen(PORT, () => { 44 | console.log('React Notes listening at 4000...'); 45 | }); 46 | 47 | function handleErrors(fn) { 48 | return async function(req, res, next) { 49 | try { 50 | return await fn(req, res); 51 | } catch (x) { 52 | next(x); 53 | } 54 | }; 55 | } 56 | 57 | app.get( 58 | '/', 59 | handleErrors(async function(_req, res) { 60 | await waitForWebpack(); 61 | const html = readFileSync( 62 | path.resolve(__dirname, '../build/index.html'), 63 | 'utf8' 64 | ); 65 | // Note: this is sending an empty HTML shell, like a client-side-only app. 66 | // However, the intended solution (which isn't built out yet) is to read 67 | // from the Server endpoint and turn its response into an HTML stream. 68 | res.send(html); 69 | }) 70 | ); 71 | 72 | async function renderReactTree(res, props) { 73 | await waitForWebpack(); 74 | const manifest = readFileSync( 75 | path.resolve(__dirname, '../build/react-client-manifest.json'), 76 | 'utf8' 77 | ); 78 | const moduleMap = JSON.parse(manifest); 79 | pipeToNodeWritable(React.createElement(ReactApp, props), res, moduleMap); 80 | } 81 | 82 | function sendResponse(req, res, redirectToId) { 83 | const location = JSON.parse(req.query.location); 84 | if (redirectToId) { 85 | location.selectedId = redirectToId; 86 | } 87 | res.set('X-Location', JSON.stringify(location)); 88 | renderReactTree(res, { 89 | selectedId: location.selectedId, 90 | isEditing: location.isEditing, 91 | searchText: location.searchText, 92 | }); 93 | } 94 | 95 | app.get('/react', function(req, res) { 96 | sendResponse(req, res, null); 97 | }); 98 | 99 | const NOTES_PATH = path.resolve(__dirname, '../notes'); 100 | 101 | app.post( 102 | '/notes', 103 | handleErrors(async function(req, res) { 104 | const note = insertNote(req.body.title, req.body.body); 105 | const insertedId = note.id; 106 | await writeFile( 107 | path.resolve(NOTES_PATH, `${insertedId}.md`), 108 | req.body.body, 109 | 'utf8' 110 | ); 111 | sendResponse(req, res, insertedId); 112 | }) 113 | ); 114 | 115 | app.put( 116 | '/notes/:id', 117 | handleErrors(async function(req, res) { 118 | const updatedId = Number(req.params.id); 119 | editNote(updatedId, req.body.title, req.body.body); 120 | 121 | await writeFile( 122 | path.resolve(NOTES_PATH, `${updatedId}.md`), 123 | req.body.body, 124 | 'utf8' 125 | ); 126 | sendResponse(req, res, null); 127 | }) 128 | ); 129 | 130 | app.delete( 131 | '/notes/:id', 132 | handleErrors(async function(req, res) { 133 | deleteNote(req.params.id); 134 | await unlink(path.resolve(NOTES_PATH, `${req.params.id}.md`)); 135 | sendResponse(req, res, null); 136 | }) 137 | ); 138 | 139 | app.get( 140 | '/notes', 141 | handleErrors(async function(_req, res) { 142 | res.json(db); 143 | }) 144 | ); 145 | 146 | app.get( 147 | '/notes/:id', 148 | handleErrors(async function(req, res) { 149 | const note = findNote(req.params.id); 150 | res.json(note); 151 | }) 152 | ); 153 | 154 | app.get('/sleep/:ms', function(req, res) { 155 | setTimeout(() => { 156 | res.json({ok: true}); 157 | }, req.params.ms); 158 | }); 159 | 160 | app.use(express.static('build')); 161 | app.use(express.static('public')); 162 | 163 | app.on('error', function(error) { 164 | if (error.syscall !== 'listen') { 165 | throw error; 166 | } 167 | var bind = typeof port === 'string' ? 'Pipe ' + port : 'Port ' + port; 168 | switch (error.code) { 169 | case 'EACCES': 170 | console.error(bind + ' requires elevated privileges'); 171 | process.exit(1); 172 | break; 173 | case 'EADDRINUSE': 174 | console.error(bind + ' is already in use'); 175 | process.exit(1); 176 | break; 177 | default: 178 | throw error; 179 | } 180 | }); 181 | 182 | async function waitForWebpack() { 183 | while (true) { 184 | try { 185 | readFileSync(path.resolve(__dirname, '../build/index.html')); 186 | return; 187 | } catch (err) { 188 | console.log( 189 | 'Could not find webpack build output. Will retry in a second...' 190 | ); 191 | await new Promise((resolve) => setTimeout(resolve, 1000)); 192 | } 193 | } 194 | } 195 | -------------------------------------------------------------------------------- /server/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "type": "commonjs", 3 | "main": "./api.server.js" 4 | } 5 | -------------------------------------------------------------------------------- /src/App.server.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) Facebook, Inc. and its affiliates. 3 | * 4 | * This source code is licensed under the MIT license found in the 5 | * LICENSE file in the root directory of this source tree. 6 | * 7 | */ 8 | 9 | import {Suspense} from 'react'; 10 | 11 | import Note from './Note.server'; 12 | import NoteList from './NoteList.server'; 13 | import EditButton from './EditButton.client'; 14 | import SearchField from './SearchField.client'; 15 | import NoteSkeleton from './NoteSkeleton'; 16 | import NoteListSkeleton from './NoteListSkeleton'; 17 | 18 | export default function App({selectedId, isEditing, searchText}) { 19 | return ( 20 |
21 |
22 |
23 | 31 | React Notes 32 |
33 |
34 | 35 | New 36 |
37 | 42 |
43 |
44 | }> 45 | 46 | 47 |
48 |
49 | ); 50 | } 51 | -------------------------------------------------------------------------------- /src/Cache.client.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) Facebook, Inc. and its affiliates. 3 | * 4 | * This source code is licensed under the MIT license found in the 5 | * LICENSE file in the root directory of this source tree. 6 | * 7 | */ 8 | 9 | import {unstable_getCacheForType, unstable_useCacheRefresh} from 'react'; 10 | import {createFromFetch} from 'react-server-dom-webpack'; 11 | 12 | function createResponseCache() { 13 | return new Map(); 14 | } 15 | 16 | export function useRefresh() { 17 | const refreshCache = unstable_useCacheRefresh(); 18 | return function refresh(key, seededResponse) { 19 | refreshCache(createResponseCache, new Map([[key, seededResponse]])); 20 | }; 21 | } 22 | 23 | export function useServerResponse(location) { 24 | const key = JSON.stringify(location); 25 | const cache = unstable_getCacheForType(createResponseCache); 26 | let response = cache.get(key); 27 | if (response) { 28 | return response; 29 | } 30 | response = createFromFetch( 31 | fetch('/react?location=' + encodeURIComponent(key)) 32 | ); 33 | cache.set(key, response); 34 | return response; 35 | } 36 | -------------------------------------------------------------------------------- /src/EditButton.client.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) Facebook, Inc. and its affiliates. 3 | * 4 | * This source code is licensed under the MIT license found in the 5 | * LICENSE file in the root directory of this source tree. 6 | * 7 | */ 8 | 9 | import {unstable_useTransition} from 'react'; 10 | 11 | import {useLocation} from './LocationContext.client'; 12 | 13 | export default function EditButton({noteId, children}) { 14 | const [, setLocation] = useLocation(); 15 | const [startTransition, isPending] = unstable_useTransition(); 16 | const isDraft = noteId == null; 17 | return ( 18 | 36 | ); 37 | } 38 | -------------------------------------------------------------------------------- /src/LocationContext.client.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) Facebook, Inc. and its affiliates. 3 | * 4 | * This source code is licensed under the MIT license found in the 5 | * LICENSE file in the root directory of this source tree. 6 | * 7 | */ 8 | 9 | import {createContext, useContext} from 'react'; 10 | 11 | export const LocationContext = createContext(); 12 | export function useLocation() { 13 | return useContext(LocationContext); 14 | } 15 | -------------------------------------------------------------------------------- /src/Note.server.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) Facebook, Inc. and its affiliates. 3 | * 4 | * This source code is licensed under the MIT license found in the 5 | * LICENSE file in the root directory of this source tree. 6 | * 7 | */ 8 | 9 | import {fetch} from 'react-fetch'; 10 | import {readFile} from 'react-fs'; 11 | import {format} from 'date-fns'; 12 | import path from 'path'; 13 | 14 | import {db} from './db.server'; 15 | import NotePreview from './NotePreview'; 16 | import EditButton from './EditButton.client'; 17 | import NoteEditor from './NoteEditor.client'; 18 | 19 | export default function Note({selectedId, isEditing}) { 20 | const note = 21 | selectedId != null 22 | ? fetch(`http://localhost:4000/notes/${selectedId}`).json() 23 | : null; 24 | 25 | if (note === null) { 26 | if (isEditing) { 27 | return ( 28 | 29 | ); 30 | } else { 31 | return ( 32 |
33 | 34 | Click a note on the left to view something! 🥺 35 | 36 |
37 | ); 38 | } 39 | } 40 | 41 | let {id, title, body, updated_at} = note; 42 | const updatedAt = new Date(updated_at); 43 | 44 | // We could also read from a file instead. 45 | // body = readFile(path.resolve(`./notes/${note.id}.md`), 'utf8'); 46 | 47 | // Now let's see how the Suspense boundary above lets us not block on this. 48 | // fetch('http://localhost:4000/sleep/3000'); 49 | 50 | if (isEditing) { 51 | return ; 52 | } else { 53 | return ( 54 |
55 |
56 |

{title}

57 |
58 | 59 | Last updated on {format(updatedAt, "d MMM yyyy 'at' h:mm bb")} 60 | 61 | Edit 62 |
63 |
64 | 65 |
66 | ); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/NoteEditor.client.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) Facebook, Inc. and its affiliates. 3 | * 4 | * This source code is licensed under the MIT license found in the 5 | * LICENSE file in the root directory of this source tree. 6 | * 7 | */ 8 | 9 | import {useState, unstable_useTransition} from 'react'; 10 | import {createFromReadableStream} from 'react-server-dom-webpack'; 11 | 12 | import NotePreview from './NotePreview'; 13 | import {useRefresh} from './Cache.client'; 14 | import {useLocation} from './LocationContext.client'; 15 | 16 | export default function NoteEditor({noteId, initialTitle, initialBody}) { 17 | const refresh = useRefresh(); 18 | const [title, setTitle] = useState(initialTitle); 19 | const [body, setBody] = useState(initialBody); 20 | const [location, setLocation] = useLocation(); 21 | const [startNavigating, isNavigating] = unstable_useTransition(); 22 | const [isSaving, saveNote] = useMutation({ 23 | endpoint: noteId !== null ? `/notes/${noteId}` : `/notes`, 24 | method: noteId !== null ? 'PUT' : 'POST', 25 | }); 26 | const [isDeleting, deleteNote] = useMutation({ 27 | endpoint: `/notes/${noteId}`, 28 | method: 'DELETE', 29 | }); 30 | 31 | async function handleSave() { 32 | const payload = {title, body}; 33 | const requestedLocation = { 34 | selectedId: noteId, 35 | isEditing: false, 36 | searchText: location.searchText, 37 | }; 38 | const response = await saveNote(payload, requestedLocation); 39 | navigate(response); 40 | } 41 | 42 | async function handleDelete() { 43 | const payload = {}; 44 | const requestedLocation = { 45 | selectedId: null, 46 | isEditing: false, 47 | searchText: location.searchText, 48 | }; 49 | const response = await deleteNote(payload, requestedLocation); 50 | navigate(response); 51 | } 52 | 53 | function navigate(response) { 54 | const cacheKey = response.headers.get('X-Location'); 55 | const nextLocation = JSON.parse(cacheKey); 56 | const seededResponse = createFromReadableStream(response.body); 57 | startNavigating(() => { 58 | refresh(cacheKey, seededResponse); 59 | setLocation(nextLocation); 60 | }); 61 | } 62 | 63 | const isDraft = noteId === null; 64 | return ( 65 |
66 |
e.preventDefault()}> 70 | 73 | { 78 | setTitle(e.target.value); 79 | }} 80 | /> 81 | 84 |