UNPKG

react-storage-drafts

Version:

React Drafts is a library for creating, saving, and managing local documents in React applications.

164 lines (114 loc) โ€ข 4.5 kB
# react-storage-drafts **react-storage-drafts** is a lightweight, flexible React context provider for managing, storing, and syncing local drafts (e.g. offline forms, notes, cached edits) with optional syncing strategies like intervals, connection changes, or manual triggers. --- ## ๐Ÿš€ Features - Store drafts locally in memory - Add, update, and remove drafts - Sync unsynced drafts with external storage - Configurable sync triggers: `onChange`, `interval`, `connection`, or `manual` - Optional integration with online/offline detection (via `navigator.onLine` or React Native `NetInfo`) --- ## ๐Ÿ“ฆ Installation ```bash npm install react-storage-drafts ``` or ```bash yarn add react-storage-drafts ``` --- ## ๐Ÿ”ง Usage ### 1. Wrap your app with the `Provider` ```tsx import { Provider } from 'react-storage-drafts'; <Provider referenceKey="id" syncTrigger="connection" // or 'onChange' | 'interval' | 'manual' syncInterval={60} // optional, in seconds onSync={async (drafts) => { // Sync changed drafts to external storage (e.g. API) // Drafts will have a status property: 'changed' | 'removed' await api.saveDrafts(drafts); }} onLoadStoredData={async () => { return await api.loadDrafts(); }} > <YourApp /> </Provider> ``` --- ### 2. Access drafts with useDrafts hook ```tsx import { useDrafts } from 'react-storage-drafts'; const { drafts, count, addDraft, updateDraft, removeDraft, clearDrafts } = useDrafts(); // Example: addDraft({ id: 'draft-1', title: 'New Draft' }); ``` --- ## ๐Ÿง  API Reference ### `<Provider />` | Prop | Type | Required | Description | |--------------------|----------------------------------|----------|-------------| | `referenceKey` | `string` | โŒ | Key used to identify and manage each draft. If not provided, `_duid` autogenerated property will be used. | | `syncTrigger` | `'onChange' \| 'interval' \| 'connection' \| 'manual'` | โœ… | Determines when to trigger syncing. | | `syncInterval` | `number` | โŒ | Sync interval (in seconds) if `syncTrigger` is `'interval'`. Defaults to `120`. | | `onSync` | `(drafts: any[]) => void \| Promise<void>` | โœ… | Callback to sync unsynced drafts to external storage. Unsyced drafts will have `changed` (new/modified) or `removed` status | | `onLoadStoredData` | `() => Promise<any[]>` | โŒ | Callback to load initial data into the context. | --- ### useDrafts Hook Use `useDrafts()` to access these values: ```ts { drafts: any[]; count: number; addDraft: (draft: any) => void; updateDraft: (key: string | number, changes: any) => void; removeDraft: (key: string | number) => void; clearDrafts: () => void; handleSync: () => void; // Manually trigger sync (if set 'manual' syncTrigger in provider) } ``` ### useDraft Hook Use `useDraft({ draftId: '' })` to manage a single draft: `draftId` is the key of the draft you want to manage. Setting `draftId` to `first` or `last` will return the first or last draft in the list, respectively. ```ts { draft: any; update: (changes: any) => void; remove: () => void; } ``` --- ## ๐Ÿงช Sync Triggers - `onChange`: Syncs immediately after adding/updating/removing a draft. - `interval`: Syncs every `syncInterval` seconds. - `connection`: Syncs automatically when the app goes online. - `manual`: You can call the exposed `syncData` manually (exposing this is coming soon). --- ## ๐ŸŒ Online Detection When using `syncTrigger="connection"`, the provider uses the `useOnline` hook to detect if the app is online. > Web: uses `navigator.onLine` and `online/offline` events > > React Native: uses `@react-native-community/netinfo` --- ## ๐Ÿ“ File Structure ```bash src/ โ”œโ”€โ”€ Provider.tsx # The main context provider โ”œโ”€โ”€ Context.ts # React Context object โ”œโ”€โ”€ useOnline.ts # Hook for online status โ”œโ”€โ”€ useDrafts.ts # Hook for drafts management โ”œโ”€โ”€ types.ts # Type declarations ``` --- ## ๐Ÿ“ License MIT ยฉ [BossBele](https://github.com/BossBele) --- ## ๐Ÿ’ฌ Contributing / Issues PRs and issues are welcome! Open an issue on the [GitHub repo](https://github.com/BossBele/react-drafts/issues). --- ## ๐Ÿ› ๏ธ TODOs - [x] Expose manual sync method - [ ] Add persistent storage with localStorage/AsyncStorage - [ ] Bundle as ESM + CJS for wider compatibility