Compare commits
9 commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5baead043a | |||
| c25700d7cd | |||
| 8b6f09cead | |||
| a961f387d6 | |||
| bceee7ecf1 | |||
| f05b95ddc7 | |||
| 4246c94ac5 | |||
| 2178304c76 | |||
| 9ebc2ee5fe |
13 changed files with 437 additions and 180 deletions
134
README.md
134
README.md
|
|
@ -1,11 +1,87 @@
|
||||||
# zulip
|
# zulip
|
||||||
|
|
||||||
A minimal, no-build web app kit using only Lit-based web components via importmaps.
|
A modern, no-build web application using Lit web components with Zustand state management.
|
||||||
|
|
||||||
|
## Architecture Overview
|
||||||
|
|
||||||
## Quick Start Example
|
* **Lit-based web components** - Standards-based, framework-agnostic UI
|
||||||
|
* **No-build tooling** - Import maps only, no bundler required
|
||||||
|
* **Zustand state management** - Lightweight, vanilla JS store
|
||||||
|
* **StoreController** - Reactive controller that bridges Zustand and Lit
|
||||||
|
|
||||||
### 1. Define a Component
|
## Project Structure
|
||||||
|
|
||||||
|
```
|
||||||
|
components/ -> standard lit components, including top-level "app-root"
|
||||||
|
controllers/ -> lit reactive controllers (such as Zustand<->Lit bridge)
|
||||||
|
store/ -> defines specific accessor sets
|
||||||
|
index.html -> entry point with inline importmap and app-root
|
||||||
|
README.md
|
||||||
|
```
|
||||||
|
|
||||||
|
## Data Flow Diagram
|
||||||
|
|
||||||
|
```mermaid
|
||||||
|
sequenceDiagram
|
||||||
|
participant User
|
||||||
|
participant LitComponent
|
||||||
|
participant StoreController
|
||||||
|
participant ZustandStore
|
||||||
|
|
||||||
|
User->>LitComponent: Click button
|
||||||
|
LitComponent->>ZustandStore: Call action (e.g., addItem)
|
||||||
|
ZustandStore->>ZustandStore: Update state
|
||||||
|
ZustandStore->>StoreController: Notify subscribers
|
||||||
|
StoreController->>LitComponent: requestUpdate()
|
||||||
|
LitComponent->>LitComponent: Re-render
|
||||||
|
LitComponent->>User: Show updated UI
|
||||||
|
```
|
||||||
|
|
||||||
|
## Example
|
||||||
|
|
||||||
|
### 1. Define Store (Zustand Vanilla)
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
// store/index.js
|
||||||
|
import { createStore } from 'zustand/vanilla'
|
||||||
|
|
||||||
|
export const store = createStore((set, get) => ({
|
||||||
|
count: 0,
|
||||||
|
increment: () => set(s => ({ count: s.count + 1 })),
|
||||||
|
decrement: () => set(s => ({ count: s.count - 1 })),
|
||||||
|
}))
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. Create StoreController (Bridge)
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
// controllers/store.js
|
||||||
|
export class StoreController {
|
||||||
|
constructor(host, store, selector) {
|
||||||
|
this.host = host
|
||||||
|
this.store = store
|
||||||
|
this.selector = selector
|
||||||
|
host.addController(this)
|
||||||
|
}
|
||||||
|
|
||||||
|
hostConnected() {
|
||||||
|
this._unsub = this.store.subscribe((state) => {
|
||||||
|
const next = this.selector(state)
|
||||||
|
if (next !== this.value) {
|
||||||
|
this.value = next
|
||||||
|
this.host.requestUpdate()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
this.value = this.selector(this.store.getState())
|
||||||
|
}
|
||||||
|
|
||||||
|
hostDisconnected() {
|
||||||
|
this._unsub?.()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. Create Component (Lit + StoreController)
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
// components/counter.js
|
// components/counter.js
|
||||||
|
|
@ -22,16 +98,15 @@ class Counter extends LitElement {
|
||||||
}
|
}
|
||||||
|
|
||||||
static styles = css`
|
static styles = css`
|
||||||
button { padding: 1rem; font-size: 1.2rem; }
|
button { padding: 1rem; font-size: 1.2rem; margin: 0.5rem; }
|
||||||
`
|
`
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
return html`
|
return html`
|
||||||
<div>
|
<div>
|
||||||
<p>Count: ${this.count}</p>
|
<p>Count: ${this.#count.value}</p>
|
||||||
<button @click=${() => this.count++}>
|
<button @click=${() => store.getState().decrement()}>-</button>
|
||||||
Increment
|
<button @click=${() => store.getState().increment()}>+</button>
|
||||||
</button>
|
|
||||||
</div>
|
</div>
|
||||||
`
|
`
|
||||||
}
|
}
|
||||||
|
|
@ -40,53 +115,24 @@ class Counter extends LitElement {
|
||||||
customElements.define('my-counter', Counter)
|
customElements.define('my-counter', Counter)
|
||||||
```
|
```
|
||||||
|
|
||||||
### 2. Use in App root (or other composition node)
|
### 4. Instantiate in parent (such as app-root)
|
||||||
|
|
||||||
```js
|
```js
|
||||||
import "./my-counter.js";
|
import counter from "./counter.js":
|
||||||
|
|
||||||
class AppRoot extends LitElement {
|
class AppRoot extends LitElement {
|
||||||
...
|
|
||||||
render() {
|
render() {
|
||||||
return html`<my-counter .count=${0}></my-counter>`;
|
return html`
|
||||||
|
<counter></counter>
|
||||||
|
`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### 3. Define Data Flows
|
|
||||||
|
|
||||||
#### Parent-to-Child (Props)
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
// Parent passes data down
|
|
||||||
html`<child-component .items=${this.items}></child-component>`
|
|
||||||
|
|
||||||
// Child receives via properties
|
|
||||||
static properties = {
|
|
||||||
items: { type: Array }
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Child-to-Parent (Events)
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
// Child emits event
|
|
||||||
this.dispatchEvent(new CustomEvent('add-item', {
|
|
||||||
detail: { item: newItem },
|
|
||||||
bubbles: true,
|
|
||||||
composed: true
|
|
||||||
}))
|
|
||||||
|
|
||||||
// Parent listens
|
|
||||||
html`<child-component @add-item=${(e) => this.handleAdd(e.detail.item)}></child-component>`
|
|
||||||
```
|
|
||||||
|
|
||||||
## Benefits Over TSX
|
## Benefits Over TSX
|
||||||
|
|
||||||
1. **No build step** - Edit and refresh, instant feedback
|
1. **No build step** - Edit and refresh, instant feedback
|
||||||
|
|
||||||
1. **Minimal dependencies** - Just Lit, nothing else
|
|
||||||
|
|
||||||
1. **Smaller bundle** - No framework runtime, just standards
|
1. **Smaller bundle** - No framework runtime, just standards
|
||||||
|
|
||||||
1. **Better encapsulation** - Shadow DOM, scoped styles
|
1. **Better encapsulation** - Shadow DOM, scoped styles
|
||||||
|
|
@ -95,4 +141,6 @@ html`<child-component @add-item=${(e) => this.handleAdd(e.detail.item)}></child-
|
||||||
|
|
||||||
1. **Future-proof** - Built on web standards
|
1. **Future-proof** - Built on web standards
|
||||||
|
|
||||||
1. **Easy to learn** - Simple mental model for state, with natural evolution into Zustand/IndexedDB or similar extensions
|
1. **Familiar patterns** - Zustand works like Redux/Context
|
||||||
|
|
||||||
|
1. **Type-safe** - Can add JSDoc or TypeScript without build step
|
||||||
|
|
|
||||||
66
TASKS.md
Normal file
66
TASKS.md
Normal file
|
|
@ -0,0 +1,66 @@
|
||||||
|
# Tasks & Issues
|
||||||
|
|
||||||
|
## 1. Critical Issues
|
||||||
|
|
||||||
|
- [x] **1.1 Fix missing `del` import in store/index.js** ✅ COMPLETE
|
||||||
|
- Line 3 now imports `get, set, del` from idb-keyval
|
||||||
|
- Used in removeItem function on line 13
|
||||||
|
- No more ReferenceError
|
||||||
|
|
||||||
|
- [x] **1.2 Fix hydration flag initialization** ✅ COMPLETE
|
||||||
|
- Implemented Option A: Initialize `_hydrated: true` by default
|
||||||
|
- Removed problematic `onRehydrateStorage` callback
|
||||||
|
- App now loads immediately without hanging on "loading..."
|
||||||
|
- Persistence happens in background automatically
|
||||||
|
|
||||||
|
- [x] **1.3 Consolidate duplicate storage implementation** ✅ COMPLETE
|
||||||
|
- Single storage implementation in `store/index.js`
|
||||||
|
- Uses idb-keyval directly with createJSONStorage
|
||||||
|
- Clean, simple approach without extra middleware files
|
||||||
|
|
||||||
|
## 2. Architectural Improvements
|
||||||
|
|
||||||
|
- [x] **2.1 Add error handling** ✅ COMPLETE
|
||||||
|
- Added try/catch blocks around all IndexedDB operations (getItem, setItem, removeItem)
|
||||||
|
- Implemented fallback: returns null on getItem error, silently fails on setItem/removeItem
|
||||||
|
- Added error state to store with auto-clear after 3 seconds
|
||||||
|
- Added validation in addItem and removeItem actions
|
||||||
|
- Created error-toast component with slide-in animation
|
||||||
|
- Toast shows error messages and allows manual dismissal
|
||||||
|
|
||||||
|
- [x] **2.2 Add partialize option to persist middleware** ✅ COMPLETE
|
||||||
|
- Added partialize to exclude `_hydrated` flag from persistence
|
||||||
|
- Only persists: user, items, and route
|
||||||
|
- Prevents unnecessary data in IndexedDB
|
||||||
|
|
||||||
|
- [ ] **2.3 Add basic CSS reset to index.html**
|
||||||
|
- No base styles, margins, or font settings currently
|
||||||
|
- Consider adding minimal reset or normalize.css
|
||||||
|
|
||||||
|
- [ ] **2.4 Add loading states for async operations**
|
||||||
|
- No loading indicators for add/remove item operations
|
||||||
|
- Consider adding optimistic updates
|
||||||
|
|
||||||
|
## 3. Nice to Have
|
||||||
|
|
||||||
|
- [ ] **3.1 Add TypeScript types** (optional)
|
||||||
|
- JSDoc comments for better IDE support
|
||||||
|
- Or migrate to .ts files with no-build setup
|
||||||
|
|
||||||
|
- [ ] **3.2 Add cross-tab synchronization**
|
||||||
|
- README mentions optional `sync.js` middleware
|
||||||
|
- Implement BroadcastChannel for cross-tab state sync
|
||||||
|
|
||||||
|
- [ ] **3.3 Add route history management**
|
||||||
|
- Integrate with browser history API
|
||||||
|
- Support back/forward navigation
|
||||||
|
|
||||||
|
- [ ] **3.4 Add unit tests**
|
||||||
|
- Test store actions and selectors
|
||||||
|
- Test component rendering
|
||||||
|
- Test persistence layer
|
||||||
|
|
||||||
|
- [ ] **3.5 Add Storybook support**
|
||||||
|
- Introduces parallel build (vite internally) but core app remains no-build
|
||||||
|
- Storybook includes raw/native web component support
|
||||||
|
- Ensure individual components and layouts are properly organized, documented, demonstrated, and tested
|
||||||
|
|
@ -1,64 +1,46 @@
|
||||||
import { LitElement, html, css } from "lit";
|
import { LitElement, html, css } from "lit";
|
||||||
|
import { store} from "../store/index.js";
|
||||||
|
import { StoreController } from "../controllers/store-controller.js";
|
||||||
import "./nav-bar.js";
|
import "./nav-bar.js";
|
||||||
import "./page-home.js";
|
import "./page-home.js";
|
||||||
import "./page-items.js";
|
import "./page-items.js";
|
||||||
|
import "./error-toast.js";
|
||||||
|
|
||||||
class AppRoot extends LitElement {
|
class AppRoot extends LitElement {
|
||||||
static properties = {
|
#hydrated = new StoreController(this, store, s => s._hydrated);
|
||||||
user: { type: Object, state: true },
|
#user = new StoreController(this, store, s => s.user);
|
||||||
route: { type: String, state: true },
|
#route = new StoreController(this, store, s => s.route);
|
||||||
items: { type: Array, state: true }
|
|
||||||
};
|
|
||||||
|
|
||||||
constructor() {
|
|
||||||
super();
|
|
||||||
this.user = null;
|
|
||||||
this.route = 'home';
|
|
||||||
this.items = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
static styles = css`
|
static styles = css`
|
||||||
:host {
|
:host {
|
||||||
display: block;
|
display: block;
|
||||||
min-height: 100vh;
|
min-height: 100vh;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.loading {
|
||||||
|
display: grid;
|
||||||
|
place-items: center;
|
||||||
|
height: 100vh;
|
||||||
|
opacity: 0.4;
|
||||||
|
}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
#navigate(route) {
|
|
||||||
this.route = route;
|
|
||||||
}
|
|
||||||
|
|
||||||
#addItem(item) {
|
|
||||||
this.items = [...this.items, item];
|
|
||||||
}
|
|
||||||
|
|
||||||
#removeItem(id) {
|
|
||||||
this.items = this.items.filter(i => i.id !== id);
|
|
||||||
}
|
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
|
if (!this.#hydrated.value) {
|
||||||
|
return html`<div class="loading">loading...</div>`;
|
||||||
|
}
|
||||||
return html`
|
return html`
|
||||||
<nav-bar
|
<nav-bar .user=${this.#user.value}></nav-bar>
|
||||||
.user=${this.user}
|
|
||||||
.route=${this.route}
|
|
||||||
@navigate=${(e) => this.#navigate(e.detail.route)}
|
|
||||||
></nav-bar>
|
|
||||||
<main>${this.#renderRoute()}</main>
|
<main>${this.#renderRoute()}</main>
|
||||||
|
<error-toast></error-toast>
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
#renderRoute() {
|
#renderRoute() {
|
||||||
switch (this.route) {
|
switch (this.#route.value) {
|
||||||
case "home":
|
case "home": return html`<page-home></page-home>`;
|
||||||
return html`<page-home .user=${this.user} .items=${this.items}></page-home>`;
|
case "items": return html`<page-items></page-items>`;
|
||||||
case "items":
|
default: return html`<page-home></page-home>`;
|
||||||
return html`<page-items
|
|
||||||
.items=${this.items}
|
|
||||||
@add-item=${(e) => this.#addItem(e.detail.item)}
|
|
||||||
@remove-item=${(e) => this.#removeItem(e.detail.id)}
|
|
||||||
></page-items>`;
|
|
||||||
default:
|
|
||||||
return html`<page-home .user=${this.user} .items=${this.items}></page-home>`;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
79
components/error-toast.js
Normal file
79
components/error-toast.js
Normal file
|
|
@ -0,0 +1,79 @@
|
||||||
|
// components/error-toast.js
|
||||||
|
import { LitElement, html, css } from 'lit'
|
||||||
|
import { store } from '../store/index.js'
|
||||||
|
import { StoreController } from '../controllers/store-controller.js'
|
||||||
|
|
||||||
|
class ErrorToast extends LitElement {
|
||||||
|
#error = new StoreController(this, store, s => s.error)
|
||||||
|
|
||||||
|
static styles = css`
|
||||||
|
:host {
|
||||||
|
position: fixed;
|
||||||
|
top: 1rem;
|
||||||
|
right: 1rem;
|
||||||
|
z-index: 1000;
|
||||||
|
}
|
||||||
|
|
||||||
|
.toast {
|
||||||
|
background: #ef4444;
|
||||||
|
color: white;
|
||||||
|
padding: 1rem 1.5rem;
|
||||||
|
border-radius: 8px;
|
||||||
|
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 1rem;
|
||||||
|
min-width: 300px;
|
||||||
|
animation: slideIn 0.3s ease-out;
|
||||||
|
}
|
||||||
|
|
||||||
|
@keyframes slideIn {
|
||||||
|
from {
|
||||||
|
transform: translateX(400px);
|
||||||
|
opacity: 0;
|
||||||
|
}
|
||||||
|
to {
|
||||||
|
transform: translateX(0);
|
||||||
|
opacity: 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.message {
|
||||||
|
flex: 1;
|
||||||
|
font-size: 0.9rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.close {
|
||||||
|
background: none;
|
||||||
|
border: none;
|
||||||
|
color: white;
|
||||||
|
cursor: pointer;
|
||||||
|
font-size: 1.2rem;
|
||||||
|
padding: 0;
|
||||||
|
opacity: 0.8;
|
||||||
|
}
|
||||||
|
|
||||||
|
.close:hover {
|
||||||
|
opacity: 1;
|
||||||
|
}
|
||||||
|
`
|
||||||
|
|
||||||
|
render() {
|
||||||
|
if (!this.#error.value) {
|
||||||
|
return html``
|
||||||
|
}
|
||||||
|
|
||||||
|
return html`
|
||||||
|
<div class="toast">
|
||||||
|
<span class="message">${this.#error.value}</span>
|
||||||
|
<button
|
||||||
|
class="close"
|
||||||
|
@click=${() => store.getState().clearError()}
|
||||||
|
aria-label="Close"
|
||||||
|
>×</button>
|
||||||
|
</div>
|
||||||
|
`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
customElements.define('error-toast', ErrorToast)
|
||||||
|
|
@ -1,10 +1,13 @@
|
||||||
// components/nav-bar.js
|
// components/nav-bar.js
|
||||||
import { LitElement, html, css } from 'lit'
|
import { LitElement, html, css } from 'lit'
|
||||||
|
import { store } from '../store/index.js'
|
||||||
|
import { StoreController } from "../controllers/store-controller.js";
|
||||||
|
|
||||||
class NavBar extends LitElement {
|
class NavBar extends LitElement {
|
||||||
|
#route = new StoreController(this, store, s => s.route)
|
||||||
|
|
||||||
static properties = {
|
static properties = {
|
||||||
user: { type: Object },
|
user: { type: Object }
|
||||||
route: { type: String }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static styles = css`
|
static styles = css`
|
||||||
|
|
@ -32,18 +35,14 @@ class NavBar extends LitElement {
|
||||||
|
|
||||||
#navigate(route, e) {
|
#navigate(route, e) {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
this.dispatchEvent(new CustomEvent('navigate', {
|
store.getState().navigate(route)
|
||||||
detail: { route },
|
|
||||||
bubbles: true,
|
|
||||||
composed: true
|
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#link(route, label) {
|
#link(route, label) {
|
||||||
return html`
|
return html`
|
||||||
<a
|
<a
|
||||||
href="/${route}"
|
href="/${route}"
|
||||||
?active=${this.route === route}
|
?active=${this.#route.value === route}
|
||||||
@click=${(e) => this.#navigate(route, e)}
|
@click=${(e) => this.#navigate(route, e)}
|
||||||
>${label}</a>
|
>${label}</a>
|
||||||
`
|
`
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,11 @@
|
||||||
// components/page-home.js
|
// components/page-home.js
|
||||||
import { LitElement, html, css } from 'lit'
|
import { LitElement, html, css } from 'lit'
|
||||||
|
import { store } from '../store/index.js'
|
||||||
|
import { StoreController } from "../controllers/store-controller.js";
|
||||||
|
|
||||||
class PageHome extends LitElement {
|
class PageHome extends LitElement {
|
||||||
static properties = {
|
#user = new StoreController(this, store, s => s.user)
|
||||||
user: { type: Object },
|
#items = new StoreController(this, store, s => s.items)
|
||||||
items: { type: Array }
|
|
||||||
}
|
|
||||||
|
|
||||||
static styles = css`
|
static styles = css`
|
||||||
:host { display: block; padding: 2rem 1.5rem; }
|
:host { display: block; padding: 2rem 1.5rem; }
|
||||||
|
|
@ -25,8 +25,8 @@ class PageHome extends LitElement {
|
||||||
`
|
`
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const name = this.user?.name ?? 'there'
|
const name = this.#user.value?.name ?? 'there'
|
||||||
const count = this.items?.length ?? 0
|
const count = this.#items.value.length
|
||||||
|
|
||||||
return html`
|
return html`
|
||||||
<h1>Hey, ${name}</h1>
|
<h1>Hey, ${name}</h1>
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,16 @@
|
||||||
// components/page-items.js
|
// components/page-items.js
|
||||||
import { LitElement, html, css } from 'lit'
|
import { LitElement, html, css } from 'lit'
|
||||||
|
import { store } from '../store/index.js'
|
||||||
|
import { StoreController } from "../controllers/store-controller.js";
|
||||||
|
|
||||||
class PageItems extends LitElement {
|
class PageItems extends LitElement {
|
||||||
|
#items = new StoreController(this, store, s => s.items)
|
||||||
|
|
||||||
static properties = {
|
static properties = {
|
||||||
items: { type: Array },
|
|
||||||
_draft: { type: String, state: true }
|
_draft: { type: String, state: true }
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor() {
|
_draft = ''
|
||||||
super();
|
|
||||||
this.items = [];
|
|
||||||
this._draft = '';
|
|
||||||
}
|
|
||||||
|
|
||||||
static styles = css`
|
static styles = css`
|
||||||
:host { display: block; padding: 2rem 1.5rem; }
|
:host { display: block; padding: 2rem 1.5rem; }
|
||||||
|
|
@ -64,22 +63,12 @@ class PageItems extends LitElement {
|
||||||
#add() {
|
#add() {
|
||||||
const name = this._draft.trim()
|
const name = this._draft.trim()
|
||||||
if (!name) return
|
if (!name) return
|
||||||
|
store.getState().addItem({ id: crypto.randomUUID(), name })
|
||||||
this.dispatchEvent(new CustomEvent('add-item', {
|
|
||||||
detail: { item: { id: crypto.randomUUID(), name } },
|
|
||||||
bubbles: true,
|
|
||||||
composed: true
|
|
||||||
}))
|
|
||||||
|
|
||||||
this._draft = ''
|
this._draft = ''
|
||||||
}
|
}
|
||||||
|
|
||||||
#remove(id) {
|
#remove(id) {
|
||||||
this.dispatchEvent(new CustomEvent('remove-item', {
|
store.getState().removeItem(id)
|
||||||
detail: { id },
|
|
||||||
bubbles: true,
|
|
||||||
composed: true
|
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#onKeydown(e) {
|
#onKeydown(e) {
|
||||||
|
|
@ -87,7 +76,7 @@ class PageItems extends LitElement {
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const items = this.items ?? []
|
const items = this.#items.value
|
||||||
|
|
||||||
return html`
|
return html`
|
||||||
<h1>Items</h1>
|
<h1>Items</h1>
|
||||||
|
|
|
||||||
23
controllers/store-controller.js
Normal file
23
controllers/store-controller.js
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
export class StoreController {
|
||||||
|
constructor(host, store, selector) {
|
||||||
|
this.host = host
|
||||||
|
this.store = store
|
||||||
|
this.selector = selector
|
||||||
|
host.addController(this)
|
||||||
|
}
|
||||||
|
|
||||||
|
hostConnected() {
|
||||||
|
this._unsub = this.store.subscribe(
|
||||||
|
(state) => {
|
||||||
|
const next = this.selector(state)
|
||||||
|
if (next !== this.value) {
|
||||||
|
this.value = next
|
||||||
|
this.host.requestUpdate()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
this.value = this.selector(this.store.getState())
|
||||||
|
}
|
||||||
|
|
||||||
|
hostDisconnected() { this._unsub?.() }
|
||||||
|
}
|
||||||
|
|
@ -8,7 +8,10 @@
|
||||||
{
|
{
|
||||||
"imports": {
|
"imports": {
|
||||||
"lit": "https://esm.sh/lit@3",
|
"lit": "https://esm.sh/lit@3",
|
||||||
"lit/decorators.js": "https://esm.sh/lit@3/decorators.js"
|
"lit/decorators.js": "https://esm.sh/lit@3/decorators.js",
|
||||||
|
"zustand/vanilla": "https://esm.sh/zustand@5/vanilla",
|
||||||
|
"zustand/middleware": "https://esm.sh/zustand@5/middleware",
|
||||||
|
"idb-keyval": "https://esm.sh/idb-keyval@6"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
|
||||||
33
store/idb.js
Normal file
33
store/idb.js
Normal file
|
|
@ -0,0 +1,33 @@
|
||||||
|
// lib/idb.js
|
||||||
|
import { createStore, get, set, del, entries, clear } from 'idb-keyval'
|
||||||
|
|
||||||
|
// Named stores — each maps to a distinct IDBObjectStore
|
||||||
|
export const itemsStore = createStore('app-db', 'items')
|
||||||
|
export const userStore = createStore('app-db', 'user')
|
||||||
|
export const cacheStore = createStore('app-db', 'cache')
|
||||||
|
|
||||||
|
// Typed wrappers — keeps raw idb-keyval calls out of the rest of the app
|
||||||
|
// and gives you one place to add validation, logging, or migration logic
|
||||||
|
|
||||||
|
export const db = {
|
||||||
|
items: {
|
||||||
|
getAll: () => entries(itemsStore),
|
||||||
|
get: (id) => get(id, itemsStore),
|
||||||
|
set: (id, value) => set(id, value, itemsStore),
|
||||||
|
remove: (id) => del(id, itemsStore),
|
||||||
|
clear: () => clear(itemsStore),
|
||||||
|
},
|
||||||
|
|
||||||
|
user: {
|
||||||
|
get: () => get('user', userStore),
|
||||||
|
set: (value) => set('user', value, userStore),
|
||||||
|
clear: () => del('user', userStore),
|
||||||
|
},
|
||||||
|
|
||||||
|
cache: {
|
||||||
|
get: (key) => get(key, cacheStore),
|
||||||
|
set: (key, value) => set(key, value, cacheStore),
|
||||||
|
remove: (key) => del(key, cacheStore),
|
||||||
|
clear: () => clear(cacheStore),
|
||||||
|
}
|
||||||
|
}
|
||||||
86
store/index.js
Normal file
86
store/index.js
Normal file
|
|
@ -0,0 +1,86 @@
|
||||||
|
import { createStore } from 'zustand/vanilla'
|
||||||
|
import { persist, createJSONStorage } from 'zustand/middleware'
|
||||||
|
import { get, set, del } from 'idb-keyval'
|
||||||
|
|
||||||
|
// Create IndexedDB storage adapter with error handling
|
||||||
|
const storage = createJSONStorage(() => ({
|
||||||
|
getItem: async (name) => {
|
||||||
|
try {
|
||||||
|
const value = await get(name)
|
||||||
|
return value ?? null
|
||||||
|
} catch (error) {
|
||||||
|
console.error('IndexedDB getItem error:', error)
|
||||||
|
return null // Fallback to null if IndexedDB fails
|
||||||
|
}
|
||||||
|
},
|
||||||
|
setItem: async (name, value) => {
|
||||||
|
try {
|
||||||
|
await set(name, value)
|
||||||
|
} catch (error) {
|
||||||
|
console.error('IndexedDB setItem error:', error)
|
||||||
|
// Silently fail - app continues to work without persistence
|
||||||
|
}
|
||||||
|
},
|
||||||
|
removeItem: async (name) => {
|
||||||
|
try {
|
||||||
|
await del(name)
|
||||||
|
} catch (error) {
|
||||||
|
console.error('IndexedDB removeItem error:', error)
|
||||||
|
// Silently fail
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}))
|
||||||
|
|
||||||
|
export const store = createStore(
|
||||||
|
persist(
|
||||||
|
(set, get) => ({
|
||||||
|
_hydrated: true, // Start as true - persistence happens in background
|
||||||
|
user: null,
|
||||||
|
items: [],
|
||||||
|
route: 'home',
|
||||||
|
error: null, // For error notifications
|
||||||
|
|
||||||
|
// Actions
|
||||||
|
setUser: (user) => set({ user }),
|
||||||
|
|
||||||
|
addItem: (item) => {
|
||||||
|
try {
|
||||||
|
if (!item || !item.name || !item.name.trim()) {
|
||||||
|
throw new Error('Item name is required')
|
||||||
|
}
|
||||||
|
set(s => ({ items: [...s.items, item], error: null }))
|
||||||
|
} catch (error) {
|
||||||
|
console.error('addItem error:', error)
|
||||||
|
set({ error: error.message })
|
||||||
|
setTimeout(() => set({ error: null }), 3000) // Clear after 3s
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
removeItem: (id) => {
|
||||||
|
try {
|
||||||
|
if (!id) {
|
||||||
|
throw new Error('Item ID is required')
|
||||||
|
}
|
||||||
|
set(s => ({ items: s.items.filter(i => i.id !== id), error: null }))
|
||||||
|
} catch (error) {
|
||||||
|
console.error('removeItem error:', error)
|
||||||
|
set({ error: error.message })
|
||||||
|
setTimeout(() => set({ error: null }), 3000)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
navigate: (route) => set({ route }),
|
||||||
|
|
||||||
|
clearError: () => set({ error: null }),
|
||||||
|
}),
|
||||||
|
{
|
||||||
|
name: 'app-store',
|
||||||
|
storage,
|
||||||
|
partialize: (state) => ({
|
||||||
|
user: state.user,
|
||||||
|
items: state.items,
|
||||||
|
route: state.route,
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
)
|
||||||
9
store/persistence.js
Normal file
9
store/persistence.js
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
// store/middleware/persistence.js
|
||||||
|
import { db } from './idb.js'
|
||||||
|
|
||||||
|
export const makeIdbStorage = (storeName) =>
|
||||||
|
createJSONStorage(() => ({
|
||||||
|
getItem: (name) => db[storeName].get(name),
|
||||||
|
setItem: (name, value) => db[storeName].set(name, value),
|
||||||
|
removeItem: (name) => db[storeName].remove(name),
|
||||||
|
}))
|
||||||
60
tasks.md
60
tasks.md
|
|
@ -1,60 +0,0 @@
|
||||||
# Tasks & Issues
|
|
||||||
|
|
||||||
## 1. Critical Issues
|
|
||||||
|
|
||||||
- [ ] **1.1 Fix missing `del` import in store/index.js**
|
|
||||||
- Line 3 imports `get, set` from idb-keyval but `del` is used on line 13
|
|
||||||
- Add `del` to the import statement
|
|
||||||
- This will cause a ReferenceError when removeItem is called
|
|
||||||
|
|
||||||
- [ ] **1.2 Fix hydration flag initialization**
|
|
||||||
- The `onRehydrateStorage` callback uses `store.setState()` before `store` is fully initialized
|
|
||||||
- Creates a circular reference issue preventing app from loading
|
|
||||||
- **Options:**
|
|
||||||
- A) Initialize `_hydrated: true` by default (simplest)
|
|
||||||
- B) Use Zustand's persist middleware built-in hydration detection
|
|
||||||
- C) Move hydration callback outside store creation
|
|
||||||
|
|
||||||
- [ ] **1.3 Consolidate duplicate storage implementation**
|
|
||||||
- Storage is defined in both `store/index.js` AND `store/middleware/persistence.js`
|
|
||||||
- The middleware file exports `idbStorage` but it's not being used
|
|
||||||
- **Decision needed:** Use one or the other, not both
|
|
||||||
|
|
||||||
## 2. Architectural Improvements
|
|
||||||
|
|
||||||
- [ ] **2.1 Add error handling**
|
|
||||||
- Add try/catch blocks around IndexedDB operations
|
|
||||||
- Implement fallback if IndexedDB fails or is unavailable
|
|
||||||
- Add user feedback for errors (toast notifications?)
|
|
||||||
|
|
||||||
- [ ] **2.2 Add partialize option to persist middleware**
|
|
||||||
- Currently saves ALL state including `_hydrated` flag and `route`
|
|
||||||
- Should use `partialize: (state) => ({ user: state.user, items: state.items })`
|
|
||||||
- Prevents unnecessary data in IndexedDB
|
|
||||||
|
|
||||||
- [ ] **2.3 Add basic CSS reset to index.html**
|
|
||||||
- No base styles, margins, or font settings currently
|
|
||||||
- Consider adding minimal reset or normalize.css
|
|
||||||
|
|
||||||
- [ ] **2.4 Add loading states for async operations**
|
|
||||||
- No loading indicators for add/remove item operations
|
|
||||||
- Consider adding optimistic updates
|
|
||||||
|
|
||||||
## 3. Nice to Have
|
|
||||||
|
|
||||||
- [ ] **3.1 Add TypeScript types** (optional)
|
|
||||||
- JSDoc comments for better IDE support
|
|
||||||
- Or migrate to .ts files with no-build setup
|
|
||||||
|
|
||||||
- [ ] **3.2 Add cross-tab synchronization**
|
|
||||||
- README mentions optional `sync.js` middleware
|
|
||||||
- Implement BroadcastChannel for cross-tab state sync
|
|
||||||
|
|
||||||
- [ ] **3.3 Add route history management**
|
|
||||||
- Integrate with browser history API
|
|
||||||
- Support back/forward navigation
|
|
||||||
|
|
||||||
- [ ] **3.4 Add unit tests**
|
|
||||||
- Test store actions and selectors
|
|
||||||
- Test component rendering
|
|
||||||
- Test persistence layer
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue