From b50f5667f12179e9c3717bfca12e0879e3569a03 Mon Sep 17 00:00:00 2001 From: Erik Arvidsson Date: Thu, 29 Feb 2024 11:55:11 +0100 Subject: [PATCH 1/2] feat: SolidJS port MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is a basic SolidJS port It does not yet figure out how to make Replicache ❤️ SolidJS. This is just a baseline to start from. --- client/.eslintignore | 2 +- client/package.json | 31 +- client/src/app.tsx | 211 +- client/src/components/footer.tsx | 49 +- client/src/components/header.tsx | 46 +- client/src/components/link.tsx | 31 +- client/src/components/main-section.tsx | 73 +- client/src/components/share.tsx | 108 +- client/src/components/todo-item.tsx | 77 +- client/src/components/todo-list.tsx | 28 +- client/src/components/todo-text-input.tsx | 58 +- client/src/create-effect-accessor.ts | 12 + client/src/index.css | 17 +- client/src/index.tsx | 85 +- client/src/mutators.ts | 14 +- client/tsconfig.json | 3 +- client/vite.config.ts | 4 +- package-lock.json | 3969 +++++++++++++++------ package.json | 7 +- shared/src/list.ts | 2 +- 20 files changed, 3382 insertions(+), 1445 deletions(-) create mode 100644 client/src/create-effect-accessor.ts diff --git a/client/.eslintignore b/client/.eslintignore index 7758a6a..722408a 100644 --- a/client/.eslintignore +++ b/client/.eslintignore @@ -6,4 +6,4 @@ bin dist lib env.d.ts -vite.config.ts \ No newline at end of file +vite.config.ts diff --git a/client/package.json b/client/package.json index f914d61..90e1def 100644 --- a/client/package.json +++ b/client/package.json @@ -18,29 +18,26 @@ "watch": "concurrently --kill-others 'npm run server' 'npm run check-types -- --watch --preserveWatchOutput' 'sleep 3; npm run dev'" }, "dependencies": { - "classnames": "^2.3.1", - "navigo": "^8.11.1", - "qs": "^6.11.0", - "react": "^18.2.0", - "react-dom": "^18.2.0", - "replicache-react": "5.0.1", + "@solidjs/router": "^0.12.4", "shared": "^0.1.0", + "solid-js": "^1.8.15", "todomvc-app-css": "^2.4.2" }, "devDependencies": { - "@rocicorp/eslint-config": "^0.1.2", - "@rocicorp/prettier-config": "^0.1.1", - "@types/react": "^18.0.17", - "@types/react-dom": "^18.0.6", - "@vitejs/plugin-react": "^2.0.1", - "concurrently": "^7.4.0", - "prettier": "^2.2.1", - "typescript": "^4.7.4", - "use-debounce": "^9.0.4", - "vite": "^3.0.7" + "@rocicorp/eslint-config": "^0.5.1", + "@rocicorp/prettier-config": "^0.2.0", + "concurrently": "^8.2.2", + "typescript": "^5.3.3", + "vite": "^5.1.4", + "vite-plugin-solid": "^2.10.1" }, "eslintConfig": { - "extends": "@rocicorp/eslint-config" + "extends": "@rocicorp/eslint-config", + "rules": { + "@typescript-eslint/naming-convention": [ + "off" + ] + } }, "prettier": "@rocicorp/prettier-config" } diff --git a/client/src/app.tsx b/client/src/app.tsx index c40cd1e..e016d85 100644 --- a/client/src/app.tsx +++ b/client/src/app.tsx @@ -1,142 +1,145 @@ -import {Dialog} from '@headlessui/react'; +import {A, useNavigate, useParams} from '@solidjs/router'; import {nanoid} from 'nanoid'; -import Navigo from 'navigo'; -import {useEffect, useState} from 'react'; -import {ReadTransaction, Replicache} from 'replicache'; -import {useSubscribe} from 'replicache-react'; -import {TodoUpdate, todosByList} from 'shared'; -import {getList, listLists} from 'shared/src/list'; -import Header from './components/header'; -import MainSection from './components/main-section'; -import {Share} from './components/share'; +import {Replicache} from 'replicache'; +import {List, Todo, TodoUpdate, getList, listLists, todosByList} from 'shared'; +import { + Component, + For, + JSX, + createEffect, + createSignal, + onCleanup, +} from 'solid-js'; +import Header from './components/header.jsx'; +import MainSection from './components/main-section.jsx'; +import Share from './components/share.jsx'; +import {createEffectAccessor} from './create-effect-accessor.js'; import {M} from './mutators'; // This is the top-level component for our app. -const App = ({ - rep, - userID, - onUserIDChange, -}: { +const App = (props: { rep: Replicache; userID: string; onUserIDChange: (userID: string) => void; }) => { - const router = new Navigo('/'); - const [listID, setListID] = useState(''); - const [showingShare, setShowingShare] = useState(false); - - router.on('/list/:listID', match => { - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - const {data} = match!; - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - const {listID} = data!; - setListID(listID); - }); - - useEffect(() => { - router.resolve(); - }, []); + const [showingShare, setShowingShare] = createSignal(false); - // Listen for pokes related to just this list. - useEventSourcePoke(`/api/replicache/poke?channel=list/${listID}`, rep); - // Listen for pokes related to the docs this user has access to. - useEventSourcePoke(`/api/replicache/poke?channel=user/${userID}`, rep); + const params = useParams(); - const lists = useSubscribe(rep, listLists, {default: []}); - lists.sort((a, b) => a.name.localeCompare(b.name)); + createEffect(() => { + const {listID} = params; + if (!listID) { + return; + } + // Listen for pokes related to just this list. + useEventSourcePoke( + `/api/replicache/poke?channel=list/${listID}`, + props.rep, + ); + // Listen for pokes related to the docs this user has access to. + useEventSourcePoke( + `/api/replicache/poke?channel=user/${props.userID}`, + props.rep, + ); + }); - const selectedList = useSubscribe( - rep, - (tx: ReadTransaction) => getList(tx, listID), - {dependencies: [listID]}, + const lists = createEffectAccessor( + set => + onCleanup( + props.rep.subscribe(async tx => { + const arr = await listLists(tx); + return arr.sort((a, b) => a.name.localeCompare(b.name)); + }, set), + ), + [], ); + const selectedList = createEffectAccessor(async set => { + const {listID} = params; + set(await props.rep.query(tx => getList(tx, listID))); + }, undefined); + // Subscribe to all todos and sort them. - const todos = useSubscribe(rep, async tx => todosByList(tx, listID), { - default: [], - dependencies: [listID], - }); - todos.sort((a, b) => a.sort - b.sort); + const todos = createEffectAccessor(set => { + const {rep} = props; + const {listID} = params; + onCleanup( + rep.subscribe(async tx => { + const todos = await todosByList(tx, listID); + return todos.sort((a, b) => a.sort - b.sort); + }, set), + ); + }, []); // Define event handlers and connect them to Replicache mutators. Each // of these mutators runs immediately (optimistically) locally, then runs // again on the server-side automatically. const handleNewItem = (text: string) => { - void rep.mutate.createTodo({ + void props.rep.mutate.createTodo({ id: nanoid(), - listID, + listID: params.listID, text, completed: false, }); }; const handleUpdateTodo = (update: TodoUpdate) => - rep.mutate.updateTodo(update); + props.rep.mutate.updateTodo(update); const handleDeleteTodos = async (ids: string[]) => { for (const id of ids) { - await rep.mutate.deleteTodo(id); + await props.rep.mutate.deleteTodo(id); } }; const handleCompleteTodos = async (completed: boolean, ids: string[]) => { for (const id of ids) { - await rep.mutate.updateTodo({ + await props.rep.mutate.updateTodo({ id, completed, }); } }; + const navigate = useNavigate(); + const handleNewList = async (name: string) => { + const {userID} = props; const id = nanoid(); - await rep.mutate.createList({ + await props.rep.mutate.createList({ id, ownerID: userID, name, }); - router.navigate(`/list/${id}`); + navigate(`/list/${id}`); }; const handleDeleteList = async () => { - await rep.mutate.deleteList(listID); + await props.rep.mutate.deleteList(params.listID); + navigate('/'); }; // Render app. - return (
}> + {list => {list.name}} +
-
+
setShowingShare(!showingShare)} + onUserIDChange={props.onUserIDChange} + onShare={() => setShowingShare(!showingShare())} /> - {selectedList ? ( + {selectedList() ? ( No list selected
)}
-
- setShowingShare(false)}> - +
+ setShowingShare(false)} + class="share-dialog" + > +
); }; function useEventSourcePoke(url: string, rep: Replicache) { - useEffect(() => { + createEffect(() => { const ev = new EventSource(url); ev.onmessage = () => { void rep.pull(); }; - return () => ev.close(); - }, [url, rep]); + onCleanup(() => ev.close()); + }); } export default App; + +const Dialog: Component<{ + open: boolean; + children: JSX.Element; + onClose: () => void; + class?: string; +}> = props => { + let el!: HTMLDialogElement; + const onClick = (e: MouseEvent) => { + const dialogDimensions = el.getBoundingClientRect(); + if ( + e.clientX < dialogDimensions.left || + e.clientX > dialogDimensions.right || + e.clientY < dialogDimensions.top || + e.clientY > dialogDimensions.bottom + ) { + el.close(); + } + }; + + const onClose = () => { + props.onClose(); + }; + + createEffect(() => { + if (props.open) { + el.showModal(); + } else { + el.close(); + } + }); + + return ( + + {props.children} + + ); +}; diff --git a/client/src/components/footer.tsx b/client/src/components/footer.tsx index 3406d5f..f64c3fb 100644 --- a/client/src/components/footer.tsx +++ b/client/src/components/footer.tsx @@ -1,41 +1,40 @@ -import React from 'react'; +import {For} from 'solid-js'; import FilterLink from './link'; const FILTER_TITLES = ['All', 'Active', 'Completed']; -const Footer = ({ - active, - completed, - currentFilter, - onFilter, - onDeleteCompleted, -}: { +const Footer = (props: { active: number; completed: number; currentFilter: string; onFilter: (filter: string) => void; onDeleteCompleted: () => void; }) => { - const itemWord = active === 1 ? 'item' : 'items'; + const itemWord = () => (props.active === 1 ? 'item' : 'items'); return ( -