mirror of
https://github.com/dergigi/boris.git
synced 2026-02-16 20:45:01 +01:00
Compare commits
201 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9ab6847501 | ||
|
|
31afe3792e | ||
|
|
ebe8ecf63b | ||
|
|
c418000a0c | ||
|
|
15fd19f6a4 | ||
|
|
2a44b4e3c0 | ||
|
|
aa7807e3d2 | ||
|
|
359d3d0dd6 | ||
|
|
d40b3c0048 | ||
|
|
7b4ca50b16 | ||
|
|
76e001aba4 | ||
|
|
0b42aeb383 | ||
|
|
a4554e5176 | ||
|
|
2e844fc26b | ||
|
|
8c0a4cac16 | ||
|
|
c6eccc9589 | ||
|
|
2e5536c331 | ||
|
|
fc025b9579 | ||
|
|
88db14c352 | ||
|
|
49c5f0c3ad | ||
|
|
dbed4ad253 | ||
|
|
b117b1e6cf | ||
|
|
627ffd6c5d | ||
|
|
0d53027818 | ||
|
|
811d96dee0 | ||
|
|
21335d56dc | ||
|
|
f7e50023a3 | ||
|
|
6b09212fe9 | ||
|
|
cecff6b8d5 | ||
|
|
2b061afa47 | ||
|
|
7516013e67 | ||
|
|
567641de77 | ||
|
|
4e86907663 | ||
|
|
ec34e00573 | ||
|
|
5e6c8b7516 | ||
|
|
e50af42c96 | ||
|
|
73470987be | ||
|
|
31e203825d | ||
|
|
6f9c0a35e2 | ||
|
|
96f59a54f3 | ||
|
|
87c0a0454b | ||
|
|
77c2ef1794 | ||
|
|
8d08911bd3 | ||
|
|
31b005a989 | ||
|
|
337bfe5432 | ||
|
|
2f275375f7 | ||
|
|
27cbcb56ec | ||
|
|
7f150003b5 | ||
|
|
1f50d8e1b6 | ||
|
|
f53decef16 | ||
|
|
f272943b64 | ||
|
|
49745e1b8a | ||
|
|
470f4fb34e | ||
|
|
8cde36c08c | ||
|
|
c21f96f5bb | ||
|
|
c9fef5804b | ||
|
|
8337622a22 | ||
|
|
572f0fed6f | ||
|
|
27a55ec329 | ||
|
|
7ba362a3bb | ||
|
|
dc1844907e | ||
|
|
28123b5e13 | ||
|
|
d9eb87aa5c | ||
|
|
a0ff0daf9d | ||
|
|
8c3baf1416 | ||
|
|
e0c169edbc | ||
|
|
d2181ad772 | ||
|
|
8ff3f08d8c | ||
|
|
e17e1bc824 | ||
|
|
948674ae8c | ||
|
|
431f14f56d | ||
|
|
4cc9d557a0 | ||
|
|
cc60f9584a | ||
|
|
94f1f9035b | ||
|
|
e5b1594933 | ||
|
|
2bf9b9789b | ||
|
|
d3405a4029 | ||
|
|
763f7bef4d | ||
|
|
e8e629f4e1 | ||
|
|
a0829e834f | ||
|
|
ff938aa384 | ||
|
|
3991bfeeb2 | ||
|
|
e8c35c8914 | ||
|
|
46345c154b | ||
|
|
f43dae92aa | ||
|
|
99c164a5e9 | ||
|
|
569b4357f2 | ||
|
|
de287c625b | ||
|
|
1424f6ebc5 | ||
|
|
b0a368fc64 | ||
|
|
6f8cf641b7 | ||
|
|
23b4c3475f | ||
|
|
5633dc640c | ||
|
|
0f1dfa445a | ||
|
|
ab5225de50 | ||
|
|
b89705cf43 | ||
|
|
740dd53299 | ||
|
|
eb61553c20 | ||
|
|
8b708535ca | ||
|
|
f77761c002 | ||
|
|
b900666eb8 | ||
|
|
2639c78957 | ||
|
|
8320911bc9 | ||
|
|
00d6bd4c46 | ||
|
|
cd377b6f26 | ||
|
|
84b0339505 | ||
|
|
12fa1db0db | ||
|
|
0919091f19 | ||
|
|
e1c04b4e7f | ||
|
|
b9642067a1 | ||
|
|
ceca37df08 | ||
|
|
dfdc5d0946 | ||
|
|
3619cd2585 | ||
|
|
f93e52611e | ||
|
|
ecb81cb151 | ||
|
|
adf73cb9d1 | ||
|
|
4202807777 | ||
|
|
1c21615103 | ||
|
|
732070e89b | ||
|
|
d9a00dd157 | ||
|
|
103be75f6e | ||
|
|
8dd4e358b4 | ||
|
|
2e8dfaee09 | ||
|
|
db3084b373 | ||
|
|
83e4a2ad4c | ||
|
|
c1d23fac7b | ||
|
|
de32310801 | ||
|
|
5c82dff8df | ||
|
|
abe2d6528a | ||
|
|
8b56fe3d6e | ||
|
|
bdce7c9358 | ||
|
|
81a4ae392f | ||
|
|
6e438b8ee2 | ||
|
|
31974e7271 | ||
|
|
676be1a932 | ||
|
|
9883f2eb1a | ||
|
|
87e46be86f | ||
|
|
b745a92a7e | ||
|
|
5a79da4024 | ||
|
|
a7d05a29f5 | ||
|
|
0740d53d37 | ||
|
|
914738abb4 | ||
|
|
4fac5f42c9 | ||
|
|
16b3668e73 | ||
|
|
f3a83256a8 | ||
|
|
0e98ddeef4 | ||
|
|
1ba375e93e | ||
|
|
5d14d25d0e | ||
|
|
616038a23a | ||
|
|
14fce2c3dc | ||
|
|
7c511de474 | ||
|
|
3a10ac8691 | ||
|
|
205879f948 | ||
|
|
bff43f4a28 | ||
|
|
2a7fffd594 | ||
|
|
50a4161e16 | ||
|
|
5fd8976097 | ||
|
|
80b26abff2 | ||
|
|
c0638851c6 | ||
|
|
9b6b14cfe8 | ||
|
|
b6ad62a3ab | ||
|
|
85d87bac29 | ||
|
|
3b31eceeab | ||
|
|
442c138d6a | ||
|
|
61e6027252 | ||
|
|
7d373015b4 | ||
|
|
32b1286079 | ||
|
|
17fdd92827 | ||
|
|
aa6aeb2723 | ||
|
|
4b0f275f57 | ||
|
|
73e2e060e3 | ||
|
|
3007ae83c2 | ||
|
|
a862eb880e | ||
|
|
016e369fb1 | ||
|
|
4f21982c48 | ||
|
|
f6d3fe9aba | ||
|
|
fc60e6b80a | ||
|
|
d9cdbb7279 | ||
|
|
401d333e0f | ||
|
|
d32a47e3c3 | ||
|
|
35efdb6d3f | ||
|
|
c7f7792d73 | ||
|
|
8aa26caae0 | ||
|
|
6c00904bd5 | ||
|
|
23526954ea | ||
|
|
9a437dd97b | ||
|
|
0baf75462c | ||
|
|
30b8f1af92 | ||
|
|
07aea9d35f | ||
|
|
41a4abff37 | ||
|
|
c9998984c3 | ||
|
|
a799709e62 | ||
|
|
18c6c3e68a | ||
|
|
5e7395652f | ||
|
|
83076e7b01 | ||
|
|
c79f4122da | ||
|
|
179fe0bbc2 | ||
|
|
20b4f2b1b2 | ||
|
|
936f9093cf | ||
|
|
3149e5b824 | ||
|
|
8619cecaf3 |
241
CHANGELOG.md
241
CHANGELOG.md
@@ -7,6 +7,239 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
## [0.8.6] - 2025-10-20
|
||||
|
||||
### Fixed
|
||||
|
||||
- React Hooks violations in NostrMentionLink component
|
||||
- Fixed useEffect dependency warnings by removing isMounted from dependencies
|
||||
- Reverted to inline mount tracking with useRef for safer lifecycle handling
|
||||
|
||||
## [0.8.4] - 2024-10-20
|
||||
|
||||
### Added
|
||||
|
||||
- Progressive article hydration for reads tab
|
||||
- Articles now load titles, summaries, images, and author information progressively
|
||||
- Implemented readsController following the same pattern as bookmarkController
|
||||
- Uses AddressLoader for efficient batched article event retrieval
|
||||
- Articles rehydrate as data arrives from relays without blocking initial display
|
||||
- Event store integration for caching article events
|
||||
- Centralized reads data fetching following DRY principles
|
||||
|
||||
### Fixed
|
||||
|
||||
- Fixed React type imports in useArticleLoader
|
||||
- Import `Dispatch` and `SetStateAction` directly from 'react' instead of using `React.` prefix
|
||||
- Resolves ESLint no-undef errors
|
||||
|
||||
## [0.8.3] - 2025-01-19
|
||||
|
||||
### Fixed
|
||||
|
||||
- Highlight creation now shows immediate UI feedback without page refresh
|
||||
- Fixed streaming highlight merge logic to preserve newly created highlights
|
||||
- Decoupled cached highlight sync from content loading to prevent unintended reloads
|
||||
- Newly created highlights appear instantly in both reader and highlights panel
|
||||
- Highlights remain visible while remote results stream in and merge properly
|
||||
|
||||
### Changed
|
||||
|
||||
- Improved highlight creation user experience
|
||||
- Selection clearing and synchronous rendering for immediate highlight display
|
||||
- Better error handling for bunker permission issues with user-friendly messages
|
||||
|
||||
## [0.8.2] - 2025-10-19
|
||||
|
||||
### Added
|
||||
|
||||
- Reading progress indicator in compact bookmark cards
|
||||
- Shows progress bar for articles and web bookmarks with reading data
|
||||
- Progress bar aligned with bookmark text for better visual association
|
||||
- Color-coded progress (blue for reading, green for completed, gray for started)
|
||||
|
||||
### Changed
|
||||
|
||||
- Compact cards layout optimizations for more space-efficient display
|
||||
- Reduced vertical padding from 0.5rem to 0.25rem
|
||||
- Reduced compact row height from 28px to 24px
|
||||
- Reduced gap between compact cards from 0.5rem to 0.25rem
|
||||
- Reading progress bar styling for compact view
|
||||
- Bar height reduced from 2px to 1px for more subtle appearance
|
||||
- Left margin of 1.5rem aligns bar with bookmark text instead of appearing as separator
|
||||
|
||||
### Fixed
|
||||
|
||||
- Removed borders from compact bookmarks in bookmarks sidebar
|
||||
- Border styling from `.bookmarks-list` no longer applies to compact cards
|
||||
- Compact cards now display as truly borderless and transparent
|
||||
|
||||
## [0.8.0] - 2025-10-19
|
||||
|
||||
### Added
|
||||
|
||||
- Centralized reading progress controller for non-blocking reading position sync
|
||||
- Progressive loading with caching from event store
|
||||
- Streaming updates from relays with proper merging
|
||||
- 2-second completion hold at 100% reading position to prevent UI jitter
|
||||
- Configurable auto-mark-as-read at 100% reading progress
|
||||
- Reading progress indicators on blog post cards
|
||||
- Visual progress bars on article cards in Explore and bookmarks sidebar
|
||||
- Persistent reading position synced across devices via NIP-85
|
||||
|
||||
### Changed
|
||||
|
||||
- Reading position sync now enabled by default in runtime paths
|
||||
- Improved auto-mark-as-read behavior with reliable completion detection
|
||||
- Reading progress events use proper NIP-85 specification (kind 39802)
|
||||
|
||||
### Fixed
|
||||
|
||||
- Reading position saves with proper validation and event store integration
|
||||
- Profile page writings loading now fetches all writings without limits
|
||||
- Consistent reading progress calculation and event publishing
|
||||
|
||||
### Performance
|
||||
|
||||
- Non-blocking reading progress controller with streaming updates
|
||||
- Cache-first loading strategy with local event store before relay queries
|
||||
- Efficient progress merging and deduplication
|
||||
|
||||
## [0.7.4] - 2025-10-18
|
||||
|
||||
### Added
|
||||
|
||||
- Profile page data preloading for instant tab switching
|
||||
- Automatically preloads all highlights and writings when viewing a profile (`/p/` pages)
|
||||
- Non-blocking background fetch stores all events in event store
|
||||
- Tab switching becomes instant after initial preload
|
||||
|
||||
### Changed
|
||||
|
||||
- `/me/bookmarks` tab now displays in cards view only
|
||||
- Removed view mode toggle buttons (compact, large) from bookmarks tab
|
||||
- Cards view provides optimal bookmark browsing experience
|
||||
- Grouping toggle (grouped/flat) still available
|
||||
- Highlights sidebar filters simplified when logged out
|
||||
- Only nostrverse filter button shown when not logged in
|
||||
- Friends and personal highlight filters hidden when logged out
|
||||
- Cleaner UX showing only available options
|
||||
|
||||
### Fixed
|
||||
|
||||
- Profile page tabs now display cached content instantly
|
||||
- Highlights and writings show immediately from event store cache
|
||||
- Network fetches happen in background without blocking UI
|
||||
- Matches Explore and Debug page non-blocking loading pattern
|
||||
- Eliminated loading delays when switching between tabs
|
||||
|
||||
### Performance
|
||||
|
||||
- Cache-first profile loading strategy
|
||||
- Instant display of cached highlights and writings from event store
|
||||
- Background refresh updates data without blocking
|
||||
- Tab switches show content immediately without loading states
|
||||
|
||||
## [0.7.3] - 2025-10-18
|
||||
|
||||
### Added
|
||||
|
||||
- Centralized nostrverse writings controller for kind 30023 content
|
||||
- Automatically starts at app initialization
|
||||
- Streams nostrverse blog posts progressively to Explore page
|
||||
- Provides non-blocking, cache-first loading strategy
|
||||
- Centralized nostrverse highlights controller
|
||||
- Pre-loads nostrverse highlights at app start for instant toggling
|
||||
- Streams highlights progressively to Explore page
|
||||
- Integrated with EventStore for caching
|
||||
- Writings loading debug section on `/debug` page
|
||||
- Diagnostics for writings controller and loading states
|
||||
|
||||
### Changed
|
||||
|
||||
- Explore page now uses centralized `writingsController` for user's own writings
|
||||
- Auto-loads user writings at login for instant availability
|
||||
- Non-blocking fetch with progressive streaming
|
||||
- Explore page loading strategy optimized
|
||||
- Shows skeleton placeholders instead of blocking spinners
|
||||
- Seeds from cache, then streams and merges results progressively
|
||||
- Keeps nostrverse fetches non-blocking
|
||||
- User's own writings now included in Explore when enabled
|
||||
- Lazy-loads on 'mine' toggle when logged in
|
||||
- Streams in parallel with friends/nostrverse content
|
||||
|
||||
### Fixed
|
||||
|
||||
- Explore page works correctly in logged-out mode
|
||||
- Relies solely on centralized nostrverse controllers
|
||||
- Controllers start even when logged out
|
||||
- Fetches nostrverse content properly without authentication
|
||||
- Explore page no longer allows disabling all scope filters
|
||||
- Ensures at least one filter (mine/friends/nostrverse) remains active
|
||||
- Prevents blank content state
|
||||
- Explore page reflects default scope setting immediately
|
||||
- No more blank lists on initial load
|
||||
- Pre-loads and merges nostrverse from event store
|
||||
- Explore page highlights properly scoped
|
||||
- Nostrverse highlights never block the page
|
||||
- Shows empty state instead of spinner
|
||||
- Streams results into store immediately
|
||||
- Highlights are merged and loaded correctly
|
||||
- Article-specific highlights properly filtered
|
||||
- Highlights scoped to current article on `/a/` and `/r/` routes
|
||||
- Derives coordinate from naddr for early filtering
|
||||
- Sidebar and content only show relevant highlights
|
||||
- ContentPanel shows only article-specific highlights for nostr articles
|
||||
- Explore writings properly deduplicated
|
||||
- Deduplication by replaceable event (author:d-tag) happens before visibility filtering
|
||||
- Consistent dedupe/sort behavior across all loading scenarios
|
||||
- Debug page writings loading section added
|
||||
- No infinite loop when loading nostrverse content
|
||||
|
||||
### Performance
|
||||
|
||||
- Non-blocking explore page loading
|
||||
- Fully non-blocking loading strategy
|
||||
- Seeds caches then streams and merges results progressively
|
||||
- Lazy-loading for content filters
|
||||
- Nostrverse writings lazy-load when toggled on while logged in
|
||||
- Avoids redundant loading with guard flags
|
||||
- Streaming callbacks for progressive updates
|
||||
- Writings stream to UI via onPost callback
|
||||
- Posts appear instantly as they arrive from cache or network
|
||||
|
||||
## [0.7.2] - 2025-01-27
|
||||
|
||||
### Added
|
||||
|
||||
- Cached-first loading with EventStore across the app
|
||||
- Instant display of cached highlights and writings from local event store
|
||||
- Progressive loading with streaming updates from relays
|
||||
- Centralized event storage for improved performance and offline support
|
||||
- Default explore scope setting for controlling content visibility
|
||||
- Configurable default scope for explore page content
|
||||
- Dedicated Explore section in settings for better organization
|
||||
|
||||
### Changed
|
||||
|
||||
- Highlights and writings now load from cache first, then stream from relays
|
||||
- Explore page shows cached content instantly before network updates
|
||||
- Article-specific highlights stored in centralized event store for faster access
|
||||
- Nostrverse content cached locally for improved performance
|
||||
|
||||
### Fixed
|
||||
|
||||
- Prevent "No highlights yet" flash on `/me/highlights` page
|
||||
- Force React to remount tab content when switching tabs for proper state management
|
||||
- Deduplicate blog posts by author:d-tag instead of event ID for better accuracy
|
||||
- Show skeleton placeholders while highlights are loading for better UX
|
||||
|
||||
### Performance
|
||||
|
||||
- Local-first loading strategy reduces perceived loading times
|
||||
- Cached content displays immediately while background sync occurs
|
||||
- Centralized event storage eliminates redundant network requests
|
||||
|
||||
## [0.7.0] - 2025-10-18
|
||||
|
||||
### Added
|
||||
@@ -1878,7 +2111,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
- Optimize relay usage following applesauce-relay best practices
|
||||
- Use applesauce-react event models for better profile handling
|
||||
|
||||
[Unreleased]: https://github.com/dergigi/boris/compare/v0.7.0...HEAD
|
||||
[Unreleased]: https://github.com/dergigi/boris/compare/v0.8.3...HEAD
|
||||
[0.8.3]: https://github.com/dergigi/boris/compare/v0.8.2...v0.8.3
|
||||
[0.8.2]: https://github.com/dergigi/boris/compare/v0.8.0...v0.8.2
|
||||
[0.8.0]: https://github.com/dergigi/boris/compare/v0.7.4...v0.8.0
|
||||
[0.7.4]: https://github.com/dergigi/boris/compare/v0.7.3...v0.7.4
|
||||
[0.7.3]: https://github.com/dergigi/boris/compare/v0.7.2...v0.7.3
|
||||
[0.7.2]: https://github.com/dergigi/boris/compare/v0.7.0...v0.7.2
|
||||
[0.7.0]: https://github.com/dergigi/boris/compare/v0.6.24...v0.7.0
|
||||
[0.6.24]: https://github.com/dergigi/boris/compare/v0.6.23...v0.6.24
|
||||
[0.6.23]: https://github.com/dergigi/boris/compare/v0.6.22...v0.6.23
|
||||
|
||||
@@ -15,7 +15,6 @@ const RELAYS = [
|
||||
'wss://relay.dergigi.com',
|
||||
'wss://wot.dergigi.com',
|
||||
'wss://relay.snort.social',
|
||||
'wss://relay.current.fyi',
|
||||
'wss://nostr-pub.wellorder.net',
|
||||
'wss://purplepag.es',
|
||||
'wss://relay.primal.net'
|
||||
@@ -215,12 +214,6 @@ export default async function handler(req: VercelRequest, res: VercelResponse) {
|
||||
|
||||
const debugEnabled = req.query.debug === '1' || req.headers['x-boris-debug'] === '1'
|
||||
if (debugEnabled) {
|
||||
console.log('[article-og] request', JSON.stringify({
|
||||
naddr,
|
||||
ua: userAgent || null,
|
||||
isCrawlerRequest,
|
||||
path: req.url || null
|
||||
}))
|
||||
res.setHeader('X-Boris-Debug', '1')
|
||||
}
|
||||
|
||||
@@ -257,7 +250,7 @@ export default async function handler(req: VercelRequest, res: VercelResponse) {
|
||||
res.setHeader('Content-Type', 'text/html; charset=utf-8')
|
||||
res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate')
|
||||
if (debugEnabled) {
|
||||
console.log('[article-og] response', JSON.stringify({ mode: 'browser', naddr }))
|
||||
// Debug mode enabled
|
||||
}
|
||||
return res.status(200).send(html)
|
||||
}
|
||||
@@ -268,7 +261,7 @@ export default async function handler(req: VercelRequest, res: VercelResponse) {
|
||||
if (cached && cached.expires > now) {
|
||||
setCacheHeaders(res)
|
||||
if (debugEnabled) {
|
||||
console.log('[article-og] response', JSON.stringify({ mode: 'bot', naddr, cache: true }))
|
||||
// Debug mode enabled
|
||||
}
|
||||
return res.status(200).send(cached.html)
|
||||
}
|
||||
@@ -286,7 +279,7 @@ export default async function handler(req: VercelRequest, res: VercelResponse) {
|
||||
// Send response
|
||||
setCacheHeaders(res)
|
||||
if (debugEnabled) {
|
||||
console.log('[article-og] response', JSON.stringify({ mode: 'bot', naddr, cache: false }))
|
||||
// Debug mode enabled
|
||||
}
|
||||
return res.status(200).send(html)
|
||||
} catch (err) {
|
||||
@@ -296,7 +289,7 @@ export default async function handler(req: VercelRequest, res: VercelResponse) {
|
||||
const html = generateHtml(naddr, null)
|
||||
setCacheHeaders(res, 3600)
|
||||
if (debugEnabled) {
|
||||
console.log('[article-og] response', JSON.stringify({ mode: 'bot-fallback', naddr }))
|
||||
// Debug mode enabled
|
||||
}
|
||||
return res.status(200).send(html)
|
||||
}
|
||||
|
||||
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "boris",
|
||||
"version": "0.6.13",
|
||||
"version": "0.8.4",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "boris",
|
||||
"version": "0.6.13",
|
||||
"version": "0.8.4",
|
||||
"dependencies": {
|
||||
"@fortawesome/fontawesome-svg-core": "^7.1.0",
|
||||
"@fortawesome/free-regular-svg-icons": "^7.1.0",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "boris",
|
||||
"version": "0.7.2",
|
||||
"version": "0.9.0",
|
||||
"description": "A minimal nostr client for bookmark management",
|
||||
"homepage": "https://read.withboris.com/",
|
||||
"type": "module",
|
||||
|
||||
75
public/md/NIP-85.md
Normal file
75
public/md/NIP-85.md
Normal file
@@ -0,0 +1,75 @@
|
||||
# NIP-85
|
||||
|
||||
## Reading Progress
|
||||
|
||||
`draft` `optional`
|
||||
|
||||
This NIP defines kind `39802`, a parameterized replaceable event for tracking reading progress across articles and web content.
|
||||
|
||||
## Table of Contents
|
||||
|
||||
* [Format](#format)
|
||||
* [Tags](#tags)
|
||||
* [Content](#content)
|
||||
* [Examples](#examples)
|
||||
|
||||
## Format
|
||||
|
||||
Reading progress events use NIP-33 parameterized replaceable semantics. The `d` tag serves as the unique identifier per author and target content.
|
||||
|
||||
### Tags
|
||||
|
||||
Events SHOULD tag the source of the reading progress, whether nostr-native or not. `a` tags should be used for nostr events and `r` tags for URLs.
|
||||
|
||||
When tagging a URL, clients generating these events SHOULD do a best effort of cleaning the URL from trackers or obvious non-useful information from the query string.
|
||||
|
||||
- `d` (required): Unique identifier for the target content
|
||||
- For Nostr articles: `30023:<pubkey>:<identifier>` (matching the article's coordinate)
|
||||
- For external URLs: `url:<base64url-encoded-url>`
|
||||
- `a` (optional but recommended for Nostr articles): Article coordinate `30023:<pubkey>:<identifier>`
|
||||
- `r` (optional but recommended for URLs): Raw URL of the external content
|
||||
|
||||
### Content
|
||||
|
||||
The content is a JSON object with the following fields:
|
||||
|
||||
- `progress` (required): Number between 0 and 1 representing reading progress (0 = not started, 1 = completed)
|
||||
- `loc` (optional): Number representing a location marker (e.g., pixel scroll position, page number, etc.)
|
||||
- `ts` (optional): Unix timestamp (seconds) when the progress was recorded
|
||||
- `ver` (optional): Schema version string
|
||||
|
||||
The latest event by `created_at` per (`pubkey`, `d`) pair is authoritative (NIP-33 semantics).
|
||||
|
||||
Clients SHOULD implement rate limiting to avoid excessive relay traffic (debounce writes, only save significant changes).
|
||||
|
||||
## Examples
|
||||
|
||||
### Nostr Article
|
||||
|
||||
```json
|
||||
{
|
||||
"kind": 39802,
|
||||
"pubkey": "<user-pubkey>",
|
||||
"created_at": 1734635012,
|
||||
"content": "{\"progress\":0.66,\"loc\":1432,\"ts\":1734635012,\"ver\":\"1\"}",
|
||||
"tags": [
|
||||
["d", "30023:<author-pubkey>:<article-identifier>"],
|
||||
["a", "30023:<author-pubkey>:<article-identifier>"]
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
### External URL
|
||||
|
||||
```json
|
||||
{
|
||||
"kind": 39802,
|
||||
"pubkey": "<user-pubkey>",
|
||||
"created_at": 1734635999,
|
||||
"content": "{\"progress\":1,\"ts\":1734635999,\"ver\":\"1\"}",
|
||||
"tags": [
|
||||
["d", "url:aHR0cHM6Ly9leGFtcGxlLmNvbS9wb3N0"],
|
||||
["r", "https://example.com/post"]
|
||||
]
|
||||
}
|
||||
```
|
||||
312
src/App.tsx
312
src/App.tsx
@@ -18,11 +18,18 @@ import { useToast } from './hooks/useToast'
|
||||
import { useOnlineStatus } from './hooks/useOnlineStatus'
|
||||
import { RELAYS } from './config/relays'
|
||||
import { SkeletonThemeProvider } from './components/Skeletons'
|
||||
import { DebugBus } from './utils/debugBus'
|
||||
import { loadUserRelayList, loadBlockedRelays, computeRelaySet } from './services/relayListService'
|
||||
import { applyRelaySetToPool, getActiveRelayUrls, ALWAYS_LOCAL_RELAYS } from './services/relayManager'
|
||||
import { Bookmark } from './types/bookmarks'
|
||||
import { bookmarkController } from './services/bookmarkController'
|
||||
import { contactsController } from './services/contactsController'
|
||||
import { highlightsController } from './services/highlightsController'
|
||||
import { writingsController } from './services/writingsController'
|
||||
import { readingProgressController } from './services/readingProgressController'
|
||||
// import { fetchNostrverseHighlights } from './services/nostrverseService'
|
||||
import { nostrverseHighlightsController } from './services/nostrverseHighlightsController'
|
||||
import { nostrverseWritingsController } from './services/nostrverseWritingsController'
|
||||
import { archiveController } from './services/archiveController'
|
||||
|
||||
const DEFAULT_ARTICLE = import.meta.env.VITE_DEFAULT_ARTICLE_NADDR ||
|
||||
'naddr1qvzqqqr4gupzqmjxss3dld622uu8q25gywum9qtg4w4cv4064jmg20xsac2aam5nqqxnzd3cxqmrzv3exgmr2wfesgsmew'
|
||||
@@ -50,18 +57,14 @@ function AppRoutes({
|
||||
|
||||
// Subscribe to bookmark controller
|
||||
useEffect(() => {
|
||||
console.log('[bookmark] 🎧 Subscribing to bookmark controller')
|
||||
const unsubBookmarks = bookmarkController.onBookmarks((bookmarks) => {
|
||||
console.log('[bookmark] 📥 Received bookmarks:', bookmarks.length)
|
||||
setBookmarks(bookmarks)
|
||||
})
|
||||
const unsubLoading = bookmarkController.onLoading((loading) => {
|
||||
console.log('[bookmark] 📥 Loading state:', loading)
|
||||
setBookmarksLoading(loading)
|
||||
})
|
||||
|
||||
return () => {
|
||||
console.log('[bookmark] 🔇 Unsubscribing from bookmark controller')
|
||||
unsubBookmarks()
|
||||
unsubLoading()
|
||||
}
|
||||
@@ -69,18 +72,14 @@ function AppRoutes({
|
||||
|
||||
// Subscribe to contacts controller
|
||||
useEffect(() => {
|
||||
console.log('[contacts] 🎧 Subscribing to contacts controller')
|
||||
const unsubContacts = contactsController.onContacts((contacts) => {
|
||||
console.log('[contacts] 📥 Received contacts:', contacts.size)
|
||||
setContacts(contacts)
|
||||
})
|
||||
const unsubLoading = contactsController.onLoading((loading) => {
|
||||
console.log('[contacts] 📥 Loading state:', loading)
|
||||
setContactsLoading(loading)
|
||||
})
|
||||
|
||||
return () => {
|
||||
console.log('[contacts] 🔇 Unsubscribing from contacts controller')
|
||||
unsubContacts()
|
||||
unsubLoading()
|
||||
}
|
||||
@@ -94,31 +93,55 @@ function AppRoutes({
|
||||
|
||||
// Load bookmarks
|
||||
if (bookmarks.length === 0 && !bookmarksLoading) {
|
||||
console.log('[bookmark] 🚀 Auto-loading bookmarks on mount/login')
|
||||
bookmarkController.start({ relayPool, activeAccount, accountManager })
|
||||
}
|
||||
|
||||
// Load contacts
|
||||
if (pubkey && contacts.size === 0 && !contactsLoading) {
|
||||
console.log('[contacts] 🚀 Auto-loading contacts on mount/login')
|
||||
contactsController.start({ relayPool, pubkey })
|
||||
}
|
||||
|
||||
// Load highlights (controller manages its own state)
|
||||
if (pubkey && eventStore && !highlightsController.isLoadedFor(pubkey)) {
|
||||
console.log('[highlights] 🚀 Auto-loading highlights on mount/login')
|
||||
highlightsController.start({ relayPool, eventStore, pubkey })
|
||||
}
|
||||
|
||||
// Load writings (controller manages its own state)
|
||||
if (pubkey && eventStore && !writingsController.isLoadedFor(pubkey)) {
|
||||
writingsController.start({ relayPool, eventStore, pubkey })
|
||||
}
|
||||
|
||||
// Load reading progress (controller manages its own state)
|
||||
if (pubkey && eventStore && !readingProgressController.isLoadedFor(pubkey)) {
|
||||
readingProgressController.start({ relayPool, eventStore, pubkey })
|
||||
}
|
||||
|
||||
// Load archive (marked-as-read) controller
|
||||
if (pubkey && eventStore && !archiveController.isLoadedFor(pubkey)) {
|
||||
archiveController.start({ relayPool, eventStore, pubkey })
|
||||
}
|
||||
|
||||
// Start centralized nostrverse highlights controller (non-blocking)
|
||||
if (eventStore) {
|
||||
nostrverseHighlightsController.start({ relayPool, eventStore })
|
||||
nostrverseWritingsController.start({ relayPool, eventStore })
|
||||
}
|
||||
}
|
||||
}, [activeAccount, relayPool, eventStore, bookmarks.length, bookmarksLoading, contacts.size, contactsLoading, accountManager])
|
||||
|
||||
// Ensure nostrverse controllers run even when logged out
|
||||
useEffect(() => {
|
||||
if (relayPool && eventStore) {
|
||||
nostrverseHighlightsController.start({ relayPool, eventStore })
|
||||
nostrverseWritingsController.start({ relayPool, eventStore })
|
||||
}
|
||||
}, [relayPool, eventStore])
|
||||
|
||||
// Manual refresh (for sidebar button)
|
||||
const handleRefreshBookmarks = useCallback(async () => {
|
||||
if (!relayPool || !activeAccount) {
|
||||
console.warn('[bookmark] Cannot refresh: missing relayPool or activeAccount')
|
||||
return
|
||||
}
|
||||
console.log('[bookmark] 🔄 Manual refresh triggered')
|
||||
bookmarkController.reset()
|
||||
await bookmarkController.start({ relayPool, activeAccount, accountManager })
|
||||
}, [relayPool, activeAccount, accountManager])
|
||||
@@ -128,6 +151,8 @@ function AppRoutes({
|
||||
bookmarkController.reset() // Clear bookmarks via controller
|
||||
contactsController.reset() // Clear contacts via controller
|
||||
highlightsController.reset() // Clear highlights via controller
|
||||
readingProgressController.reset() // Clear reading progress via controller
|
||||
archiveController.reset() // Clear archive state
|
||||
showToast('Logged out successfully')
|
||||
}
|
||||
|
||||
@@ -269,6 +294,18 @@ function AppRoutes({
|
||||
/>
|
||||
}
|
||||
/>
|
||||
<Route
|
||||
path="/me/links/:filter"
|
||||
element={
|
||||
<Bookmarks
|
||||
relayPool={relayPool}
|
||||
onLogout={handleLogout}
|
||||
bookmarks={bookmarks}
|
||||
bookmarksLoading={bookmarksLoading}
|
||||
onRefreshBookmarks={handleRefreshBookmarks}
|
||||
/>
|
||||
}
|
||||
/>
|
||||
<Route
|
||||
path="/me/writings"
|
||||
element={
|
||||
@@ -361,43 +398,32 @@ function App() {
|
||||
// Return an already-resolved promise so upstream await finishes immediately
|
||||
return Promise.resolve()
|
||||
}
|
||||
console.log('[bunker] ✅ Wired NostrConnectSigner to RelayPool publish/subscription (before account load)')
|
||||
|
||||
// Create a relay group for better event deduplication and management
|
||||
pool.group(RELAYS)
|
||||
console.log('[bunker] Created relay group with', RELAYS.length, 'relays (including local)')
|
||||
console.log('[relay-init] Initial pool setup - added RELAYS:', RELAYS.length, 'relays')
|
||||
console.log('[relay-init] Pool now has:', Array.from(pool.relays.keys()).length, 'relays')
|
||||
|
||||
// Load persisted accounts from localStorage
|
||||
try {
|
||||
const accountsJson = localStorage.getItem('accounts')
|
||||
console.log('[bunker] Raw accounts from localStorage:', accountsJson)
|
||||
|
||||
const json = JSON.parse(accountsJson || '[]')
|
||||
console.log('[bunker] Parsed accounts:', json.length, 'accounts')
|
||||
|
||||
await accounts.fromJSON(json)
|
||||
console.log('[bunker] Loaded', accounts.accounts.length, 'accounts from storage')
|
||||
console.log('[bunker] Account types:', accounts.accounts.map(a => ({ id: a.id, type: a.type })))
|
||||
|
||||
// Load active account from storage
|
||||
const activeId = localStorage.getItem('active')
|
||||
console.log('[bunker] Active ID from localStorage:', activeId)
|
||||
|
||||
if (activeId) {
|
||||
const account = accounts.getAccount(activeId)
|
||||
console.log('[bunker] Found account for ID?', !!account, account?.type)
|
||||
|
||||
if (account) {
|
||||
accounts.setActive(activeId)
|
||||
console.log('[bunker] ✅ Restored active account:', activeId, 'type:', account.type)
|
||||
} else {
|
||||
console.warn('[bunker] ⚠️ Active ID found but account not in list')
|
||||
}
|
||||
} else {
|
||||
console.log('[bunker] No active account ID in localStorage')
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('[bunker] ❌ Failed to load accounts from storage:', err)
|
||||
console.error('Failed to load accounts from storage:', err)
|
||||
}
|
||||
|
||||
// Subscribe to accounts changes and persist to localStorage
|
||||
@@ -419,11 +445,6 @@ function App() {
|
||||
const reconnectedAccounts = new Set<string>()
|
||||
|
||||
const bunkerReconnectSub = accounts.active$.subscribe(async (account) => {
|
||||
console.log('[bunker] Active account changed:', {
|
||||
hasAccount: !!account,
|
||||
type: account?.type,
|
||||
id: account?.id
|
||||
})
|
||||
|
||||
if (account && account.type === 'nostr-connect') {
|
||||
const nostrConnectAccount = account as Accounts.NostrConnectAccount<unknown>
|
||||
@@ -431,23 +452,17 @@ function App() {
|
||||
try {
|
||||
if (!(nostrConnectAccount as unknown as { disableQueue?: boolean }).disableQueue) {
|
||||
(nostrConnectAccount as unknown as { disableQueue?: boolean }).disableQueue = true
|
||||
console.log('[bunker] ⚙️ Disabled account request queueing for nostr-connect')
|
||||
}
|
||||
} catch (err) { console.warn('[bunker] failed to disable queue', err) }
|
||||
} catch (err) {
|
||||
// Ignore queue disable errors
|
||||
}
|
||||
// Note: for Amber bunker, the remote signer pubkey is the user's pubkey. This is expected.
|
||||
|
||||
// Skip if we've already reconnected this account
|
||||
if (reconnectedAccounts.has(account.id)) {
|
||||
console.log('[bunker] ⏭️ Already reconnected this account, skipping')
|
||||
return
|
||||
}
|
||||
|
||||
console.log('[bunker] Account detected. Status:', {
|
||||
listening: nostrConnectAccount.signer.listening,
|
||||
isConnected: nostrConnectAccount.signer.isConnected,
|
||||
hasRemote: !!nostrConnectAccount.signer.remote,
|
||||
bunkerRelays: nostrConnectAccount.signer.relays
|
||||
})
|
||||
|
||||
try {
|
||||
// For restored signers, ensure they have the pool's subscription methods
|
||||
@@ -461,10 +476,9 @@ function App() {
|
||||
const newBunkerRelays = bunkerRelays.filter(url => !existingRelayUrls.has(url))
|
||||
|
||||
if (newBunkerRelays.length > 0) {
|
||||
console.log('[bunker] Adding bunker relays to pool BEFORE signer recreation:', newBunkerRelays)
|
||||
pool.group(newBunkerRelays)
|
||||
} else {
|
||||
console.log('[bunker] Bunker relays already in pool')
|
||||
// Bunker relays already in pool
|
||||
}
|
||||
|
||||
const recreatedSigner = new NostrConnectSigner({
|
||||
@@ -478,85 +492,42 @@ function App() {
|
||||
try {
|
||||
const mergedRelays = Array.from(new Set([...(signerData.relays || []), ...RELAYS]))
|
||||
recreatedSigner.relays = mergedRelays
|
||||
console.log('[bunker] 🔗 Signer relays merged with app RELAYS:', mergedRelays)
|
||||
} catch (err) { console.warn('[bunker] failed to merge signer relays', err) }
|
||||
} catch (err) { /* ignore */ }
|
||||
|
||||
// Replace the signer on the account
|
||||
nostrConnectAccount.signer = recreatedSigner
|
||||
console.log('[bunker] ✅ Signer recreated with pool context')
|
||||
|
||||
// Debug: log publish/subscription calls made by signer (decrypt/sign requests)
|
||||
|
||||
// Fire-and-forget publish for bunker: trigger but don't wait for completion
|
||||
// IMPORTANT: bind originals to preserve `this` context used internally by the signer
|
||||
const originalPublish = (recreatedSigner as unknown as { publishMethod: (relays: string[], event: unknown) => unknown }).publishMethod.bind(recreatedSigner)
|
||||
;(recreatedSigner as unknown as { publishMethod: (relays: string[], event: unknown) => unknown }).publishMethod = (relays: string[], event: unknown) => {
|
||||
try {
|
||||
let method: string | undefined
|
||||
const content = (event as { content?: unknown })?.content
|
||||
if (typeof content === 'string') {
|
||||
try {
|
||||
const parsed = JSON.parse(content) as { method?: string; id?: unknown }
|
||||
method = parsed?.method
|
||||
} catch (err) { console.warn('[bunker] failed to parse event content', err) }
|
||||
}
|
||||
const summary = {
|
||||
relays,
|
||||
kind: (event as { kind?: number })?.kind,
|
||||
method,
|
||||
// include tags array for debugging (NIP-46 expects method tag)
|
||||
tags: (event as { tags?: unknown })?.tags,
|
||||
contentLength: typeof content === 'string' ? content.length : undefined
|
||||
}
|
||||
console.log('[bunker] publish via signer:', summary)
|
||||
try { DebugBus.info('bunker', 'publish', summary) } catch (err) { console.warn('[bunker] failed to log to DebugBus', err) }
|
||||
} catch (err) { console.warn('[bunker] failed to log publish summary', err) }
|
||||
// Fire-and-forget publish: trigger the publish but do not return the
|
||||
// Observable/Promise to upstream to avoid their awaiting of completion.
|
||||
const result = originalPublish(relays, event)
|
||||
if (result && typeof (result as { subscribe?: unknown }).subscribe === 'function') {
|
||||
// Subscribe to the observable but ignore completion/errors (fire-and-forget)
|
||||
try { (result as { subscribe: (h: { complete?: () => void; error?: (e: unknown) => void }) => unknown }).subscribe({ complete: () => { /* noop */ }, error: () => { /* noop */ } }) } catch { /* ignore */ }
|
||||
}
|
||||
// If it's a Promise, simply ignore it (no await) so it resolves in the background.
|
||||
// Return a benign object so callers that probe for a "subscribe" property
|
||||
// (e.g., applesauce makeRequest) won't throw on `"subscribe" in result`.
|
||||
return {} as unknown as never
|
||||
}
|
||||
const originalSubscribe = (recreatedSigner as unknown as { subscriptionMethod: (relays: string[], filters: unknown[]) => unknown }).subscriptionMethod.bind(recreatedSigner)
|
||||
;(recreatedSigner as unknown as { subscriptionMethod: (relays: string[], filters: unknown[]) => unknown }).subscriptionMethod = (relays: string[], filters: unknown[]) => {
|
||||
try {
|
||||
console.log('[bunker] subscribe via signer:', { relays, filters })
|
||||
try { DebugBus.info('bunker', 'subscribe', { relays, filters }) } catch (err) { console.warn('[bunker] failed to log subscribe to DebugBus', err) }
|
||||
} catch (err) { console.warn('[bunker] failed to log subscribe summary', err) }
|
||||
return originalSubscribe(relays, filters)
|
||||
}
|
||||
|
||||
|
||||
// Just ensure the signer is listening for responses - don't call connect() again
|
||||
// The fromBunkerURI already connected with permissions during login
|
||||
if (!nostrConnectAccount.signer.listening) {
|
||||
console.log('[bunker] Opening signer subscription...')
|
||||
await nostrConnectAccount.signer.open()
|
||||
console.log('[bunker] ✅ Signer subscription opened')
|
||||
} else {
|
||||
console.log('[bunker] ✅ Signer already listening')
|
||||
}
|
||||
|
||||
// Attempt a guarded reconnect to ensure Amber authorizes decrypt operations
|
||||
try {
|
||||
if (nostrConnectAccount.signer.remote && !reconnectedAccounts.has(account.id)) {
|
||||
const permissions = getDefaultBunkerPermissions()
|
||||
console.log('[bunker] Attempting guarded connect() with permissions to ensure decrypt perms', { count: permissions.length })
|
||||
await nostrConnectAccount.signer.connect(undefined, permissions)
|
||||
console.log('[bunker] ✅ Guarded connect() succeeded with permissions')
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('[bunker] ⚠️ Guarded connect() failed:', e)
|
||||
// Ignore reconnect errors
|
||||
}
|
||||
|
||||
// Give the subscription a moment to fully establish before allowing decrypt operations
|
||||
// This ensures the signer is ready to handle and receive responses
|
||||
await new Promise(resolve => setTimeout(resolve, 100))
|
||||
console.log("[bunker] Subscription ready after startup delay")
|
||||
// Fire-and-forget: probe decrypt path to verify Amber responds to NIP-46 decrypt
|
||||
try {
|
||||
const withTimeout = async <T,>(p: Promise<T>, ms = 10000): Promise<T> => {
|
||||
@@ -569,52 +540,160 @@ function App() {
|
||||
const self = nostrConnectAccount.pubkey
|
||||
// Try a roundtrip so the bunker can respond successfully
|
||||
try {
|
||||
console.log('[bunker] 🔎 Probe nip44 roundtrip (encrypt→decrypt)…')
|
||||
const cipher44 = await withTimeout(nostrConnectAccount.signer.nip44!.encrypt(self, 'probe-nip44'))
|
||||
const plain44 = await withTimeout(nostrConnectAccount.signer.nip44!.decrypt(self, cipher44))
|
||||
console.log('[bunker] 🔎 Probe nip44 responded:', typeof plain44 === 'string' ? plain44 : typeof plain44)
|
||||
} catch (err) {
|
||||
console.log('[bunker] 🔎 Probe nip44 result:', err instanceof Error ? err.message : err)
|
||||
await withTimeout(nostrConnectAccount.signer.nip44!.encrypt(self, 'probe-nip44'))
|
||||
await withTimeout(nostrConnectAccount.signer.nip44!.decrypt(self, ''))
|
||||
} catch (_err) {
|
||||
// Ignore probe errors
|
||||
}
|
||||
try {
|
||||
console.log('[bunker] 🔎 Probe nip04 roundtrip (encrypt→decrypt)…')
|
||||
const cipher04 = await withTimeout(nostrConnectAccount.signer.nip04!.encrypt(self, 'probe-nip04'))
|
||||
const plain04 = await withTimeout(nostrConnectAccount.signer.nip04!.decrypt(self, cipher04))
|
||||
console.log('[bunker] 🔎 Probe nip04 responded:', typeof plain04 === 'string' ? plain04 : typeof plain04)
|
||||
} catch (err) {
|
||||
console.log('[bunker] 🔎 Probe nip04 result:', err instanceof Error ? err.message : err)
|
||||
await withTimeout(nostrConnectAccount.signer.nip04!.encrypt(self, 'probe-nip04'))
|
||||
await withTimeout(nostrConnectAccount.signer.nip04!.decrypt(self, ''))
|
||||
} catch (_err) {
|
||||
// Ignore probe errors
|
||||
}
|
||||
}, 0)
|
||||
} catch (err) {
|
||||
console.log('[bunker] 🔎 Probe setup failed:', err)
|
||||
} catch (_err) {
|
||||
// Ignore signer setup errors
|
||||
}
|
||||
// The bunker remembers the permissions from the initial connection
|
||||
nostrConnectAccount.signer.isConnected = true
|
||||
|
||||
console.log('[bunker] Final signer status:', {
|
||||
listening: nostrConnectAccount.signer.listening,
|
||||
isConnected: nostrConnectAccount.signer.isConnected,
|
||||
remote: nostrConnectAccount.signer.remote,
|
||||
relays: nostrConnectAccount.signer.relays
|
||||
})
|
||||
|
||||
// Mark this account as reconnected
|
||||
reconnectedAccounts.add(account.id)
|
||||
console.log('[bunker] 🎉 Signer ready for signing')
|
||||
} catch (error) {
|
||||
console.error('[bunker] ❌ Failed to open signer:', error)
|
||||
console.error('Failed to open signer:', error)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
// Handle user relay list and blocked relays when account changes
|
||||
const userRelaysSub = accounts.active$.subscribe((account) => {
|
||||
console.log('[relay-init] userRelaysSub fired, account:', account ? 'logged in' : 'logged out')
|
||||
console.log('[relay-init] Pool has', Array.from(pool.relays.keys()).length, 'relays before applying changes')
|
||||
if (account) {
|
||||
// User logged in - start with hardcoded relays immediately, then stream user relay list updates
|
||||
const pubkey = account.pubkey
|
||||
|
||||
// Bunker relays (if any)
|
||||
let bunkerRelays: string[] = []
|
||||
if (account.type === 'nostr-connect') {
|
||||
const nostrConnectAccount = account as Accounts.NostrConnectAccount<unknown>
|
||||
const signerData = nostrConnectAccount.toJSON().signer
|
||||
bunkerRelays = signerData.relays || []
|
||||
}
|
||||
console.log('[relay-init] Bunker relays:', bunkerRelays.length, 'relays', bunkerRelays)
|
||||
|
||||
// Start with hardcoded + bunker relays immediately (non-blocking)
|
||||
const initialRelays = computeRelaySet({
|
||||
hardcoded: RELAYS,
|
||||
bunker: bunkerRelays,
|
||||
userList: [],
|
||||
blocked: [],
|
||||
alwaysIncludeLocal: ALWAYS_LOCAL_RELAYS
|
||||
})
|
||||
console.log('[relay-init] Initial relay set (hardcoded):', initialRelays.length, 'relays', initialRelays)
|
||||
|
||||
// Apply initial set immediately
|
||||
applyRelaySetToPool(pool, initialRelays)
|
||||
console.log('[relay-init] After initial applyRelaySetToPool, pool has:', Array.from(pool.relays.keys()).length, 'relays')
|
||||
|
||||
// Prepare keep-alive helper
|
||||
const updateKeepAlive = () => {
|
||||
const poolWithSub = pool as unknown as { _keepAliveSubscription?: { unsubscribe: () => void } }
|
||||
if (poolWithSub._keepAliveSubscription) {
|
||||
poolWithSub._keepAliveSubscription.unsubscribe()
|
||||
}
|
||||
const activeRelays = getActiveRelayUrls(pool)
|
||||
const newKeepAliveSub = pool.subscription(activeRelays, { kinds: [0], limit: 0 }).subscribe({
|
||||
next: () => {},
|
||||
error: () => {}
|
||||
})
|
||||
poolWithSub._keepAliveSubscription = newKeepAliveSub
|
||||
}
|
||||
|
||||
// Begin loading blocked relays in background
|
||||
const blockedPromise = loadBlockedRelays(pool, pubkey)
|
||||
|
||||
// Stream user relay list; apply immediately on first/updated event
|
||||
loadUserRelayList(pool, pubkey, {
|
||||
onUpdate: (userRelays) => {
|
||||
const interimRelays = computeRelaySet({
|
||||
hardcoded: [],
|
||||
bunker: bunkerRelays,
|
||||
userList: userRelays,
|
||||
blocked: [],
|
||||
alwaysIncludeLocal: ALWAYS_LOCAL_RELAYS
|
||||
})
|
||||
console.log('[relay-init] Interim relay set from first user list:', interimRelays.length, 'relays', interimRelays)
|
||||
applyRelaySetToPool(pool, interimRelays)
|
||||
updateKeepAlive()
|
||||
}
|
||||
}).then(async (userRelayList) => {
|
||||
const blockedRelays = await blockedPromise.catch(() => [])
|
||||
console.log('[relay-init] User relay list (10002):', userRelayList.length, 'relays', userRelayList.map(r => r.url))
|
||||
console.log('[relay-init] Blocked relays (10006):', blockedRelays.length, 'relays', blockedRelays)
|
||||
|
||||
const finalRelays = computeRelaySet({
|
||||
hardcoded: userRelayList.length > 0 ? [] : RELAYS,
|
||||
bunker: bunkerRelays,
|
||||
userList: userRelayList,
|
||||
blocked: blockedRelays,
|
||||
alwaysIncludeLocal: ALWAYS_LOCAL_RELAYS
|
||||
})
|
||||
console.log('[relay-init] Final relay set (with user preferences):', finalRelays.length, 'relays', finalRelays)
|
||||
applyRelaySetToPool(pool, finalRelays)
|
||||
console.log('[relay-init] After user relay list apply, pool has:', Array.from(pool.relays.keys()).length, 'relays')
|
||||
console.log('[relay-init] Final relay URLs:', Array.from(pool.relays.keys()))
|
||||
updateKeepAlive()
|
||||
|
||||
// Update address loader with new relays
|
||||
const activeRelays = getActiveRelayUrls(pool)
|
||||
const addressLoader = createAddressLoader(pool, {
|
||||
eventStore: store,
|
||||
lookupRelays: activeRelays
|
||||
})
|
||||
store.addressableLoader = addressLoader
|
||||
store.replaceableLoader = addressLoader
|
||||
}).catch((error) => {
|
||||
console.error('[relay-init] Failed to load user relay list (continuing with initial set):', error)
|
||||
// Continue with initial relay set on error - no need to change anything
|
||||
})
|
||||
} else {
|
||||
// User logged out - reset to hardcoded relays
|
||||
console.log('[relay-init] Applying RELAYS for logged out user, RELAYS.length:', RELAYS.length)
|
||||
applyRelaySetToPool(pool, RELAYS)
|
||||
console.log('[relay-init] After applyRelaySetToPool (logged out), pool has:', Array.from(pool.relays.keys()).length, 'relays')
|
||||
console.log('[relay-init] Relay URLs:', Array.from(pool.relays.keys()))
|
||||
|
||||
// Update keep-alive subscription
|
||||
const poolWithSub = pool as unknown as { _keepAliveSubscription?: { unsubscribe: () => void } }
|
||||
if (poolWithSub._keepAliveSubscription) {
|
||||
poolWithSub._keepAliveSubscription.unsubscribe()
|
||||
}
|
||||
const newKeepAliveSub = pool.subscription(RELAYS, { kinds: [0], limit: 0 }).subscribe({
|
||||
next: () => {},
|
||||
error: () => {}
|
||||
})
|
||||
poolWithSub._keepAliveSubscription = newKeepAliveSub
|
||||
|
||||
// Reset address loader
|
||||
const addressLoader = createAddressLoader(pool, {
|
||||
eventStore: store,
|
||||
lookupRelays: RELAYS
|
||||
})
|
||||
store.addressableLoader = addressLoader
|
||||
store.replaceableLoader = addressLoader
|
||||
}
|
||||
})
|
||||
|
||||
// Keep all relay connections alive indefinitely by creating a persistent subscription
|
||||
// This prevents disconnection when no other subscriptions are active
|
||||
// Create a minimal subscription that never completes to keep connections alive
|
||||
const keepAliveSub = pool.subscription(RELAYS, { kinds: [0], limit: 0 }).subscribe({
|
||||
next: () => {}, // No-op, we don't care about events
|
||||
error: (err) => console.warn('Keep-alive subscription error:', err)
|
||||
next: () => {},
|
||||
error: () => {}
|
||||
})
|
||||
console.log('🔗 Created keep-alive subscription for', RELAYS.length, 'relay(s)')
|
||||
|
||||
// Store subscription for cleanup
|
||||
;(pool as unknown as { _keepAliveSubscription: typeof keepAliveSub })._keepAliveSubscription = keepAliveSub
|
||||
@@ -636,6 +715,7 @@ function App() {
|
||||
accountsSub.unsubscribe()
|
||||
activeSub.unsubscribe()
|
||||
bunkerReconnectSub.unsubscribe()
|
||||
userRelaysSub.unsubscribe()
|
||||
// Clean up keep-alive subscription if it exists
|
||||
const poolWithSub = pool as unknown as { _keepAliveSubscription?: { unsubscribe: () => void } }
|
||||
if (poolWithSub._keepAliveSubscription) {
|
||||
|
||||
@@ -16,7 +16,7 @@ const ArchiveFilters: React.FC<ArchiveFiltersProps> = ({ selectedFilter, onFilte
|
||||
{ type: 'to-read' as const, icon: faBookmark, label: 'To Read' },
|
||||
{ type: 'reading' as const, icon: faBookOpen, label: 'Reading' },
|
||||
{ type: 'completed' as const, icon: faCheckCircle, label: 'Completed' },
|
||||
{ type: 'marked' as const, icon: faBooks, label: 'Marked as Read' }
|
||||
{ type: 'marked' as const, icon: faBooks, label: 'Archived' }
|
||||
]
|
||||
|
||||
return (
|
||||
|
||||
@@ -33,6 +33,11 @@ const BlogPostCard: React.FC<BlogPostCardProps> = ({ post, href, level, readingP
|
||||
} else if (readingProgress && readingProgress > 0 && readingProgress <= 0.10) {
|
||||
progressColor = 'var(--color-text)' // Neutral text color (started)
|
||||
}
|
||||
|
||||
// Debug log - reading progress shown as visual indicator
|
||||
if (readingProgress !== undefined) {
|
||||
// Reading progress display
|
||||
}
|
||||
|
||||
return (
|
||||
<Link
|
||||
|
||||
@@ -19,9 +19,10 @@ interface BookmarkItemProps {
|
||||
index: number
|
||||
onSelectUrl?: (url: string, bookmark?: { id: string; kind: number; tags: string[][]; pubkey: string }) => void
|
||||
viewMode?: ViewMode
|
||||
readingProgress?: number
|
||||
}
|
||||
|
||||
export const BookmarkItem: React.FC<BookmarkItemProps> = ({ bookmark, index, onSelectUrl, viewMode = 'cards' }) => {
|
||||
export const BookmarkItem: React.FC<BookmarkItemProps> = ({ bookmark, index, onSelectUrl, viewMode = 'cards', readingProgress }) => {
|
||||
const [ogImage, setOgImage] = useState<string | null>(null)
|
||||
|
||||
const short = (v: string) => `${v.slice(0, 8)}...${v.slice(-8)}`
|
||||
@@ -139,7 +140,8 @@ export const BookmarkItem: React.FC<BookmarkItemProps> = ({ bookmark, index, onS
|
||||
handleReadNow,
|
||||
articleImage,
|
||||
articleSummary,
|
||||
contentTypeIcon: getContentTypeIcon()
|
||||
contentTypeIcon: getContentTypeIcon(),
|
||||
readingProgress
|
||||
}
|
||||
|
||||
if (viewMode === 'compact') {
|
||||
|
||||
@@ -13,15 +13,19 @@ import { ViewMode } from './Bookmarks'
|
||||
import { usePullToRefresh } from 'use-pull-to-refresh'
|
||||
import RefreshIndicator from './RefreshIndicator'
|
||||
import { BookmarkSkeleton } from './Skeletons'
|
||||
import { groupIndividualBookmarks, hasContent, getBookmarkSets, getBookmarksWithoutSet } from '../utils/bookmarkUtils'
|
||||
import { groupIndividualBookmarks, hasContent, getBookmarkSets, getBookmarksWithoutSet, hasCreationDate } from '../utils/bookmarkUtils'
|
||||
import { UserSettings } from '../services/settingsService'
|
||||
import AddBookmarkModal from './AddBookmarkModal'
|
||||
import { createWebBookmark } from '../services/webBookmarkService'
|
||||
import { RELAYS } from '../config/relays'
|
||||
import { Hooks } from 'applesauce-react'
|
||||
import { getActiveRelayUrls } from '../services/relayManager'
|
||||
import BookmarkFilters, { BookmarkFilterType } from './BookmarkFilters'
|
||||
import { filterBookmarksByType } from '../utils/bookmarkTypeClassifier'
|
||||
import LoginOptions from './LoginOptions'
|
||||
import { useEffect } from 'react'
|
||||
import { readingProgressController } from '../services/readingProgressController'
|
||||
import { nip19 } from 'nostr-tools'
|
||||
import { extractUrlsFromContent } from '../services/bookmarkHelpers'
|
||||
|
||||
interface BookmarkListProps {
|
||||
bookmarks: Bookmark[]
|
||||
@@ -70,6 +74,45 @@ export const BookmarkList: React.FC<BookmarkListProps> = ({
|
||||
return saved === 'flat' ? 'flat' : 'grouped'
|
||||
})
|
||||
const activeAccount = Hooks.useActiveAccount()
|
||||
const [readingProgressMap, setReadingProgressMap] = useState<Map<string, number>>(new Map())
|
||||
|
||||
// Subscribe to reading progress updates
|
||||
useEffect(() => {
|
||||
// Get initial progress map
|
||||
setReadingProgressMap(readingProgressController.getProgressMap())
|
||||
|
||||
// Subscribe to updates
|
||||
const unsubProgress = readingProgressController.onProgress(setReadingProgressMap)
|
||||
|
||||
return () => {
|
||||
unsubProgress()
|
||||
}
|
||||
}, [])
|
||||
|
||||
// Helper to get reading progress for a bookmark
|
||||
const getBookmarkReadingProgress = (bookmark: IndividualBookmark): number | undefined => {
|
||||
if (bookmark.kind === 30023) {
|
||||
// For articles, use naddr as key
|
||||
const dTag = bookmark.tags.find(t => t[0] === 'd')?.[1]
|
||||
if (!dTag) return undefined
|
||||
try {
|
||||
const naddr = nip19.naddrEncode({
|
||||
kind: 30023,
|
||||
pubkey: bookmark.pubkey,
|
||||
identifier: dTag
|
||||
})
|
||||
return readingProgressMap.get(naddr)
|
||||
} catch (err) {
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
// For web bookmarks and other types, try to use URL if available
|
||||
const urls = extractUrlsFromContent(bookmark.content)
|
||||
if (urls.length > 0) {
|
||||
return readingProgressMap.get(urls[0])
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
const toggleGroupingMode = () => {
|
||||
const newMode = groupingMode === 'grouped' ? 'flat' : 'grouped'
|
||||
@@ -82,7 +125,7 @@ export const BookmarkList: React.FC<BookmarkListProps> = ({
|
||||
throw new Error('Please login to create bookmarks')
|
||||
}
|
||||
|
||||
await createWebBookmark(url, title, description, tags, activeAccount, relayPool, RELAYS)
|
||||
await createWebBookmark(url, title, description, tags, activeAccount, relayPool, getActiveRelayUrls(relayPool))
|
||||
}
|
||||
|
||||
// Pull-to-refresh for bookmarks
|
||||
@@ -100,6 +143,7 @@ export const BookmarkList: React.FC<BookmarkListProps> = ({
|
||||
// Merge and flatten all individual bookmarks from all lists
|
||||
const allIndividualBookmarks = bookmarks.flatMap(b => b.individualBookmarks || [])
|
||||
.filter(hasContent)
|
||||
.filter(b => !settings?.hideBookmarksWithoutCreationDate || hasCreationDate(b))
|
||||
|
||||
// Apply filter
|
||||
const filteredBookmarks = filterBookmarksByType(allIndividualBookmarks, selectedFilter)
|
||||
@@ -116,8 +160,8 @@ export const BookmarkList: React.FC<BookmarkListProps> = ({
|
||||
: [
|
||||
{ key: 'nip51-private', title: 'Private Bookmarks', items: groups.nip51Private },
|
||||
{ key: 'nip51-public', title: 'My Bookmarks', items: groups.nip51Public },
|
||||
{ key: 'amethyst-private', title: 'Amethyst Private', items: groups.amethystPrivate },
|
||||
{ key: 'amethyst-public', title: 'Amethyst Lists', items: groups.amethystPublic },
|
||||
{ key: 'amethyst-private', title: 'Private Lists', items: groups.amethystPrivate },
|
||||
{ key: 'amethyst-public', title: 'My Lists', items: groups.amethystPublic },
|
||||
{ key: 'web', title: 'Web Bookmarks', items: groups.standaloneWeb }
|
||||
]
|
||||
|
||||
@@ -220,6 +264,7 @@ export const BookmarkList: React.FC<BookmarkListProps> = ({
|
||||
index={index}
|
||||
onSelectUrl={onSelectUrl}
|
||||
viewMode={viewMode}
|
||||
readingProgress={getBookmarkReadingProgress(individualBookmark)}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
|
||||
@@ -5,7 +5,7 @@ import { faChevronDown, faChevronUp } from '@fortawesome/free-solid-svg-icons'
|
||||
import { IconDefinition } from '@fortawesome/fontawesome-svg-core'
|
||||
import { IndividualBookmark } from '../../types/bookmarks'
|
||||
import { formatDate, renderParsedContent } from '../../utils/bookmarkUtils'
|
||||
import ContentWithResolvedProfiles from '../ContentWithResolvedProfiles'
|
||||
import RichContent from '../RichContent'
|
||||
import { classifyUrl } from '../../utils/helpers'
|
||||
import { useImageCache } from '../../hooks/useImageCache'
|
||||
import { getPreviewImage, fetchOgImage } from '../../utils/imagePreview'
|
||||
@@ -24,6 +24,7 @@ interface CardViewProps {
|
||||
articleImage?: string
|
||||
articleSummary?: string
|
||||
contentTypeIcon: IconDefinition
|
||||
readingProgress?: number
|
||||
}
|
||||
|
||||
export const CardView: React.FC<CardViewProps> = ({
|
||||
@@ -38,7 +39,8 @@ export const CardView: React.FC<CardViewProps> = ({
|
||||
handleReadNow,
|
||||
articleImage,
|
||||
articleSummary,
|
||||
contentTypeIcon
|
||||
contentTypeIcon,
|
||||
readingProgress
|
||||
}) => {
|
||||
const firstUrl = hasUrls ? extractedUrls[0] : null
|
||||
const firstUrlClassificationType = firstUrl ? classifyUrl(firstUrl)?.type : null
|
||||
@@ -52,6 +54,14 @@ export const CardView: React.FC<CardViewProps> = ({
|
||||
const shouldTruncate = !expanded && contentLength > 210
|
||||
const isArticle = bookmark.kind === 30023
|
||||
|
||||
// Calculate progress color (matching BlogPostCard logic)
|
||||
let progressColor = '#6366f1' // Default blue (reading)
|
||||
if (readingProgress && readingProgress >= 0.95) {
|
||||
progressColor = '#10b981' // Green (completed)
|
||||
} else if (readingProgress && readingProgress > 0 && readingProgress <= 0.10) {
|
||||
progressColor = 'var(--color-text)' // Neutral text color (started)
|
||||
}
|
||||
|
||||
// Determine which image to use (article image, instant preview, or OG image)
|
||||
const previewImage = articleImage || instantPreview || ogImage
|
||||
const cachedImage = useImageCache(previewImage || undefined)
|
||||
@@ -137,19 +147,15 @@ export const CardView: React.FC<CardViewProps> = ({
|
||||
)}
|
||||
|
||||
{isArticle && articleSummary ? (
|
||||
<div className="bookmark-content article-summary">
|
||||
<ContentWithResolvedProfiles content={articleSummary} />
|
||||
</div>
|
||||
<RichContent content={articleSummary} className="bookmark-content article-summary" />
|
||||
) : bookmark.parsedContent ? (
|
||||
<div className="bookmark-content">
|
||||
{shouldTruncate && bookmark.content
|
||||
? <ContentWithResolvedProfiles content={`${bookmark.content.slice(0, 210).trimEnd()}…`} />
|
||||
? <RichContent content={`${bookmark.content.slice(0, 210).trimEnd()}…`} className="" />
|
||||
: renderParsedContent(bookmark.parsedContent)}
|
||||
</div>
|
||||
) : bookmark.content && (
|
||||
<div className="bookmark-content">
|
||||
<ContentWithResolvedProfiles content={shouldTruncate ? `${bookmark.content.slice(0, 210).trimEnd()}…` : bookmark.content} />
|
||||
</div>
|
||||
<RichContent content={shouldTruncate ? `${bookmark.content.slice(0, 210).trimEnd()}…` : bookmark.content} />
|
||||
)}
|
||||
|
||||
{contentLength > 210 && (
|
||||
@@ -163,6 +169,28 @@ export const CardView: React.FC<CardViewProps> = ({
|
||||
</button>
|
||||
)}
|
||||
|
||||
{/* Reading progress indicator for articles */}
|
||||
{isArticle && readingProgress !== undefined && readingProgress > 0 && (
|
||||
<div
|
||||
style={{
|
||||
height: '3px',
|
||||
width: '100%',
|
||||
background: 'var(--color-border)',
|
||||
overflow: 'hidden',
|
||||
marginTop: '0.75rem'
|
||||
}}
|
||||
>
|
||||
<div
|
||||
style={{
|
||||
height: '100%',
|
||||
width: `${Math.round(readingProgress * 100)}%`,
|
||||
background: progressColor,
|
||||
transition: 'width 0.3s ease, background 0.3s ease'
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="bookmark-footer">
|
||||
<div className="bookmark-meta-minimal">
|
||||
<Link
|
||||
|
||||
@@ -3,7 +3,7 @@ import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
|
||||
import { IconDefinition } from '@fortawesome/fontawesome-svg-core'
|
||||
import { IndividualBookmark } from '../../types/bookmarks'
|
||||
import { formatDateCompact } from '../../utils/bookmarkUtils'
|
||||
import ContentWithResolvedProfiles from '../ContentWithResolvedProfiles'
|
||||
import RichContent from '../RichContent'
|
||||
|
||||
interface CompactViewProps {
|
||||
bookmark: IndividualBookmark
|
||||
@@ -13,6 +13,7 @@ interface CompactViewProps {
|
||||
onSelectUrl?: (url: string, bookmark?: { id: string; kind: number; tags: string[][]; pubkey: string }) => void
|
||||
articleSummary?: string
|
||||
contentTypeIcon: IconDefinition
|
||||
readingProgress?: number
|
||||
}
|
||||
|
||||
export const CompactView: React.FC<CompactViewProps> = ({
|
||||
@@ -22,12 +23,21 @@ export const CompactView: React.FC<CompactViewProps> = ({
|
||||
extractedUrls,
|
||||
onSelectUrl,
|
||||
articleSummary,
|
||||
contentTypeIcon
|
||||
contentTypeIcon,
|
||||
readingProgress
|
||||
}) => {
|
||||
const isArticle = bookmark.kind === 30023
|
||||
const isWebBookmark = bookmark.kind === 39701
|
||||
const isClickable = hasUrls || isArticle || isWebBookmark
|
||||
|
||||
// Calculate progress color (matching BlogPostCard logic)
|
||||
let progressColor = '#6366f1' // Default blue (reading)
|
||||
if (readingProgress && readingProgress >= 0.95) {
|
||||
progressColor = '#10b981' // Green (completed)
|
||||
} else if (readingProgress && readingProgress > 0 && readingProgress <= 0.10) {
|
||||
progressColor = 'var(--color-text)' // Neutral text color (started)
|
||||
}
|
||||
|
||||
const handleCompactClick = () => {
|
||||
if (!onSelectUrl) return
|
||||
|
||||
@@ -56,12 +66,35 @@ export const CompactView: React.FC<CompactViewProps> = ({
|
||||
</span>
|
||||
{displayText && (
|
||||
<div className="compact-text">
|
||||
<ContentWithResolvedProfiles content={displayText.slice(0, 60) + (displayText.length > 60 ? '…' : '')} />
|
||||
<RichContent content={displayText.slice(0, 60) + (displayText.length > 60 ? '…' : '')} className="" />
|
||||
</div>
|
||||
)}
|
||||
<span className="bookmark-date-compact">{formatDateCompact(bookmark.created_at)}</span>
|
||||
{/* CTA removed */}
|
||||
</div>
|
||||
|
||||
{/* Reading progress indicator for all bookmark types with reading data */}
|
||||
{readingProgress !== undefined && readingProgress > 0 && (
|
||||
<div
|
||||
style={{
|
||||
height: '1px',
|
||||
width: '100%',
|
||||
background: 'var(--color-border)',
|
||||
overflow: 'hidden',
|
||||
margin: '0',
|
||||
marginLeft: '1.5rem'
|
||||
}}
|
||||
>
|
||||
<div
|
||||
style={{
|
||||
height: '100%',
|
||||
width: `${Math.round(readingProgress * 100)}%`,
|
||||
background: progressColor,
|
||||
transition: 'width 0.3s ease, background 0.3s ease'
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
|
||||
import { IconDefinition } from '@fortawesome/fontawesome-svg-core'
|
||||
import { IndividualBookmark } from '../../types/bookmarks'
|
||||
import { formatDate } from '../../utils/bookmarkUtils'
|
||||
import ContentWithResolvedProfiles from '../ContentWithResolvedProfiles'
|
||||
import RichContent from '../RichContent'
|
||||
import { IconGetter } from './shared'
|
||||
import { useImageCache } from '../../hooks/useImageCache'
|
||||
import { getEventUrl } from '../../config/nostrGateways'
|
||||
@@ -95,13 +95,9 @@ export const LargeView: React.FC<LargeViewProps> = ({
|
||||
|
||||
<div className="large-content">
|
||||
{isArticle && articleSummary ? (
|
||||
<div className="large-text article-summary">
|
||||
<ContentWithResolvedProfiles content={articleSummary} />
|
||||
</div>
|
||||
<RichContent content={articleSummary} className="large-text article-summary" />
|
||||
) : bookmark.content && (
|
||||
<div className="large-text">
|
||||
<ContentWithResolvedProfiles content={bookmark.content} />
|
||||
</div>
|
||||
<RichContent content={bookmark.content} className="large-text" />
|
||||
)}
|
||||
|
||||
{/* Reading progress indicator for articles - shown only if there's progress */}
|
||||
|
||||
@@ -17,6 +17,7 @@ import { Bookmark } from '../types/bookmarks'
|
||||
import ThreePaneLayout from './ThreePaneLayout'
|
||||
import Explore from './Explore'
|
||||
import Me from './Me'
|
||||
import Profile from './Profile'
|
||||
import Support from './Support'
|
||||
import { classifyHighlights } from '../utils/highlightClassification'
|
||||
|
||||
@@ -63,7 +64,7 @@ const Bookmarks: React.FC<BookmarksProps> = ({
|
||||
location.pathname === '/me/highlights' ? 'highlights' :
|
||||
location.pathname === '/me/reading-list' ? 'reading-list' :
|
||||
location.pathname.startsWith('/me/reads') ? 'reads' :
|
||||
location.pathname === '/me/links' ? 'links' :
|
||||
location.pathname.startsWith('/me/links') ? 'links' :
|
||||
location.pathname === '/me/writings' ? 'writings' : 'highlights'
|
||||
|
||||
// Extract tab from profile routes
|
||||
@@ -327,10 +328,10 @@ const Bookmarks: React.FC<BookmarksProps> = ({
|
||||
relayPool ? <Explore relayPool={relayPool} eventStore={eventStore} settings={settings} activeTab={exploreTab} /> : null
|
||||
) : undefined}
|
||||
me={showMe ? (
|
||||
relayPool ? <Me relayPool={relayPool} eventStore={eventStore} activeTab={meTab} bookmarks={bookmarks} bookmarksLoading={bookmarksLoading} /> : null
|
||||
relayPool ? <Me relayPool={relayPool} eventStore={eventStore} activeTab={meTab} bookmarks={bookmarks} bookmarksLoading={bookmarksLoading} settings={settings} /> : null
|
||||
) : undefined}
|
||||
profile={showProfile && profilePubkey ? (
|
||||
relayPool ? <Me relayPool={relayPool} eventStore={eventStore} activeTab={profileTab} pubkey={profilePubkey} bookmarks={bookmarks} bookmarksLoading={bookmarksLoading} /> : null
|
||||
relayPool ? <Profile relayPool={relayPool} eventStore={eventStore} pubkey={profilePubkey} activeTab={profileTab} /> : null
|
||||
) : undefined}
|
||||
support={showSupport ? (
|
||||
relayPool ? <Support relayPool={relayPool} eventStore={eventStore} settings={settings} /> : null
|
||||
|
||||
@@ -10,8 +10,8 @@ import { faSpinner, faCheckCircle, faEllipsisH, faExternalLinkAlt, faMobileAlt,
|
||||
import { ContentSkeleton } from './Skeletons'
|
||||
import { nip19 } from 'nostr-tools'
|
||||
import { getNostrUrl, getSearchUrl } from '../config/nostrGateways'
|
||||
import { RELAYS } from '../config/relays'
|
||||
import { RelayPool } from 'applesauce-relay'
|
||||
import { getActiveRelayUrls } from '../services/relayManager'
|
||||
import { IAccount } from 'applesauce-accounts'
|
||||
import { NostrEvent } from 'nostr-tools'
|
||||
import { Highlight } from '../types/highlights'
|
||||
@@ -29,10 +29,12 @@ import {
|
||||
hasMarkedEventAsRead,
|
||||
hasMarkedWebsiteAsRead
|
||||
} from '../services/reactionService'
|
||||
import { unarchiveEvent, unarchiveWebsite } from '../services/unarchiveService'
|
||||
import { archiveController } from '../services/archiveController'
|
||||
import AuthorCard from './AuthorCard'
|
||||
import { faBooks } from '../icons/customIcons'
|
||||
import { extractYouTubeId, getYouTubeMeta } from '../services/youtubeMetaService'
|
||||
import { classifyUrl } from '../utils/helpers'
|
||||
import { classifyUrl, shouldTrackReadingProgress } from '../utils/helpers'
|
||||
import { buildNativeVideoUrl } from '../utils/videoHelpers'
|
||||
import { useReadingPosition } from '../hooks/useReadingPosition'
|
||||
import { ReadingProgressIndicator } from './ReadingProgressIndicator'
|
||||
@@ -151,20 +153,18 @@ const ContentPanel: React.FC<ContentPanelProps> = ({
|
||||
// Callback to save reading position
|
||||
const handleSavePosition = useCallback(async (position: number) => {
|
||||
if (!activeAccount || !relayPool || !eventStore || !articleIdentifier) {
|
||||
console.log('⏭️ [ContentPanel] Skipping save - missing requirements:', {
|
||||
hasAccount: !!activeAccount,
|
||||
hasRelayPool: !!relayPool,
|
||||
hasEventStore: !!eventStore,
|
||||
hasIdentifier: !!articleIdentifier
|
||||
})
|
||||
return
|
||||
}
|
||||
if (!settings?.syncReadingPosition) {
|
||||
console.log('⏭️ [ContentPanel] Sync disabled in settings')
|
||||
return
|
||||
}
|
||||
|
||||
// Check if content is long enough to track reading progress
|
||||
if (!shouldTrackReadingProgress(html, markdown)) {
|
||||
return
|
||||
}
|
||||
|
||||
console.log('💾 [ContentPanel] Saving position:', Math.round(position * 100) + '%', 'for article:', selectedUrl?.slice(0, 50))
|
||||
const scrollTop = window.pageYOffset || document.documentElement.scrollTop
|
||||
|
||||
try {
|
||||
const factory = new EventFactory({ signer: activeAccount })
|
||||
@@ -176,45 +176,44 @@ const ContentPanel: React.FC<ContentPanelProps> = ({
|
||||
{
|
||||
position,
|
||||
timestamp: Math.floor(Date.now() / 1000),
|
||||
scrollTop: window.pageYOffset || document.documentElement.scrollTop
|
||||
scrollTop
|
||||
}
|
||||
)
|
||||
} catch (error) {
|
||||
console.error('❌ [ContentPanel] Failed to save reading position:', error)
|
||||
console.error('[progress] ❌ ContentPanel: Failed to save reading position:', error)
|
||||
}
|
||||
}, [activeAccount, relayPool, eventStore, articleIdentifier, settings?.syncReadingPosition, selectedUrl])
|
||||
}, [activeAccount, relayPool, eventStore, articleIdentifier, settings?.syncReadingPosition, html, markdown])
|
||||
|
||||
const { isReadingComplete, progressPercentage, saveNow } = useReadingPosition({
|
||||
const { progressPercentage, saveNow } = useReadingPosition({
|
||||
enabled: isTextContent,
|
||||
syncEnabled: settings?.syncReadingPosition,
|
||||
syncEnabled: settings?.syncReadingPosition !== false,
|
||||
onSave: handleSavePosition,
|
||||
onReadingComplete: () => {
|
||||
// Optional: Auto-mark as read when reading is complete
|
||||
if (activeAccount && !isMarkedAsRead) {
|
||||
// Could trigger auto-mark as read here if desired
|
||||
// Auto-mark as read when reading is complete (if enabled in settings)
|
||||
if (!settings?.autoMarkAsReadOnCompletion || !activeAccount) return
|
||||
if (!isMarkedAsRead) {
|
||||
handleMarkAsRead()
|
||||
} else {
|
||||
// Already archived: still show the success animation for feedback
|
||||
setShowCheckAnimation(true)
|
||||
setTimeout(() => setShowCheckAnimation(false), 600)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
// Log sync status when it changes
|
||||
useEffect(() => {
|
||||
}, [isTextContent, settings?.syncReadingPosition, activeAccount, relayPool, eventStore, articleIdentifier, progressPercentage])
|
||||
|
||||
// Load saved reading position when article loads
|
||||
useEffect(() => {
|
||||
if (!isTextContent || !activeAccount || !relayPool || !eventStore || !articleIdentifier) {
|
||||
console.log('⏭️ [ContentPanel] Skipping position restore - missing requirements:', {
|
||||
isTextContent,
|
||||
hasAccount: !!activeAccount,
|
||||
hasRelayPool: !!relayPool,
|
||||
hasEventStore: !!eventStore,
|
||||
hasIdentifier: !!articleIdentifier
|
||||
})
|
||||
return
|
||||
}
|
||||
if (!settings?.syncReadingPosition) {
|
||||
console.log('⏭️ [ContentPanel] Sync disabled - not restoring position')
|
||||
if (settings?.syncReadingPosition === false) {
|
||||
return
|
||||
}
|
||||
|
||||
console.log('📖 [ContentPanel] Loading position for article:', selectedUrl?.slice(0, 50))
|
||||
|
||||
const loadPosition = async () => {
|
||||
try {
|
||||
const savedPosition = await loadReadingPosition(
|
||||
@@ -225,7 +224,6 @@ const ContentPanel: React.FC<ContentPanelProps> = ({
|
||||
)
|
||||
|
||||
if (savedPosition && savedPosition.position > 0.05 && savedPosition.position < 1) {
|
||||
console.log('🎯 [ContentPanel] Restoring position:', Math.round(savedPosition.position * 100) + '%')
|
||||
// Wait for content to be fully rendered before scrolling
|
||||
setTimeout(() => {
|
||||
const documentHeight = document.documentElement.scrollHeight
|
||||
@@ -236,14 +234,12 @@ const ContentPanel: React.FC<ContentPanelProps> = ({
|
||||
top: scrollTop,
|
||||
behavior: 'smooth'
|
||||
})
|
||||
|
||||
console.log('✅ [ContentPanel] Restored to position:', Math.round(savedPosition.position * 100) + '%', 'scrollTop:', scrollTop)
|
||||
}, 500) // Give content time to render
|
||||
} else if (savedPosition) {
|
||||
if (savedPosition.position === 1) {
|
||||
console.log('✅ [ContentPanel] Article completed (100%), starting from top')
|
||||
// Article was completed, start from top
|
||||
} else {
|
||||
console.log('⏭️ [ContentPanel] Position too early (<5%):', Math.round(savedPosition.position * 100) + '%')
|
||||
// Position was too early, skip restore
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
@@ -361,7 +357,8 @@ const ContentPanel: React.FC<ContentPanelProps> = ({
|
||||
if (!currentArticle) return null
|
||||
|
||||
const dTag = currentArticle.tags.find(t => t[0] === 'd')?.[1] || ''
|
||||
const relayHints = RELAYS.filter(r =>
|
||||
const activeRelays = relayPool ? getActiveRelayUrls(relayPool) : []
|
||||
const relayHints = activeRelays.filter(r =>
|
||||
!r.includes('localhost') && !r.includes('127.0.0.1')
|
||||
).slice(0, 3)
|
||||
|
||||
@@ -577,12 +574,25 @@ const ContentPanel: React.FC<ContentPanelProps> = ({
|
||||
activeAccount.pubkey,
|
||||
relayPool
|
||||
)
|
||||
// Also check archiveController
|
||||
const dTag = currentArticle.tags.find(t => t[0] === 'd')?.[1]
|
||||
if (dTag) {
|
||||
try {
|
||||
const naddr = nip19.naddrEncode({ kind: 30023, pubkey: currentArticle.pubkey, identifier: dTag })
|
||||
hasRead = hasRead || archiveController.isMarked(naddr)
|
||||
} catch (e) {
|
||||
// Silently ignore encoding errors
|
||||
}
|
||||
}
|
||||
} else {
|
||||
hasRead = await hasMarkedWebsiteAsRead(
|
||||
selectedUrl,
|
||||
activeAccount.pubkey,
|
||||
relayPool
|
||||
)
|
||||
// Also check archiveController
|
||||
const ctrl = archiveController.isMarked(selectedUrl)
|
||||
hasRead = hasRead || ctrl
|
||||
}
|
||||
setIsMarkedAsRead(hasRead)
|
||||
} catch (error) {
|
||||
@@ -596,7 +606,35 @@ const ContentPanel: React.FC<ContentPanelProps> = ({
|
||||
}, [selectedUrl, currentArticle, activeAccount, relayPool, isNostrArticle])
|
||||
|
||||
const handleMarkAsRead = () => {
|
||||
if (!activeAccount || !relayPool || isMarkedAsRead) {
|
||||
if (!activeAccount || !relayPool) return
|
||||
|
||||
// Toggle archive state: if already archived, request deletion; else archive
|
||||
if (isMarkedAsRead) {
|
||||
// Optimistically unarchive in UI; background deletion request (NIP-09)
|
||||
setIsMarkedAsRead(false)
|
||||
;(async () => {
|
||||
try {
|
||||
if (isNostrArticle && currentArticle) {
|
||||
// Send deletion for all matching reactions
|
||||
await unarchiveEvent(currentArticle.id, activeAccount, relayPool)
|
||||
// Also clear controller mark so lists update
|
||||
try {
|
||||
const dTag = currentArticle.tags.find(t => t[0] === 'd')?.[1]
|
||||
if (dTag) {
|
||||
const naddr = nip19.naddrEncode({ kind: 30023, pubkey: currentArticle.pubkey, identifier: dTag })
|
||||
archiveController.unmark(naddr)
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('[archive][content] encode naddr failed', e)
|
||||
}
|
||||
} else if (selectedUrl) {
|
||||
await unarchiveWebsite(selectedUrl, activeAccount, relayPool)
|
||||
archiveController.unmark(selectedUrl)
|
||||
}
|
||||
} catch (err) {
|
||||
console.warn('[archive][content] unarchive failed', err)
|
||||
}
|
||||
})()
|
||||
return
|
||||
}
|
||||
|
||||
@@ -618,16 +656,34 @@ const ContentPanel: React.FC<ContentPanelProps> = ({
|
||||
currentArticle.pubkey,
|
||||
currentArticle.kind,
|
||||
activeAccount,
|
||||
relayPool
|
||||
relayPool,
|
||||
{
|
||||
aCoord: (() => {
|
||||
try {
|
||||
const dTag = currentArticle.tags.find(t => t[0] === 'd')?.[1]
|
||||
if (!dTag) return undefined
|
||||
return `${30023}:${currentArticle.pubkey}:${dTag}`
|
||||
} catch { return undefined }
|
||||
})()
|
||||
}
|
||||
)
|
||||
console.log('✅ Marked nostr article as read')
|
||||
// Update archiveController immediately
|
||||
try {
|
||||
const dTag = currentArticle.tags.find(t => t[0] === 'd')?.[1]
|
||||
if (dTag) {
|
||||
const naddr = nip19.naddrEncode({ kind: 30023, pubkey: currentArticle.pubkey, identifier: dTag })
|
||||
archiveController.mark(naddr)
|
||||
}
|
||||
} catch (err) {
|
||||
console.warn('[archive][content] optimistic article mark failed', err)
|
||||
}
|
||||
} else if (selectedUrl) {
|
||||
await createWebsiteReaction(
|
||||
selectedUrl,
|
||||
activeAccount,
|
||||
relayPool
|
||||
)
|
||||
console.log('✅ Marked website as read')
|
||||
archiveController.mark(selectedUrl)
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to mark as read:', error)
|
||||
@@ -661,7 +717,8 @@ const ContentPanel: React.FC<ContentPanelProps> = ({
|
||||
{isTextContent && (
|
||||
<ReadingProgressIndicator
|
||||
progress={progressPercentage}
|
||||
isComplete={isReadingComplete}
|
||||
// Consider complete only at 95%+
|
||||
isComplete={progressPercentage >= 95}
|
||||
showPercentage={true}
|
||||
isSidebarCollapsed={isSidebarCollapsed}
|
||||
isHighlightsCollapsed={isHighlightsCollapsed}
|
||||
@@ -767,8 +824,9 @@ const ContentPanel: React.FC<ContentPanelProps> = ({
|
||||
<button
|
||||
className={`mark-as-read-btn ${isMarkedAsRead ? 'marked' : ''} ${showCheckAnimation ? 'animating' : ''}`}
|
||||
onClick={handleMarkAsRead}
|
||||
disabled={isMarkedAsRead || isCheckingReadStatus}
|
||||
disabled={isCheckingReadStatus}
|
||||
title={isMarkedAsRead ? 'Already Marked as Watched' : 'Mark as Watched'}
|
||||
style={isMarkedAsRead ? { opacity: 0.85 } : undefined}
|
||||
>
|
||||
<FontAwesomeIcon
|
||||
icon={isCheckingReadStatus ? faSpinner : isMarkedAsRead ? faCheckCircle : faBooks}
|
||||
@@ -930,21 +988,22 @@ const ContentPanel: React.FC<ContentPanelProps> = ({
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Mark as Read button */}
|
||||
{/* Archive button */}
|
||||
{activeAccount && (
|
||||
<div className="mark-as-read-container">
|
||||
<button
|
||||
className={`mark-as-read-btn ${isMarkedAsRead ? 'marked' : ''} ${showCheckAnimation ? 'animating' : ''}`}
|
||||
onClick={handleMarkAsRead}
|
||||
disabled={isMarkedAsRead || isCheckingReadStatus}
|
||||
title={isMarkedAsRead ? 'Already Marked as Read' : 'Mark as Read'}
|
||||
disabled={isCheckingReadStatus}
|
||||
title={isMarkedAsRead ? 'Already Archived' : 'Move to Archive'}
|
||||
style={isMarkedAsRead ? { opacity: 0.85 } : undefined}
|
||||
>
|
||||
<FontAwesomeIcon
|
||||
icon={isCheckingReadStatus ? faSpinner : isMarkedAsRead ? faCheckCircle : faBooks}
|
||||
spin={isCheckingReadStatus}
|
||||
/>
|
||||
<span>
|
||||
{isCheckingReadStatus ? 'Checking...' : isMarkedAsRead ? 'Marked as Read' : 'Mark as Read'}
|
||||
{isCheckingReadStatus ? 'Checking...' : isMarkedAsRead ? 'Archived' : 'Move to Archive'}
|
||||
</span>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
@@ -18,6 +18,9 @@ import { useBookmarksUI } from '../hooks/useBookmarksUI'
|
||||
import { useSettings } from '../hooks/useSettings'
|
||||
import { fetchHighlights, fetchHighlightsFromAuthors } from '../services/highlightService'
|
||||
import { contactsController } from '../services/contactsController'
|
||||
import { writingsController } from '../services/writingsController'
|
||||
import { readingProgressController } from '../services/readingProgressController'
|
||||
import { fetchBlogPostsFromAuthors, BlogPostPreview } from '../services/exploreService'
|
||||
|
||||
const defaultPayload = 'The quick brown fox jumps over the lazy dog.'
|
||||
|
||||
@@ -94,6 +97,33 @@ const Debug: React.FC<DebugProps> = ({
|
||||
const [tLoadHighlights, setTLoadHighlights] = useState<number | null>(null)
|
||||
const [tFirstHighlight, setTFirstHighlight] = useState<number | null>(null)
|
||||
|
||||
// Writings loading state
|
||||
const [isLoadingWritings, setIsLoadingWritings] = useState(false)
|
||||
const [writingPosts, setWritingPosts] = useState<BlogPostPreview[]>([])
|
||||
const [tLoadWritings, setTLoadWritings] = useState<number | null>(null)
|
||||
const [tFirstWriting, setTFirstWriting] = useState<number | null>(null)
|
||||
|
||||
// Reading Progress loading state
|
||||
const [isLoadingReadingProgress, setIsLoadingReadingProgress] = useState(false)
|
||||
const [readingProgressEvents, setReadingProgressEvents] = useState<NostrEvent[]>([])
|
||||
const [tLoadReadingProgress, setTLoadReadingProgress] = useState<number | null>(null)
|
||||
const [tFirstReadingProgress, setTFirstReadingProgress] = useState<number | null>(null)
|
||||
|
||||
// Mark-as-read reactions loading state
|
||||
const [isLoadingMarkAsRead, setIsLoadingMarkAsRead] = useState(false)
|
||||
const [markAsReadReactions, setMarkAsReadReactions] = useState<NostrEvent[]>([])
|
||||
const [tLoadMarkAsRead, setTLoadMarkAsRead] = useState<number | null>(null)
|
||||
const [tFirstMarkAsRead, setTFirstMarkAsRead] = useState<number | null>(null)
|
||||
|
||||
// Relay list loading state
|
||||
const [isLoadingRelayList, setIsLoadingRelayList] = useState(false)
|
||||
const [relayListEvents, setRelayListEvents] = useState<NostrEvent[]>([])
|
||||
const [tLoadRelayList, setTLoadRelayList] = useState<number | null>(null)
|
||||
const [tFirstRelayList, setTFirstRelayList] = useState<number | null>(null)
|
||||
|
||||
// Deduplicated reading progress from controller
|
||||
const [deduplicatedProgressMap, setDeduplicatedProgressMap] = useState<Map<string, number>>(new Map())
|
||||
|
||||
// Live timing state
|
||||
const [liveTiming, setLiveTiming] = useState<{
|
||||
nip44?: { type: 'encrypt' | 'decrypt'; startTime: number }
|
||||
@@ -101,6 +131,9 @@ const Debug: React.FC<DebugProps> = ({
|
||||
loadBookmarks?: { startTime: number }
|
||||
decryptBookmarks?: { startTime: number }
|
||||
loadHighlights?: { startTime: number }
|
||||
loadReadingProgress?: { startTime: number }
|
||||
loadMarkAsRead?: { startTime: number }
|
||||
loadRelayList?: { startTime: number }
|
||||
}>({})
|
||||
|
||||
// Web of Trust state
|
||||
@@ -302,10 +335,6 @@ const Debug: React.FC<DebugProps> = ({
|
||||
|
||||
// Subscribe to decrypt complete events for Debug UI display
|
||||
const unsubscribeDecrypt = bookmarkController.onDecryptComplete((eventId, publicCount, privateCount) => {
|
||||
console.log('[bunker] ✅ Auto-decrypted:', eventId.slice(0, 8), {
|
||||
public: publicCount,
|
||||
private: privateCount
|
||||
})
|
||||
setDecryptedEvents(prev => new Map(prev).set(eventId, {
|
||||
public: publicCount,
|
||||
private: privateCount
|
||||
@@ -538,6 +567,396 @@ const Debug: React.FC<DebugProps> = ({
|
||||
}
|
||||
}
|
||||
|
||||
const handleLoadMyWritings = async () => {
|
||||
if (!relayPool || !activeAccount?.pubkey || !eventStore) {
|
||||
DebugBus.warn('debug', 'Please log in to load your writings')
|
||||
return
|
||||
}
|
||||
const start = performance.now()
|
||||
setWritingPosts([])
|
||||
setIsLoadingWritings(true)
|
||||
setTLoadWritings(null)
|
||||
setTFirstWriting(null)
|
||||
DebugBus.info('debug', 'Loading my writings via writingsController...')
|
||||
try {
|
||||
let firstEventTime: number | null = null
|
||||
const unsub = writingsController.onWritings((posts) => {
|
||||
if (firstEventTime === null && posts.length > 0) {
|
||||
firstEventTime = performance.now() - start
|
||||
setTFirstWriting(Math.round(firstEventTime))
|
||||
}
|
||||
setWritingPosts(posts)
|
||||
})
|
||||
|
||||
await writingsController.start({
|
||||
relayPool,
|
||||
eventStore,
|
||||
pubkey: activeAccount.pubkey,
|
||||
force: true
|
||||
})
|
||||
|
||||
unsub()
|
||||
const currentWritings = writingsController.getWritings()
|
||||
setWritingPosts(currentWritings)
|
||||
DebugBus.info('debug', `Loaded ${currentWritings.length} writings via controller`)
|
||||
} finally {
|
||||
setIsLoadingWritings(false)
|
||||
const elapsed = Math.round(performance.now() - start)
|
||||
setTLoadWritings(elapsed)
|
||||
DebugBus.info('debug', `Loaded my writings in ${elapsed}ms`)
|
||||
}
|
||||
}
|
||||
|
||||
const handleLoadFriendsWritings = async () => {
|
||||
if (!relayPool || !activeAccount?.pubkey) {
|
||||
DebugBus.warn('debug', 'Please log in to load friends writings')
|
||||
return
|
||||
}
|
||||
const start = performance.now()
|
||||
setWritingPosts([])
|
||||
setIsLoadingWritings(true)
|
||||
setTLoadWritings(null)
|
||||
setTFirstWriting(null)
|
||||
DebugBus.info('debug', 'Loading friends writings...')
|
||||
try {
|
||||
// Get contacts first
|
||||
await contactsController.start({ relayPool, pubkey: activeAccount.pubkey })
|
||||
const friends = contactsController.getContacts()
|
||||
const friendsArray = Array.from(friends)
|
||||
DebugBus.info('debug', `Found ${friendsArray.length} friends`)
|
||||
|
||||
if (friendsArray.length === 0) {
|
||||
DebugBus.warn('debug', 'No friends found to load writings from')
|
||||
return
|
||||
}
|
||||
|
||||
let firstEventTime: number | null = null
|
||||
const relayUrls = Array.from(relayPool.relays.values()).map(relay => relay.url)
|
||||
const posts = await fetchBlogPostsFromAuthors(
|
||||
relayPool,
|
||||
friendsArray,
|
||||
relayUrls,
|
||||
(post) => {
|
||||
if (firstEventTime === null) {
|
||||
firstEventTime = performance.now() - start
|
||||
setTFirstWriting(Math.round(firstEventTime))
|
||||
}
|
||||
setWritingPosts(prev => {
|
||||
const dTag = post.event.tags.find(t => t[0] === 'd')?.[1] || ''
|
||||
const key = `${post.author}:${dTag}`
|
||||
const exists = prev.find(p => {
|
||||
const pDTag = p.event.tags.find(t => t[0] === 'd')?.[1] || ''
|
||||
return `${p.author}:${pDTag}` === key
|
||||
})
|
||||
if (exists) return prev
|
||||
return [...prev, post].sort((a, b) => {
|
||||
const timeA = a.published || a.event.created_at
|
||||
const timeB = b.published || b.event.created_at
|
||||
return timeB - timeA
|
||||
})
|
||||
})
|
||||
}
|
||||
)
|
||||
|
||||
setWritingPosts(posts)
|
||||
DebugBus.info('debug', `Loaded ${posts.length} friend writings`)
|
||||
} finally {
|
||||
setIsLoadingWritings(false)
|
||||
const elapsed = Math.round(performance.now() - start)
|
||||
setTLoadWritings(elapsed)
|
||||
DebugBus.info('debug', `Loaded friend writings in ${elapsed}ms`)
|
||||
}
|
||||
}
|
||||
|
||||
const handleLoadNostrverseWritings = async () => {
|
||||
if (!relayPool) {
|
||||
DebugBus.warn('debug', 'Relay pool not available')
|
||||
return
|
||||
}
|
||||
const start = performance.now()
|
||||
setWritingPosts([])
|
||||
setIsLoadingWritings(true)
|
||||
setTLoadWritings(null)
|
||||
setTFirstWriting(null)
|
||||
DebugBus.info('debug', 'Loading nostrverse writings (kind:30023)...')
|
||||
try {
|
||||
let firstEventTime: number | null = null
|
||||
const relayUrls = Array.from(relayPool.relays.values()).map(relay => relay.url)
|
||||
|
||||
const { queryEvents } = await import('../services/dataFetch')
|
||||
const { Helpers } = await import('applesauce-core')
|
||||
const { getArticleTitle, getArticleSummary, getArticleImage, getArticlePublished } = Helpers
|
||||
|
||||
const uniqueEvents = new Map<string, NostrEvent>()
|
||||
await queryEvents(relayPool, { kinds: [30023], limit: 50 }, {
|
||||
relayUrls,
|
||||
onEvent: (evt) => {
|
||||
const dTag = evt.tags.find(t => t[0] === 'd')?.[1] || ''
|
||||
const key = `${evt.pubkey}:${dTag}`
|
||||
const existing = uniqueEvents.get(key)
|
||||
if (!existing || evt.created_at > existing.created_at) {
|
||||
uniqueEvents.set(key, evt)
|
||||
|
||||
if (firstEventTime === null) {
|
||||
firstEventTime = performance.now() - start
|
||||
setTFirstWriting(Math.round(firstEventTime))
|
||||
}
|
||||
|
||||
const posts = Array.from(uniqueEvents.values()).map(event => ({
|
||||
event,
|
||||
title: getArticleTitle(event) || 'Untitled',
|
||||
summary: getArticleSummary(event),
|
||||
image: getArticleImage(event),
|
||||
published: getArticlePublished(event),
|
||||
author: event.pubkey
|
||||
} as BlogPostPreview)).sort((a, b) => {
|
||||
const timeA = a.published || a.event.created_at
|
||||
const timeB = b.published || b.event.created_at
|
||||
return timeB - timeA
|
||||
})
|
||||
|
||||
setWritingPosts(posts)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const finalPosts = Array.from(uniqueEvents.values()).map(event => ({
|
||||
event,
|
||||
title: getArticleTitle(event) || 'Untitled',
|
||||
summary: getArticleSummary(event),
|
||||
image: getArticleImage(event),
|
||||
published: getArticlePublished(event),
|
||||
author: event.pubkey
|
||||
} as BlogPostPreview)).sort((a, b) => {
|
||||
const timeA = a.published || a.event.created_at
|
||||
const timeB = b.published || b.event.created_at
|
||||
return timeB - timeA
|
||||
})
|
||||
|
||||
setWritingPosts(finalPosts)
|
||||
DebugBus.info('debug', `Loaded ${finalPosts.length} nostrverse writings`)
|
||||
} finally {
|
||||
setIsLoadingWritings(false)
|
||||
const elapsed = Math.round(performance.now() - start)
|
||||
setTLoadWritings(elapsed)
|
||||
DebugBus.info('debug', `Loaded nostrverse writings in ${elapsed}ms`)
|
||||
}
|
||||
}
|
||||
|
||||
const handleClearWritings = () => {
|
||||
setWritingPosts([])
|
||||
setTLoadWritings(null)
|
||||
setTFirstWriting(null)
|
||||
}
|
||||
|
||||
const handleLoadReadingProgress = async () => {
|
||||
if (!relayPool || !eventStore || !activeAccount?.pubkey) {
|
||||
DebugBus.warn('debug', 'Please log in to load reading progress')
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
setIsLoadingReadingProgress(true)
|
||||
setReadingProgressEvents([])
|
||||
setTLoadReadingProgress(null)
|
||||
setTFirstReadingProgress(null)
|
||||
setDeduplicatedProgressMap(new Map())
|
||||
DebugBus.info('debug', 'Loading reading progress events...')
|
||||
|
||||
const start = performance.now()
|
||||
let firstEventTime: number | null = null
|
||||
setLiveTiming(prev => ({ ...prev, loadReadingProgress: { startTime: start } }))
|
||||
|
||||
const { queryEvents } = await import('../services/dataFetch')
|
||||
const { KINDS } = await import('../config/kinds')
|
||||
|
||||
// Load raw events for display
|
||||
const rawEvents: NostrEvent[] = []
|
||||
const rawQueryPromise = queryEvents(relayPool, { kinds: [KINDS.ReadingProgress], authors: [activeAccount.pubkey] }, {
|
||||
onEvent: (evt) => {
|
||||
if (firstEventTime === null) {
|
||||
firstEventTime = performance.now() - start
|
||||
setTFirstReadingProgress(Math.round(firstEventTime))
|
||||
}
|
||||
rawEvents.push(evt)
|
||||
setReadingProgressEvents([...rawEvents])
|
||||
}
|
||||
})
|
||||
|
||||
// Load deduplicated results via controller
|
||||
const unsubProgress = readingProgressController.onProgress((progressMap) => {
|
||||
setDeduplicatedProgressMap(new Map(progressMap))
|
||||
})
|
||||
|
||||
// Run both in parallel
|
||||
await Promise.all([
|
||||
rawQueryPromise,
|
||||
readingProgressController.start({ relayPool, eventStore, pubkey: activeAccount.pubkey, force: true })
|
||||
])
|
||||
|
||||
unsubProgress()
|
||||
|
||||
const elapsed = Math.round(performance.now() - start)
|
||||
setTLoadReadingProgress(elapsed)
|
||||
setLiveTiming(prev => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars, no-unused-vars
|
||||
const { loadReadingProgress, ...rest } = prev
|
||||
return rest
|
||||
})
|
||||
|
||||
const finalMap = readingProgressController.getProgressMap()
|
||||
DebugBus.info('debug', `Loaded ${rawEvents.length} raw events, deduplicated to ${finalMap.size} articles in ${elapsed}ms`)
|
||||
} catch (err) {
|
||||
console.error('Failed to load reading progress:', err)
|
||||
DebugBus.error('debug', `Failed to load reading progress: ${err instanceof Error ? err.message : String(err)}`)
|
||||
} finally {
|
||||
setIsLoadingReadingProgress(false)
|
||||
}
|
||||
}
|
||||
|
||||
const handleClearReadingProgress = () => {
|
||||
setReadingProgressEvents([])
|
||||
setTLoadReadingProgress(null)
|
||||
setTFirstReadingProgress(null)
|
||||
setDeduplicatedProgressMap(new Map())
|
||||
DebugBus.info('debug', 'Cleared reading progress data')
|
||||
}
|
||||
|
||||
const handleLoadMarkAsReadReactions = async () => {
|
||||
if (!relayPool || !activeAccount?.pubkey) {
|
||||
DebugBus.warn('debug', 'Please log in to load mark-as-read reactions')
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
setIsLoadingMarkAsRead(true)
|
||||
setMarkAsReadReactions([])
|
||||
setTLoadMarkAsRead(null)
|
||||
setTFirstMarkAsRead(null)
|
||||
DebugBus.info('debug', 'Loading mark-as-read reactions...')
|
||||
|
||||
const start = performance.now()
|
||||
let firstEventTime: number | null = null
|
||||
setLiveTiming(prev => ({ ...prev, loadMarkAsRead: { startTime: start } }))
|
||||
|
||||
const { queryEvents } = await import('../services/dataFetch')
|
||||
const { ARCHIVE_EMOJI } = await import('../services/reactionService')
|
||||
|
||||
// Load both kind:7 (reactions to events) and kind:17 (reactions to URLs)
|
||||
const [kind7Events, kind17Events] = await Promise.all([
|
||||
queryEvents(relayPool, { kinds: [7], authors: [activeAccount.pubkey] }, {
|
||||
onEvent: (evt) => {
|
||||
if (evt.content === ARCHIVE_EMOJI) {
|
||||
if (firstEventTime === null) {
|
||||
firstEventTime = performance.now() - start
|
||||
setTFirstMarkAsRead(Math.round(firstEventTime))
|
||||
}
|
||||
setMarkAsReadReactions(prev => [...prev, evt])
|
||||
}
|
||||
}
|
||||
}),
|
||||
queryEvents(relayPool, { kinds: [17], authors: [activeAccount.pubkey] }, {
|
||||
onEvent: (evt) => {
|
||||
if (evt.content === ARCHIVE_EMOJI) {
|
||||
if (firstEventTime === null) {
|
||||
firstEventTime = performance.now() - start
|
||||
setTFirstMarkAsRead(Math.round(firstEventTime))
|
||||
}
|
||||
setMarkAsReadReactions(prev => [...prev, evt])
|
||||
}
|
||||
}
|
||||
})
|
||||
])
|
||||
|
||||
const totalEvents = kind7Events.length + kind17Events.length
|
||||
const elapsed = Math.round(performance.now() - start)
|
||||
setTLoadMarkAsRead(elapsed)
|
||||
setLiveTiming(prev => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars, no-unused-vars
|
||||
const { loadMarkAsRead, ...rest } = prev
|
||||
return rest
|
||||
})
|
||||
|
||||
DebugBus.info('debug', `Loaded ${totalEvents} mark-as-read reactions in ${elapsed}ms`)
|
||||
} catch (err) {
|
||||
console.error('Failed to load mark-as-read reactions:', err)
|
||||
DebugBus.error('debug', `Failed to load mark-as-read reactions: ${err instanceof Error ? err.message : String(err)}`)
|
||||
} finally {
|
||||
setIsLoadingMarkAsRead(false)
|
||||
}
|
||||
}
|
||||
|
||||
const handleClearMarkAsRead = () => {
|
||||
setMarkAsReadReactions([])
|
||||
setTLoadMarkAsRead(null)
|
||||
setTFirstMarkAsRead(null)
|
||||
DebugBus.info('debug', 'Cleared mark-as-read reactions data')
|
||||
}
|
||||
|
||||
const handleLoadRelayList = async () => {
|
||||
if (!relayPool || !activeAccount?.pubkey) {
|
||||
DebugBus.warn('debug', 'Please log in to load relay list')
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
setIsLoadingRelayList(true)
|
||||
setRelayListEvents([])
|
||||
setTLoadRelayList(null)
|
||||
setTFirstRelayList(null)
|
||||
DebugBus.info('debug', 'Loading relay list (kind 10002)...')
|
||||
|
||||
const start = performance.now()
|
||||
let firstEventTime: number | null = null
|
||||
setLiveTiming(prev => ({ ...prev, loadRelayList: { startTime: start } }))
|
||||
|
||||
const { queryEvents } = await import('../services/dataFetch')
|
||||
|
||||
// Query for kind:10002 (relay list)
|
||||
const events = await queryEvents(relayPool, {
|
||||
kinds: [10002],
|
||||
authors: [activeAccount.pubkey],
|
||||
limit: 10
|
||||
}, {
|
||||
onEvent: (evt) => {
|
||||
if (firstEventTime === null) {
|
||||
firstEventTime = performance.now() - start
|
||||
setTFirstRelayList(Math.round(firstEventTime))
|
||||
}
|
||||
setRelayListEvents(prev => [...prev, evt])
|
||||
}
|
||||
})
|
||||
|
||||
const elapsed = Math.round(performance.now() - start)
|
||||
setTLoadRelayList(elapsed)
|
||||
setLiveTiming(prev => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars, no-unused-vars
|
||||
const { loadRelayList, ...rest } = prev
|
||||
return rest
|
||||
})
|
||||
|
||||
DebugBus.info('debug', `Loaded ${events.length} relay list events in ${elapsed}ms`)
|
||||
|
||||
// Log details about the events
|
||||
events.forEach((event, index) => {
|
||||
const relayCount = event.tags.filter(tag => tag[0] === 'r').length
|
||||
DebugBus.info('debug', `Event ${index + 1}: ${relayCount} relays, created ${new Date(event.created_at * 1000).toISOString()}`)
|
||||
})
|
||||
} catch (err) {
|
||||
console.error('Failed to load relay list:', err)
|
||||
DebugBus.error('debug', `Failed to load relay list: ${err instanceof Error ? err.message : String(err)}`)
|
||||
} finally {
|
||||
setIsLoadingRelayList(false)
|
||||
}
|
||||
}
|
||||
|
||||
const handleClearRelayList = () => {
|
||||
setRelayListEvents([])
|
||||
setTLoadRelayList(null)
|
||||
setTFirstRelayList(null)
|
||||
DebugBus.info('debug', 'Cleared relay list data')
|
||||
}
|
||||
|
||||
const handleLoadFriendsList = async () => {
|
||||
if (!relayPool || !activeAccount?.pubkey) {
|
||||
DebugBus.warn('debug', 'Please log in to load friends list')
|
||||
@@ -552,7 +971,6 @@ const Debug: React.FC<DebugProps> = ({
|
||||
|
||||
// Subscribe to controller updates to see streaming
|
||||
const unsubscribe = contactsController.onContacts((contacts) => {
|
||||
console.log('[debug] Received contacts update:', contacts.size)
|
||||
setFriendsPubkeys(new Set(contacts))
|
||||
})
|
||||
|
||||
@@ -1070,6 +1488,353 @@ const Debug: React.FC<DebugProps> = ({
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Writings Loading Section */}
|
||||
<div className="settings-section">
|
||||
<h3 className="section-title">Writings Loading</h3>
|
||||
|
||||
<div className="mb-3 text-sm opacity-70">Quick load options:</div>
|
||||
<div className="flex gap-2 mb-3 flex-wrap">
|
||||
<button
|
||||
className="btn btn-secondary text-sm"
|
||||
onClick={handleLoadMyWritings}
|
||||
disabled={isLoadingWritings || !relayPool || !activeAccount || !eventStore}
|
||||
>
|
||||
{isLoadingWritings ? (
|
||||
<FontAwesomeIcon icon={faSpinner} className="animate-spin" />
|
||||
) : (
|
||||
'Load My Writings'
|
||||
)}
|
||||
</button>
|
||||
<button
|
||||
className="btn btn-secondary text-sm"
|
||||
onClick={handleLoadFriendsWritings}
|
||||
disabled={isLoadingWritings || !relayPool || !activeAccount}
|
||||
>
|
||||
{isLoadingWritings ? (
|
||||
<FontAwesomeIcon icon={faSpinner} className="animate-spin" />
|
||||
) : (
|
||||
'Load Friends Writings'
|
||||
)}
|
||||
</button>
|
||||
<button
|
||||
className="btn btn-secondary text-sm"
|
||||
onClick={handleLoadNostrverseWritings}
|
||||
disabled={isLoadingWritings || !relayPool}
|
||||
>
|
||||
{isLoadingWritings ? (
|
||||
<FontAwesomeIcon icon={faSpinner} className="animate-spin" />
|
||||
) : (
|
||||
'Load Nostrverse Writings'
|
||||
)}
|
||||
</button>
|
||||
<button
|
||||
className="btn btn-secondary text-sm ml-auto"
|
||||
onClick={handleClearWritings}
|
||||
disabled={writingPosts.length === 0}
|
||||
>
|
||||
Clear
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div className="mb-3 flex gap-2 flex-wrap">
|
||||
<Stat label="total" value={tLoadWritings} />
|
||||
<Stat label="first event" value={tFirstWriting} />
|
||||
</div>
|
||||
|
||||
{writingPosts.length > 0 && (
|
||||
<div className="mb-3">
|
||||
<div className="text-sm opacity-70 mb-2">Loaded Writings ({writingPosts.length}):</div>
|
||||
<div className="space-y-2 max-h-96 overflow-y-auto">
|
||||
{writingPosts.map((post, idx) => {
|
||||
const title = post.title
|
||||
const summary = post.summary
|
||||
const dTag = post.event.tags.find(t => t[0] === 'd')?.[1] || ''
|
||||
|
||||
return (
|
||||
<div key={idx} className="font-mono text-xs p-2 bg-gray-100 dark:bg-gray-800 rounded">
|
||||
<div className="font-semibold mb-1">Writing #{idx + 1}</div>
|
||||
<div className="opacity-70 mb-1">
|
||||
<div>Author: {post.author.slice(0, 16)}...</div>
|
||||
<div>Published: {post.published ? new Date(post.published * 1000).toLocaleString() : new Date(post.event.created_at * 1000).toLocaleString()}</div>
|
||||
<div>d-tag: {dTag || '(empty)'}</div>
|
||||
</div>
|
||||
<div className="mt-1">
|
||||
<div className="font-semibold text-[11px]">Title:</div>
|
||||
<div>"{title}"</div>
|
||||
</div>
|
||||
{summary && (
|
||||
<div className="mt-1 text-[11px] opacity-70">
|
||||
<div>Summary: {summary.substring(0, 100)}{summary.length > 100 ? '...' : ''}</div>
|
||||
</div>
|
||||
)}
|
||||
{post.image && (
|
||||
<div className="mt-1 text-[11px] opacity-70">
|
||||
<div>Image: {post.image.substring(0, 40)}...</div>
|
||||
</div>
|
||||
)}
|
||||
<div className="opacity-50 mt-1 text-[10px] break-all">ID: {post.event.id}</div>
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Reading Progress Loading Section */}
|
||||
<div className="settings-section">
|
||||
<h3 className="section-title">Reading Progress Loading</h3>
|
||||
<div className="text-sm opacity-70 mb-3">Test reading progress loading (kind: 39802) for the logged-in user</div>
|
||||
<div className="flex gap-2 mb-3 items-center">
|
||||
<button
|
||||
className="btn btn-primary"
|
||||
onClick={handleLoadReadingProgress}
|
||||
disabled={isLoadingReadingProgress || !relayPool || !activeAccount}
|
||||
>
|
||||
{isLoadingReadingProgress ? (
|
||||
<>
|
||||
<FontAwesomeIcon icon={faSpinner} className="animate-spin mr-2" />
|
||||
Loading...
|
||||
</>
|
||||
) : (
|
||||
'Load Reading Progress'
|
||||
)}
|
||||
</button>
|
||||
<button
|
||||
className="btn btn-secondary ml-auto"
|
||||
onClick={handleClearReadingProgress}
|
||||
disabled={readingProgressEvents.length === 0}
|
||||
>
|
||||
Clear
|
||||
</button>
|
||||
</div>
|
||||
<div className="mb-3 flex gap-2 flex-wrap">
|
||||
<Stat label="total" value={tLoadReadingProgress} />
|
||||
<Stat label="first event" value={tFirstReadingProgress} />
|
||||
</div>
|
||||
{readingProgressEvents.length > 0 && (
|
||||
<div className="mb-3">
|
||||
<div className="text-sm opacity-70 mb-2">Loaded Reading Progress ({readingProgressEvents.length}):</div>
|
||||
<div className="space-y-2 max-h-96 overflow-y-auto">
|
||||
{readingProgressEvents.map((evt, idx) => {
|
||||
const dTag = evt.tags?.find((t: string[]) => t[0] === 'd')?.[1]
|
||||
const aTag = evt.tags?.find((t: string[]) => t[0] === 'a')?.[1]
|
||||
const content = evt.content || ''
|
||||
|
||||
return (
|
||||
<div key={idx} className="font-mono text-xs p-2 bg-gray-100 dark:bg-gray-800 rounded">
|
||||
<div className="font-semibold mb-1">Reading Progress #{idx + 1}</div>
|
||||
<div className="opacity-70 mb-1">
|
||||
<div>Author: {evt.pubkey.slice(0, 16)}...</div>
|
||||
<div>Created: {new Date(evt.created_at * 1000).toLocaleString()}</div>
|
||||
</div>
|
||||
<div className="mt-1">
|
||||
{dTag && <div>d-tag: {dTag}</div>}
|
||||
{aTag && <div className="text-[11px] opacity-70">#a: {aTag}</div>}
|
||||
{content && <div>Progress: {content}</div>}
|
||||
</div>
|
||||
<div className="opacity-50 mt-1 text-[10px] break-all">ID: {evt.id}</div>
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{deduplicatedProgressMap.size > 0 && (
|
||||
<div className="mb-3">
|
||||
<div className="text-sm opacity-70 mb-2">Deduplicated Reading Progress ({deduplicatedProgressMap.size} articles):</div>
|
||||
|
||||
{/* Category breakdown */}
|
||||
<div className="mb-3 p-2 bg-purple-50 dark:bg-purple-900/20 rounded border border-purple-200 dark:border-purple-700">
|
||||
<div className="text-sm font-semibold mb-2">Breakdown by Category:</div>
|
||||
<div className="space-y-1">
|
||||
{(() => {
|
||||
let unopened = 0, started = 0, reading = 0, completed = 0
|
||||
for (const progress of deduplicatedProgressMap.values()) {
|
||||
if (progress === 0) unopened++
|
||||
else if (progress > 0 && progress <= 0.10) started++
|
||||
else if (progress > 0.10 && progress <= 0.94) reading++
|
||||
else if (progress >= 0.95) completed++
|
||||
}
|
||||
return (
|
||||
<>
|
||||
<div className="flex justify-between text-xs">
|
||||
<span>Unopened (0%):</span>
|
||||
<span className="font-semibold">{unopened}</span>
|
||||
</div>
|
||||
<div className="flex justify-between text-xs">
|
||||
<span>Started (0% < progress ≤ 10%):</span>
|
||||
<span className="font-semibold">{started}</span>
|
||||
</div>
|
||||
<div className="flex justify-between text-xs bg-green-100 dark:bg-green-900/30 px-1 py-0.5 rounded">
|
||||
<span>Reading (10% < progress ≤ 94%) ✓:</span>
|
||||
<span className="font-semibold text-green-700 dark:text-green-400">{reading}</span>
|
||||
</div>
|
||||
<div className="flex justify-between text-xs">
|
||||
<span>Completed (≥ 95%):</span>
|
||||
<span className="font-semibold">{completed}</span>
|
||||
</div>
|
||||
</>
|
||||
)
|
||||
})()}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2 max-h-96 overflow-y-auto">
|
||||
{Array.from(deduplicatedProgressMap.entries()).map(([articleId, progress], idx) => {
|
||||
return (
|
||||
<div key={idx} className="font-mono text-xs p-2 bg-blue-50 dark:bg-blue-900/20 rounded border border-blue-200 dark:border-blue-700">
|
||||
<div className="font-semibold mb-1">Article #{idx + 1}</div>
|
||||
<div className="mt-1">
|
||||
<div className="break-all">ID: {articleId}</div>
|
||||
<div className="mt-1">
|
||||
<div className="text-[11px] opacity-70">Progress: {(progress * 100).toFixed(1)}%</div>
|
||||
<div className="w-full bg-gray-300 dark:bg-gray-700 rounded-full h-1.5 mt-1 overflow-hidden">
|
||||
<div
|
||||
className="bg-blue-600 h-full"
|
||||
style={{ width: `${progress * 100}%` }}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Mark-as-read Reactions Loading Section */}
|
||||
<div className="settings-section">
|
||||
<h3 className="section-title">Mark-as-read Reactions Loading</h3>
|
||||
<div className="text-sm opacity-70 mb-3">Test loading mark-as-read reactions (kind: 7 and 17) with the ARCHIVE_EMOJI for the logged-in user</div>
|
||||
<div className="flex gap-2 mb-3 items-center">
|
||||
<button
|
||||
className="btn btn-primary"
|
||||
onClick={handleLoadMarkAsReadReactions}
|
||||
disabled={isLoadingMarkAsRead || !relayPool || !activeAccount}
|
||||
>
|
||||
{isLoadingMarkAsRead ? (
|
||||
<>
|
||||
<FontAwesomeIcon icon={faSpinner} className="animate-spin mr-2" />
|
||||
Loading...
|
||||
</>
|
||||
) : (
|
||||
'Load Mark-as-read Reactions'
|
||||
)}
|
||||
</button>
|
||||
<button
|
||||
className="btn btn-secondary ml-auto"
|
||||
onClick={handleClearMarkAsRead}
|
||||
disabled={markAsReadReactions.length === 0}
|
||||
>
|
||||
Clear
|
||||
</button>
|
||||
</div>
|
||||
<div className="mb-3 flex gap-2 flex-wrap">
|
||||
<Stat label="total" value={tLoadMarkAsRead} />
|
||||
<Stat label="first event" value={tFirstMarkAsRead} />
|
||||
</div>
|
||||
{markAsReadReactions.length > 0 && (
|
||||
<div className="mb-3">
|
||||
<div className="text-sm opacity-70 mb-2">Loaded Mark-as-read Reactions ({markAsReadReactions.length}):</div>
|
||||
<div className="space-y-2 max-h-96 overflow-y-auto">
|
||||
{markAsReadReactions.map((evt, idx) => {
|
||||
const eTag = evt.tags?.find((t: string[]) => t[0] === 'e')?.[1]
|
||||
const rTag = evt.tags?.find((t: string[]) => t[0] === 'r')?.[1]
|
||||
const pTag = evt.tags?.find((t: string[]) => t[0] === 'p')?.[1]
|
||||
|
||||
return (
|
||||
<div key={idx} className="font-mono text-xs p-2 bg-gray-100 dark:bg-gray-800 rounded">
|
||||
<div className="font-semibold mb-1">Mark-as-read Reaction #{idx + 1}</div>
|
||||
<div className="opacity-70 mb-1">
|
||||
<div>Kind: {evt.kind}</div>
|
||||
<div>Author: {evt.pubkey.slice(0, 16)}...</div>
|
||||
<div>Created: {new Date(evt.created_at * 1000).toLocaleString()}</div>
|
||||
</div>
|
||||
<div className="mt-1">
|
||||
<div>Emoji: {evt.content}</div>
|
||||
{eTag && <div className="text-[11px] opacity-70">#e: {eTag.slice(0, 16)}...</div>}
|
||||
{rTag && <div className="text-[11px] opacity-70">#r: {rTag.length > 60 ? rTag.substring(0, 60) + '...' : rTag}</div>}
|
||||
{pTag && <div className="text-[11px] opacity-70">#p: {pTag.slice(0, 16)}...</div>}
|
||||
</div>
|
||||
<div className="opacity-50 mt-1 text-[10px] break-all">ID: {evt.id}</div>
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Relay List Loading Section */}
|
||||
<div className="settings-section">
|
||||
<h3 className="section-title">Relay List Loading (kind 10002)</h3>
|
||||
<div className="text-sm opacity-70 mb-3">Load your relay list to debug dynamic relay integration:</div>
|
||||
|
||||
<div className="flex gap-2 mb-3 items-center">
|
||||
<button
|
||||
className="btn btn-primary"
|
||||
onClick={handleLoadRelayList}
|
||||
disabled={isLoadingRelayList || !relayPool || !activeAccount}
|
||||
>
|
||||
{isLoadingRelayList ? (
|
||||
<>
|
||||
<FontAwesomeIcon icon={faSpinner} className="animate-spin mr-2" />
|
||||
Loading...
|
||||
</>
|
||||
) : (
|
||||
'Load Relay List'
|
||||
)}
|
||||
</button>
|
||||
<button
|
||||
className="btn btn-secondary ml-auto"
|
||||
onClick={handleClearRelayList}
|
||||
disabled={relayListEvents.length === 0}
|
||||
>
|
||||
Clear
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div className="flex gap-4 mb-3 text-sm">
|
||||
<Stat label="total" value={tLoadRelayList} />
|
||||
<Stat label="first event" value={tFirstRelayList} />
|
||||
</div>
|
||||
{relayListEvents.length > 0 && (
|
||||
<div className="mb-3">
|
||||
<div className="text-sm opacity-70 mb-2">Loaded Relay List Events ({relayListEvents.length}):</div>
|
||||
<div className="space-y-2 max-h-96 overflow-y-auto">
|
||||
{relayListEvents.map((evt, idx) => {
|
||||
const relayTags = evt.tags?.filter((t: string[]) => t[0] === 'r') || []
|
||||
|
||||
return (
|
||||
<div key={idx} className="font-mono text-xs p-2 bg-gray-100 dark:bg-gray-800 rounded">
|
||||
<div className="font-semibold mb-1">Relay List Event #{idx + 1}</div>
|
||||
<div className="opacity-70 mb-1">
|
||||
<div>Kind: {evt.kind}</div>
|
||||
<div>Author: {evt.pubkey.slice(0, 16)}...</div>
|
||||
<div>Created: {new Date(evt.created_at * 1000).toLocaleString()}</div>
|
||||
<div>Relays: {relayTags.length}</div>
|
||||
</div>
|
||||
<div className="mt-1">
|
||||
<div className="text-[11px] opacity-70 mb-1">Relay URLs:</div>
|
||||
{relayTags.map((tag, tagIdx) => (
|
||||
<div key={tagIdx} className="text-[10px] opacity-60 break-all">
|
||||
{tag[1]} {tag[2] ? `(${tag[2]})` : ''}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
<div className="opacity-50 mt-1 text-[10px] break-all">ID: {evt.id}</div>
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Web of Trust Section */}
|
||||
<div className="settings-section">
|
||||
<h3 className="section-title">Web of Trust</h3>
|
||||
|
||||
@@ -13,6 +13,7 @@ import { fetchBlogPostsFromAuthors, BlogPostPreview } from '../services/exploreS
|
||||
import { fetchHighlightsFromAuthors } from '../services/highlightService'
|
||||
import { fetchProfiles } from '../services/profileService'
|
||||
import { fetchNostrverseBlogPosts, fetchNostrverseHighlights } from '../services/nostrverseService'
|
||||
import { nostrverseHighlightsController } from '../services/nostrverseHighlightsController'
|
||||
import { highlightsController } from '../services/highlightsController'
|
||||
import { Highlight } from '../types/highlights'
|
||||
import { UserSettings } from '../services/settingsService'
|
||||
@@ -27,6 +28,9 @@ import { KINDS } from '../config/kinds'
|
||||
import { eventToHighlight } from '../services/highlightEventProcessor'
|
||||
import { useStoreTimeline } from '../hooks/useStoreTimeline'
|
||||
import { dedupeHighlightsById, dedupeWritingsByReplaceable } from '../utils/dedupe'
|
||||
import { writingsController } from '../services/writingsController'
|
||||
import { nostrverseWritingsController } from '../services/nostrverseWritingsController'
|
||||
import { readingProgressController } from '../services/readingProgressController'
|
||||
|
||||
const { getArticleTitle, getArticleImage, getArticlePublished, getArticleSummary } = Helpers
|
||||
|
||||
@@ -48,10 +52,16 @@ const Explore: React.FC<ExploreProps> = ({ relayPool, eventStore, settings, acti
|
||||
const [followedPubkeys, setFollowedPubkeys] = useState<Set<string>>(new Set())
|
||||
const [loading, setLoading] = useState(true)
|
||||
const [refreshTrigger, setRefreshTrigger] = useState(0)
|
||||
const [hasLoadedNostrverse, setHasLoadedNostrverse] = useState(false)
|
||||
const [hasLoadedMine, setHasLoadedMine] = useState(false)
|
||||
const [hasLoadedNostrverseHighlights, setHasLoadedNostrverseHighlights] = useState(false)
|
||||
|
||||
// Get myHighlights directly from controller
|
||||
const [myHighlights, setMyHighlights] = useState<Highlight[]>([])
|
||||
const [myHighlightsLoading, setMyHighlightsLoading] = useState(false)
|
||||
// Remove unused loading state to avoid warnings
|
||||
|
||||
// Reading progress state (naddr -> progress 0-1)
|
||||
const [readingProgressMap, setReadingProgressMap] = useState<Map<string, number>>(new Map())
|
||||
|
||||
// Load cached content from event store (instant display)
|
||||
const cachedHighlights = useStoreTimeline(eventStore, { kinds: [KINDS.Highlights] }, eventToHighlight, [])
|
||||
@@ -66,24 +76,153 @@ const Explore: React.FC<ExploreProps> = ({ relayPool, eventStore, settings, acti
|
||||
}), [])
|
||||
|
||||
const cachedWritings = useStoreTimeline(eventStore, { kinds: [30023] }, toBlogPostPreview, [])
|
||||
|
||||
|
||||
// Visibility filters (defaults from settings)
|
||||
|
||||
// Visibility filters (defaults from settings or nostrverse when logged out)
|
||||
const [visibility, setVisibility] = useState<HighlightVisibility>({
|
||||
nostrverse: settings?.defaultExploreScopeNostrverse ?? false,
|
||||
nostrverse: activeAccount ? (settings?.defaultExploreScopeNostrverse ?? false) : true,
|
||||
friends: settings?.defaultExploreScopeFriends ?? true,
|
||||
mine: settings?.defaultExploreScopeMine ?? false
|
||||
})
|
||||
|
||||
// Ensure at least one scope remains active
|
||||
const toggleScope = useCallback((key: 'nostrverse' | 'friends' | 'mine') => {
|
||||
setVisibility(prev => {
|
||||
const next = { ...prev, [key]: !prev[key] }
|
||||
if (!next.nostrverse && !next.friends && !next.mine) {
|
||||
return prev // ignore toggle that would disable all scopes
|
||||
}
|
||||
return next
|
||||
})
|
||||
}, [])
|
||||
|
||||
// Subscribe to highlights controller
|
||||
useEffect(() => {
|
||||
const unsubHighlights = highlightsController.onHighlights(setMyHighlights)
|
||||
const unsubLoading = highlightsController.onLoading(setMyHighlightsLoading)
|
||||
return () => {
|
||||
unsubHighlights()
|
||||
unsubLoading()
|
||||
}
|
||||
}, [])
|
||||
|
||||
// Subscribe to nostrverse highlights controller for global stream
|
||||
useEffect(() => {
|
||||
const apply = (incoming: Highlight[]) => {
|
||||
setHighlights(prev => {
|
||||
const byId = new Map(prev.map(h => [h.id, h]))
|
||||
for (const h of incoming) byId.set(h.id, h)
|
||||
return Array.from(byId.values()).sort((a, b) => b.created_at - a.created_at)
|
||||
})
|
||||
}
|
||||
// seed immediately
|
||||
apply(nostrverseHighlightsController.getHighlights())
|
||||
const unsub = nostrverseHighlightsController.onHighlights(apply)
|
||||
return () => unsub()
|
||||
}, [])
|
||||
|
||||
// Subscribe to nostrverse writings controller for global stream
|
||||
useEffect(() => {
|
||||
const apply = (incoming: BlogPostPreview[]) => {
|
||||
setBlogPosts(prev => {
|
||||
const byKey = new Map<string, BlogPostPreview>()
|
||||
for (const p of prev) {
|
||||
const dTag = p.event.tags.find(t => t[0] === 'd')?.[1] || ''
|
||||
const key = `${p.author}:${dTag}`
|
||||
byKey.set(key, p)
|
||||
}
|
||||
for (const p of incoming) {
|
||||
const dTag = p.event.tags.find(t => t[0] === 'd')?.[1] || ''
|
||||
const key = `${p.author}:${dTag}`
|
||||
const existing = byKey.get(key)
|
||||
if (!existing || p.event.created_at > existing.event.created_at) byKey.set(key, p)
|
||||
}
|
||||
return Array.from(byKey.values()).sort((a, b) => (b.published || b.event.created_at) - (a.published || a.event.created_at))
|
||||
})
|
||||
}
|
||||
apply(nostrverseWritingsController.getWritings())
|
||||
const unsub = nostrverseWritingsController.onWritings(apply)
|
||||
return () => unsub()
|
||||
}, [])
|
||||
|
||||
// Subscribe to writings controller for "mine" posts and seed immediately
|
||||
useEffect(() => {
|
||||
// Seed from controller's current state
|
||||
const seed = writingsController.getWritings()
|
||||
if (seed.length > 0) {
|
||||
setBlogPosts(prev => {
|
||||
const merged = dedupeWritingsByReplaceable([...prev, ...seed])
|
||||
return merged.sort((a, b) => {
|
||||
const timeA = a.published || a.event.created_at
|
||||
const timeB = b.published || b.event.created_at
|
||||
return timeB - timeA
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// Stream updates
|
||||
const unsub = writingsController.onWritings((posts) => {
|
||||
setBlogPosts(prev => {
|
||||
const merged = dedupeWritingsByReplaceable([...prev, ...posts])
|
||||
return merged.sort((a, b) => {
|
||||
const timeA = a.published || a.event.created_at
|
||||
const timeB = b.published || b.event.created_at
|
||||
return timeB - timeA
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
return () => unsub()
|
||||
}, [])
|
||||
|
||||
// Subscribe to reading progress controller
|
||||
useEffect(() => {
|
||||
// Get initial state immediately
|
||||
const initialMap = readingProgressController.getProgressMap()
|
||||
setReadingProgressMap(initialMap)
|
||||
|
||||
// Subscribe to updates
|
||||
const unsubProgress = readingProgressController.onProgress((newMap) => {
|
||||
setReadingProgressMap(newMap)
|
||||
})
|
||||
|
||||
return () => {
|
||||
unsubProgress()
|
||||
}
|
||||
}, [])
|
||||
|
||||
// Load reading progress data when logged in
|
||||
useEffect(() => {
|
||||
if (!activeAccount?.pubkey) {
|
||||
return
|
||||
}
|
||||
|
||||
readingProgressController.start({
|
||||
relayPool,
|
||||
eventStore,
|
||||
pubkey: activeAccount.pubkey,
|
||||
force: refreshTrigger > 0
|
||||
})
|
||||
}, [activeAccount?.pubkey, relayPool, eventStore, refreshTrigger])
|
||||
|
||||
// Update visibility when settings/login state changes
|
||||
useEffect(() => {
|
||||
if (!activeAccount) {
|
||||
// When logged out, show nostrverse by default
|
||||
setVisibility(prev => ({ ...prev, nostrverse: true, friends: false, mine: false }))
|
||||
setHasLoadedNostrverse(true) // logged out path loads nostrverse immediately
|
||||
setHasLoadedNostrverseHighlights(true)
|
||||
} else {
|
||||
// When logged in, use settings defaults immediately
|
||||
setVisibility({
|
||||
nostrverse: settings?.defaultExploreScopeNostrverse ?? false,
|
||||
friends: settings?.defaultExploreScopeFriends ?? true,
|
||||
mine: settings?.defaultExploreScopeMine ?? false
|
||||
})
|
||||
setHasLoadedNostrverse(false)
|
||||
setHasLoadedNostrverseHighlights(false)
|
||||
}
|
||||
}, [activeAccount, settings?.defaultExploreScopeNostrverse, settings?.defaultExploreScopeFriends, settings?.defaultExploreScopeMine])
|
||||
|
||||
// Update local state when prop changes
|
||||
useEffect(() => {
|
||||
if (propActiveTab) {
|
||||
@@ -93,21 +232,22 @@ const Explore: React.FC<ExploreProps> = ({ relayPool, eventStore, settings, acti
|
||||
|
||||
useEffect(() => {
|
||||
const loadData = async () => {
|
||||
if (!activeAccount) {
|
||||
setLoading(false)
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
// show spinner but keep existing data
|
||||
// begin load, but do not block rendering
|
||||
setLoading(true)
|
||||
|
||||
// If not logged in, only fetch nostrverse content with streaming posts
|
||||
if (!activeAccount) {
|
||||
// Logged out: rely entirely on centralized controllers; do not fetch here
|
||||
setLoading(false)
|
||||
}
|
||||
|
||||
// Seed from in-memory cache if available to avoid empty flash
|
||||
const memoryCachedPosts = getCachedPosts(activeAccount.pubkey)
|
||||
const memoryCachedPosts = activeAccount ? getCachedPosts(activeAccount.pubkey) : []
|
||||
if (memoryCachedPosts && memoryCachedPosts.length > 0) {
|
||||
setBlogPosts(prev => prev.length === 0 ? memoryCachedPosts : prev)
|
||||
}
|
||||
const memoryCachedHighlights = getCachedHighlights(activeAccount.pubkey)
|
||||
const memoryCachedHighlights = activeAccount ? getCachedHighlights(activeAccount.pubkey) : []
|
||||
if (memoryCachedHighlights && memoryCachedHighlights.length > 0) {
|
||||
setHighlights(prev => prev.length === 0 ? memoryCachedHighlights : prev)
|
||||
}
|
||||
@@ -133,10 +273,13 @@ const Explore: React.FC<ExploreProps> = ({ relayPool, eventStore, settings, acti
|
||||
})
|
||||
}
|
||||
|
||||
// At this point, we have seeded any available data; lift the loading state
|
||||
setLoading(false)
|
||||
|
||||
// Fetch the user's contacts (friends)
|
||||
const contacts = await fetchContacts(
|
||||
relayPool,
|
||||
activeAccount.pubkey,
|
||||
activeAccount?.pubkey || '',
|
||||
(partial) => {
|
||||
// Store followed pubkeys for highlight classification
|
||||
setFollowedPubkeys(partial)
|
||||
@@ -184,7 +327,7 @@ const Explore: React.FC<ExploreProps> = ({ relayPool, eventStore, settings, acti
|
||||
return timeB - timeA
|
||||
})
|
||||
})
|
||||
setCachedPosts(activeAccount.pubkey, upsertCachedPost(activeAccount.pubkey, post))
|
||||
if (activeAccount) setCachedPosts(activeAccount.pubkey, upsertCachedPost(activeAccount.pubkey, post))
|
||||
}
|
||||
).then((all) => {
|
||||
setBlogPosts((prev) => {
|
||||
@@ -213,7 +356,7 @@ const Explore: React.FC<ExploreProps> = ({ relayPool, eventStore, settings, acti
|
||||
const timeB = b.published || b.event.created_at
|
||||
return timeB - timeA
|
||||
})
|
||||
setCachedPosts(activeAccount.pubkey, merged)
|
||||
if (activeAccount) setCachedPosts(activeAccount.pubkey, merged)
|
||||
return merged
|
||||
})
|
||||
})
|
||||
@@ -229,14 +372,14 @@ const Explore: React.FC<ExploreProps> = ({ relayPool, eventStore, settings, acti
|
||||
const next = [...prev, highlight]
|
||||
return next.sort((a, b) => b.created_at - a.created_at)
|
||||
})
|
||||
setCachedHighlights(activeAccount.pubkey, upsertCachedHighlight(activeAccount.pubkey, highlight))
|
||||
if (activeAccount) setCachedHighlights(activeAccount.pubkey, upsertCachedHighlight(activeAccount.pubkey, highlight))
|
||||
}
|
||||
).then((all) => {
|
||||
setHighlights((prev) => {
|
||||
const byId = new Map(prev.map(h => [h.id, h]))
|
||||
for (const highlight of all) byId.set(highlight.id, highlight)
|
||||
const merged = Array.from(byId.values()).sort((a, b) => b.created_at - a.created_at)
|
||||
setCachedHighlights(activeAccount.pubkey, merged)
|
||||
if (activeAccount) setCachedHighlights(activeAccount.pubkey, merged)
|
||||
return merged
|
||||
})
|
||||
})
|
||||
@@ -250,52 +393,143 @@ const Explore: React.FC<ExploreProps> = ({ relayPool, eventStore, settings, acti
|
||||
// Store final followed pubkeys
|
||||
setFollowedPubkeys(contacts)
|
||||
|
||||
// Fetch both friends content and nostrverse content in parallel
|
||||
// Fetch friends content and (optionally) nostrverse + mine content in parallel
|
||||
const relayUrls = Array.from(relayPool.relays.values()).map(relay => relay.url)
|
||||
const contactsArray = Array.from(contacts)
|
||||
const [friendsPosts, friendsHighlights, nostrversePosts, nostriverseHighlights] = await Promise.all([
|
||||
fetchBlogPostsFromAuthors(relayPool, contactsArray, relayUrls),
|
||||
fetchHighlightsFromAuthors(relayPool, contactsArray),
|
||||
fetchNostrverseBlogPosts(relayPool, relayUrls, 50, eventStore || undefined),
|
||||
fetchNostrverseHighlights(relayPool, 100, eventStore || undefined)
|
||||
])
|
||||
// Use centralized writingsController for my posts (non-blocking)
|
||||
// pull from writingsController; no need to store promise
|
||||
setBlogPosts(prev => dedupeWritingsByReplaceable([...prev, ...writingsController.getWritings()]).sort((a, b) => (b.published || b.event.created_at) - (a.published || a.event.created_at)))
|
||||
setHasLoadedMine(true)
|
||||
const nostrversePostsPromise = visibility.nostrverse
|
||||
? fetchNostrverseBlogPosts(relayPool, relayUrls, 50, eventStore || undefined, (post) => {
|
||||
// Stream nostrverse posts too when logged in
|
||||
setBlogPosts(prev => {
|
||||
const dTag = post.event.tags.find(t => t[0] === 'd')?.[1] || ''
|
||||
const key = `${post.author}:${dTag}`
|
||||
const existingIndex = prev.findIndex(p => {
|
||||
const pDTag = p.event.tags.find(t => t[0] === 'd')?.[1] || ''
|
||||
return `${p.author}:${pDTag}` === key
|
||||
})
|
||||
if (existingIndex >= 0) {
|
||||
const existing = prev[existingIndex]
|
||||
if (post.event.created_at <= existing.event.created_at) return prev
|
||||
const next = [...prev]
|
||||
next[existingIndex] = post
|
||||
return next.sort((a, b) => (b.published || b.event.created_at) - (a.published || a.event.created_at))
|
||||
}
|
||||
const next = [...prev, post]
|
||||
return next.sort((a, b) => (b.published || b.event.created_at) - (a.published || a.event.created_at))
|
||||
})
|
||||
})
|
||||
: Promise.resolve([] as BlogPostPreview[])
|
||||
|
||||
// Merge and deduplicate all posts
|
||||
const allPosts = [...friendsPosts, ...nostrversePosts]
|
||||
const uniquePosts = dedupeWritingsByReplaceable(allPosts).sort((a, b) => {
|
||||
const timeA = a.published || a.event.created_at
|
||||
const timeB = b.published || b.event.created_at
|
||||
return timeB - timeA
|
||||
})
|
||||
// Fire non-blocking fetches and merge as they resolve
|
||||
fetchBlogPostsFromAuthors(relayPool, contactsArray, relayUrls)
|
||||
.then((friendsPosts) => {
|
||||
setBlogPosts(prev => {
|
||||
const merged = dedupeWritingsByReplaceable([...prev, ...friendsPosts])
|
||||
const sorted = merged.sort((a, b) => (b.published || b.event.created_at) - (a.published || a.event.created_at))
|
||||
if (activeAccount) setCachedPosts(activeAccount.pubkey, sorted)
|
||||
// Pre-cache profiles in background
|
||||
const authorPubkeys = Array.from(new Set(sorted.map(p => p.author)))
|
||||
fetchProfiles(relayPool, eventStore, authorPubkeys, settings).catch(() => {})
|
||||
return sorted
|
||||
})
|
||||
}).catch(() => {})
|
||||
|
||||
// Merge and deduplicate all highlights (mine from controller + friends + nostrverse)
|
||||
const allHighlights = [...myHighlights, ...friendsHighlights, ...nostriverseHighlights]
|
||||
const uniqueHighlights = dedupeHighlightsById(allHighlights).sort((a, b) => b.created_at - a.created_at)
|
||||
fetchHighlightsFromAuthors(relayPool, contactsArray)
|
||||
.then((friendsHighlights) => {
|
||||
setHighlights(prev => {
|
||||
const merged = dedupeHighlightsById([...prev, ...friendsHighlights])
|
||||
const sorted = merged.sort((a, b) => b.created_at - a.created_at)
|
||||
if (activeAccount) setCachedHighlights(activeAccount.pubkey, sorted)
|
||||
return sorted
|
||||
})
|
||||
}).catch(() => {})
|
||||
|
||||
// Fetch profiles for all blog post authors to cache them
|
||||
if (uniquePosts.length > 0) {
|
||||
const authorPubkeys = Array.from(new Set(uniquePosts.map(p => p.author)))
|
||||
fetchProfiles(relayPool, eventStore, authorPubkeys, settings).catch(err => {
|
||||
console.error('Failed to fetch author profiles:', err)
|
||||
})
|
||||
}
|
||||
nostrversePostsPromise.then((nostrversePosts) => {
|
||||
setBlogPosts(prev => dedupeWritingsByReplaceable([...prev, ...nostrversePosts]).sort((a, b) => (b.published || b.event.created_at) - (a.published || a.event.created_at)))
|
||||
}).catch(() => {})
|
||||
|
||||
// No blocking errors - let empty states handle messaging
|
||||
setBlogPosts(uniquePosts)
|
||||
setCachedPosts(activeAccount.pubkey, uniquePosts)
|
||||
|
||||
setHighlights(uniqueHighlights)
|
||||
setCachedHighlights(activeAccount.pubkey, uniqueHighlights)
|
||||
fetchNostrverseHighlights(relayPool, 100, eventStore || undefined)
|
||||
.then((nostriverseHighlights) => {
|
||||
setHighlights(prev => dedupeHighlightsById([...prev, ...nostriverseHighlights]).sort((a, b) => b.created_at - a.created_at))
|
||||
}).catch(() => {})
|
||||
} catch (err) {
|
||||
console.error('Failed to load data:', err)
|
||||
// No blocking error - user can pull-to-refresh
|
||||
} finally {
|
||||
setLoading(false)
|
||||
// loading is already turned off after seeding
|
||||
}
|
||||
}
|
||||
|
||||
loadData()
|
||||
}, [relayPool, activeAccount, refreshTrigger, eventStore, settings, myHighlights, cachedHighlights, cachedWritings])
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [relayPool, activeAccount, refreshTrigger, eventStore, settings])
|
||||
|
||||
// Lazy-load nostrverse writings when user toggles it on (logged in)
|
||||
useEffect(() => {
|
||||
if (!activeAccount || !relayPool || !visibility.nostrverse || hasLoadedNostrverse) return
|
||||
const relayUrls = Array.from(relayPool.relays.values()).map(relay => relay.url)
|
||||
setHasLoadedNostrverse(true)
|
||||
fetchNostrverseBlogPosts(
|
||||
relayPool,
|
||||
relayUrls,
|
||||
50,
|
||||
eventStore || undefined,
|
||||
(post) => {
|
||||
setBlogPosts(prev => {
|
||||
const dTag = post.event.tags.find(t => t[0] === 'd')?.[1] || ''
|
||||
const key = `${post.author}:${dTag}`
|
||||
const existingIndex = prev.findIndex(p => {
|
||||
const pDTag = p.event.tags.find(t => t[0] === 'd')?.[1] || ''
|
||||
return `${p.author}:${pDTag}` === key
|
||||
})
|
||||
if (existingIndex >= 0) {
|
||||
const existing = prev[existingIndex]
|
||||
if (post.event.created_at <= existing.event.created_at) return prev
|
||||
const next = [...prev]
|
||||
next[existingIndex] = post
|
||||
return next.sort((a, b) => (b.published || b.event.created_at) - (a.published || a.event.created_at))
|
||||
}
|
||||
const next = [...prev, post]
|
||||
return next.sort((a, b) => (b.published || b.event.created_at) - (a.published || a.event.created_at))
|
||||
})
|
||||
}
|
||||
).then((finalPosts) => {
|
||||
// Ensure final deduped list
|
||||
setBlogPosts(prev => {
|
||||
const byKey = new Map<string, BlogPostPreview>()
|
||||
for (const p of [...prev, ...finalPosts]) {
|
||||
const dTag = p.event.tags.find(t => t[0] === 'd')?.[1] || ''
|
||||
const key = `${p.author}:${dTag}`
|
||||
const existing = byKey.get(key)
|
||||
if (!existing || p.event.created_at > existing.event.created_at) byKey.set(key, p)
|
||||
}
|
||||
return Array.from(byKey.values()).sort((a, b) => (b.published || b.event.created_at) - (a.published || a.event.created_at))
|
||||
})
|
||||
}).catch(() => {})
|
||||
}, [visibility.nostrverse, activeAccount, relayPool, eventStore, hasLoadedNostrverse])
|
||||
|
||||
// Lazy-load nostrverse highlights when user toggles it on (logged in)
|
||||
useEffect(() => {
|
||||
if (!activeAccount || !relayPool || !visibility.nostrverse || hasLoadedNostrverseHighlights) return
|
||||
setHasLoadedNostrverseHighlights(true)
|
||||
fetchNostrverseHighlights(relayPool, 100, eventStore || undefined)
|
||||
.then((hl) => {
|
||||
if (hl && hl.length > 0) {
|
||||
setHighlights(prev => dedupeHighlightsById([...prev, ...hl]).sort((a, b) => b.created_at - a.created_at))
|
||||
}
|
||||
})
|
||||
.catch(() => {})
|
||||
}, [visibility.nostrverse, activeAccount, relayPool, eventStore, hasLoadedNostrverseHighlights])
|
||||
|
||||
// Lazy-load my writings when user toggles "mine" on (logged in)
|
||||
// No direct fetch here; writingsController streams my posts centrally
|
||||
useEffect(() => {
|
||||
if (!activeAccount || !visibility.mine || hasLoadedMine) return
|
||||
setHasLoadedMine(true)
|
||||
}, [visibility.mine, activeAccount, hasLoadedMine])
|
||||
|
||||
// Pull-to-refresh
|
||||
const { isRefreshing, pullPosition } = usePullToRefresh({
|
||||
@@ -333,10 +567,20 @@ const Explore: React.FC<ExploreProps> = ({ relayPool, eventStore, settings, acti
|
||||
})
|
||||
}, [highlights, activeAccount?.pubkey, followedPubkeys, visibility])
|
||||
|
||||
// Dedupe and sort posts once for rendering
|
||||
const uniqueSortedPosts = useMemo(() => {
|
||||
const unique = dedupeWritingsByReplaceable(blogPosts)
|
||||
return unique.sort((a, b) => {
|
||||
const timeA = a.published || a.event.created_at
|
||||
const timeB = b.published || b.event.created_at
|
||||
return timeB - timeA
|
||||
})
|
||||
}, [blogPosts])
|
||||
|
||||
// Filter blog posts by future dates and visibility, and add level classification
|
||||
const filteredBlogPosts = useMemo(() => {
|
||||
const maxFutureTime = Date.now() / 1000 + (24 * 60 * 60) // 1 day from now
|
||||
return blogPosts
|
||||
return uniqueSortedPosts
|
||||
.filter(post => {
|
||||
// Filter out future dates
|
||||
const publishedTime = post.published || post.event.created_at
|
||||
@@ -360,7 +604,29 @@ const Explore: React.FC<ExploreProps> = ({ relayPool, eventStore, settings, acti
|
||||
const level: 'mine' | 'friends' | 'nostrverse' = isMine ? 'mine' : isFriend ? 'friends' : 'nostrverse'
|
||||
return { ...post, level }
|
||||
})
|
||||
}, [blogPosts, activeAccount, followedPubkeys, visibility])
|
||||
}, [uniqueSortedPosts, activeAccount, followedPubkeys, visibility])
|
||||
|
||||
// Helper to get reading progress for a post
|
||||
const getReadingProgress = useCallback((post: BlogPostPreview): number | undefined => {
|
||||
const dTag = post.event.tags.find(t => t[0] === 'd')?.[1]
|
||||
if (!dTag) {
|
||||
return undefined
|
||||
}
|
||||
|
||||
try {
|
||||
const naddr = nip19.naddrEncode({
|
||||
kind: 30023,
|
||||
pubkey: post.author,
|
||||
identifier: dTag
|
||||
})
|
||||
const progress = readingProgressMap.get(naddr)
|
||||
|
||||
return progress
|
||||
} catch (err) {
|
||||
console.error('[progress] ❌ Error encoding naddr:', err)
|
||||
return undefined
|
||||
}
|
||||
}, [readingProgressMap])
|
||||
|
||||
const renderTabContent = () => {
|
||||
switch (activeTab) {
|
||||
@@ -386,6 +652,7 @@ const Explore: React.FC<ExploreProps> = ({ relayPool, eventStore, settings, acti
|
||||
post={post}
|
||||
href={getPostUrl(post)}
|
||||
level={post.level}
|
||||
readingProgress={getReadingProgress(post)}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
@@ -403,7 +670,7 @@ const Explore: React.FC<ExploreProps> = ({ relayPool, eventStore, settings, acti
|
||||
}
|
||||
return classifiedHighlights.length === 0 ? (
|
||||
<div className="explore-loading" style={{ gridColumn: '1/-1', display: 'flex', justifyContent: 'center', alignItems: 'center', padding: '4rem', color: 'var(--text-secondary)' }}>
|
||||
<FontAwesomeIcon icon={faSpinner} spin size="2x" />
|
||||
<span>No highlights to show for the selected scope.</span>
|
||||
</div>
|
||||
) : (
|
||||
<div className="explore-grid">
|
||||
@@ -422,9 +689,9 @@ const Explore: React.FC<ExploreProps> = ({ relayPool, eventStore, settings, acti
|
||||
}
|
||||
}
|
||||
|
||||
// Show content progressively - no blocking error screens
|
||||
// Show skeletons while first load in this session
|
||||
const hasData = highlights.length > 0 || blogPosts.length > 0
|
||||
const showSkeletons = (loading || myHighlightsLoading) && !hasData
|
||||
const showSkeletons = loading && !hasData
|
||||
|
||||
return (
|
||||
<div className="explore-container">
|
||||
@@ -451,7 +718,7 @@ const Explore: React.FC<ExploreProps> = ({ relayPool, eventStore, settings, acti
|
||||
/>
|
||||
<IconButton
|
||||
icon={faNetworkWired}
|
||||
onClick={() => setVisibility({ ...visibility, nostrverse: !visibility.nostrverse })}
|
||||
onClick={() => toggleScope('nostrverse')}
|
||||
title="Toggle nostrverse content"
|
||||
ariaLabel="Toggle nostrverse content"
|
||||
variant="ghost"
|
||||
@@ -462,7 +729,7 @@ const Explore: React.FC<ExploreProps> = ({ relayPool, eventStore, settings, acti
|
||||
/>
|
||||
<IconButton
|
||||
icon={faUserGroup}
|
||||
onClick={() => setVisibility({ ...visibility, friends: !visibility.friends })}
|
||||
onClick={() => toggleScope('friends')}
|
||||
title={activeAccount ? "Toggle friends content" : "Login to see friends content"}
|
||||
ariaLabel="Toggle friends content"
|
||||
variant="ghost"
|
||||
@@ -474,7 +741,7 @@ const Explore: React.FC<ExploreProps> = ({ relayPool, eventStore, settings, acti
|
||||
/>
|
||||
<IconButton
|
||||
icon={faUser}
|
||||
onClick={() => setVisibility({ ...visibility, mine: !visibility.mine })}
|
||||
onClick={() => toggleScope('mine')}
|
||||
title={activeAccount ? "Toggle my content" : "Login to see your content"}
|
||||
ariaLabel="Toggle my content"
|
||||
variant="ghost"
|
||||
|
||||
@@ -27,7 +27,6 @@ export const HighlightCitation: React.FC<HighlightCitationProps> = ({
|
||||
// Fallback: extract directly from p tag
|
||||
const pTag = highlight.tags.find(t => t[0] === 'p')
|
||||
if (pTag && pTag[1]) {
|
||||
console.log('📝 Found author from p tag:', pTag[1])
|
||||
return pTag[1]
|
||||
}
|
||||
|
||||
@@ -45,6 +44,12 @@ export const HighlightCitation: React.FC<HighlightCitationProps> = ({
|
||||
try {
|
||||
if (!highlight.eventReference) return
|
||||
|
||||
// Skip if it's a raw event ID (hex string without colons)
|
||||
// Raw event IDs cannot be decoded to nadrs without additional context
|
||||
if (!highlight.eventReference.includes(':') && !highlight.eventReference.startsWith('naddr')) {
|
||||
return
|
||||
}
|
||||
|
||||
// Convert eventReference to naddr if needed
|
||||
let naddr: string
|
||||
if (highlight.eventReference.includes(':')) {
|
||||
|
||||
@@ -8,8 +8,8 @@ import { Models, IEventStore } from 'applesauce-core'
|
||||
import { RelayPool } from 'applesauce-relay'
|
||||
import { Hooks } from 'applesauce-react'
|
||||
import { onSyncStateChange, isEventSyncing } from '../services/offlineSyncService'
|
||||
import { RELAYS } from '../config/relays'
|
||||
import { areAllRelaysLocal } from '../utils/helpers'
|
||||
import { getActiveRelayUrls } from '../services/relayManager'
|
||||
import { nip19 } from 'nostr-tools'
|
||||
import { formatDateCompact } from '../utils/bookmarkUtils'
|
||||
import { createDeletionRequest } from '../services/deletionService'
|
||||
@@ -17,6 +17,7 @@ import { getNostrUrl } from '../config/nostrGateways'
|
||||
import CompactButton from './CompactButton'
|
||||
import { HighlightCitation } from './HighlightCitation'
|
||||
import { useNavigate } from 'react-router-dom'
|
||||
import NostrMentionLink from './NostrMentionLink'
|
||||
|
||||
// Helper to detect if a URL is an image
|
||||
const isImageUrl = (url: string): boolean => {
|
||||
@@ -29,99 +30,6 @@ const isImageUrl = (url: string): boolean => {
|
||||
}
|
||||
}
|
||||
|
||||
// Helper to render a nostr identifier
|
||||
const renderNostrId = (nostrUri: string, index: number): React.ReactElement => {
|
||||
try {
|
||||
// Remove nostr: prefix
|
||||
const identifier = nostrUri.replace(/^nostr:/, '')
|
||||
const decoded = nip19.decode(identifier)
|
||||
|
||||
switch (decoded.type) {
|
||||
case 'npub': {
|
||||
const pubkey = decoded.data
|
||||
return (
|
||||
<a
|
||||
key={index}
|
||||
href={`/p/${nip19.npubEncode(pubkey)}`}
|
||||
className="highlight-comment-link"
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
>
|
||||
@{pubkey.slice(0, 8)}...
|
||||
</a>
|
||||
)
|
||||
}
|
||||
case 'nprofile': {
|
||||
const { pubkey } = decoded.data
|
||||
const npub = nip19.npubEncode(pubkey)
|
||||
return (
|
||||
<a
|
||||
key={index}
|
||||
href={`/p/${npub}`}
|
||||
className="highlight-comment-link"
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
>
|
||||
@{pubkey.slice(0, 8)}...
|
||||
</a>
|
||||
)
|
||||
}
|
||||
case 'naddr': {
|
||||
const { kind, pubkey, identifier } = decoded.data
|
||||
// Check if it's a blog post (kind:30023)
|
||||
if (kind === 30023) {
|
||||
const naddr = nip19.naddrEncode({ kind, pubkey, identifier })
|
||||
return (
|
||||
<a
|
||||
key={index}
|
||||
href={`/a/${naddr}`}
|
||||
className="highlight-comment-link"
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
>
|
||||
{identifier || 'Article'}
|
||||
</a>
|
||||
)
|
||||
}
|
||||
// For other kinds, show shortened identifier
|
||||
return (
|
||||
<span key={index} className="highlight-comment-nostr-id">
|
||||
nostr:{identifier.slice(0, 12)}...
|
||||
</span>
|
||||
)
|
||||
}
|
||||
case 'note': {
|
||||
const eventId = decoded.data
|
||||
return (
|
||||
<span key={index} className="highlight-comment-nostr-id">
|
||||
note:{eventId.slice(0, 12)}...
|
||||
</span>
|
||||
)
|
||||
}
|
||||
case 'nevent': {
|
||||
const { id } = decoded.data
|
||||
return (
|
||||
<span key={index} className="highlight-comment-nostr-id">
|
||||
event:{id.slice(0, 12)}...
|
||||
</span>
|
||||
)
|
||||
}
|
||||
default:
|
||||
// Fallback for unrecognized types
|
||||
return (
|
||||
<span key={index} className="highlight-comment-nostr-id">
|
||||
{identifier.slice(0, 20)}...
|
||||
</span>
|
||||
)
|
||||
}
|
||||
} catch (error) {
|
||||
// If decoding fails, show shortened identifier
|
||||
const identifier = nostrUri.replace(/^nostr:/, '')
|
||||
return (
|
||||
<span key={index} className="highlight-comment-nostr-id">
|
||||
{identifier.slice(0, 20)}...
|
||||
</span>
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// Component to render comment with links, inline images, and nostr identifiers
|
||||
const CommentContent: React.FC<{ text: string }> = ({ text }) => {
|
||||
// Pattern to match both http(s) URLs and nostr: URIs
|
||||
@@ -131,9 +39,15 @@ const CommentContent: React.FC<{ text: string }> = ({ text }) => {
|
||||
return (
|
||||
<>
|
||||
{parts.map((part, index) => {
|
||||
// Handle nostr: URIs
|
||||
// Handle nostr: URIs - now with profile resolution
|
||||
if (part.startsWith('nostr:')) {
|
||||
return renderNostrId(part, index)
|
||||
return (
|
||||
<NostrMentionLink
|
||||
key={index}
|
||||
nostrUri={part}
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
// Handle http(s) URLs
|
||||
@@ -236,10 +150,10 @@ export const HighlightItem: React.FC<HighlightItemProps> = ({
|
||||
setShowOfflineIndicator(false)
|
||||
|
||||
// Update the highlight with all relays after successful sync
|
||||
if (onHighlightUpdate && highlight.isLocalOnly) {
|
||||
if (onHighlightUpdate && highlight.isLocalOnly && relayPool) {
|
||||
const updatedHighlight = {
|
||||
...highlight,
|
||||
publishedRelays: RELAYS,
|
||||
publishedRelays: getActiveRelayUrls(relayPool),
|
||||
isLocalOnly: false,
|
||||
isOfflineCreated: false
|
||||
}
|
||||
@@ -250,7 +164,7 @@ export const HighlightItem: React.FC<HighlightItemProps> = ({
|
||||
})
|
||||
|
||||
return unsubscribe
|
||||
}, [highlight, onHighlightUpdate])
|
||||
}, [highlight, onHighlightUpdate, relayPool])
|
||||
|
||||
useEffect(() => {
|
||||
if (isSelected && itemRef.current) {
|
||||
@@ -310,7 +224,8 @@ export const HighlightItem: React.FC<HighlightItemProps> = ({
|
||||
const getHighlightLinks = () => {
|
||||
// Encode the highlight event itself (kind 9802) as a nevent
|
||||
// Get non-local relays for the hint
|
||||
const relayHints = RELAYS.filter(r =>
|
||||
const activeRelays = relayPool ? getActiveRelayUrls(relayPool) : []
|
||||
const relayHints = activeRelays.filter(r =>
|
||||
!r.includes('localhost') && !r.includes('127.0.0.1')
|
||||
).slice(0, 3) // Include up to 3 relay hints
|
||||
|
||||
@@ -346,13 +261,11 @@ export const HighlightItem: React.FC<HighlightItemProps> = ({
|
||||
}
|
||||
|
||||
// Publish to all configured relays - let the relay pool handle connection state
|
||||
const targetRelays = RELAYS
|
||||
const targetRelays = getActiveRelayUrls(relayPool)
|
||||
|
||||
console.log('📡 Rebroadcasting highlight to', targetRelays.length, 'relay(s):', targetRelays)
|
||||
|
||||
await relayPool.publish(targetRelays, event)
|
||||
|
||||
console.log('✅ Rebroadcast successful!')
|
||||
|
||||
// Update the highlight with new relay info
|
||||
const isLocalOnly = areAllRelaysLocal(targetRelays)
|
||||
@@ -416,7 +329,8 @@ export const HighlightItem: React.FC<HighlightItemProps> = ({
|
||||
}
|
||||
|
||||
// Fallback: show all relays we queried (where this was likely fetched from)
|
||||
const relayNames = RELAYS.map(url =>
|
||||
const activeRelays = relayPool ? getActiveRelayUrls(relayPool) : []
|
||||
const relayNames = activeRelays.map(url =>
|
||||
url.replace(/^wss?:\/\//, '').replace(/\/$/, '')
|
||||
)
|
||||
return {
|
||||
@@ -449,7 +363,6 @@ export const HighlightItem: React.FC<HighlightItemProps> = ({
|
||||
relayPool
|
||||
)
|
||||
|
||||
console.log('✅ Highlight deletion request published')
|
||||
|
||||
// Notify parent to remove this highlight from the list
|
||||
if (onHighlightDelete) {
|
||||
|
||||
@@ -46,36 +46,38 @@ const HighlightsPanelHeader: React.FC<HighlightsPanelHeaderProps> = ({
|
||||
opacity: highlightVisibility.nostrverse ? 1 : 0.4
|
||||
}}
|
||||
/>
|
||||
<IconButton
|
||||
icon={faUserGroup}
|
||||
onClick={() => onHighlightVisibilityChange({
|
||||
...highlightVisibility,
|
||||
friends: !highlightVisibility.friends
|
||||
})}
|
||||
title={currentUserPubkey ? "Toggle friends highlights" : "Login to see friends highlights"}
|
||||
ariaLabel="Toggle friends highlights"
|
||||
variant="ghost"
|
||||
disabled={!currentUserPubkey}
|
||||
style={{
|
||||
color: highlightVisibility.friends ? 'var(--highlight-color-friends, #f97316)' : undefined,
|
||||
opacity: highlightVisibility.friends ? 1 : 0.4
|
||||
}}
|
||||
/>
|
||||
<IconButton
|
||||
icon={faUser}
|
||||
onClick={() => onHighlightVisibilityChange({
|
||||
...highlightVisibility,
|
||||
mine: !highlightVisibility.mine
|
||||
})}
|
||||
title={currentUserPubkey ? "Toggle my highlights" : "Login to see your highlights"}
|
||||
ariaLabel="Toggle my highlights"
|
||||
variant="ghost"
|
||||
disabled={!currentUserPubkey}
|
||||
style={{
|
||||
color: highlightVisibility.mine ? 'var(--highlight-color-mine, #eab308)' : undefined,
|
||||
opacity: highlightVisibility.mine ? 1 : 0.4
|
||||
}}
|
||||
/>
|
||||
{currentUserPubkey && (
|
||||
<>
|
||||
<IconButton
|
||||
icon={faUserGroup}
|
||||
onClick={() => onHighlightVisibilityChange({
|
||||
...highlightVisibility,
|
||||
friends: !highlightVisibility.friends
|
||||
})}
|
||||
title="Toggle friends highlights"
|
||||
ariaLabel="Toggle friends highlights"
|
||||
variant="ghost"
|
||||
style={{
|
||||
color: highlightVisibility.friends ? 'var(--highlight-color-friends, #f97316)' : undefined,
|
||||
opacity: highlightVisibility.friends ? 1 : 0.4
|
||||
}}
|
||||
/>
|
||||
<IconButton
|
||||
icon={faUser}
|
||||
onClick={() => onHighlightVisibilityChange({
|
||||
...highlightVisibility,
|
||||
mine: !highlightVisibility.mine
|
||||
})}
|
||||
title="Toggle my highlights"
|
||||
ariaLabel="Toggle my highlights"
|
||||
variant="ghost"
|
||||
style={{
|
||||
color: highlightVisibility.mine ? 'var(--highlight-color-mine, #eab308)' : undefined,
|
||||
opacity: highlightVisibility.mine ? 1 : 0.4
|
||||
}}
|
||||
/>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
{onRefresh && (
|
||||
|
||||
@@ -1,76 +1,68 @@
|
||||
import React, { useState, useEffect, useMemo } from 'react'
|
||||
import React, { useState, useEffect } from 'react'
|
||||
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
|
||||
import { faHighlighter, faBookmark, faList, faThLarge, faImage, faPenToSquare, faLink, faLayerGroup, faBars } from '@fortawesome/free-solid-svg-icons'
|
||||
import { faHighlighter, faBookmark, faPenToSquare, faLink, faLayerGroup, faBars } from '@fortawesome/free-solid-svg-icons'
|
||||
import { Hooks } from 'applesauce-react'
|
||||
import { IEventStore, Helpers } from 'applesauce-core'
|
||||
import { IEventStore } from 'applesauce-core'
|
||||
import { BlogPostSkeleton, HighlightSkeleton, BookmarkSkeleton } from './Skeletons'
|
||||
import { RelayPool } from 'applesauce-relay'
|
||||
import { nip19, NostrEvent } from 'nostr-tools'
|
||||
import { nip19 } from 'nostr-tools'
|
||||
import { useNavigate, useParams } from 'react-router-dom'
|
||||
import { Highlight } from '../types/highlights'
|
||||
import { HighlightItem } from './HighlightItem'
|
||||
import { fetchHighlights } from '../services/highlightService'
|
||||
import { highlightsController } from '../services/highlightsController'
|
||||
import { fetchAllReads, ReadItem } from '../services/readsService'
|
||||
import { writingsController } from '../services/writingsController'
|
||||
import { fetchLinks } from '../services/linksService'
|
||||
import { BlogPostPreview, fetchBlogPostsFromAuthors } from '../services/exploreService'
|
||||
import { RELAYS } from '../config/relays'
|
||||
import { ReadItem, readsController } from '../services/readsController'
|
||||
import { BlogPostPreview } from '../services/exploreService'
|
||||
import { Bookmark, IndividualBookmark } from '../types/bookmarks'
|
||||
import AuthorCard from './AuthorCard'
|
||||
import BlogPostCard from './BlogPostCard'
|
||||
import { BookmarkItem } from './BookmarkItem'
|
||||
import IconButton from './IconButton'
|
||||
import { ViewMode } from './Bookmarks'
|
||||
import { getCachedMeData, updateCachedHighlights } from '../services/meCache'
|
||||
import { faBooks } from '../icons/customIcons'
|
||||
import { usePullToRefresh } from 'use-pull-to-refresh'
|
||||
import RefreshIndicator from './RefreshIndicator'
|
||||
import { groupIndividualBookmarks, hasContent } from '../utils/bookmarkUtils'
|
||||
import { groupIndividualBookmarks, hasContent, hasCreationDate } from '../utils/bookmarkUtils'
|
||||
import BookmarkFilters, { BookmarkFilterType } from './BookmarkFilters'
|
||||
import { filterBookmarksByType } from '../utils/bookmarkTypeClassifier'
|
||||
import ReadingProgressFilters, { ReadingProgressFilterType } from './ReadingProgressFilters'
|
||||
import { filterByReadingProgress } from '../utils/readingProgressUtils'
|
||||
import { deriveReadsFromBookmarks } from '../utils/readsFromBookmarks'
|
||||
import { deriveLinksFromBookmarks } from '../utils/linksFromBookmarks'
|
||||
import { mergeReadItem } from '../utils/readItemMerge'
|
||||
import { useStoreTimeline } from '../hooks/useStoreTimeline'
|
||||
import { eventToHighlight } from '../services/highlightEventProcessor'
|
||||
import { KINDS } from '../config/kinds'
|
||||
|
||||
const { getArticleTitle, getArticleImage, getArticlePublished, getArticleSummary } = Helpers
|
||||
import { readingProgressController } from '../services/readingProgressController'
|
||||
import { archiveController } from '../services/archiveController'
|
||||
import { UserSettings } from '../services/settingsService'
|
||||
|
||||
interface MeProps {
|
||||
relayPool: RelayPool
|
||||
eventStore: IEventStore
|
||||
activeTab?: TabType
|
||||
pubkey?: string // Optional pubkey for viewing other users' profiles
|
||||
bookmarks: Bookmark[] // From centralized App.tsx state
|
||||
bookmarksLoading?: boolean // From centralized App.tsx state (reserved for future use)
|
||||
settings: UserSettings
|
||||
}
|
||||
|
||||
type TabType = 'highlights' | 'reading-list' | 'reads' | 'links' | 'writings'
|
||||
|
||||
// Valid reading progress filters
|
||||
const VALID_FILTERS: ReadingProgressFilterType[] = ['all', 'unopened', 'started', 'reading', 'completed']
|
||||
const VALID_FILTERS: ReadingProgressFilterType[] = ['all', 'unopened', 'started', 'reading', 'completed', 'highlighted', 'archive']
|
||||
|
||||
const Me: React.FC<MeProps> = ({
|
||||
relayPool,
|
||||
eventStore,
|
||||
activeTab: propActiveTab,
|
||||
pubkey: propPubkey,
|
||||
bookmarks
|
||||
activeTab: propActiveTab,
|
||||
bookmarks,
|
||||
settings
|
||||
}) => {
|
||||
const activeAccount = Hooks.useActiveAccount()
|
||||
const navigate = useNavigate()
|
||||
const { filter: urlFilter } = useParams<{ filter?: string }>()
|
||||
const [activeTab, setActiveTab] = useState<TabType>(propActiveTab || 'highlights')
|
||||
|
||||
// Use provided pubkey or fall back to active account
|
||||
const viewingPubkey = propPubkey || activeAccount?.pubkey
|
||||
const isOwnProfile = !propPubkey || (activeAccount?.pubkey === propPubkey)
|
||||
// Only for own profile
|
||||
const viewingPubkey = activeAccount?.pubkey
|
||||
const [highlights, setHighlights] = useState<Highlight[]>([])
|
||||
const [reads, setReads] = useState<ReadItem[]>([])
|
||||
const [, setReadsMap] = useState<Map<string, ReadItem>>(new Map())
|
||||
const [links, setLinks] = useState<ReadItem[]>([])
|
||||
const [, setLinksMap] = useState<Map<string, ReadItem>>(new Map())
|
||||
const [writings, setWritings] = useState<BlogPostPreview[]>([])
|
||||
@@ -81,30 +73,10 @@ const Me: React.FC<MeProps> = ({
|
||||
const [myHighlights, setMyHighlights] = useState<Highlight[]>([])
|
||||
const [myHighlightsLoading, setMyHighlightsLoading] = useState(false)
|
||||
|
||||
// Load cached data from event store for OTHER profiles (not own)
|
||||
const cachedHighlights = useStoreTimeline(
|
||||
eventStore,
|
||||
!isOwnProfile && viewingPubkey ? { kinds: [KINDS.Highlights], authors: [viewingPubkey] } : { kinds: [KINDS.Highlights], limit: 0 },
|
||||
eventToHighlight,
|
||||
[viewingPubkey, isOwnProfile]
|
||||
)
|
||||
// Get myWritings directly from controller
|
||||
const [myWritings, setMyWritings] = useState<BlogPostPreview[]>([])
|
||||
const [myWritingsLoading, setMyWritingsLoading] = useState(false)
|
||||
|
||||
const toBlogPostPreview = useMemo(() => (event: NostrEvent): BlogPostPreview => ({
|
||||
event,
|
||||
title: getArticleTitle(event) || 'Untitled',
|
||||
summary: getArticleSummary(event),
|
||||
image: getArticleImage(event),
|
||||
published: getArticlePublished(event),
|
||||
author: event.pubkey
|
||||
}), [])
|
||||
|
||||
const cachedWritings = useStoreTimeline(
|
||||
eventStore,
|
||||
!isOwnProfile && viewingPubkey ? { kinds: [30023], authors: [viewingPubkey] } : { kinds: [30023], limit: 0 },
|
||||
toBlogPostPreview,
|
||||
[viewingPubkey, isOwnProfile]
|
||||
)
|
||||
const [viewMode, setViewMode] = useState<ViewMode>('cards')
|
||||
const [refreshTrigger, setRefreshTrigger] = useState(0)
|
||||
const [bookmarkFilter, setBookmarkFilter] = useState<BookmarkFilterType>('all')
|
||||
const [groupingMode, setGroupingMode] = useState<'grouped' | 'flat'>(() => {
|
||||
@@ -119,10 +91,15 @@ const Me: React.FC<MeProps> = ({
|
||||
}
|
||||
|
||||
// Initialize reading progress filter from URL param
|
||||
const initialFilter = urlFilter && VALID_FILTERS.includes(urlFilter as ReadingProgressFilterType)
|
||||
? (urlFilter as ReadingProgressFilterType)
|
||||
// Backward compat: map legacy 'emoji' route to 'archive'
|
||||
const normalizedUrlFilter = urlFilter === 'emoji' ? 'archive' : urlFilter
|
||||
const initialFilter = normalizedUrlFilter && VALID_FILTERS.includes(normalizedUrlFilter as ReadingProgressFilterType)
|
||||
? (normalizedUrlFilter as ReadingProgressFilterType)
|
||||
: 'all'
|
||||
const [readingProgressFilter, setReadingProgressFilter] = useState<ReadingProgressFilterType>(initialFilter)
|
||||
|
||||
// Reading progress state for writings tab (naddr -> progress 0-1)
|
||||
const [readingProgressMap, setReadingProgressMap] = useState<Map<string, number>>(new Map())
|
||||
|
||||
// Subscribe to highlights controller
|
||||
useEffect(() => {
|
||||
@@ -138,6 +115,20 @@ const Me: React.FC<MeProps> = ({
|
||||
}
|
||||
}, [])
|
||||
|
||||
// Subscribe to writings controller
|
||||
useEffect(() => {
|
||||
// Get initial state immediately
|
||||
setMyWritings(writingsController.getWritings())
|
||||
|
||||
// Subscribe to updates
|
||||
const unsubWritings = writingsController.onWritings(setMyWritings)
|
||||
const unsubLoading = writingsController.onLoading(setMyWritingsLoading)
|
||||
return () => {
|
||||
unsubWritings()
|
||||
unsubLoading()
|
||||
}
|
||||
}, [])
|
||||
|
||||
// Update local state when prop changes
|
||||
useEffect(() => {
|
||||
if (propActiveTab) {
|
||||
@@ -147,8 +138,9 @@ const Me: React.FC<MeProps> = ({
|
||||
|
||||
// Sync filter state with URL changes
|
||||
useEffect(() => {
|
||||
const filterFromUrl = urlFilter && VALID_FILTERS.includes(urlFilter as ReadingProgressFilterType)
|
||||
? (urlFilter as ReadingProgressFilterType)
|
||||
const normalized = urlFilter === 'emoji' ? 'archive' : urlFilter
|
||||
const filterFromUrl = normalized && VALID_FILTERS.includes(normalized as ReadingProgressFilterType)
|
||||
? (normalized as ReadingProgressFilterType)
|
||||
: 'all'
|
||||
setReadingProgressFilter(filterFromUrl)
|
||||
}, [urlFilter])
|
||||
@@ -162,70 +154,86 @@ const Me: React.FC<MeProps> = ({
|
||||
} else {
|
||||
navigate(`/me/reads/${filter}`, { replace: true })
|
||||
}
|
||||
} else if (activeTab === 'links') {
|
||||
if (filter === 'all') {
|
||||
navigate('/me/links', { replace: true })
|
||||
} else {
|
||||
navigate(`/me/links/${filter}`, { replace: true })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Subscribe to reads controller
|
||||
useEffect(() => {
|
||||
// Get initial state immediately
|
||||
setReads(readsController.getReads())
|
||||
|
||||
// Subscribe to updates
|
||||
const unsubReads = readsController.onReads(setReads)
|
||||
|
||||
return () => {
|
||||
unsubReads()
|
||||
}
|
||||
}, [])
|
||||
|
||||
// Subscribe to reading progress map for writings and links enrichment
|
||||
useEffect(() => {
|
||||
// Get initial state immediately
|
||||
setReadingProgressMap(readingProgressController.getProgressMap())
|
||||
|
||||
// Subscribe to updates
|
||||
const unsubProgress = readingProgressController.onProgress(setReadingProgressMap)
|
||||
|
||||
return () => {
|
||||
unsubProgress()
|
||||
}
|
||||
}, [])
|
||||
|
||||
|
||||
// Load reading progress data for writings tab
|
||||
useEffect(() => {
|
||||
if (!viewingPubkey) {
|
||||
return
|
||||
}
|
||||
|
||||
readingProgressController.start({
|
||||
relayPool,
|
||||
eventStore,
|
||||
pubkey: viewingPubkey,
|
||||
force: refreshTrigger > 0
|
||||
})
|
||||
}, [viewingPubkey, relayPool, eventStore, refreshTrigger])
|
||||
|
||||
// Tab-specific loading functions
|
||||
const loadHighlightsTab = async () => {
|
||||
if (!viewingPubkey) return
|
||||
|
||||
// Only show loading skeleton if tab hasn't been loaded yet
|
||||
const hasBeenLoaded = loadedTabs.has('highlights')
|
||||
|
||||
try {
|
||||
if (!hasBeenLoaded) setLoading(true)
|
||||
|
||||
// For own profile, highlights come from controller subscription (sync effect handles it)
|
||||
// For viewing other users, seed with cached data then fetch fresh
|
||||
if (!isOwnProfile) {
|
||||
// Seed with cached highlights first
|
||||
if (cachedHighlights.length > 0) {
|
||||
setHighlights(cachedHighlights.sort((a, b) => b.created_at - a.created_at))
|
||||
}
|
||||
|
||||
// Fetch fresh highlights
|
||||
const userHighlights = await fetchHighlights(relayPool, viewingPubkey)
|
||||
setHighlights(userHighlights)
|
||||
}
|
||||
|
||||
setLoadedTabs(prev => new Set(prev).add('highlights'))
|
||||
} catch (err) {
|
||||
console.error('Failed to load highlights:', err)
|
||||
} finally {
|
||||
if (!hasBeenLoaded) setLoading(false)
|
||||
}
|
||||
// Highlights come from controller subscription (sync effect handles it)
|
||||
setLoadedTabs(prev => new Set(prev).add('highlights'))
|
||||
setLoading(false)
|
||||
}
|
||||
|
||||
const loadWritingsTab = async () => {
|
||||
if (!viewingPubkey) return
|
||||
|
||||
const hasBeenLoaded = loadedTabs.has('writings')
|
||||
|
||||
try {
|
||||
if (!hasBeenLoaded) setLoading(true)
|
||||
|
||||
// Seed with cached writings first
|
||||
if (!isOwnProfile && cachedWritings.length > 0) {
|
||||
setWritings(cachedWritings.sort((a, b) => {
|
||||
const timeA = a.published || a.event.created_at
|
||||
const timeB = b.published || b.event.created_at
|
||||
return timeB - timeA
|
||||
}))
|
||||
}
|
||||
|
||||
// Fetch fresh writings
|
||||
const userWritings = await fetchBlogPostsFromAuthors(relayPool, [viewingPubkey], RELAYS)
|
||||
setWritings(userWritings)
|
||||
// Use centralized controller
|
||||
await writingsController.start({
|
||||
relayPool,
|
||||
eventStore,
|
||||
pubkey: viewingPubkey,
|
||||
force: refreshTrigger > 0
|
||||
})
|
||||
setLoadedTabs(prev => new Set(prev).add('writings'))
|
||||
setLoading(false)
|
||||
} catch (err) {
|
||||
console.error('Failed to load writings:', err)
|
||||
} finally {
|
||||
if (!hasBeenLoaded) setLoading(false)
|
||||
setLoading(false)
|
||||
}
|
||||
}
|
||||
|
||||
const loadReadingListTab = async () => {
|
||||
if (!viewingPubkey || !isOwnProfile || !activeAccount) return
|
||||
if (!viewingPubkey || !activeAccount) return
|
||||
|
||||
const hasBeenLoaded = loadedTabs.has('reading-list')
|
||||
|
||||
@@ -241,49 +249,23 @@ const Me: React.FC<MeProps> = ({
|
||||
}
|
||||
|
||||
const loadReadsTab = async () => {
|
||||
if (!viewingPubkey || !isOwnProfile || !activeAccount) return
|
||||
if (!viewingPubkey || !activeAccount) return
|
||||
|
||||
const hasBeenLoaded = loadedTabs.has('reads')
|
||||
|
||||
try {
|
||||
if (!hasBeenLoaded) setLoading(true)
|
||||
|
||||
// Derive reads from bookmarks immediately (bookmarks come from centralized loading in App.tsx)
|
||||
const initialReads = deriveReadsFromBookmarks(bookmarks)
|
||||
const initialMap = new Map(initialReads.map(item => [item.id, item]))
|
||||
setReadsMap(initialMap)
|
||||
setReads(initialReads)
|
||||
// Use readsController to get reads with progressive hydration
|
||||
await readsController.start({
|
||||
relayPool,
|
||||
eventStore,
|
||||
pubkey: viewingPubkey
|
||||
})
|
||||
|
||||
setLoadedTabs(prev => new Set(prev).add('reads'))
|
||||
if (!hasBeenLoaded) setLoading(false)
|
||||
|
||||
// Background enrichment: merge reading progress and mark-as-read
|
||||
// Only update items that are already in our map
|
||||
fetchAllReads(relayPool, viewingPubkey, bookmarks, (item) => {
|
||||
console.log('📈 [Reads] Enrichment item received:', {
|
||||
id: item.id.slice(0, 20) + '...',
|
||||
progress: item.readingProgress,
|
||||
hasProgress: item.readingProgress !== undefined && item.readingProgress > 0
|
||||
})
|
||||
|
||||
setReadsMap(prevMap => {
|
||||
// Only update if item exists in our current map
|
||||
if (!prevMap.has(item.id)) {
|
||||
console.log('⚠️ [Reads] Item not in map, skipping:', item.id.slice(0, 20) + '...')
|
||||
return prevMap
|
||||
}
|
||||
|
||||
const newMap = new Map(prevMap)
|
||||
const merged = mergeReadItem(newMap, item)
|
||||
if (merged) {
|
||||
console.log('✅ [Reads] Merged progress:', item.id.slice(0, 20) + '...', item.readingProgress)
|
||||
// Update reads array after map is updated
|
||||
setReads(Array.from(newMap.values()))
|
||||
return newMap
|
||||
}
|
||||
return prevMap
|
||||
})
|
||||
}).catch(err => console.warn('Failed to enrich reads:', err))
|
||||
|
||||
} catch (err) {
|
||||
console.error('Failed to load reads:', err)
|
||||
if (!hasBeenLoaded) setLoading(false)
|
||||
@@ -291,7 +273,7 @@ const Me: React.FC<MeProps> = ({
|
||||
}
|
||||
|
||||
const loadLinksTab = async () => {
|
||||
if (!viewingPubkey || !isOwnProfile || !activeAccount) return
|
||||
if (!viewingPubkey || !activeAccount) return
|
||||
|
||||
const hasBeenLoaded = loadedTabs.has('links')
|
||||
|
||||
@@ -314,12 +296,13 @@ const Me: React.FC<MeProps> = ({
|
||||
if (!prevMap.has(item.id)) return prevMap
|
||||
|
||||
const newMap = new Map(prevMap)
|
||||
if (mergeReadItem(newMap, item)) {
|
||||
// Update links array after map is updated
|
||||
setLinks(Array.from(newMap.values()))
|
||||
return newMap
|
||||
if (item.type === 'article' && item.author) {
|
||||
const progress = readingProgressMap.get(item.id)
|
||||
if (progress !== undefined) {
|
||||
newMap.set(item.id, { ...item, readingProgress: progress })
|
||||
}
|
||||
}
|
||||
return prevMap
|
||||
return newMap
|
||||
})
|
||||
}).catch(err => console.warn('Failed to enrich links:', err))
|
||||
|
||||
@@ -337,14 +320,12 @@ const Me: React.FC<MeProps> = ({
|
||||
}
|
||||
|
||||
// Load cached data immediately if available
|
||||
if (isOwnProfile) {
|
||||
const cached = getCachedMeData(viewingPubkey)
|
||||
if (cached) {
|
||||
setHighlights(cached.highlights)
|
||||
// Bookmarks come from App.tsx centralized state, no local caching needed
|
||||
setReads(cached.reads || [])
|
||||
setLinks(cached.links || [])
|
||||
}
|
||||
const cached = getCachedMeData(viewingPubkey)
|
||||
if (cached) {
|
||||
setHighlights(cached.highlights)
|
||||
// Bookmarks come from App.tsx centralized state, no local caching needed
|
||||
setReads(cached.reads || [])
|
||||
setLinks(cached.links || [])
|
||||
}
|
||||
|
||||
// Load data for active tab (refresh in background if already loaded)
|
||||
@@ -366,14 +347,17 @@ const Me: React.FC<MeProps> = ({
|
||||
break
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [activeTab, viewingPubkey, refreshTrigger])
|
||||
}, [activeTab, viewingPubkey, refreshTrigger, bookmarks])
|
||||
|
||||
// Sync myHighlights from controller when viewing own profile
|
||||
// Sync myHighlights from controller
|
||||
useEffect(() => {
|
||||
if (isOwnProfile) {
|
||||
setHighlights(myHighlights)
|
||||
}
|
||||
}, [isOwnProfile, myHighlights])
|
||||
setHighlights(myHighlights)
|
||||
}, [myHighlights])
|
||||
|
||||
// Sync myWritings from controller
|
||||
useEffect(() => {
|
||||
setWritings(myWritings)
|
||||
}, [myWritings])
|
||||
|
||||
// Pull-to-refresh - reload active tab without clearing state
|
||||
const { isRefreshing, pullPosition } = usePullToRefresh({
|
||||
@@ -389,8 +373,8 @@ const Me: React.FC<MeProps> = ({
|
||||
const handleHighlightDelete = (highlightId: string) => {
|
||||
setHighlights(prev => {
|
||||
const updated = prev.filter(h => h.id !== highlightId)
|
||||
// Update cache when highlight is deleted (own profile only)
|
||||
if (isOwnProfile && viewingPubkey) {
|
||||
// Update cache when highlight is deleted
|
||||
if (viewingPubkey) {
|
||||
updateCachedHighlights(viewingPubkey, updated)
|
||||
}
|
||||
return updated
|
||||
@@ -468,33 +452,132 @@ const Me: React.FC<MeProps> = ({
|
||||
navigate(`/r/${encodeURIComponent(url)}`)
|
||||
}
|
||||
}
|
||||
|
||||
// Helper to get reading progress for a post
|
||||
const getWritingReadingProgress = (post: BlogPostPreview): number | undefined => {
|
||||
const dTag = post.event.tags.find(t => t[0] === 'd')?.[1]
|
||||
if (!dTag) return undefined
|
||||
|
||||
try {
|
||||
const naddr = nip19.naddrEncode({
|
||||
kind: 30023,
|
||||
pubkey: post.author,
|
||||
identifier: dTag
|
||||
})
|
||||
return readingProgressMap.get(naddr)
|
||||
} catch (err) {
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
|
||||
// Helper to get reading progress for a bookmark
|
||||
const getBookmarkReadingProgress = (bookmark: IndividualBookmark): number | undefined => {
|
||||
if (bookmark.kind === 30023) {
|
||||
const dTag = bookmark.tags.find(t => t[0] === 'd')?.[1]
|
||||
if (!dTag) return undefined
|
||||
try {
|
||||
const naddr = nip19.naddrEncode({
|
||||
kind: 30023,
|
||||
pubkey: bookmark.pubkey,
|
||||
identifier: dTag
|
||||
})
|
||||
return readingProgressMap.get(naddr)
|
||||
} catch (err) {
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
// Merge and flatten all individual bookmarks
|
||||
const allIndividualBookmarks = bookmarks.flatMap(b => b.individualBookmarks || [])
|
||||
.filter(hasContent)
|
||||
.filter(b => !settings?.hideBookmarksWithoutCreationDate || hasCreationDate(b))
|
||||
|
||||
// Apply bookmark filter
|
||||
const filteredBookmarks = filterBookmarksByType(allIndividualBookmarks, bookmarkFilter)
|
||||
|
||||
const groups = groupIndividualBookmarks(filteredBookmarks)
|
||||
|
||||
// Apply reading progress filter
|
||||
const filteredReads = filterByReadingProgress(reads, readingProgressFilter)
|
||||
const filteredLinks = filterByReadingProgress(links, readingProgressFilter)
|
||||
// Enrich links with reading progress (reads already have progress from controller)
|
||||
const linksWithProgress = links.map(item => {
|
||||
if (item.url) {
|
||||
const progress = readingProgressMap.get(item.url)
|
||||
if (progress !== undefined) {
|
||||
return { ...item, readingProgress: progress }
|
||||
}
|
||||
}
|
||||
return item
|
||||
})
|
||||
|
||||
// Apply reading progress filter with simple type separation to keep Views distinct and DRY
|
||||
const filteredReads = filterByReadingProgress(
|
||||
reads.filter(item => item.type === 'article'),
|
||||
readingProgressFilter,
|
||||
highlights
|
||||
)
|
||||
const filteredLinks = filterByReadingProgress(
|
||||
linksWithProgress.filter(item => item.type === 'external'),
|
||||
readingProgressFilter,
|
||||
highlights
|
||||
)
|
||||
|
||||
// Helper: build archive-only list from marked IDs and a base list
|
||||
const buildArchiveOnly = (
|
||||
baseItems: ReadItem[],
|
||||
options: { kind: 'article' | 'external' }
|
||||
): ReadItem[] => {
|
||||
const allMarked = archiveController.getMarkedIds()
|
||||
const relevantMarked = options.kind === 'article'
|
||||
? allMarked.filter(id => id.startsWith('naddr1'))
|
||||
: allMarked.filter(id => !id.startsWith('naddr1'))
|
||||
const markedSet = new Set(relevantMarked)
|
||||
|
||||
const items: ReadItem[] = []
|
||||
for (const item of baseItems) {
|
||||
const key = options.kind === 'article' ? item.id : (item.url || item.id)
|
||||
if (key && markedSet.has(key)) {
|
||||
items.push({ ...item, markedAsRead: true })
|
||||
}
|
||||
}
|
||||
for (const id of markedSet) {
|
||||
const exists = items.find(i => (options.kind === 'article' ? i.id : (i.url || i.id)) === id)
|
||||
if (!exists) {
|
||||
items.push({
|
||||
id,
|
||||
source: 'marked-as-read',
|
||||
type: options.kind,
|
||||
url: options.kind === 'article' ? undefined : id,
|
||||
markedAsRead: true,
|
||||
readingTimestamp: Math.floor(Date.now() / 1000)
|
||||
})
|
||||
}
|
||||
}
|
||||
return items
|
||||
}
|
||||
|
||||
// Archive-only lists: independent of reading progress
|
||||
const archiveOnlyReads: ReadItem[] = readingProgressFilter === 'archive'
|
||||
? buildArchiveOnly(reads, { kind: 'article' })
|
||||
: []
|
||||
const archiveOnlyLinks: ReadItem[] = readingProgressFilter === 'archive'
|
||||
? buildArchiveOnly(linksWithProgress, { kind: 'external' })
|
||||
: []
|
||||
|
||||
const sections: Array<{ key: string; title: string; items: IndividualBookmark[] }> =
|
||||
groupingMode === 'flat'
|
||||
? [{ key: 'all', title: `All Bookmarks (${filteredBookmarks.length})`, items: filteredBookmarks }]
|
||||
: [
|
||||
{ key: 'nip51-private', title: 'Private Bookmarks', items: groups.nip51Private },
|
||||
{ key: 'nip51-public', title: 'My Bookmarks', items: groups.nip51Public },
|
||||
{ key: 'amethyst-private', title: 'Amethyst Private', items: groups.amethystPrivate },
|
||||
{ key: 'amethyst-public', title: 'Amethyst Lists', items: groups.amethystPublic },
|
||||
{ key: 'amethyst-private', title: 'Private Lists', items: groups.amethystPrivate },
|
||||
{ key: 'amethyst-public', title: 'My Lists', items: groups.amethystPublic },
|
||||
{ key: 'web', title: 'Web Bookmarks', items: groups.standaloneWeb }
|
||||
]
|
||||
|
||||
// Show content progressively - no blocking error screens
|
||||
const hasData = highlights.length > 0 || bookmarks.length > 0 || reads.length > 0 || links.length > 0 || writings.length > 0
|
||||
const showSkeletons = (loading || (isOwnProfile && myHighlightsLoading)) && !hasData
|
||||
const showSkeletons = (loading || myHighlightsLoading) && !hasData
|
||||
|
||||
const renderTabContent = () => {
|
||||
switch (activeTab) {
|
||||
@@ -508,7 +591,7 @@ const Me: React.FC<MeProps> = ({
|
||||
</div>
|
||||
)
|
||||
}
|
||||
return highlights.length === 0 && !loading && !(isOwnProfile && myHighlightsLoading) ? (
|
||||
return highlights.length === 0 && !loading && !myHighlightsLoading ? (
|
||||
<div className="explore-loading" style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', padding: '4rem', color: 'var(--text-secondary)' }}>
|
||||
No highlights yet.
|
||||
</div>
|
||||
@@ -529,9 +612,9 @@ const Me: React.FC<MeProps> = ({
|
||||
if (showSkeletons) {
|
||||
return (
|
||||
<div className="bookmarks-list">
|
||||
<div className={`bookmarks-grid bookmarks-${viewMode}`}>
|
||||
<div className="bookmarks-grid bookmarks-cards">
|
||||
{Array.from({ length: 6 }).map((_, i) => (
|
||||
<BookmarkSkeleton key={i} viewMode={viewMode} />
|
||||
<BookmarkSkeleton key={i} viewMode="cards" />
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
@@ -557,14 +640,15 @@ const Me: React.FC<MeProps> = ({
|
||||
sections.filter(s => s.items.length > 0).map(section => (
|
||||
<div key={section.key} className="bookmarks-section">
|
||||
<h3 className="bookmarks-section-title">{section.title}</h3>
|
||||
<div className={`bookmarks-grid bookmarks-${viewMode}`}>
|
||||
<div className="bookmarks-grid bookmarks-cards">
|
||||
{section.items.map((individualBookmark, index) => (
|
||||
<BookmarkItem
|
||||
key={`${section.key}-${individualBookmark.id}-${index}`}
|
||||
bookmark={individualBookmark}
|
||||
index={index}
|
||||
viewMode={viewMode}
|
||||
viewMode="cards"
|
||||
onSelectUrl={handleSelectUrl}
|
||||
readingProgress={getBookmarkReadingProgress(individualBookmark)}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
@@ -585,27 +669,6 @@ const Me: React.FC<MeProps> = ({
|
||||
ariaLabel={groupingMode === 'grouped' ? 'Switch to flat view' : 'Switch to grouped view'}
|
||||
variant="ghost"
|
||||
/>
|
||||
<IconButton
|
||||
icon={faList}
|
||||
onClick={() => setViewMode('compact')}
|
||||
title="Compact list view"
|
||||
ariaLabel="Compact list view"
|
||||
variant={viewMode === 'compact' ? 'primary' : 'ghost'}
|
||||
/>
|
||||
<IconButton
|
||||
icon={faThLarge}
|
||||
onClick={() => setViewMode('cards')}
|
||||
title="Cards view"
|
||||
ariaLabel="Cards view"
|
||||
variant={viewMode === 'cards' ? 'primary' : 'ghost'}
|
||||
/>
|
||||
<IconButton
|
||||
icon={faImage}
|
||||
onClick={() => setViewMode('large')}
|
||||
title="Large preview view"
|
||||
ariaLabel="Large preview view"
|
||||
variant={viewMode === 'large' ? 'primary' : 'ghost'}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
@@ -638,21 +701,42 @@ const Me: React.FC<MeProps> = ({
|
||||
selectedFilter={readingProgressFilter}
|
||||
onFilterChange={handleReadingProgressFilterChange}
|
||||
/>
|
||||
{filteredReads.length === 0 ? (
|
||||
<div className="explore-loading" style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', padding: '4rem', color: 'var(--text-secondary)' }}>
|
||||
No articles match this filter.
|
||||
</div>
|
||||
{readingProgressFilter === 'archive' ? (
|
||||
archiveOnlyReads.length === 0 ? (
|
||||
<div className="explore-loading" style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', padding: '4rem', color: 'var(--text-secondary)' }}>
|
||||
No articles in archive.
|
||||
</div>
|
||||
) : (
|
||||
<div className="explore-grid">
|
||||
{archiveOnlyReads
|
||||
.filter(item => item.type === 'article')
|
||||
.map((item) => (
|
||||
<BlogPostCard
|
||||
key={item.id}
|
||||
post={convertReadItemToBlogPostPreview(item)}
|
||||
href={getReadItemUrl(item)}
|
||||
readingProgress={item.readingProgress}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
)
|
||||
) : (
|
||||
<div className="explore-grid">
|
||||
{filteredReads.map((item) => (
|
||||
<BlogPostCard
|
||||
key={item.id}
|
||||
post={convertReadItemToBlogPostPreview(item)}
|
||||
href={getReadItemUrl(item)}
|
||||
readingProgress={item.readingProgress}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
filteredReads.length === 0 ? (
|
||||
<div className="explore-loading" style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', padding: '4rem', color: 'var(--text-secondary)' }}>
|
||||
No articles match this filter.
|
||||
</div>
|
||||
) : (
|
||||
<div className="explore-grid">
|
||||
{filteredReads.map((item) => (
|
||||
<BlogPostCard
|
||||
key={item.id}
|
||||
post={convertReadItemToBlogPostPreview(item)}
|
||||
href={getReadItemUrl(item)}
|
||||
readingProgress={item.readingProgress}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
)
|
||||
)}
|
||||
</>
|
||||
)
|
||||
@@ -685,21 +769,40 @@ const Me: React.FC<MeProps> = ({
|
||||
selectedFilter={readingProgressFilter}
|
||||
onFilterChange={handleReadingProgressFilterChange}
|
||||
/>
|
||||
{filteredLinks.length === 0 ? (
|
||||
<div className="explore-loading" style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', padding: '4rem', color: 'var(--text-secondary)' }}>
|
||||
No links match this filter.
|
||||
</div>
|
||||
{readingProgressFilter === 'archive' ? (
|
||||
archiveOnlyLinks.length === 0 ? (
|
||||
<div className="explore-loading" style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', padding: '4rem', color: 'var(--text-secondary)' }}>
|
||||
No links in archive.
|
||||
</div>
|
||||
) : (
|
||||
<div className="explore-grid">
|
||||
{archiveOnlyLinks.map((item) => (
|
||||
<BlogPostCard
|
||||
key={item.id}
|
||||
post={convertReadItemToBlogPostPreview(item)}
|
||||
href={getReadItemUrl(item)}
|
||||
readingProgress={item.readingProgress}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
)
|
||||
) : (
|
||||
<div className="explore-grid">
|
||||
{filteredLinks.map((item) => (
|
||||
<BlogPostCard
|
||||
key={item.id}
|
||||
post={convertReadItemToBlogPostPreview(item)}
|
||||
href={getReadItemUrl(item)}
|
||||
readingProgress={item.readingProgress}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
filteredLinks.length === 0 ? (
|
||||
<div className="explore-loading" style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', padding: '4rem', color: 'var(--text-secondary)' }}>
|
||||
No links match this filter.
|
||||
</div>
|
||||
) : (
|
||||
<div className="explore-grid">
|
||||
{filteredLinks.map((item) => (
|
||||
<BlogPostCard
|
||||
key={item.id}
|
||||
post={convertReadItemToBlogPostPreview(item)}
|
||||
href={getReadItemUrl(item)}
|
||||
readingProgress={item.readingProgress}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
)
|
||||
)}
|
||||
</>
|
||||
)
|
||||
@@ -714,7 +817,7 @@ const Me: React.FC<MeProps> = ({
|
||||
</div>
|
||||
)
|
||||
}
|
||||
return writings.length === 0 && !loading ? (
|
||||
return writings.length === 0 && !loading && !myWritingsLoading ? (
|
||||
<div className="explore-loading" style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', padding: '4rem', color: 'var(--text-secondary)' }}>
|
||||
No articles written yet.
|
||||
</div>
|
||||
@@ -725,6 +828,7 @@ const Me: React.FC<MeProps> = ({
|
||||
key={post.event.id}
|
||||
post={post}
|
||||
href={getPostUrl(post)}
|
||||
readingProgress={getWritingReadingProgress(post)}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
@@ -748,43 +852,39 @@ const Me: React.FC<MeProps> = ({
|
||||
<button
|
||||
className={`me-tab ${activeTab === 'highlights' ? 'active' : ''}`}
|
||||
data-tab="highlights"
|
||||
onClick={() => navigate(isOwnProfile ? '/me/highlights' : `/p/${propPubkey && nip19.npubEncode(propPubkey)}`)}
|
||||
onClick={() => navigate('/me/highlights')}
|
||||
>
|
||||
<FontAwesomeIcon icon={faHighlighter} />
|
||||
<span className="tab-label">Highlights</span>
|
||||
</button>
|
||||
{isOwnProfile && (
|
||||
<>
|
||||
<button
|
||||
className={`me-tab ${activeTab === 'reading-list' ? 'active' : ''}`}
|
||||
data-tab="reading-list"
|
||||
onClick={() => navigate('/me/reading-list')}
|
||||
>
|
||||
<FontAwesomeIcon icon={faBookmark} />
|
||||
<span className="tab-label">Bookmarks</span>
|
||||
</button>
|
||||
<button
|
||||
className={`me-tab ${activeTab === 'reads' ? 'active' : ''}`}
|
||||
data-tab="reads"
|
||||
onClick={() => navigate('/me/reads')}
|
||||
>
|
||||
<FontAwesomeIcon icon={faBooks} />
|
||||
<span className="tab-label">Reads</span>
|
||||
</button>
|
||||
<button
|
||||
className={`me-tab ${activeTab === 'links' ? 'active' : ''}`}
|
||||
data-tab="links"
|
||||
onClick={() => navigate('/me/links')}
|
||||
>
|
||||
<FontAwesomeIcon icon={faLink} />
|
||||
<span className="tab-label">Links</span>
|
||||
</button>
|
||||
</>
|
||||
)}
|
||||
<button
|
||||
className={`me-tab ${activeTab === 'reading-list' ? 'active' : ''}`}
|
||||
data-tab="reading-list"
|
||||
onClick={() => navigate('/me/reading-list')}
|
||||
>
|
||||
<FontAwesomeIcon icon={faBookmark} />
|
||||
<span className="tab-label">Bookmarks</span>
|
||||
</button>
|
||||
<button
|
||||
className={`me-tab ${activeTab === 'reads' ? 'active' : ''}`}
|
||||
data-tab="reads"
|
||||
onClick={() => navigate('/me/reads')}
|
||||
>
|
||||
<FontAwesomeIcon icon={faBooks} />
|
||||
<span className="tab-label">Reads</span>
|
||||
</button>
|
||||
<button
|
||||
className={`me-tab ${activeTab === 'links' ? 'active' : ''}`}
|
||||
data-tab="links"
|
||||
onClick={() => navigate('/me/links')}
|
||||
>
|
||||
<FontAwesomeIcon icon={faLink} />
|
||||
<span className="tab-label">Links</span>
|
||||
</button>
|
||||
<button
|
||||
className={`me-tab ${activeTab === 'writings' ? 'active' : ''}`}
|
||||
data-tab="writings"
|
||||
onClick={() => navigate(isOwnProfile ? '/me/writings' : `/p/${propPubkey && nip19.npubEncode(propPubkey)}/writings`)}
|
||||
onClick={() => navigate('/me/writings')}
|
||||
>
|
||||
<FontAwesomeIcon icon={faPenToSquare} />
|
||||
<span className="tab-label">Writings</span>
|
||||
|
||||
134
src/components/NostrMentionLink.tsx
Normal file
134
src/components/NostrMentionLink.tsx
Normal file
@@ -0,0 +1,134 @@
|
||||
import React from 'react'
|
||||
import { nip19 } from 'nostr-tools'
|
||||
import { useEventModel } from 'applesauce-react/hooks'
|
||||
import { Models } from 'applesauce-core'
|
||||
|
||||
interface NostrMentionLinkProps {
|
||||
nostrUri: string
|
||||
onClick?: (e: React.MouseEvent) => void
|
||||
className?: string
|
||||
}
|
||||
|
||||
/**
|
||||
* Component to render nostr mentions with resolved profile names
|
||||
* Handles npub, nprofile, note, nevent, and naddr URIs
|
||||
*/
|
||||
const NostrMentionLink: React.FC<NostrMentionLinkProps> = ({
|
||||
nostrUri,
|
||||
onClick,
|
||||
className = 'highlight-comment-link'
|
||||
}) => {
|
||||
// Decode the nostr URI first
|
||||
let decoded: ReturnType<typeof nip19.decode> | null = null
|
||||
let pubkey: string | undefined
|
||||
|
||||
try {
|
||||
const identifier = nostrUri.replace(/^nostr:/, '')
|
||||
decoded = nip19.decode(identifier)
|
||||
|
||||
// Extract pubkey for profile fetching (works for npub and nprofile)
|
||||
if (decoded.type === 'npub') {
|
||||
pubkey = decoded.data
|
||||
} else if (decoded.type === 'nprofile') {
|
||||
pubkey = decoded.data.pubkey
|
||||
}
|
||||
} catch (error) {
|
||||
// Decoding failed, will fallback to shortened identifier
|
||||
}
|
||||
|
||||
// Fetch profile at top level (Rules of Hooks)
|
||||
const profile = useEventModel(Models.ProfileModel, pubkey ? [pubkey] : null)
|
||||
|
||||
// If decoding failed, show shortened identifier
|
||||
if (!decoded) {
|
||||
const identifier = nostrUri.replace(/^nostr:/, '')
|
||||
return (
|
||||
<span className="highlight-comment-nostr-id">
|
||||
{identifier.slice(0, 20)}...
|
||||
</span>
|
||||
)
|
||||
}
|
||||
|
||||
// Render based on decoded type
|
||||
switch (decoded.type) {
|
||||
case 'npub': {
|
||||
const pk = decoded.data
|
||||
const displayName = profile?.name || profile?.display_name || profile?.nip05 || `${pk.slice(0, 8)}...`
|
||||
|
||||
return (
|
||||
<a
|
||||
href={`/p/${nip19.npubEncode(pk)}`}
|
||||
className={className}
|
||||
onClick={onClick}
|
||||
>
|
||||
@{displayName}
|
||||
</a>
|
||||
)
|
||||
}
|
||||
case 'nprofile': {
|
||||
const { pubkey: pk } = decoded.data
|
||||
const displayName = profile?.name || profile?.display_name || profile?.nip05 || `${pk.slice(0, 8)}...`
|
||||
const npub = nip19.npubEncode(pk)
|
||||
|
||||
return (
|
||||
<a
|
||||
href={`/p/${npub}`}
|
||||
className={className}
|
||||
onClick={onClick}
|
||||
>
|
||||
@{displayName}
|
||||
</a>
|
||||
)
|
||||
}
|
||||
case 'naddr': {
|
||||
const { kind, pubkey: pk, identifier: addrIdentifier } = decoded.data
|
||||
// Check if it's a blog post (kind:30023)
|
||||
if (kind === 30023) {
|
||||
const naddr = nip19.naddrEncode({ kind, pubkey: pk, identifier: addrIdentifier })
|
||||
return (
|
||||
<a
|
||||
href={`/a/${naddr}`}
|
||||
className={className}
|
||||
onClick={onClick}
|
||||
>
|
||||
{addrIdentifier || 'Article'}
|
||||
</a>
|
||||
)
|
||||
}
|
||||
// For other kinds, show shortened identifier
|
||||
return (
|
||||
<span className="highlight-comment-nostr-id">
|
||||
nostr:{addrIdentifier.slice(0, 12)}...
|
||||
</span>
|
||||
)
|
||||
}
|
||||
case 'note': {
|
||||
const eventId = decoded.data
|
||||
return (
|
||||
<span className="highlight-comment-nostr-id">
|
||||
note:{eventId.slice(0, 12)}...
|
||||
</span>
|
||||
)
|
||||
}
|
||||
case 'nevent': {
|
||||
const { id } = decoded.data
|
||||
return (
|
||||
<span className="highlight-comment-nostr-id">
|
||||
event:{id.slice(0, 12)}...
|
||||
</span>
|
||||
)
|
||||
}
|
||||
default: {
|
||||
// Fallback for unrecognized types
|
||||
const identifier = nostrUri.replace(/^nostr:/, '')
|
||||
return (
|
||||
<span className="highlight-comment-nostr-id">
|
||||
{identifier.slice(0, 20)}...
|
||||
</span>
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default NostrMentionLink
|
||||
|
||||
271
src/components/Profile.tsx
Normal file
271
src/components/Profile.tsx
Normal file
@@ -0,0 +1,271 @@
|
||||
import React, { useState, useEffect, useCallback } from 'react'
|
||||
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
|
||||
import { faHighlighter, faPenToSquare } from '@fortawesome/free-solid-svg-icons'
|
||||
import { IEventStore } from 'applesauce-core'
|
||||
import { RelayPool } from 'applesauce-relay'
|
||||
import { nip19 } from 'nostr-tools'
|
||||
import { useNavigate } from 'react-router-dom'
|
||||
import { HighlightItem } from './HighlightItem'
|
||||
import { BlogPostPreview, fetchBlogPostsFromAuthors } from '../services/exploreService'
|
||||
import { fetchHighlights } from '../services/highlightService'
|
||||
import { KINDS } from '../config/kinds'
|
||||
import { getActiveRelayUrls } from '../services/relayManager'
|
||||
import AuthorCard from './AuthorCard'
|
||||
import BlogPostCard from './BlogPostCard'
|
||||
import { BlogPostSkeleton, HighlightSkeleton } from './Skeletons'
|
||||
import { useStoreTimeline } from '../hooks/useStoreTimeline'
|
||||
import { eventToHighlight } from '../services/highlightEventProcessor'
|
||||
import { toBlogPostPreview } from '../utils/toBlogPostPreview'
|
||||
import { usePullToRefresh } from 'use-pull-to-refresh'
|
||||
import RefreshIndicator from './RefreshIndicator'
|
||||
import { Hooks } from 'applesauce-react'
|
||||
import { readingProgressController } from '../services/readingProgressController'
|
||||
|
||||
interface ProfileProps {
|
||||
relayPool: RelayPool
|
||||
eventStore: IEventStore
|
||||
pubkey: string
|
||||
activeTab?: 'highlights' | 'writings'
|
||||
}
|
||||
|
||||
const Profile: React.FC<ProfileProps> = ({
|
||||
relayPool,
|
||||
eventStore,
|
||||
pubkey,
|
||||
activeTab: propActiveTab
|
||||
}) => {
|
||||
const navigate = useNavigate()
|
||||
const activeAccount = Hooks.useActiveAccount()
|
||||
const [activeTab, setActiveTab] = useState<'highlights' | 'writings'>(propActiveTab || 'highlights')
|
||||
const [refreshTrigger, setRefreshTrigger] = useState(0)
|
||||
|
||||
// Reading progress state (naddr -> progress 0-1)
|
||||
const [readingProgressMap, setReadingProgressMap] = useState<Map<string, number>>(new Map())
|
||||
|
||||
// Load cached data from event store instantly
|
||||
const cachedHighlights = useStoreTimeline(
|
||||
eventStore,
|
||||
{ kinds: [KINDS.Highlights], authors: [pubkey] },
|
||||
eventToHighlight,
|
||||
[pubkey]
|
||||
)
|
||||
|
||||
const cachedWritings = useStoreTimeline(
|
||||
eventStore,
|
||||
{ kinds: [30023], authors: [pubkey] },
|
||||
toBlogPostPreview,
|
||||
[pubkey]
|
||||
)
|
||||
|
||||
// Update local state when prop changes
|
||||
useEffect(() => {
|
||||
if (propActiveTab) {
|
||||
setActiveTab(propActiveTab)
|
||||
}
|
||||
}, [propActiveTab])
|
||||
|
||||
// Subscribe to reading progress controller
|
||||
useEffect(() => {
|
||||
// Get initial state immediately
|
||||
const initialMap = readingProgressController.getProgressMap()
|
||||
setReadingProgressMap(initialMap)
|
||||
|
||||
// Subscribe to updates
|
||||
const unsubProgress = readingProgressController.onProgress((newMap) => {
|
||||
setReadingProgressMap(newMap)
|
||||
})
|
||||
|
||||
return () => {
|
||||
unsubProgress()
|
||||
}
|
||||
}, [])
|
||||
|
||||
// Load reading progress data when logged in
|
||||
useEffect(() => {
|
||||
if (!activeAccount?.pubkey) {
|
||||
return
|
||||
}
|
||||
|
||||
readingProgressController.start({
|
||||
relayPool,
|
||||
eventStore,
|
||||
pubkey: activeAccount.pubkey,
|
||||
force: refreshTrigger > 0
|
||||
})
|
||||
}, [activeAccount?.pubkey, relayPool, eventStore, refreshTrigger])
|
||||
|
||||
// Background fetch to populate event store (non-blocking)
|
||||
useEffect(() => {
|
||||
if (!pubkey || !relayPool || !eventStore) return
|
||||
|
||||
|
||||
// Fetch highlights in background
|
||||
fetchHighlights(relayPool, pubkey, undefined, undefined, false, eventStore)
|
||||
.then(() => {
|
||||
// Highlights fetched
|
||||
})
|
||||
.catch(err => {
|
||||
console.warn('⚠️ [Profile] Failed to fetch highlights:', err)
|
||||
})
|
||||
|
||||
// Fetch writings in background (no limit for single user profile)
|
||||
fetchBlogPostsFromAuthors(relayPool, [pubkey], getActiveRelayUrls(relayPool), undefined, null)
|
||||
.then(writings => {
|
||||
writings.forEach(w => eventStore.add(w.event))
|
||||
})
|
||||
.catch(err => {
|
||||
console.warn('⚠️ [Profile] Failed to fetch writings:', err)
|
||||
})
|
||||
}, [pubkey, relayPool, eventStore, refreshTrigger])
|
||||
|
||||
// Pull-to-refresh
|
||||
const { isRefreshing, pullPosition } = usePullToRefresh({
|
||||
onRefresh: () => {
|
||||
setRefreshTrigger(prev => prev + 1)
|
||||
},
|
||||
maximumPullLength: 240,
|
||||
refreshThreshold: 80,
|
||||
isDisabled: !pubkey
|
||||
})
|
||||
|
||||
const getPostUrl = (post: BlogPostPreview) => {
|
||||
const dTag = post.event.tags.find(t => t[0] === 'd')?.[1] || ''
|
||||
const naddr = nip19.naddrEncode({
|
||||
kind: 30023,
|
||||
pubkey: post.author,
|
||||
identifier: dTag
|
||||
})
|
||||
return `/a/${naddr}`
|
||||
}
|
||||
|
||||
// Helper to get reading progress for a post
|
||||
const getReadingProgress = useCallback((post: BlogPostPreview): number | undefined => {
|
||||
const dTag = post.event.tags.find(t => t[0] === 'd')?.[1]
|
||||
if (!dTag) return undefined
|
||||
|
||||
try {
|
||||
const naddr = nip19.naddrEncode({
|
||||
kind: 30023,
|
||||
pubkey: post.author,
|
||||
identifier: dTag
|
||||
})
|
||||
const progress = readingProgressMap.get(naddr)
|
||||
|
||||
// Only log when found or map is empty
|
||||
if (progress || readingProgressMap.size === 0) {
|
||||
// Progress found or map is empty
|
||||
}
|
||||
|
||||
return progress
|
||||
} catch (err) {
|
||||
return undefined
|
||||
}
|
||||
}, [readingProgressMap])
|
||||
|
||||
const handleHighlightDelete = () => {
|
||||
// Not allowed to delete other users' highlights
|
||||
return
|
||||
}
|
||||
|
||||
const npub = nip19.npubEncode(pubkey)
|
||||
const showSkeletons = cachedHighlights.length === 0 && cachedWritings.length === 0
|
||||
|
||||
const renderTabContent = () => {
|
||||
switch (activeTab) {
|
||||
case 'highlights':
|
||||
if (showSkeletons) {
|
||||
return (
|
||||
<div className="explore-grid">
|
||||
{Array.from({ length: 8 }).map((_, i) => (
|
||||
<HighlightSkeleton key={i} />
|
||||
))}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
return cachedHighlights.length === 0 ? (
|
||||
<div className="explore-loading" style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', padding: '4rem', color: 'var(--text-secondary)' }}>
|
||||
No highlights yet.
|
||||
</div>
|
||||
) : (
|
||||
<div className="highlights-list me-highlights-list">
|
||||
{cachedHighlights.map((highlight) => (
|
||||
<HighlightItem
|
||||
key={highlight.id}
|
||||
highlight={{ ...highlight, level: 'mine' }}
|
||||
relayPool={relayPool}
|
||||
onHighlightDelete={handleHighlightDelete}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
)
|
||||
|
||||
case 'writings':
|
||||
if (showSkeletons) {
|
||||
return (
|
||||
<div className="explore-grid">
|
||||
{Array.from({ length: 6 }).map((_, i) => (
|
||||
<BlogPostSkeleton key={i} />
|
||||
))}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
return cachedWritings.length === 0 ? (
|
||||
<div className="explore-loading" style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', padding: '4rem', color: 'var(--text-secondary)' }}>
|
||||
No articles written yet.
|
||||
</div>
|
||||
) : (
|
||||
<div className="explore-grid">
|
||||
{cachedWritings.map((post) => (
|
||||
<BlogPostCard
|
||||
key={post.event.id}
|
||||
post={post}
|
||||
href={getPostUrl(post)}
|
||||
readingProgress={getReadingProgress(post)}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
)
|
||||
|
||||
default:
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="explore-container">
|
||||
<RefreshIndicator
|
||||
isRefreshing={isRefreshing}
|
||||
pullPosition={pullPosition}
|
||||
/>
|
||||
<div className="explore-header">
|
||||
<AuthorCard authorPubkey={pubkey} clickable={false} />
|
||||
|
||||
<div className="me-tabs">
|
||||
<button
|
||||
className={`me-tab ${activeTab === 'highlights' ? 'active' : ''}`}
|
||||
data-tab="highlights"
|
||||
onClick={() => navigate(`/p/${npub}`)}
|
||||
>
|
||||
<FontAwesomeIcon icon={faHighlighter} />
|
||||
<span className="tab-label">Highlights</span>
|
||||
</button>
|
||||
<button
|
||||
className={`me-tab ${activeTab === 'writings' ? 'active' : ''}`}
|
||||
data-tab="writings"
|
||||
onClick={() => navigate(`/p/${npub}/writings`)}
|
||||
>
|
||||
<FontAwesomeIcon icon={faPenToSquare} />
|
||||
<span className="tab-label">Writings</span>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="me-tab-content">
|
||||
{renderTabContent()}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default Profile
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import React from 'react'
|
||||
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
|
||||
import { faBookOpen, faCheckCircle, faAsterisk } from '@fortawesome/free-solid-svg-icons'
|
||||
import { faBookOpen, faCheckCircle, faAsterisk, faHighlighter } from '@fortawesome/free-solid-svg-icons'
|
||||
import { faBooks } from '../icons/customIcons'
|
||||
import { faEnvelope, faEnvelopeOpen } from '@fortawesome/free-regular-svg-icons'
|
||||
|
||||
export type ReadingProgressFilterType = 'all' | 'unopened' | 'started' | 'reading' | 'completed'
|
||||
export type ReadingProgressFilterType = 'all' | 'unopened' | 'started' | 'reading' | 'completed' | 'highlighted' | 'archive'
|
||||
|
||||
interface ReadingProgressFiltersProps {
|
||||
selectedFilter: ReadingProgressFilterType
|
||||
@@ -13,18 +14,30 @@ interface ReadingProgressFiltersProps {
|
||||
const ReadingProgressFilters: React.FC<ReadingProgressFiltersProps> = ({ selectedFilter, onFilterChange }) => {
|
||||
const filters = [
|
||||
{ type: 'all' as const, icon: faAsterisk, label: 'All' },
|
||||
{ type: 'highlighted' as const, icon: faHighlighter, label: 'Highlighted' },
|
||||
{ type: 'unopened' as const, icon: faEnvelope, label: 'Unopened' },
|
||||
{ type: 'started' as const, icon: faEnvelopeOpen, label: 'Started' },
|
||||
{ type: 'reading' as const, icon: faBookOpen, label: 'Reading' },
|
||||
{ type: 'completed' as const, icon: faCheckCircle, label: 'Completed' }
|
||||
{ type: 'completed' as const, icon: faCheckCircle, label: 'Completed' },
|
||||
// Archive-marked items (previously emoji-marked)
|
||||
{ type: 'archive' as const, icon: faBooks, label: 'Archive' }
|
||||
]
|
||||
|
||||
return (
|
||||
<div className="bookmark-filters">
|
||||
{filters.map(filter => {
|
||||
const isActive = selectedFilter === filter.type
|
||||
// Only "completed" gets green color, everything else uses default blue
|
||||
const activeStyle = isActive && filter.type === 'completed' ? { color: '#10b981' } : undefined
|
||||
// Only "completed" gets green color, "highlighted" gets yellow, everything else uses default blue
|
||||
let activeStyle: Record<string, string> | undefined = undefined
|
||||
if (isActive) {
|
||||
if (filter.type === 'completed') {
|
||||
activeStyle = { color: '#10b981' } // green
|
||||
} else if (filter.type === 'highlighted') {
|
||||
activeStyle = { color: '#fde047' } // yellow
|
||||
} else if (filter.type === 'archive') {
|
||||
activeStyle = { color: '#60a5fa' } // blue accent
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<button
|
||||
|
||||
@@ -50,16 +50,8 @@ export const RelayStatusIndicator: React.FC<RelayStatusIndicatorProps> = ({
|
||||
|
||||
// Debug logging
|
||||
useEffect(() => {
|
||||
console.log('🔌 Relay Status Indicator:', {
|
||||
mode: isConnecting ? 'CONNECTING' : offlineMode ? 'OFFLINE' : localOnlyMode ? 'LOCAL_ONLY' : 'ONLINE',
|
||||
totalStatuses: relayStatuses.length,
|
||||
connectedCount: connectedUrls.length,
|
||||
connectedUrls: connectedUrls.map(u => u.replace(/^wss?:\/\//, '')),
|
||||
hasLocalRelay,
|
||||
hasRemoteRelay,
|
||||
isConnecting
|
||||
})
|
||||
}, [offlineMode, localOnlyMode, connectedUrls, relayStatuses.length, hasLocalRelay, hasRemoteRelay, isConnecting])
|
||||
// Mode and relay status determined
|
||||
}, [isConnecting, offlineMode, localOnlyMode, relayStatuses, hasLocalRelay, hasRemoteRelay])
|
||||
|
||||
// Don't show indicator when fully connected (but show when connecting)
|
||||
if (!localOnlyMode && !offlineMode && !isConnecting) return null
|
||||
@@ -156,7 +148,7 @@ export const RelayStatusIndicator: React.FC<RelayStatusIndicatorProps> = ({
|
||||
fontWeight: 400
|
||||
}}
|
||||
>
|
||||
{connectedUrls.length} local relay{connectedUrls.length !== 1 ? 's' : ''}
|
||||
Local relays only
|
||||
</span>
|
||||
</>
|
||||
)}
|
||||
|
||||
77
src/components/RichContent.tsx
Normal file
77
src/components/RichContent.tsx
Normal file
@@ -0,0 +1,77 @@
|
||||
import React from 'react'
|
||||
import NostrMentionLink from './NostrMentionLink'
|
||||
|
||||
interface RichContentProps {
|
||||
content: string
|
||||
className?: string
|
||||
}
|
||||
|
||||
/**
|
||||
* Component to render text content with:
|
||||
* - Clickable links
|
||||
* - Resolved nostr mentions (npub, nprofile, note, nevent, naddr)
|
||||
* - Plain text
|
||||
*
|
||||
* Handles both nostr:npub1... and plain npub1... formats
|
||||
*/
|
||||
const RichContent: React.FC<RichContentProps> = ({
|
||||
content,
|
||||
className = 'bookmark-content'
|
||||
}) => {
|
||||
// Pattern to match:
|
||||
// 1. nostr: URIs (nostr:npub1..., nostr:note1..., etc.)
|
||||
// 2. Plain nostr identifiers (npub1..., nprofile1..., note1..., etc.)
|
||||
// 3. http(s) URLs
|
||||
const pattern = /(nostr:[a-z0-9]+|npub1[a-z0-9]+|nprofile1[a-z0-9]+|note1[a-z0-9]+|nevent1[a-z0-9]+|naddr1[a-z0-9]+|https?:\/\/[^\s]+)/gi
|
||||
|
||||
const parts = content.split(pattern)
|
||||
|
||||
return (
|
||||
<div className={className}>
|
||||
{parts.map((part, index) => {
|
||||
// Handle nostr: URIs
|
||||
if (part.startsWith('nostr:')) {
|
||||
return (
|
||||
<NostrMentionLink
|
||||
key={index}
|
||||
nostrUri={part}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
// Handle plain nostr identifiers (add nostr: prefix)
|
||||
if (
|
||||
part.match(/^(npub1|nprofile1|note1|nevent1|naddr1)[a-z0-9]+$/i)
|
||||
) {
|
||||
return (
|
||||
<NostrMentionLink
|
||||
key={index}
|
||||
nostrUri={`nostr:${part}`}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
// Handle http(s) URLs
|
||||
if (part.match(/^https?:\/\//)) {
|
||||
return (
|
||||
<a
|
||||
key={index}
|
||||
href={part}
|
||||
className="nostr-link"
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
>
|
||||
{part}
|
||||
</a>
|
||||
)
|
||||
}
|
||||
|
||||
// Plain text
|
||||
return <React.Fragment key={index}>{part}</React.Fragment>
|
||||
})}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default RichContent
|
||||
|
||||
@@ -39,7 +39,9 @@ const DEFAULT_SETTINGS: UserSettings = {
|
||||
useLocalRelayAsCache: true,
|
||||
rebroadcastToAllRelays: false,
|
||||
paragraphAlignment: 'justify',
|
||||
syncReadingPosition: false,
|
||||
syncReadingPosition: true,
|
||||
autoMarkAsReadOnCompletion: false,
|
||||
hideBookmarksWithoutCreationDate: true,
|
||||
}
|
||||
|
||||
interface SettingsProps {
|
||||
|
||||
@@ -117,6 +117,32 @@ const LayoutBehaviorSettings: React.FC<LayoutBehaviorSettingsProps> = ({ setting
|
||||
<span>Sync reading position across devices</span>
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<div className="setting-group">
|
||||
<label htmlFor="autoMarkAsReadOnCompletion" className="checkbox-label">
|
||||
<input
|
||||
id="autoMarkAsReadOnCompletion"
|
||||
type="checkbox"
|
||||
checked={settings.autoMarkAsReadOnCompletion ?? false}
|
||||
onChange={(e) => onUpdate({ autoMarkAsReadOnCompletion: e.target.checked })}
|
||||
className="setting-checkbox"
|
||||
/>
|
||||
<span>Automatically move to archive at 100%</span>
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<div className="setting-group">
|
||||
<label htmlFor="hideBookmarksWithoutCreationDate" className="checkbox-label">
|
||||
<input
|
||||
id="hideBookmarksWithoutCreationDate"
|
||||
type="checkbox"
|
||||
checked={settings.hideBookmarksWithoutCreationDate ?? false}
|
||||
onChange={(e) => onUpdate({ hideBookmarksWithoutCreationDate: e.target.checked })}
|
||||
className="setting-checkbox"
|
||||
/>
|
||||
<span>Hide bookmarks missing a creation date</span>
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ const PWASettings: React.FC<PWASettingsProps> = ({ settings, onUpdate, onClose }
|
||||
if (isInstalled) return
|
||||
const success = await installApp()
|
||||
if (success) {
|
||||
console.log('App installed successfully')
|
||||
// Installation successful
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -368,7 +368,9 @@ const ThreePaneLayout: React.FC<ThreePaneLayoutProps> = (props) => {
|
||||
summary={props.readerContent?.summary}
|
||||
published={props.readerContent?.published}
|
||||
selectedUrl={props.selectedUrl}
|
||||
highlights={props.classifiedHighlights}
|
||||
highlights={props.selectedUrl && props.selectedUrl.startsWith('nostr:')
|
||||
? props.highlights // article-specific highlights only
|
||||
: props.classifiedHighlights}
|
||||
showHighlights={props.showHighlights}
|
||||
highlightStyle={props.settings.highlightStyle || 'marker'}
|
||||
highlightColor={props.settings.highlightColor || '#ffff00'}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
// Nostr event kinds used throughout the application
|
||||
export const KINDS = {
|
||||
Highlights: 9802, // NIP-?? user highlights
|
||||
Highlights: 9802, // NIP-84 user highlights
|
||||
BlogPost: 30023, // NIP-23 long-form article
|
||||
AppData: 30078, // NIP-78 application data (reading positions)
|
||||
AppData: 30078, // NIP-78 application data
|
||||
ReadingProgress: 39802, // NIP-85 reading progress
|
||||
List: 30001, // NIP-51 list (addressable)
|
||||
ListReplaceable: 30003, // NIP-51 replaceable list
|
||||
ListSimple: 10003, // NIP-51 simple list
|
||||
@@ -13,3 +14,9 @@ export const KINDS = {
|
||||
|
||||
export type KindValue = typeof KINDS[keyof typeof KINDS]
|
||||
|
||||
// Reading progress tracking configuration
|
||||
export const READING_PROGRESS = {
|
||||
// Minimum character count to track reading progress (roughly 150 words)
|
||||
MIN_CONTENT_LENGTH: 1000
|
||||
} as const
|
||||
|
||||
|
||||
@@ -11,13 +11,11 @@ export const RELAYS = [
|
||||
'wss://relay.damus.io',
|
||||
'wss://nos.lol',
|
||||
'wss://relay.nostr.band',
|
||||
'wss://relay.dergigi.com',
|
||||
'wss://wot.dergigi.com',
|
||||
'wss://relay.snort.social',
|
||||
'wss://relay.current.fyi',
|
||||
'wss://nostr-pub.wellorder.net',
|
||||
'wss://purplepag.es',
|
||||
'wss://relay.primal.net',
|
||||
'wss://proxy.nostr-relay.app/5d0d38afc49c4b84ca0da951a336affa18438efed302aeedfa92eb8b0d3fcb87'
|
||||
'wss://proxy.nostr-relay.app/5d0d38afc49c4b84ca0da951a336affa18438efed302aeedfa92eb8b0d3fcb87',
|
||||
]
|
||||
|
||||
|
||||
@@ -43,21 +43,14 @@ export function useAdaptiveTextColor(imageUrl: string | undefined): AdaptiveText
|
||||
height: Math.floor(height * 0.25)
|
||||
})
|
||||
|
||||
console.log('Adaptive color detected:', {
|
||||
hex: color.hex,
|
||||
rgb: color.rgb,
|
||||
isLight: color.isLight,
|
||||
isDark: color.isDark
|
||||
})
|
||||
// Color analysis complete
|
||||
|
||||
// Use library's built-in isLight check for optimal contrast
|
||||
if (color.isLight) {
|
||||
console.log('Light background detected, using black text')
|
||||
setColors({
|
||||
textColor: '#000000'
|
||||
})
|
||||
} else {
|
||||
console.log('Dark background detected, using white text')
|
||||
setColors({
|
||||
textColor: '#ffffff'
|
||||
})
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useEffect } from 'react'
|
||||
import { useEffect, useRef, Dispatch, SetStateAction } from 'react'
|
||||
import { RelayPool } from 'applesauce-relay'
|
||||
import { fetchArticleByNaddr } from '../services/articleService'
|
||||
import { fetchHighlightsForArticle } from '../services/highlightService'
|
||||
@@ -14,7 +14,7 @@ interface UseArticleLoaderProps {
|
||||
setReaderContent: (content: ReadableContent | undefined) => void
|
||||
setReaderLoading: (loading: boolean) => void
|
||||
setIsCollapsed: (collapsed: boolean) => void
|
||||
setHighlights: (highlights: Highlight[]) => void
|
||||
setHighlights: Dispatch<SetStateAction<Highlight[]>>
|
||||
setHighlightsLoading: (loading: boolean) => void
|
||||
setCurrentArticleCoordinate: (coord: string | undefined) => void
|
||||
setCurrentArticleEventId: (id: string | undefined) => void
|
||||
@@ -36,18 +36,26 @@ export function useArticleLoader({
|
||||
setCurrentArticle,
|
||||
settings
|
||||
}: UseArticleLoaderProps) {
|
||||
const mountedRef = useRef(true)
|
||||
|
||||
useEffect(() => {
|
||||
mountedRef.current = true
|
||||
|
||||
if (!relayPool || !naddr) return
|
||||
|
||||
const loadArticle = async () => {
|
||||
if (!mountedRef.current) return
|
||||
|
||||
setReaderLoading(true)
|
||||
setReaderContent(undefined)
|
||||
setSelectedUrl(`nostr:${naddr}`)
|
||||
setIsCollapsed(true)
|
||||
// Keep highlights panel collapsed by default - only open on user interaction
|
||||
|
||||
try {
|
||||
const article = await fetchArticleByNaddr(relayPool, naddr, false, settings)
|
||||
|
||||
if (!mountedRef.current) return
|
||||
|
||||
setReaderContent({
|
||||
title: article.title,
|
||||
markdown: article.markdown,
|
||||
@@ -63,52 +71,56 @@ export function useArticleLoader({
|
||||
setCurrentArticleCoordinate(articleCoordinate)
|
||||
setCurrentArticleEventId(article.event.id)
|
||||
setCurrentArticle?.(article.event)
|
||||
|
||||
console.log('📰 Article loaded:', article.title)
|
||||
console.log('📍 Coordinate:', articleCoordinate)
|
||||
|
||||
// Set reader loading to false immediately after article content is ready
|
||||
// Don't wait for highlights to finish loading
|
||||
setReaderLoading(false)
|
||||
|
||||
// Fetch highlights asynchronously without blocking article display
|
||||
// Stream them as they arrive for instant rendering
|
||||
try {
|
||||
if (!mountedRef.current) return
|
||||
|
||||
setHighlightsLoading(true)
|
||||
setHighlights([]) // Clear old highlights
|
||||
const highlightsMap = new Map<string, Highlight>()
|
||||
setHighlights([])
|
||||
|
||||
await fetchHighlightsForArticle(
|
||||
relayPool,
|
||||
articleCoordinate,
|
||||
article.event.id,
|
||||
(highlight) => {
|
||||
// Deduplicate highlights by ID as they arrive
|
||||
if (!highlightsMap.has(highlight.id)) {
|
||||
highlightsMap.set(highlight.id, highlight)
|
||||
const highlightsList = Array.from(highlightsMap.values())
|
||||
setHighlights(highlightsList.sort((a, b) => b.created_at - a.created_at))
|
||||
}
|
||||
if (!mountedRef.current) return
|
||||
|
||||
setHighlights((prev: Highlight[]) => {
|
||||
if (prev.some((h: Highlight) => h.id === highlight.id)) return prev
|
||||
const next = [highlight, ...prev]
|
||||
return next.sort((a, b) => b.created_at - a.created_at)
|
||||
})
|
||||
},
|
||||
settings
|
||||
)
|
||||
console.log(`📌 Found ${highlightsMap.size} highlights`)
|
||||
} catch (err) {
|
||||
console.error('Failed to fetch highlights:', err)
|
||||
} finally {
|
||||
setHighlightsLoading(false)
|
||||
if (mountedRef.current) {
|
||||
setHighlightsLoading(false)
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Failed to load article:', err)
|
||||
setReaderContent({
|
||||
title: 'Error Loading Article',
|
||||
html: `<p>Failed to load article: ${err instanceof Error ? err.message : 'Unknown error'}</p>`,
|
||||
url: `nostr:${naddr}`
|
||||
})
|
||||
setReaderLoading(false)
|
||||
if (mountedRef.current) {
|
||||
setReaderContent({
|
||||
title: 'Error Loading Article',
|
||||
html: `<p>Failed to load article: ${err instanceof Error ? err.message : 'Unknown error'}</p>`,
|
||||
url: `nostr:${naddr}`
|
||||
})
|
||||
setReaderLoading(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
loadArticle()
|
||||
}, [naddr, relayPool, setSelectedUrl, setReaderContent, setReaderLoading, setIsCollapsed, setHighlights, setHighlightsLoading, setCurrentArticleCoordinate, setCurrentArticleEventId, setCurrentArticle, settings])
|
||||
|
||||
return () => {
|
||||
mountedRef.current = false
|
||||
}
|
||||
// Intentionally excluding setter functions from dependencies to prevent race conditions
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [naddr, relayPool, settings])
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import { contactsController } from '../services/contactsController'
|
||||
import { useStoreTimeline } from './useStoreTimeline'
|
||||
import { eventToHighlight } from '../services/highlightEventProcessor'
|
||||
import { KINDS } from '../config/kinds'
|
||||
import { nip19 } from 'nostr-tools'
|
||||
|
||||
interface UseBookmarksDataParams {
|
||||
relayPool: RelayPool | null
|
||||
@@ -44,21 +45,38 @@ export const useBookmarksData = ({
|
||||
const [isRefreshing, setIsRefreshing] = useState(false)
|
||||
const [lastFetchTime, setLastFetchTime] = useState<number | null>(null)
|
||||
|
||||
// Determine effective article coordinate as early as possible
|
||||
// Prefer state-derived coordinate, but fall back to route naddr before content loads
|
||||
const effectiveArticleCoordinate = useMemo(() => {
|
||||
if (currentArticleCoordinate) return currentArticleCoordinate
|
||||
if (!naddr) return undefined
|
||||
try {
|
||||
const decoded = nip19.decode(naddr)
|
||||
if (decoded.type === 'naddr') {
|
||||
const ptr = decoded.data as { kind: number; pubkey: string; identifier: string }
|
||||
return `${ptr.kind}:${ptr.pubkey}:${ptr.identifier}`
|
||||
}
|
||||
} catch {
|
||||
// ignore decode failure; treat as no coordinate yet
|
||||
}
|
||||
return undefined
|
||||
}, [currentArticleCoordinate, naddr])
|
||||
|
||||
// Load cached article-specific highlights from event store
|
||||
const articleFilter = useMemo(() => {
|
||||
if (!currentArticleCoordinate) return null
|
||||
if (!effectiveArticleCoordinate) return null
|
||||
return {
|
||||
kinds: [KINDS.Highlights],
|
||||
'#a': [currentArticleCoordinate],
|
||||
'#a': [effectiveArticleCoordinate],
|
||||
...(currentArticleEventId ? { '#e': [currentArticleEventId] } : {})
|
||||
}
|
||||
}, [currentArticleCoordinate, currentArticleEventId])
|
||||
}, [effectiveArticleCoordinate, currentArticleEventId])
|
||||
|
||||
const cachedArticleHighlights = useStoreTimeline(
|
||||
eventStore || null,
|
||||
articleFilter || { kinds: [KINDS.Highlights], limit: 0 }, // empty filter if no article
|
||||
eventToHighlight,
|
||||
[currentArticleCoordinate, currentArticleEventId]
|
||||
[effectiveArticleCoordinate, currentArticleEventId]
|
||||
)
|
||||
|
||||
// Subscribe to centralized controllers
|
||||
@@ -84,7 +102,7 @@ export const useBookmarksData = ({
|
||||
|
||||
setHighlightsLoading(true)
|
||||
try {
|
||||
if (currentArticleCoordinate) {
|
||||
if (effectiveArticleCoordinate) {
|
||||
// Seed with cached highlights first
|
||||
if (cachedArticleHighlights.length > 0) {
|
||||
setArticleHighlights(cachedArticleHighlights.sort((a, b) => b.created_at - a.created_at))
|
||||
@@ -97,7 +115,7 @@ export const useBookmarksData = ({
|
||||
|
||||
await fetchHighlightsForArticle(
|
||||
relayPool,
|
||||
currentArticleCoordinate,
|
||||
effectiveArticleCoordinate,
|
||||
currentArticleEventId,
|
||||
(highlight) => {
|
||||
// Deduplicate highlights by ID as they arrive
|
||||
@@ -120,7 +138,7 @@ export const useBookmarksData = ({
|
||||
} finally {
|
||||
setHighlightsLoading(false)
|
||||
}
|
||||
}, [relayPool, currentArticleCoordinate, currentArticleEventId, settings, eventStore, cachedArticleHighlights])
|
||||
}, [relayPool, effectiveArticleCoordinate, currentArticleEventId, settings, eventStore, cachedArticleHighlights])
|
||||
|
||||
const handleRefreshAll = useCallback(async () => {
|
||||
if (!relayPool || !activeAccount || isRefreshing) return
|
||||
@@ -143,19 +161,20 @@ export const useBookmarksData = ({
|
||||
if (!relayPool || !activeAccount) return
|
||||
// Fetch article-specific highlights when viewing an article
|
||||
// External URLs have their highlights fetched by useExternalUrlLoader
|
||||
if (currentArticleCoordinate && !externalUrl) {
|
||||
if (effectiveArticleCoordinate && !externalUrl) {
|
||||
handleFetchHighlights()
|
||||
} else if (!naddr && !externalUrl) {
|
||||
// Clear article highlights when not viewing an article
|
||||
setArticleHighlights([])
|
||||
setHighlightsLoading(false)
|
||||
}
|
||||
}, [relayPool, activeAccount, currentArticleCoordinate, naddr, externalUrl, handleFetchHighlights])
|
||||
}, [relayPool, activeAccount, effectiveArticleCoordinate, naddr, externalUrl, handleFetchHighlights])
|
||||
|
||||
// Merge highlights from controller with article-specific highlights
|
||||
const highlights = [...myHighlights, ...articleHighlights]
|
||||
.filter((h, i, arr) => arr.findIndex(x => x.id === h.id) === i) // Deduplicate
|
||||
.sort((a, b) => b.created_at - a.created_at)
|
||||
// When viewing an article, show only article-specific highlights
|
||||
// Otherwise, show user's highlights from controller
|
||||
const highlights = effectiveArticleCoordinate || externalUrl
|
||||
? articleHighlights.sort((a, b) => b.created_at - a.created_at)
|
||||
: myHighlights
|
||||
|
||||
return {
|
||||
highlights,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useEffect, useMemo } from 'react'
|
||||
import { useEffect, useRef, useMemo } from 'react'
|
||||
import { RelayPool } from 'applesauce-relay'
|
||||
import { IEventStore } from 'applesauce-core'
|
||||
import { fetchReadableContent, ReadableContent } from '../services/readerService'
|
||||
@@ -48,6 +48,8 @@ export function useExternalUrlLoader({
|
||||
setCurrentArticleCoordinate,
|
||||
setCurrentArticleEventId
|
||||
}: UseExternalUrlLoaderProps) {
|
||||
const mountedRef = useRef(true)
|
||||
|
||||
// Load cached URL-specific highlights from event store
|
||||
const urlFilter = useMemo(() => {
|
||||
if (!url) return null
|
||||
@@ -61,80 +63,114 @@ export function useExternalUrlLoader({
|
||||
[url]
|
||||
)
|
||||
|
||||
// Load content and start streaming highlights when URL changes
|
||||
useEffect(() => {
|
||||
mountedRef.current = true
|
||||
|
||||
if (!relayPool || !url) return
|
||||
|
||||
const loadExternalUrl = async () => {
|
||||
if (!mountedRef.current) return
|
||||
|
||||
setReaderLoading(true)
|
||||
setReaderContent(undefined)
|
||||
setSelectedUrl(url)
|
||||
setIsCollapsed(true)
|
||||
// Clear article-specific state
|
||||
setCurrentArticleCoordinate(undefined)
|
||||
setCurrentArticleEventId(undefined)
|
||||
|
||||
try {
|
||||
const content = await fetchReadableContent(url)
|
||||
|
||||
if (!mountedRef.current) return
|
||||
|
||||
setReaderContent(content)
|
||||
|
||||
console.log('🌐 External URL loaded:', content.title)
|
||||
|
||||
// Set reader loading to false immediately after content is ready
|
||||
setReaderLoading(false)
|
||||
|
||||
// Fetch highlights for this URL asynchronously
|
||||
try {
|
||||
if (!mountedRef.current) return
|
||||
|
||||
setHighlightsLoading(true)
|
||||
|
||||
// Seed with cached highlights first
|
||||
if (cachedUrlHighlights.length > 0) {
|
||||
setHighlights(cachedUrlHighlights.sort((a, b) => b.created_at - a.created_at))
|
||||
setHighlights((prev) => {
|
||||
const seen = new Set<string>(cachedUrlHighlights.map(h => h.id))
|
||||
const localOnly = prev.filter(h => !seen.has(h.id))
|
||||
const next = [...cachedUrlHighlights, ...localOnly]
|
||||
return next.sort((a, b) => b.created_at - a.created_at)
|
||||
})
|
||||
} else {
|
||||
setHighlights([])
|
||||
}
|
||||
|
||||
// Check if fetchHighlightsForUrl exists, otherwise skip
|
||||
if (typeof fetchHighlightsForUrl === 'function') {
|
||||
const seen = new Set<string>()
|
||||
// Seed with cached IDs
|
||||
cachedUrlHighlights.forEach(h => seen.add(h.id))
|
||||
|
||||
await fetchHighlightsForUrl(
|
||||
relayPool,
|
||||
url,
|
||||
(highlight) => {
|
||||
if (!mountedRef.current) return
|
||||
|
||||
if (seen.has(highlight.id)) return
|
||||
seen.add(highlight.id)
|
||||
setHighlights((prev) => {
|
||||
if (prev.some(h => h.id === highlight.id)) return prev
|
||||
const next = [...prev, highlight]
|
||||
const next = [highlight, ...prev]
|
||||
return next.sort((a, b) => b.created_at - a.created_at)
|
||||
})
|
||||
},
|
||||
undefined, // settings
|
||||
false, // force
|
||||
undefined,
|
||||
false,
|
||||
eventStore || undefined
|
||||
)
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Failed to fetch highlights:', err)
|
||||
} finally {
|
||||
setHighlightsLoading(false)
|
||||
if (mountedRef.current) {
|
||||
setHighlightsLoading(false)
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Failed to load external URL:', err)
|
||||
// For videos and other media files, use the filename as the title
|
||||
const filename = getFilenameFromUrl(url)
|
||||
setReaderContent({
|
||||
title: filename,
|
||||
html: `<p>Failed to load content: ${err instanceof Error ? err.message : 'Unknown error'}</p>`,
|
||||
url
|
||||
})
|
||||
setReaderLoading(false)
|
||||
if (mountedRef.current) {
|
||||
const filename = getFilenameFromUrl(url)
|
||||
setReaderContent({
|
||||
title: filename,
|
||||
html: `<p>Failed to load content: ${err instanceof Error ? err.message : 'Unknown error'}</p>`,
|
||||
url
|
||||
})
|
||||
setReaderLoading(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
loadExternalUrl()
|
||||
}, [url, relayPool, eventStore, setSelectedUrl, setReaderContent, setReaderLoading, setIsCollapsed, setHighlights, setHighlightsLoading, setCurrentArticleCoordinate, setCurrentArticleEventId, cachedUrlHighlights])
|
||||
|
||||
return () => {
|
||||
mountedRef.current = false
|
||||
}
|
||||
// Intentionally excluding setter functions from dependencies to prevent race conditions
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [url, relayPool, eventStore, cachedUrlHighlights])
|
||||
|
||||
// Keep UI highlights synced with cached store updates without reloading content
|
||||
useEffect(() => {
|
||||
if (!url) return
|
||||
if (cachedUrlHighlights.length === 0) return
|
||||
setHighlights((prev) => {
|
||||
const seen = new Set<string>(prev.map(h => h.id))
|
||||
const additions = cachedUrlHighlights.filter(h => !seen.has(h.id))
|
||||
if (additions.length === 0) return prev
|
||||
const next = [...additions, ...prev]
|
||||
return next.sort((a, b) => b.created_at - a.created_at)
|
||||
})
|
||||
// setHighlights is intentionally excluded from dependencies - it's stable
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [cachedUrlHighlights, url])
|
||||
}
|
||||
|
||||
|
||||
@@ -60,7 +60,6 @@ export const useHighlightCreation = ({
|
||||
? currentArticle.content
|
||||
: readerContent?.markdown || readerContent?.html
|
||||
|
||||
console.log('🎯 Creating highlight...', { text: text.substring(0, 50) + '...' })
|
||||
|
||||
const newHighlight = await createHighlight(
|
||||
text,
|
||||
@@ -73,12 +72,7 @@ export const useHighlightCreation = ({
|
||||
settings
|
||||
)
|
||||
|
||||
console.log('✅ Highlight created successfully!', {
|
||||
id: newHighlight.id,
|
||||
isLocalOnly: newHighlight.isLocalOnly,
|
||||
isOfflineCreated: newHighlight.isOfflineCreated,
|
||||
publishedRelays: newHighlight.publishedRelays
|
||||
})
|
||||
// Highlight created successfully
|
||||
|
||||
// Clear the browser's text selection immediately to allow DOM update
|
||||
const selection = window.getSelection()
|
||||
|
||||
@@ -32,14 +32,7 @@ export const useHighlightedContent = ({
|
||||
}: UseHighlightedContentParams) => {
|
||||
// Filter highlights by URL and visibility settings
|
||||
const relevantHighlights = useMemo(() => {
|
||||
console.log('🔍 ContentPanel: Processing highlights', {
|
||||
totalHighlights: highlights.length,
|
||||
selectedUrl,
|
||||
showHighlights
|
||||
})
|
||||
|
||||
const urlFiltered = filterHighlightsByUrl(highlights, selectedUrl)
|
||||
console.log('📌 URL filtered highlights:', urlFiltered.length)
|
||||
|
||||
// Apply visibility filtering
|
||||
const classified = classifyHighlights(urlFiltered, currentUserPubkey, followedPubkeys)
|
||||
@@ -49,37 +42,25 @@ export const useHighlightedContent = ({
|
||||
return highlightVisibility.nostrverse
|
||||
})
|
||||
|
||||
console.log('✅ Relevant highlights after filtering:', filtered.length, filtered.map(h => h.content.substring(0, 30)))
|
||||
return filtered
|
||||
}, [selectedUrl, highlights, highlightVisibility, currentUserPubkey, followedPubkeys, showHighlights])
|
||||
}, [selectedUrl, highlights, highlightVisibility, currentUserPubkey, followedPubkeys])
|
||||
|
||||
// Prepare the final HTML with highlights applied
|
||||
const finalHtml = useMemo(() => {
|
||||
const sourceHtml = markdown ? renderedMarkdownHtml : html
|
||||
|
||||
console.log('🎨 Preparing final HTML:', {
|
||||
hasMarkdown: !!markdown,
|
||||
hasHtml: !!html,
|
||||
renderedHtmlLength: renderedMarkdownHtml.length,
|
||||
sourceHtmlLength: sourceHtml?.length || 0,
|
||||
showHighlights,
|
||||
relevantHighlightsCount: relevantHighlights.length
|
||||
})
|
||||
|
||||
// Prepare final HTML
|
||||
if (!sourceHtml) {
|
||||
console.warn('⚠️ No source HTML available')
|
||||
return ''
|
||||
}
|
||||
|
||||
if (showHighlights && relevantHighlights.length > 0) {
|
||||
console.log('✨ Applying', relevantHighlights.length, 'highlights to HTML')
|
||||
const highlightedHtml = applyHighlightsToHTML(sourceHtml, relevantHighlights, highlightStyle)
|
||||
console.log('✅ Highlights applied, result length:', highlightedHtml.length)
|
||||
return highlightedHtml
|
||||
}
|
||||
|
||||
console.log('📄 Returning source HTML without highlights')
|
||||
return sourceHtml
|
||||
|
||||
}, [html, renderedMarkdownHtml, markdown, relevantHighlights, showHighlights, highlightStyle])
|
||||
|
||||
return { finalHtml, relevantHighlights }
|
||||
|
||||
@@ -43,7 +43,6 @@ export const useMarkdownToHTML = (
|
||||
|
||||
// Replace nostr URIs with resolved titles
|
||||
processed = replaceNostrUrisInMarkdownWithTitles(markdown, articleTitles)
|
||||
console.log(`📚 Resolved ${articleTitles.size} article titles`)
|
||||
} catch (error) {
|
||||
console.warn('Failed to fetch article titles:', error)
|
||||
// Fall back to basic replacement
|
||||
@@ -58,12 +57,10 @@ export const useMarkdownToHTML = (
|
||||
|
||||
setProcessedMarkdown(processed)
|
||||
|
||||
console.log('📝 Converting markdown to HTML...')
|
||||
|
||||
const rafId = requestAnimationFrame(() => {
|
||||
if (previewRef.current && !isCancelled) {
|
||||
const html = previewRef.current.innerHTML
|
||||
console.log('✅ Markdown converted to HTML:', html.length, 'chars')
|
||||
setRenderedHtml(html)
|
||||
} else if (!isCancelled) {
|
||||
console.warn('⚠️ markdownPreviewRef.current is null')
|
||||
|
||||
28
src/hooks/useMountedState.ts
Normal file
28
src/hooks/useMountedState.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { useRef, useEffect, useCallback } from 'react'
|
||||
|
||||
/**
|
||||
* Hook to track if component is mounted and prevent state updates after unmount.
|
||||
* Returns a function to check if still mounted.
|
||||
*
|
||||
* @example
|
||||
* const isMounted = useMountedState()
|
||||
*
|
||||
* async function loadData() {
|
||||
* const data = await fetch(...)
|
||||
* if (isMounted()) {
|
||||
* setState(data)
|
||||
* }
|
||||
* }
|
||||
*/
|
||||
export function useMountedState(): () => boolean {
|
||||
const mountedRef = useRef(true)
|
||||
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
mountedRef.current = false
|
||||
}
|
||||
}, [])
|
||||
|
||||
return useCallback(() => mountedRef.current, [])
|
||||
}
|
||||
|
||||
@@ -50,16 +50,10 @@ export function useOfflineSync({
|
||||
const isNowOnline = hasRemoteRelays
|
||||
|
||||
if (wasLocalOnly && isNowOnline) {
|
||||
console.log('✈️ Detected transition: Flight Mode → Online')
|
||||
console.log('📊 Relay state:', {
|
||||
connectedRelays: connectedRelays.length,
|
||||
remoteRelays: connectedRelays.filter(r => !isLocalRelay(r.url)).length,
|
||||
localRelays: connectedRelays.filter(r => isLocalRelay(r.url)).length
|
||||
})
|
||||
// Coming back online, sync events
|
||||
|
||||
// Wait a moment for relays to fully establish connections
|
||||
setTimeout(() => {
|
||||
console.log('🚀 Starting sync after delay...')
|
||||
syncLocalEventsToRemote(relayPool, eventStore)
|
||||
}, 2000)
|
||||
}
|
||||
|
||||
@@ -5,12 +5,10 @@ export function useOnlineStatus() {
|
||||
|
||||
useEffect(() => {
|
||||
const handleOnline = () => {
|
||||
console.log('🌐 Back online')
|
||||
setIsOnline(true)
|
||||
}
|
||||
|
||||
const handleOffline = () => {
|
||||
console.log('📴 Gone offline')
|
||||
setIsOnline(false)
|
||||
}
|
||||
|
||||
|
||||
@@ -51,12 +51,10 @@ export function usePWAInstall() {
|
||||
const choiceResult = await deferredPrompt.userChoice
|
||||
|
||||
if (choiceResult.outcome === 'accepted') {
|
||||
console.log('✅ PWA installed')
|
||||
setIsInstallable(false)
|
||||
setDeferredPrompt(null)
|
||||
return true
|
||||
} else {
|
||||
console.log('❌ PWA installation dismissed')
|
||||
return false
|
||||
}
|
||||
} catch (error) {
|
||||
|
||||
@@ -4,40 +4,48 @@ interface UseReadingPositionOptions {
|
||||
enabled?: boolean
|
||||
onPositionChange?: (position: number) => void
|
||||
onReadingComplete?: () => void
|
||||
readingCompleteThreshold?: number // Default 0.9 (90%)
|
||||
readingCompleteThreshold?: number // Default 0.95 (95%) - matches filter threshold
|
||||
syncEnabled?: boolean // Whether to sync positions to Nostr
|
||||
onSave?: (position: number) => void // Callback for saving position
|
||||
autoSaveInterval?: number // Auto-save interval in ms (default 5000)
|
||||
completionHoldMs?: number // How long to hold at 100% before firing complete (default 2000)
|
||||
}
|
||||
|
||||
export const useReadingPosition = ({
|
||||
enabled = true,
|
||||
onPositionChange,
|
||||
onReadingComplete,
|
||||
readingCompleteThreshold = 0.9,
|
||||
readingCompleteThreshold = 0.95, // Match filter threshold for consistency
|
||||
syncEnabled = false,
|
||||
onSave,
|
||||
autoSaveInterval = 5000
|
||||
autoSaveInterval = 5000,
|
||||
completionHoldMs = 2000
|
||||
}: UseReadingPositionOptions = {}) => {
|
||||
const [position, setPosition] = useState(0)
|
||||
const positionRef = useRef(0)
|
||||
const [isReadingComplete, setIsReadingComplete] = useState(false)
|
||||
const hasTriggeredComplete = useRef(false)
|
||||
const lastSavedPosition = useRef(0)
|
||||
const saveTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null)
|
||||
const hasSavedOnce = useRef(false)
|
||||
const completionTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null)
|
||||
|
||||
// Debounced save function
|
||||
const scheduleSave = useCallback((currentPosition: number) => {
|
||||
if (!syncEnabled || !onSave) return
|
||||
|
||||
// Don't save if position is too low (< 5%)
|
||||
if (currentPosition < 0.05) return
|
||||
|
||||
if (!syncEnabled || !onSave) {
|
||||
return
|
||||
}
|
||||
|
||||
// Don't save if position hasn't changed significantly (less than 1%)
|
||||
// But always save if we've reached 100% (completion)
|
||||
const hasSignificantChange = Math.abs(currentPosition - lastSavedPosition.current) >= 0.01
|
||||
const hasReachedCompletion = currentPosition === 1 && lastSavedPosition.current < 1
|
||||
const isInitialSave = !hasSavedOnce.current
|
||||
|
||||
if (!hasSignificantChange && !hasReachedCompletion) return
|
||||
if (!hasSignificantChange && !hasReachedCompletion && !isInitialSave) {
|
||||
// Not significant enough to save
|
||||
return
|
||||
}
|
||||
|
||||
// Clear existing timer
|
||||
if (saveTimerRef.current) {
|
||||
@@ -47,6 +55,7 @@ export const useReadingPosition = ({
|
||||
// Schedule new save
|
||||
saveTimerRef.current = setTimeout(() => {
|
||||
lastSavedPosition.current = currentPosition
|
||||
hasSavedOnce.current = true
|
||||
onSave(currentPosition)
|
||||
}, autoSaveInterval)
|
||||
}, [syncEnabled, onSave, autoSaveInterval])
|
||||
@@ -61,11 +70,10 @@ export const useReadingPosition = ({
|
||||
saveTimerRef.current = null
|
||||
}
|
||||
|
||||
// Save if position is meaningful (>= 5%)
|
||||
if (position >= 0.05) {
|
||||
lastSavedPosition.current = position
|
||||
onSave(position)
|
||||
}
|
||||
// Always allow immediate save (including 0%)
|
||||
lastSavedPosition.current = position
|
||||
hasSavedOnce.current = true
|
||||
onSave(position)
|
||||
}, [syncEnabled, onSave, position])
|
||||
|
||||
useEffect(() => {
|
||||
@@ -89,17 +97,47 @@ export const useReadingPosition = ({
|
||||
const isAtBottom = scrollTop + windowHeight >= documentHeight - 5
|
||||
const clampedProgress = isAtBottom ? 1 : Math.max(0, Math.min(1, scrollProgress))
|
||||
|
||||
// Only log on significant changes (every 5%) to avoid flooding console
|
||||
const prevPercent = Math.floor(position * 20) // Groups by 5%
|
||||
const newPercent = Math.floor(clampedProgress * 20)
|
||||
if (prevPercent !== newPercent) {
|
||||
// Position threshold crossed
|
||||
}
|
||||
|
||||
setPosition(clampedProgress)
|
||||
positionRef.current = clampedProgress
|
||||
onPositionChange?.(clampedProgress)
|
||||
|
||||
// Schedule auto-save if sync is enabled
|
||||
scheduleSave(clampedProgress)
|
||||
|
||||
// Check if reading is complete
|
||||
if (clampedProgress >= readingCompleteThreshold && !hasTriggeredComplete.current) {
|
||||
setIsReadingComplete(true)
|
||||
hasTriggeredComplete.current = true
|
||||
onReadingComplete?.()
|
||||
// Completion detection with 2s hold at 100%
|
||||
if (!hasTriggeredComplete.current) {
|
||||
// If at exact 100%, start a hold timer; cancel if we scroll up
|
||||
if (clampedProgress === 1) {
|
||||
if (!completionTimerRef.current) {
|
||||
completionTimerRef.current = setTimeout(() => {
|
||||
if (!hasTriggeredComplete.current && positionRef.current === 1) {
|
||||
setIsReadingComplete(true)
|
||||
hasTriggeredComplete.current = true
|
||||
onReadingComplete?.()
|
||||
}
|
||||
completionTimerRef.current = null
|
||||
}, completionHoldMs)
|
||||
}
|
||||
} else {
|
||||
// If we moved off 100%, cancel any pending completion hold
|
||||
if (completionTimerRef.current) {
|
||||
clearTimeout(completionTimerRef.current)
|
||||
completionTimerRef.current = null
|
||||
// still allow threshold-based completion for near-bottom if configured
|
||||
if (clampedProgress >= readingCompleteThreshold) {
|
||||
setIsReadingComplete(true)
|
||||
hasTriggeredComplete.current = true
|
||||
onReadingComplete?.()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,7 +156,12 @@ export const useReadingPosition = ({
|
||||
if (saveTimerRef.current) {
|
||||
clearTimeout(saveTimerRef.current)
|
||||
}
|
||||
if (completionTimerRef.current) {
|
||||
clearTimeout(completionTimerRef.current)
|
||||
}
|
||||
}
|
||||
// position is intentionally not in deps - it's computed from scroll and would cause infinite re-renders
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [enabled, onPositionChange, onReadingComplete, readingCompleteThreshold, scheduleSave])
|
||||
|
||||
// Reset reading complete state when enabled changes
|
||||
@@ -126,6 +169,12 @@ export const useReadingPosition = ({
|
||||
if (!enabled) {
|
||||
setIsReadingComplete(false)
|
||||
hasTriggeredComplete.current = false
|
||||
hasSavedOnce.current = false
|
||||
lastSavedPosition.current = 0
|
||||
if (completionTimerRef.current) {
|
||||
clearTimeout(completionTimerRef.current)
|
||||
completionTimerRef.current = null
|
||||
}
|
||||
}
|
||||
}, [enabled])
|
||||
|
||||
|
||||
@@ -48,7 +48,6 @@ export function useSettings({ relayPool, eventStore, pubkey, accountManager }: U
|
||||
const root = document.documentElement.style
|
||||
const fontKey = settings.readingFont || 'system'
|
||||
|
||||
console.log('🎨 Applying settings styles:', { fontKey, fontSize: settings.fontSize, theme: settings.theme })
|
||||
|
||||
// Apply theme with color variants (defaults to 'system' if not set)
|
||||
applyTheme(
|
||||
@@ -59,9 +58,7 @@ export function useSettings({ relayPool, eventStore, pubkey, accountManager }: U
|
||||
|
||||
// Load font first and wait for it to be ready
|
||||
if (fontKey !== 'system') {
|
||||
console.log('⏳ Waiting for font to load...')
|
||||
await loadFont(fontKey)
|
||||
console.log('✅ Font loaded, applying styles')
|
||||
}
|
||||
|
||||
// Apply font settings after font is loaded
|
||||
@@ -76,7 +73,6 @@ export function useSettings({ relayPool, eventStore, pubkey, accountManager }: U
|
||||
// Set paragraph alignment
|
||||
root.setProperty('--paragraph-alignment', settings.paragraphAlignment || 'justify')
|
||||
|
||||
console.log('✅ All styles applied')
|
||||
}
|
||||
|
||||
applyStyles()
|
||||
|
||||
@@ -11,7 +11,6 @@ if ('serviceWorker' in navigator) {
|
||||
navigator.serviceWorker
|
||||
.register('/sw.js', { type: 'module' })
|
||||
.then(registration => {
|
||||
console.log('✅ Service Worker registered:', registration.scope)
|
||||
|
||||
// Check for updates periodically
|
||||
setInterval(() => {
|
||||
@@ -25,7 +24,6 @@ if ('serviceWorker' in navigator) {
|
||||
newWorker.addEventListener('statechange', () => {
|
||||
if (newWorker.state === 'installed' && navigator.serviceWorker.controller) {
|
||||
// New service worker available
|
||||
console.log('🔄 New version available! Reload to update.')
|
||||
|
||||
// Optionally show a toast notification
|
||||
const updateAvailable = new CustomEvent('sw-update-available')
|
||||
|
||||
197
src/services/archiveController.ts
Normal file
197
src/services/archiveController.ts
Normal file
@@ -0,0 +1,197 @@
|
||||
import { RelayPool } from 'applesauce-relay'
|
||||
import { IEventStore } from 'applesauce-core'
|
||||
import { NostrEvent } from 'nostr-tools'
|
||||
import { queryEvents } from './dataFetch'
|
||||
import { KINDS } from '../config/kinds'
|
||||
import { ARCHIVE_EMOJI } from './reactionService'
|
||||
import { nip19 } from 'nostr-tools'
|
||||
|
||||
type MarkedChangeCallback = (markedIds: Set<string>) => void
|
||||
|
||||
class ArchiveController {
|
||||
private markedIds: Set<string> = new Set()
|
||||
private lastLoadedPubkey: string | null = null
|
||||
private listeners: MarkedChangeCallback[] = []
|
||||
private generation = 0
|
||||
private timelineSubscription: { unsubscribe: () => void } | null = null
|
||||
private pendingEventIds: Set<string> = new Set()
|
||||
|
||||
onMarked(cb: MarkedChangeCallback): () => void {
|
||||
this.listeners.push(cb)
|
||||
// Emit current state immediately to new subscribers
|
||||
cb(new Set(this.markedIds))
|
||||
return () => {
|
||||
this.listeners = this.listeners.filter(l => l !== cb)
|
||||
}
|
||||
}
|
||||
|
||||
private emit(): void {
|
||||
const snapshot = new Set(this.markedIds)
|
||||
this.listeners.forEach(cb => cb(snapshot))
|
||||
}
|
||||
|
||||
mark(id: string): void {
|
||||
if (!this.markedIds.has(id)) {
|
||||
this.markedIds.add(id)
|
||||
this.emit()
|
||||
}
|
||||
}
|
||||
|
||||
unmark(id: string): void {
|
||||
if (this.markedIds.delete(id)) {
|
||||
this.emit()
|
||||
}
|
||||
}
|
||||
|
||||
isMarked(id: string): boolean {
|
||||
return this.markedIds.has(id)
|
||||
}
|
||||
|
||||
getMarkedIds(): string[] {
|
||||
return Array.from(this.markedIds)
|
||||
}
|
||||
|
||||
isLoadedFor(pubkey: string): boolean {
|
||||
return this.lastLoadedPubkey === pubkey
|
||||
}
|
||||
|
||||
reset(): void {
|
||||
this.generation++
|
||||
if (this.timelineSubscription) {
|
||||
try { this.timelineSubscription.unsubscribe() } catch { /* ignore */ }
|
||||
this.timelineSubscription = null
|
||||
}
|
||||
this.markedIds = new Set()
|
||||
this.pendingEventIds = new Set()
|
||||
this.lastLoadedPubkey = null
|
||||
this.emit()
|
||||
}
|
||||
|
||||
async start(options: {
|
||||
relayPool: RelayPool
|
||||
eventStore: IEventStore
|
||||
pubkey: string
|
||||
force?: boolean
|
||||
}): Promise<void> {
|
||||
const { relayPool, eventStore, pubkey, force = false } = options
|
||||
const startGen = this.generation
|
||||
|
||||
if (!force && this.isLoadedFor(pubkey)) {
|
||||
return
|
||||
}
|
||||
|
||||
// Mark as loaded immediately (fetch runs non-blocking)
|
||||
this.lastLoadedPubkey = pubkey
|
||||
|
||||
// Handlers for streaming queries
|
||||
const handleUrlReaction = (evt: NostrEvent) => {
|
||||
if (evt.content !== ARCHIVE_EMOJI) return
|
||||
const rTag = evt.tags.find(t => t[0] === 'r')?.[1]
|
||||
if (!rTag) return
|
||||
this.markedIds.add(rTag)
|
||||
this.emit()
|
||||
}
|
||||
|
||||
const handleEventReaction = (evt: NostrEvent) => {
|
||||
if (evt.content !== ARCHIVE_EMOJI) return
|
||||
// Direct coordinate tag ('a') - can be mapped immediately
|
||||
const aTag = evt.tags.find(t => t[0] === 'a')?.[1]
|
||||
if (aTag) {
|
||||
try {
|
||||
const [kindStr, pubkey, identifier] = aTag.split(':')
|
||||
const kind = Number(kindStr)
|
||||
if (kind === KINDS.BlogPost && pubkey && identifier) {
|
||||
const naddr = nip19.naddrEncode({ kind, pubkey, identifier })
|
||||
this.markedIds.add(naddr)
|
||||
this.emit()
|
||||
return
|
||||
}
|
||||
} catch { /* ignore malformed a-tag */ }
|
||||
}
|
||||
const eTag = evt.tags.find(t => t[0] === 'e')?.[1]
|
||||
if (!eTag) return
|
||||
this.pendingEventIds.add(eTag)
|
||||
}
|
||||
|
||||
try {
|
||||
// Stream kind:17 and kind:7 in parallel
|
||||
const [kind17, kind7] = await Promise.all([
|
||||
queryEvents(relayPool, { kinds: [17], authors: [pubkey] }, { onEvent: handleUrlReaction }),
|
||||
queryEvents(relayPool, { kinds: [7], authors: [pubkey] }, { onEvent: handleEventReaction })
|
||||
])
|
||||
|
||||
if (startGen !== this.generation) return
|
||||
|
||||
// Include EOSE events
|
||||
kind17.forEach(handleUrlReaction)
|
||||
kind7.forEach(handleEventReaction)
|
||||
|
||||
if (this.pendingEventIds.size > 0) {
|
||||
// Fetch referenced articles (kind:30023) and map event IDs to naddr
|
||||
const ids = Array.from(this.pendingEventIds)
|
||||
const articleEvents = await queryEvents(relayPool, { kinds: [KINDS.BlogPost], ids })
|
||||
for (const article of articleEvents) {
|
||||
const dTag = article.tags.find(t => t[0] === 'd')?.[1]
|
||||
if (!dTag) continue
|
||||
try {
|
||||
const naddr = nip19.naddrEncode({ kind: KINDS.BlogPost, pubkey: article.pubkey, identifier: dTag })
|
||||
this.markedIds.add(naddr)
|
||||
} catch {
|
||||
// skip invalid
|
||||
}
|
||||
}
|
||||
this.emit()
|
||||
}
|
||||
|
||||
// Try immediate mapping via eventStore for any still-pending e-ids
|
||||
if (this.pendingEventIds.size > 0) {
|
||||
const stillPending = new Set<string>()
|
||||
for (const eId of this.pendingEventIds) {
|
||||
try {
|
||||
const store = eventStore as unknown as { getEvent?: (id: string) => NostrEvent | undefined }
|
||||
const evt: NostrEvent | undefined = typeof store.getEvent === 'function' ? store.getEvent(eId) : undefined
|
||||
if (evt && evt.kind === KINDS.BlogPost) {
|
||||
const dTag = evt.tags.find(t => t[0] === 'd')?.[1]
|
||||
if (dTag) {
|
||||
const naddr = nip19.naddrEncode({ kind: KINDS.BlogPost, pubkey: evt.pubkey, identifier: dTag })
|
||||
this.markedIds.add(naddr)
|
||||
}
|
||||
} else {
|
||||
stillPending.add(eId)
|
||||
}
|
||||
} catch (e) { stillPending.add(eId) }
|
||||
}
|
||||
this.pendingEventIds = stillPending
|
||||
if (stillPending.size > 0) {
|
||||
// Subscribe to future 30023 arrivals to finalize mapping
|
||||
if (this.timelineSubscription) {
|
||||
try { this.timelineSubscription.unsubscribe() } catch { /* ignore */ }
|
||||
this.timelineSubscription = null
|
||||
}
|
||||
const sub$ = eventStore.timeline({ kinds: [KINDS.BlogPost] })
|
||||
const genAtSub = this.generation
|
||||
this.timelineSubscription = sub$.subscribe((events: NostrEvent[]) => {
|
||||
if (genAtSub !== this.generation) return
|
||||
for (const evt of events) {
|
||||
if (!this.pendingEventIds.has(evt.id)) continue
|
||||
const dTag = evt.tags.find(t => t[0] === 'd')?.[1]
|
||||
if (!dTag) continue
|
||||
try {
|
||||
const naddr = nip19.naddrEncode({ kind: KINDS.BlogPost, pubkey: evt.pubkey, identifier: dTag })
|
||||
this.markedIds.add(naddr)
|
||||
this.pendingEventIds.delete(evt.id)
|
||||
this.emit()
|
||||
} catch { /* ignore */ }
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
// Non-blocking fetch; ignore errors here
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const archiveController = new ArchiveController()
|
||||
|
||||
|
||||
@@ -48,7 +48,6 @@ function getFromCache(naddr: string): ArticleContent | null {
|
||||
return null
|
||||
}
|
||||
|
||||
console.log('📦 Loaded article from cache:', naddr)
|
||||
return content
|
||||
} catch {
|
||||
return null
|
||||
@@ -63,7 +62,6 @@ function saveToCache(naddr: string, content: ArticleContent): void {
|
||||
timestamp: Date.now()
|
||||
}
|
||||
localStorage.setItem(cacheKey, JSON.stringify(cached))
|
||||
console.log('💾 Saved article to cache:', naddr)
|
||||
} catch (err) {
|
||||
console.warn('Failed to cache article:', err)
|
||||
// Silently fail if storage is full or unavailable
|
||||
|
||||
@@ -126,18 +126,14 @@ class BookmarkController {
|
||||
generation: number
|
||||
): void {
|
||||
if (!this.eventLoader) {
|
||||
console.warn('[bookmark] ⚠️ EventLoader not initialized')
|
||||
return
|
||||
}
|
||||
|
||||
// Filter to unique IDs not already hydrated
|
||||
const unique = Array.from(new Set(ids)).filter(id => !idToEvent.has(id))
|
||||
if (unique.length === 0) {
|
||||
console.log('[bookmark] 🔧 All IDs already hydrated, skipping')
|
||||
return
|
||||
}
|
||||
|
||||
console.log('[bookmark] 🔧 Hydrating', unique.length, 'IDs using EventLoader')
|
||||
|
||||
// Convert IDs to EventPointers
|
||||
const pointers: EventPointer[] = unique.map(id => ({ id }))
|
||||
@@ -159,8 +155,8 @@ class BookmarkController {
|
||||
|
||||
onProgress()
|
||||
},
|
||||
error: (error) => {
|
||||
console.error('[bookmark] ❌ EventLoader error:', error)
|
||||
error: () => {
|
||||
// Silent error - EventLoader handles retries
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -175,14 +171,11 @@ class BookmarkController {
|
||||
generation: number
|
||||
): void {
|
||||
if (!this.addressLoader) {
|
||||
console.warn('[bookmark] ⚠️ AddressLoader not initialized')
|
||||
return
|
||||
}
|
||||
|
||||
if (coords.length === 0) return
|
||||
|
||||
console.log('[bookmark] 🔧 Hydrating', coords.length, 'coordinates using AddressLoader')
|
||||
|
||||
// Convert coordinates to AddressPointers
|
||||
const pointers = coords.map(c => ({
|
||||
kind: c.kind,
|
||||
@@ -203,8 +196,8 @@ class BookmarkController {
|
||||
|
||||
onProgress()
|
||||
},
|
||||
error: (error) => {
|
||||
console.error('[bookmark] ❌ AddressLoader error:', error)
|
||||
error: () => {
|
||||
// Silent error - AddressLoader handles retries
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -223,10 +216,6 @@ class BookmarkController {
|
||||
return this.decryptedResults.has(getEventKey(evt))
|
||||
})
|
||||
|
||||
const unencryptedCount = allEvents.filter(evt => !hasEncryptedContent(evt)).length
|
||||
const decryptedCount = readyEvents.length - unencryptedCount
|
||||
console.log('[bookmark] 📋 Building bookmarks:', unencryptedCount, 'unencrypted,', decryptedCount, 'decrypted, of', allEvents.length, 'total')
|
||||
|
||||
if (readyEvents.length === 0) {
|
||||
this.bookmarksListeners.forEach(cb => cb([]))
|
||||
return
|
||||
@@ -237,17 +226,14 @@ class BookmarkController {
|
||||
const unencryptedEvents = readyEvents.filter(evt => !hasEncryptedContent(evt))
|
||||
const decryptedEvents = readyEvents.filter(evt => hasEncryptedContent(evt))
|
||||
|
||||
console.log('[bookmark] 🔧 Processing', unencryptedEvents.length, 'unencrypted events')
|
||||
// Process unencrypted events
|
||||
const { publicItemsAll: publicUnencrypted, privateItemsAll: privateUnencrypted, newestCreatedAt, latestContent, allTags } =
|
||||
await collectBookmarksFromEvents(unencryptedEvents, activeAccount, signerCandidate)
|
||||
console.log('[bookmark] 🔧 Unencrypted returned:', publicUnencrypted.length, 'public,', privateUnencrypted.length, 'private')
|
||||
|
||||
// Merge in decrypted results
|
||||
let publicItemsAll = [...publicUnencrypted]
|
||||
let privateItemsAll = [...privateUnencrypted]
|
||||
|
||||
console.log('[bookmark] 🔧 Merging', decryptedEvents.length, 'decrypted events')
|
||||
decryptedEvents.forEach(evt => {
|
||||
const eventKey = getEventKey(evt)
|
||||
const decrypted = this.decryptedResults.get(eventKey)
|
||||
@@ -256,11 +242,8 @@ class BookmarkController {
|
||||
privateItemsAll = [...privateItemsAll, ...decrypted.privateItems]
|
||||
}
|
||||
})
|
||||
|
||||
console.log('[bookmark] 🔧 Total after merge:', publicItemsAll.length, 'public,', privateItemsAll.length, 'private')
|
||||
|
||||
const allItems = [...publicItemsAll, ...privateItemsAll]
|
||||
console.log('[bookmark] 🔧 Total items to process:', allItems.length)
|
||||
|
||||
// Separate hex IDs from coordinates
|
||||
const noteIds: string[] = []
|
||||
@@ -276,14 +259,11 @@ class BookmarkController {
|
||||
|
||||
// Helper to build and emit bookmarks
|
||||
const emitBookmarks = (idToEvent: Map<string, NostrEvent>) => {
|
||||
console.log('[bookmark] 🔧 Building final bookmarks list...')
|
||||
const allBookmarks = dedupeBookmarksById([
|
||||
...hydrateItems(publicItemsAll, idToEvent),
|
||||
...hydrateItems(privateItemsAll, idToEvent)
|
||||
])
|
||||
console.log('[bookmark] 🔧 After hydration and dedup:', allBookmarks.length, 'bookmarks')
|
||||
|
||||
console.log('[bookmark] 🔧 Enriching and sorting...')
|
||||
const enriched = allBookmarks.map(b => ({
|
||||
...b,
|
||||
tags: b.tags || [],
|
||||
@@ -293,9 +273,7 @@ class BookmarkController {
|
||||
const sortedBookmarks = enriched
|
||||
.map(b => ({ ...b, urlReferences: extractUrlsFromContent(b.content) }))
|
||||
.sort((a, b) => ((b.added_at || 0) - (a.added_at || 0)) || ((b.created_at || 0) - (a.created_at || 0)))
|
||||
console.log('[bookmark] 🔧 Sorted:', sortedBookmarks.length, 'bookmarks')
|
||||
|
||||
console.log('[bookmark] 🔧 Creating final Bookmark object...')
|
||||
const bookmark: Bookmark = {
|
||||
id: `${activeAccount.pubkey}-bookmarks`,
|
||||
title: `Bookmarks (${sortedBookmarks.length})`,
|
||||
@@ -310,18 +288,14 @@ class BookmarkController {
|
||||
encryptedContent: undefined
|
||||
}
|
||||
|
||||
console.log('[bookmark] 📋 Built bookmark with', sortedBookmarks.length, 'items')
|
||||
console.log('[bookmark] 📤 Emitting to', this.bookmarksListeners.length, 'listeners')
|
||||
this.bookmarksListeners.forEach(cb => cb([bookmark]))
|
||||
}
|
||||
|
||||
// Emit immediately with empty metadata (show placeholders)
|
||||
const idToEvent: Map<string, NostrEvent> = new Map()
|
||||
console.log('[bookmark] 🚀 Emitting initial bookmarks with placeholders (IDs only)...')
|
||||
emitBookmarks(idToEvent)
|
||||
|
||||
// Now fetch events progressively in background using batched hydrators
|
||||
console.log('[bookmark] 🔧 Background hydration:', noteIds.length, 'note IDs and', coordinates.length, 'coordinates')
|
||||
|
||||
const generation = this.hydrationGeneration
|
||||
const onProgress = () => emitBookmarks(idToEvent)
|
||||
@@ -341,9 +315,7 @@ class BookmarkController {
|
||||
this.hydrateByIds(noteIds, idToEvent, onProgress, generation)
|
||||
this.hydrateByCoordinates(coordObjs, idToEvent, onProgress, generation)
|
||||
} catch (error) {
|
||||
console.error('[bookmark] ❌ Failed to build bookmarks:', error)
|
||||
console.error('[bookmark] ❌ Error details:', error instanceof Error ? error.message : String(error))
|
||||
console.error('[bookmark] ❌ Stack:', error instanceof Error ? error.stack : 'no stack')
|
||||
console.error('Failed to build bookmarks:', error)
|
||||
this.bookmarksListeners.forEach(cb => cb([]))
|
||||
}
|
||||
}
|
||||
@@ -356,7 +328,6 @@ class BookmarkController {
|
||||
const { relayPool, activeAccount, accountManager } = options
|
||||
|
||||
if (!activeAccount || typeof (activeAccount as { pubkey?: string }).pubkey !== 'string') {
|
||||
console.error('[bookmark] Invalid activeAccount')
|
||||
return
|
||||
}
|
||||
|
||||
@@ -366,7 +337,6 @@ class BookmarkController {
|
||||
this.hydrationGeneration++
|
||||
|
||||
// Initialize loaders for this session
|
||||
console.log('[bookmark] 🔧 Initializing EventLoader and AddressLoader with', RELAYS.length, 'relays')
|
||||
this.eventLoader = createEventLoader(relayPool, {
|
||||
eventStore: this.eventStore,
|
||||
extraRelays: RELAYS
|
||||
@@ -377,7 +347,6 @@ class BookmarkController {
|
||||
})
|
||||
|
||||
this.setLoading(true)
|
||||
console.log('[bookmark] 🔍 Starting bookmark load for', account.pubkey.slice(0, 8))
|
||||
|
||||
try {
|
||||
// Get signer for auto-decryption
|
||||
@@ -405,7 +374,6 @@ class BookmarkController {
|
||||
|
||||
// Add/update event
|
||||
this.currentEvents.set(key, evt)
|
||||
console.log('[bookmark] 📨 Event:', evt.kind, evt.id.slice(0, 8), 'encrypted:', hasEncryptedContent(evt))
|
||||
|
||||
// Emit raw event for Debug UI
|
||||
this.emitRawEvent(evt)
|
||||
@@ -415,12 +383,13 @@ class BookmarkController {
|
||||
if (!isEncrypted) {
|
||||
// For unencrypted events, build bookmarks immediately (progressive update)
|
||||
this.buildAndEmitBookmarks(maybeAccount, signerCandidate)
|
||||
.catch(err => console.error('[bookmark] ❌ Failed to update after event:', err))
|
||||
.catch(() => {
|
||||
// Silent error - will retry on next event
|
||||
})
|
||||
}
|
||||
|
||||
// Auto-decrypt if event has encrypted content (fire-and-forget, non-blocking)
|
||||
if (isEncrypted) {
|
||||
console.log('[bookmark] 🔓 Auto-decrypting event', evt.id.slice(0, 8))
|
||||
// Don't await - let it run in background
|
||||
collectBookmarksFromEvents([evt], account, signerCandidate)
|
||||
.then(({ publicItemsAll, privateItemsAll, newestCreatedAt, latestContent, allTags }) => {
|
||||
@@ -433,10 +402,6 @@ class BookmarkController {
|
||||
latestContent,
|
||||
allTags
|
||||
})
|
||||
console.log('[bookmark] ✅ Auto-decrypted:', evt.id.slice(0, 8), {
|
||||
public: publicItemsAll.length,
|
||||
private: privateItemsAll.length
|
||||
})
|
||||
|
||||
// Emit decrypt complete for Debug UI
|
||||
this.decryptCompleteListeners.forEach(cb =>
|
||||
@@ -445,10 +410,12 @@ class BookmarkController {
|
||||
|
||||
// Rebuild bookmarks with newly decrypted content (progressive update)
|
||||
this.buildAndEmitBookmarks(maybeAccount, signerCandidate)
|
||||
.catch(err => console.error('[bookmark] ❌ Failed to update after decrypt:', err))
|
||||
.catch(() => {
|
||||
// Silent error - will retry on next event
|
||||
})
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('[bookmark] ❌ Auto-decrypt failed:', evt.id.slice(0, 8), error)
|
||||
.catch(() => {
|
||||
// Silent error - decrypt failed
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -457,9 +424,8 @@ class BookmarkController {
|
||||
|
||||
// Final update after EOSE
|
||||
await this.buildAndEmitBookmarks(maybeAccount, signerCandidate)
|
||||
console.log('[bookmark] ✅ Bookmark load complete')
|
||||
} catch (error) {
|
||||
console.error('[bookmark] ❌ Failed to load bookmarks:', error)
|
||||
console.error('Failed to load bookmarks:', error)
|
||||
this.bookmarksListeners.forEach(cb => cb([]))
|
||||
} finally {
|
||||
this.setLoading(false)
|
||||
|
||||
@@ -62,7 +62,8 @@ export { dedupeNip51Events } from './bookmarkEvents'
|
||||
export const processApplesauceBookmarks = (
|
||||
bookmarks: unknown,
|
||||
activeAccount: ActiveAccount,
|
||||
isPrivate: boolean
|
||||
isPrivate: boolean,
|
||||
parentCreatedAt?: number
|
||||
): IndividualBookmark[] => {
|
||||
if (!bookmarks) return []
|
||||
|
||||
@@ -76,14 +77,14 @@ export const processApplesauceBookmarks = (
|
||||
allItems.push({
|
||||
id: note.id,
|
||||
content: '',
|
||||
created_at: Math.floor(Date.now() / 1000),
|
||||
created_at: parentCreatedAt || 0,
|
||||
pubkey: note.author || activeAccount.pubkey,
|
||||
kind: 1, // Short note kind
|
||||
tags: [],
|
||||
parsedContent: undefined,
|
||||
type: 'event' as const,
|
||||
isPrivate,
|
||||
added_at: Math.floor(Date.now() / 1000)
|
||||
added_at: parentCreatedAt || 0
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -96,14 +97,14 @@ export const processApplesauceBookmarks = (
|
||||
allItems.push({
|
||||
id: coordinate,
|
||||
content: '',
|
||||
created_at: Math.floor(Date.now() / 1000),
|
||||
created_at: parentCreatedAt || 0,
|
||||
pubkey: article.pubkey,
|
||||
kind: article.kind, // Usually 30023 for long-form articles
|
||||
tags: [],
|
||||
parsedContent: undefined,
|
||||
type: 'event' as const,
|
||||
isPrivate,
|
||||
added_at: Math.floor(Date.now() / 1000)
|
||||
added_at: parentCreatedAt || 0
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -114,14 +115,14 @@ export const processApplesauceBookmarks = (
|
||||
allItems.push({
|
||||
id: `hashtag-${hashtag}`,
|
||||
content: `#${hashtag}`,
|
||||
created_at: Math.floor(Date.now() / 1000),
|
||||
created_at: parentCreatedAt || 0,
|
||||
pubkey: activeAccount.pubkey,
|
||||
kind: 1,
|
||||
tags: [['t', hashtag]],
|
||||
parsedContent: undefined,
|
||||
type: 'event' as const,
|
||||
isPrivate,
|
||||
added_at: Math.floor(Date.now() / 1000)
|
||||
added_at: parentCreatedAt || 0
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -132,14 +133,14 @@ export const processApplesauceBookmarks = (
|
||||
allItems.push({
|
||||
id: `url-${url}`,
|
||||
content: url,
|
||||
created_at: Math.floor(Date.now() / 1000),
|
||||
created_at: parentCreatedAt || 0,
|
||||
pubkey: activeAccount.pubkey,
|
||||
kind: 1,
|
||||
tags: [['r', url]],
|
||||
parsedContent: undefined,
|
||||
type: 'event' as const,
|
||||
isPrivate,
|
||||
added_at: Math.floor(Date.now() / 1000)
|
||||
added_at: parentCreatedAt || 0
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -153,14 +154,14 @@ export const processApplesauceBookmarks = (
|
||||
.map((bookmark: BookmarkData) => ({
|
||||
id: bookmark.id!,
|
||||
content: bookmark.content || '',
|
||||
created_at: bookmark.created_at || Math.floor(Date.now() / 1000),
|
||||
created_at: bookmark.created_at || parentCreatedAt || 0,
|
||||
pubkey: activeAccount.pubkey,
|
||||
kind: bookmark.kind || 30001,
|
||||
tags: bookmark.tags || [],
|
||||
parsedContent: bookmark.content ? (getParsedContent(bookmark.content) as ParsedContent) : undefined,
|
||||
type: 'event' as const,
|
||||
isPrivate,
|
||||
added_at: bookmark.created_at || Math.floor(Date.now() / 1000)
|
||||
added_at: bookmark.created_at || parentCreatedAt || 0
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -169,29 +170,35 @@ export function hydrateItems(
|
||||
items: IndividualBookmark[],
|
||||
idToEvent: Map<string, NostrEvent>
|
||||
): IndividualBookmark[] {
|
||||
return items.map(item => {
|
||||
const ev = idToEvent.get(item.id)
|
||||
if (!ev) return item
|
||||
|
||||
// For long-form articles (kind:30023), use the article title as content
|
||||
let content = ev.content || item.content || ''
|
||||
if (ev.kind === 30023) {
|
||||
const articleTitle = getArticleTitle(ev)
|
||||
if (articleTitle) {
|
||||
content = articleTitle
|
||||
return items
|
||||
.map(item => {
|
||||
const ev = idToEvent.get(item.id)
|
||||
if (!ev) return item
|
||||
|
||||
// For long-form articles (kind:30023), use the article title as content
|
||||
let content = ev.content || item.content || ''
|
||||
if (ev.kind === 30023) {
|
||||
const articleTitle = getArticleTitle(ev)
|
||||
if (articleTitle) {
|
||||
content = articleTitle
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
...item,
|
||||
pubkey: ev.pubkey || item.pubkey,
|
||||
content,
|
||||
created_at: ev.created_at || item.created_at,
|
||||
kind: ev.kind || item.kind,
|
||||
tags: ev.tags || item.tags,
|
||||
parsedContent: ev.content ? (getParsedContent(content) as ParsedContent) : item.parsedContent
|
||||
}
|
||||
})
|
||||
|
||||
return {
|
||||
...item,
|
||||
pubkey: ev.pubkey || item.pubkey,
|
||||
content,
|
||||
created_at: ev.created_at || item.created_at,
|
||||
kind: ev.kind || item.kind,
|
||||
tags: ev.tags || item.tags,
|
||||
parsedContent: ev.content ? (getParsedContent(content) as ParsedContent) : item.parsedContent
|
||||
}
|
||||
})
|
||||
.filter(item => {
|
||||
// Filter out bookmark list events (they're containers, not content)
|
||||
const isBookmarkListEvent = item.kind === 10003 || item.kind === 30003 || item.kind === 30001
|
||||
return !isBookmarkListEvent
|
||||
})
|
||||
}
|
||||
|
||||
// Note: event decryption/collection lives in `bookmarkProcessing.ts`
|
||||
|
||||
@@ -30,8 +30,8 @@ async function decryptEvent(
|
||||
} catch {
|
||||
try {
|
||||
await Helpers.unlockHiddenTags(evt, signerCandidate as HiddenContentSigner, 'nip44' as UnlockMode)
|
||||
} catch (err) {
|
||||
console.log("[bunker] ❌ nip44.decrypt failed:", err instanceof Error ? err.message : String(err))
|
||||
} catch (_err) {
|
||||
// Ignore unlock errors
|
||||
}
|
||||
}
|
||||
} else if (evt.content && evt.content.length > 0) {
|
||||
@@ -45,8 +45,8 @@ async function decryptEvent(
|
||||
if (looksLikeNip44 && hasNip44Decrypt(signerCandidate)) {
|
||||
try {
|
||||
decryptedContent = await (signerCandidate as { nip44: { decrypt: DecryptFn } }).nip44.decrypt(evt.pubkey, evt.content)
|
||||
} catch (err) {
|
||||
console.log("[bunker] ❌ nip44.decrypt failed:", err instanceof Error ? err.message : String(err))
|
||||
} catch (_err) {
|
||||
// Ignore NIP-44 decryption errors
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,8 +54,8 @@ async function decryptEvent(
|
||||
if (!decryptedContent && hasNip04Decrypt(signerCandidate)) {
|
||||
try {
|
||||
decryptedContent = await (signerCandidate as { nip04: { decrypt: DecryptFn } }).nip04.decrypt(evt.pubkey, evt.content)
|
||||
} catch (err) {
|
||||
console.log("[bunker] ❌ nip04.decrypt failed:", err instanceof Error ? err.message : String(err))
|
||||
} catch (_err) {
|
||||
// Ignore NIP-04 decryption errors
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,7 +64,7 @@ async function decryptEvent(
|
||||
const hiddenTags = JSON.parse(decryptedContent) as string[][]
|
||||
const manualPrivate = Helpers.parseBookmarkTags(hiddenTags)
|
||||
privateItems.push(
|
||||
...processApplesauceBookmarks(manualPrivate, activeAccount, true).map(i => ({
|
||||
...processApplesauceBookmarks(manualPrivate, activeAccount, true, evt.created_at).map(i => ({
|
||||
...i,
|
||||
sourceKind: evt.kind,
|
||||
setName: dTag,
|
||||
@@ -84,7 +84,7 @@ async function decryptEvent(
|
||||
const priv = Helpers.getHiddenBookmarks(evt)
|
||||
if (priv) {
|
||||
privateItems.push(
|
||||
...processApplesauceBookmarks(priv, activeAccount, true).map(i => ({
|
||||
...processApplesauceBookmarks(priv, activeAccount, true, evt.created_at).map(i => ({
|
||||
...i,
|
||||
sourceKind: evt.kind,
|
||||
setName: dTag,
|
||||
@@ -155,7 +155,7 @@ export async function collectBookmarksFromEvents(
|
||||
|
||||
const pub = Helpers.getPublicBookmarks(evt)
|
||||
publicItemsAll.push(
|
||||
...processApplesauceBookmarks(pub, activeAccount, false).map(i => ({
|
||||
...processApplesauceBookmarks(pub, activeAccount, false, evt.created_at).map(i => ({
|
||||
...i,
|
||||
sourceKind: evt.kind,
|
||||
setName: dTag,
|
||||
@@ -181,7 +181,7 @@ export async function collectBookmarksFromEvents(
|
||||
const priv = Helpers.getHiddenBookmarks(evt)
|
||||
if (priv) {
|
||||
publicItemsAll.push(
|
||||
...processApplesauceBookmarks(priv, activeAccount, true).map(i => ({
|
||||
...processApplesauceBookmarks(priv, activeAccount, true, evt.created_at).map(i => ({
|
||||
...i,
|
||||
sourceKind: evt.kind,
|
||||
setName: dTag,
|
||||
|
||||
@@ -15,7 +15,6 @@ export const fetchContacts = async (
|
||||
): Promise<Set<string>> => {
|
||||
try {
|
||||
const relayUrls = prioritizeLocalRelays(Array.from(relayPool.relays.values()).map(relay => relay.url))
|
||||
console.log('🔍 Fetching contacts (kind 3) for user:', pubkey)
|
||||
|
||||
const partialFollowed = new Set<string>()
|
||||
const events = await queryEvents(
|
||||
@@ -51,9 +50,7 @@ export const fetchContacts = async (
|
||||
}
|
||||
// merged already via streams
|
||||
|
||||
console.log('📊 Contact events fetched:', events.length)
|
||||
|
||||
console.log('👥 Followed contacts:', followed.size)
|
||||
return followed
|
||||
} catch (error) {
|
||||
console.error('Failed to fetch contacts:', error)
|
||||
|
||||
@@ -73,13 +73,11 @@ class ContactsController {
|
||||
|
||||
// Skip if already loaded for this pubkey (unless forced)
|
||||
if (!force && this.isLoadedFor(pubkey)) {
|
||||
console.log('[contacts] ✅ Already loaded for', pubkey.slice(0, 8))
|
||||
this.emitContacts(this.currentContacts)
|
||||
return
|
||||
}
|
||||
|
||||
this.setLoading(true)
|
||||
console.log('[contacts] 🔍 Loading contacts for', pubkey.slice(0, 8))
|
||||
|
||||
try {
|
||||
const contacts = await fetchContacts(
|
||||
@@ -89,7 +87,6 @@ class ContactsController {
|
||||
// Stream partial updates
|
||||
this.currentContacts = new Set(partial)
|
||||
this.emitContacts(this.currentContacts)
|
||||
console.log('[contacts] 📥 Partial contacts:', partial.size)
|
||||
}
|
||||
)
|
||||
|
||||
@@ -98,7 +95,6 @@ class ContactsController {
|
||||
this.lastLoadedPubkey = pubkey
|
||||
this.emitContacts(this.currentContacts)
|
||||
|
||||
console.log('[contacts] ✅ Loaded', contacts.size, 'contacts')
|
||||
} catch (error) {
|
||||
console.error('[contacts] ❌ Failed to load contacts:', error)
|
||||
this.currentContacts.clear()
|
||||
|
||||
@@ -36,12 +36,10 @@ export async function createDeletionRequest(
|
||||
|
||||
const signed = await factory.sign(draft)
|
||||
|
||||
console.log('🗑️ Created kind:5 deletion request for event:', eventId.slice(0, 8))
|
||||
|
||||
// Publish to relays
|
||||
await relayPool.publish(RELAYS, signed)
|
||||
|
||||
console.log('✅ Deletion request published to', RELAYS.length, 'relay(s)')
|
||||
|
||||
return signed
|
||||
}
|
||||
|
||||
@@ -20,29 +20,34 @@ export interface BlogPostPreview {
|
||||
* @param relayPool - The relay pool to query
|
||||
* @param pubkeys - Array of pubkeys to fetch posts from
|
||||
* @param relayUrls - Array of relay URLs to query
|
||||
* @param onPost - Optional callback for streaming posts
|
||||
* @param limit - Limit for number of events to fetch (default: 100, pass null for no limit)
|
||||
* @returns Array of blog post previews
|
||||
*/
|
||||
export const fetchBlogPostsFromAuthors = async (
|
||||
relayPool: RelayPool,
|
||||
pubkeys: string[],
|
||||
relayUrls: string[],
|
||||
onPost?: (post: BlogPostPreview) => void
|
||||
onPost?: (post: BlogPostPreview) => void,
|
||||
limit: number | null = 100
|
||||
): Promise<BlogPostPreview[]> => {
|
||||
try {
|
||||
if (pubkeys.length === 0) {
|
||||
console.log('⚠️ No pubkeys to fetch blog posts from')
|
||||
return []
|
||||
}
|
||||
|
||||
console.log('📚 Fetching blog posts (kind 30023) from', pubkeys.length, 'authors')
|
||||
|
||||
// Deduplicate replaceable events by keeping the most recent version
|
||||
// Group by author + d-tag identifier
|
||||
const uniqueEvents = new Map<string, NostrEvent>()
|
||||
|
||||
const filter = limit !== null
|
||||
? { kinds: [KINDS.BlogPost], authors: pubkeys, limit }
|
||||
: { kinds: [KINDS.BlogPost], authors: pubkeys }
|
||||
|
||||
await queryEvents(
|
||||
relayPool,
|
||||
{ kinds: [KINDS.BlogPost], authors: pubkeys, limit: 100 },
|
||||
filter,
|
||||
{
|
||||
relayUrls,
|
||||
onEvent: (event: NostrEvent) => {
|
||||
@@ -68,7 +73,6 @@ export const fetchBlogPostsFromAuthors = async (
|
||||
}
|
||||
)
|
||||
|
||||
console.log('📊 Blog post events fetched (unique):', uniqueEvents.size)
|
||||
|
||||
// Convert to blog post previews and sort by published date (most recent first)
|
||||
const blogPosts: BlogPostPreview[] = Array.from(uniqueEvents.values())
|
||||
@@ -90,7 +94,6 @@ export const fetchBlogPostsFromAuthors = async (
|
||||
return timeB - timeA // Most recent first
|
||||
})
|
||||
|
||||
console.log('📰 Processed', blogPosts.length, 'unique blog posts')
|
||||
|
||||
return blogPosts
|
||||
} catch (error) {
|
||||
|
||||
@@ -46,7 +46,6 @@ export async function createHighlight(
|
||||
}
|
||||
|
||||
// Create EventFactory with the account as signer
|
||||
console.log("[bunker] Creating EventFactory with signer:", { signerType: account.signer?.constructor?.name })
|
||||
const factory = new EventFactory({ signer: account.signer })
|
||||
|
||||
let blueprintSource: NostrEvent | AddressPointer | string
|
||||
@@ -117,9 +116,7 @@ export async function createHighlight(
|
||||
}
|
||||
|
||||
// Sign the event
|
||||
console.log('[bunker] Signing highlight event...', { kind: highlightEvent.kind, tags: highlightEvent.tags.length })
|
||||
const signedEvent = await factory.sign(highlightEvent)
|
||||
console.log('[bunker] ✅ Highlight signed successfully!', { id: signedEvent.id.slice(0, 8) })
|
||||
|
||||
// Use unified write service to store and publish
|
||||
await publishEvent(relayPool, eventStore, signedEvent)
|
||||
|
||||
@@ -22,7 +22,6 @@ export const fetchHighlights = async (
|
||||
const cacheKey = highlightCache.authorKey(pubkey)
|
||||
const cached = highlightCache.get(cacheKey)
|
||||
if (cached) {
|
||||
console.log(`📌 Using cached highlights for author (${cached.length} items)`)
|
||||
// Stream cached highlights if callback provided
|
||||
if (onHighlight) {
|
||||
cached.forEach(h => onHighlight(h))
|
||||
@@ -50,7 +49,6 @@ export const fetchHighlights = async (
|
||||
}
|
||||
)
|
||||
|
||||
console.log(`📌 Fetched ${rawEvents.length} highlight events for author:`, pubkey.slice(0, 8))
|
||||
|
||||
// Store all events in event store if provided
|
||||
if (eventStore) {
|
||||
|
||||
@@ -23,7 +23,6 @@ export const fetchHighlightsForArticle = async (
|
||||
const cacheKey = highlightCache.articleKey(articleCoordinate)
|
||||
const cached = highlightCache.get(cacheKey)
|
||||
if (cached) {
|
||||
console.log(`📌 Using cached highlights for article (${cached.length} items)`)
|
||||
// Stream cached highlights if callback provided
|
||||
if (onHighlight) {
|
||||
cached.forEach(h => onHighlight(h))
|
||||
@@ -54,7 +53,6 @@ export const fetchHighlightsForArticle = async (
|
||||
])
|
||||
|
||||
const rawEvents = [...aTagEvents, ...eTagEvents]
|
||||
console.log(`📌 Fetched ${rawEvents.length} highlight events for article:`, articleCoordinate)
|
||||
|
||||
// Store all events in event store if provided
|
||||
if (eventStore) {
|
||||
|
||||
@@ -22,7 +22,6 @@ export const fetchHighlightsForUrl = async (
|
||||
const cacheKey = highlightCache.urlKey(url)
|
||||
const cached = highlightCache.get(cacheKey)
|
||||
if (cached) {
|
||||
console.log(`📌 Using cached highlights for URL (${cached.length} items)`)
|
||||
// Stream cached highlights if callback provided
|
||||
if (onHighlight) {
|
||||
cached.forEach(h => onHighlight(h))
|
||||
@@ -50,7 +49,6 @@ export const fetchHighlightsForUrl = async (
|
||||
}
|
||||
)
|
||||
|
||||
console.log(`📌 Fetched ${rawEvents.length} highlight events for URL:`, url)
|
||||
|
||||
// Store all events in event store if provided
|
||||
if (eventStore) {
|
||||
|
||||
@@ -21,11 +21,9 @@ export const fetchHighlightsFromAuthors = async (
|
||||
): Promise<Highlight[]> => {
|
||||
try {
|
||||
if (pubkeys.length === 0) {
|
||||
console.log('⚠️ No pubkeys to fetch highlights from')
|
||||
return []
|
||||
}
|
||||
|
||||
console.log('💡 Fetching highlights (kind 9802) from', pubkeys.length, 'authors')
|
||||
|
||||
const seenIds = new Set<string>()
|
||||
const rawEvents = await queryEvents(
|
||||
@@ -55,7 +53,6 @@ export const fetchHighlightsFromAuthors = async (
|
||||
const uniqueEvents = dedupeHighlights(rawEvents)
|
||||
const highlights = uniqueEvents.map(eventToHighlight)
|
||||
|
||||
console.log('💡 Processed', highlights.length, 'unique highlights')
|
||||
|
||||
return sortHighlights(highlights)
|
||||
} catch (error) {
|
||||
|
||||
@@ -110,7 +110,6 @@ class HighlightsController {
|
||||
|
||||
// Skip if already loaded for this pubkey (unless forced)
|
||||
if (!force && this.isLoadedFor(pubkey)) {
|
||||
console.log('[highlights] ✅ Already loaded for', pubkey.slice(0, 8))
|
||||
this.emitHighlights(this.currentHighlights)
|
||||
return
|
||||
}
|
||||
@@ -120,7 +119,6 @@ class HighlightsController {
|
||||
const currentGeneration = this.generation
|
||||
|
||||
this.setLoading(true)
|
||||
console.log('[highlights] 🔍 Loading highlights for', pubkey.slice(0, 8))
|
||||
|
||||
try {
|
||||
const seenIds = new Set<string>()
|
||||
@@ -134,7 +132,6 @@ class HighlightsController {
|
||||
}
|
||||
if (lastSyncedAt) {
|
||||
filter.since = lastSyncedAt
|
||||
console.log('[highlights] 📅 Incremental sync since', new Date(lastSyncedAt * 1000).toISOString())
|
||||
}
|
||||
|
||||
const events = await queryEvents(
|
||||
@@ -165,7 +162,6 @@ class HighlightsController {
|
||||
|
||||
// Check if still active after async operation
|
||||
if (currentGeneration !== this.generation) {
|
||||
console.log('[highlights] ⚠️ Load cancelled (generation mismatch)')
|
||||
return
|
||||
}
|
||||
|
||||
@@ -189,7 +185,6 @@ class HighlightsController {
|
||||
this.setLastSyncedAt(pubkey, newestTimestamp)
|
||||
}
|
||||
|
||||
console.log('[highlights] ✅ Loaded', sorted.length, 'highlights')
|
||||
} catch (error) {
|
||||
console.error('[highlights] ❌ Failed to load highlights:', error)
|
||||
this.currentHighlights = []
|
||||
|
||||
@@ -13,7 +13,6 @@ const CACHE_NAME = 'boris-image-cache-v1'
|
||||
export async function clearImageCache(): Promise<void> {
|
||||
try {
|
||||
await caches.delete(CACHE_NAME)
|
||||
console.log('🗑️ Cleared all cached images')
|
||||
} catch (err) {
|
||||
console.error('Failed to clear image cache:', err)
|
||||
}
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
import { RelayPool } from 'applesauce-relay'
|
||||
import { NostrEvent } from 'nostr-tools'
|
||||
import { Helpers } from 'applesauce-core'
|
||||
import { RELAYS } from '../config/relays'
|
||||
import { KINDS } from '../config/kinds'
|
||||
import { MARK_AS_READ_EMOJI } from './reactionService'
|
||||
import { ARCHIVE_EMOJI } from './reactionService'
|
||||
import { BlogPostPreview } from './exploreService'
|
||||
import { queryEvents } from './dataFetch'
|
||||
|
||||
@@ -30,15 +29,15 @@ export async function fetchReadArticles(
|
||||
try {
|
||||
// Fetch kind:7 and kind:17 reactions in parallel
|
||||
const [kind7Events, kind17Events] = await Promise.all([
|
||||
queryEvents(relayPool, { kinds: [KINDS.ReactionToEvent], authors: [userPubkey] }, { relayUrls: RELAYS }),
|
||||
queryEvents(relayPool, { kinds: [KINDS.ReactionToUrl], authors: [userPubkey] }, { relayUrls: RELAYS })
|
||||
queryEvents(relayPool, { kinds: [KINDS.ReactionToEvent], authors: [userPubkey] }),
|
||||
queryEvents(relayPool, { kinds: [KINDS.ReactionToUrl], authors: [userPubkey] })
|
||||
])
|
||||
|
||||
const readArticles: ReadArticle[] = []
|
||||
|
||||
// Process kind:7 reactions (nostr-native articles)
|
||||
for (const event of kind7Events) {
|
||||
if (event.content === MARK_AS_READ_EMOJI) {
|
||||
if (event.content === ARCHIVE_EMOJI) {
|
||||
const eTag = event.tags.find((t) => t[0] === 'e')
|
||||
const pTag = event.tags.find((t) => t[0] === 'p')
|
||||
const kTag = event.tags.find((t) => t[0] === 'k')
|
||||
@@ -58,7 +57,7 @@ export async function fetchReadArticles(
|
||||
|
||||
// Process kind:17 reactions (external URLs)
|
||||
for (const event of kind17Events) {
|
||||
if (event.content === MARK_AS_READ_EMOJI) {
|
||||
if (event.content === ARCHIVE_EMOJI) {
|
||||
const rTag = event.tags.find((t) => t[0] === 'r')
|
||||
|
||||
if (rTag && rTag[1]) {
|
||||
@@ -115,8 +114,7 @@ export async function fetchReadArticlesWithData(
|
||||
|
||||
const articleEvents = await queryEvents(
|
||||
relayPool,
|
||||
{ kinds: [KINDS.BlogPost], ids: eventIds },
|
||||
{ relayUrls: RELAYS }
|
||||
{ kinds: [KINDS.BlogPost], ids: eventIds }
|
||||
)
|
||||
|
||||
// Deduplicate article events by ID
|
||||
|
||||
@@ -4,12 +4,12 @@ import { queryEvents } from './dataFetch'
|
||||
import { RELAYS } from '../config/relays'
|
||||
import { KINDS } from '../config/kinds'
|
||||
import { ReadItem } from './readsService'
|
||||
import { processReadingPositions, processMarkedAsRead, filterValidItems, sortByReadingActivity } from './readingDataProcessor'
|
||||
import { processReadingProgress, processMarkedAsRead, filterValidItems, sortByReadingActivity } from './readingDataProcessor'
|
||||
import { mergeReadItem } from '../utils/readItemMerge'
|
||||
|
||||
/**
|
||||
* Fetches external URL links with reading progress from:
|
||||
* - URLs with reading progress (kind:30078)
|
||||
* - URLs with reading progress (kind:39802)
|
||||
* - Manually marked as read URLs (kind:7, kind:17)
|
||||
*/
|
||||
export async function fetchLinks(
|
||||
@@ -17,7 +17,6 @@ export async function fetchLinks(
|
||||
userPubkey: string,
|
||||
onItem?: (item: ReadItem) => void
|
||||
): Promise<ReadItem[]> {
|
||||
console.log('🔗 [Links] Fetching external links for user:', userPubkey.slice(0, 8))
|
||||
|
||||
const linksMap = new Map<string, ReadItem>()
|
||||
|
||||
@@ -32,18 +31,13 @@ export async function fetchLinks(
|
||||
|
||||
try {
|
||||
// Fetch all data sources in parallel
|
||||
const [readingPositionEvents, markedAsReadArticles] = await Promise.all([
|
||||
queryEvents(relayPool, { kinds: [KINDS.AppData], authors: [userPubkey] }, { relayUrls: RELAYS }),
|
||||
const [progressEvents, markedAsReadArticles] = await Promise.all([
|
||||
queryEvents(relayPool, { kinds: [KINDS.ReadingProgress], authors: [userPubkey] }, { relayUrls: RELAYS }),
|
||||
fetchReadArticles(relayPool, userPubkey)
|
||||
])
|
||||
|
||||
console.log('📊 [Links] Data fetched:', {
|
||||
readingPositions: readingPositionEvents.length,
|
||||
markedAsRead: markedAsReadArticles.length
|
||||
})
|
||||
|
||||
// Process reading positions and emit external items
|
||||
processReadingPositions(readingPositionEvents, linksMap)
|
||||
// Process reading progress events (kind 39802)
|
||||
processReadingProgress(progressEvents, linksMap)
|
||||
if (onItem) {
|
||||
linksMap.forEach(item => {
|
||||
if (item.type === 'external') {
|
||||
@@ -79,7 +73,6 @@ export async function fetchLinks(
|
||||
const validLinks = filterValidItems(links)
|
||||
const sortedLinks = sortByReadingActivity(validLinks)
|
||||
|
||||
console.log('✅ [Links] Processed', sortedLinks.length, 'total links')
|
||||
return sortedLinks
|
||||
|
||||
} catch (error) {
|
||||
|
||||
139
src/services/nostrverseHighlightsController.ts
Normal file
139
src/services/nostrverseHighlightsController.ts
Normal file
@@ -0,0 +1,139 @@
|
||||
import { RelayPool } from 'applesauce-relay'
|
||||
import { IEventStore } from 'applesauce-core'
|
||||
import { Highlight } from '../types/highlights'
|
||||
import { queryEvents } from './dataFetch'
|
||||
import { KINDS } from '../config/kinds'
|
||||
import { eventToHighlight, sortHighlights } from './highlightEventProcessor'
|
||||
|
||||
type HighlightsCallback = (highlights: Highlight[]) => void
|
||||
type LoadingCallback = (loading: boolean) => void
|
||||
|
||||
const LAST_SYNCED_KEY = 'nostrverse_highlights_last_synced'
|
||||
|
||||
class NostrverseHighlightsController {
|
||||
private highlightsListeners: HighlightsCallback[] = []
|
||||
private loadingListeners: LoadingCallback[] = []
|
||||
|
||||
private currentHighlights: Highlight[] = []
|
||||
private loaded = false
|
||||
private generation = 0
|
||||
|
||||
onHighlights(cb: HighlightsCallback): () => void {
|
||||
this.highlightsListeners.push(cb)
|
||||
return () => {
|
||||
this.highlightsListeners = this.highlightsListeners.filter(l => l !== cb)
|
||||
}
|
||||
}
|
||||
|
||||
onLoading(cb: LoadingCallback): () => void {
|
||||
this.loadingListeners.push(cb)
|
||||
return () => {
|
||||
this.loadingListeners = this.loadingListeners.filter(l => l !== cb)
|
||||
}
|
||||
}
|
||||
|
||||
private setLoading(loading: boolean): void {
|
||||
this.loadingListeners.forEach(cb => cb(loading))
|
||||
}
|
||||
|
||||
private emitHighlights(highlights: Highlight[]): void {
|
||||
this.highlightsListeners.forEach(cb => cb(highlights))
|
||||
}
|
||||
|
||||
getHighlights(): Highlight[] {
|
||||
return [...this.currentHighlights]
|
||||
}
|
||||
|
||||
isLoaded(): boolean {
|
||||
return this.loaded
|
||||
}
|
||||
|
||||
private getLastSyncedAt(): number | null {
|
||||
try {
|
||||
const raw = localStorage.getItem(LAST_SYNCED_KEY)
|
||||
if (!raw) return null
|
||||
const parsed = JSON.parse(raw)
|
||||
return typeof parsed?.ts === 'number' ? parsed.ts : null
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
private setLastSyncedAt(timestamp: number): void {
|
||||
try {
|
||||
localStorage.setItem(LAST_SYNCED_KEY, JSON.stringify({ ts: timestamp }))
|
||||
} catch { /* ignore */ }
|
||||
}
|
||||
|
||||
async start(options: {
|
||||
relayPool: RelayPool
|
||||
eventStore: IEventStore
|
||||
force?: boolean
|
||||
}): Promise<void> {
|
||||
const { relayPool, eventStore, force = false } = options
|
||||
|
||||
if (!force && this.loaded) {
|
||||
this.emitHighlights(this.currentHighlights)
|
||||
return
|
||||
}
|
||||
|
||||
this.generation++
|
||||
const currentGeneration = this.generation
|
||||
this.setLoading(true)
|
||||
|
||||
try {
|
||||
const seenIds = new Set<string>()
|
||||
const highlightsMap = new Map<string, Highlight>()
|
||||
|
||||
const lastSyncedAt = force ? null : this.getLastSyncedAt()
|
||||
const filter: { kinds: number[]; since?: number } = { kinds: [KINDS.Highlights] }
|
||||
if (lastSyncedAt) filter.since = lastSyncedAt
|
||||
|
||||
const events = await queryEvents(
|
||||
relayPool,
|
||||
filter,
|
||||
{
|
||||
onEvent: (evt) => {
|
||||
if (currentGeneration !== this.generation) return
|
||||
if (seenIds.has(evt.id)) return
|
||||
seenIds.add(evt.id)
|
||||
|
||||
eventStore.add(evt)
|
||||
const highlight = eventToHighlight(evt)
|
||||
highlightsMap.set(highlight.id, highlight)
|
||||
|
||||
const sorted = sortHighlights(Array.from(highlightsMap.values()))
|
||||
this.currentHighlights = sorted
|
||||
this.emitHighlights(sorted)
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
if (currentGeneration !== this.generation) return
|
||||
|
||||
events.forEach(evt => eventStore.add(evt))
|
||||
|
||||
const highlights = events.map(eventToHighlight)
|
||||
const unique = Array.from(new Map(highlights.map(h => [h.id, h])).values())
|
||||
const sorted = sortHighlights(unique)
|
||||
|
||||
this.currentHighlights = sorted
|
||||
this.loaded = true
|
||||
this.emitHighlights(sorted)
|
||||
|
||||
if (sorted.length > 0) {
|
||||
const newest = Math.max(...sorted.map(h => h.created_at))
|
||||
this.setLastSyncedAt(newest)
|
||||
}
|
||||
} catch (err) {
|
||||
this.currentHighlights = []
|
||||
this.emitHighlights(this.currentHighlights)
|
||||
} finally {
|
||||
if (currentGeneration === this.generation) this.setLoading(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const nostrverseHighlightsController = new NostrverseHighlightsController()
|
||||
|
||||
|
||||
@@ -20,10 +20,10 @@ export const fetchNostrverseBlogPosts = async (
|
||||
relayPool: RelayPool,
|
||||
relayUrls: string[],
|
||||
limit = 50,
|
||||
eventStore?: IEventStore
|
||||
eventStore?: IEventStore,
|
||||
onPost?: (post: BlogPostPreview) => void
|
||||
): Promise<BlogPostPreview[]> => {
|
||||
try {
|
||||
console.log('[NOSTRVERSE] 📚 Fetching blog posts (kind 30023), limit:', limit)
|
||||
|
||||
// Deduplicate replaceable events by keeping the most recent version
|
||||
const uniqueEvents = new Map<string, NostrEvent>()
|
||||
@@ -44,12 +44,24 @@ export const fetchNostrverseBlogPosts = async (
|
||||
const existing = uniqueEvents.get(key)
|
||||
if (!existing || event.created_at > existing.created_at) {
|
||||
uniqueEvents.set(key, event)
|
||||
|
||||
// Stream post immediately if callback provided
|
||||
if (onPost) {
|
||||
const post: BlogPostPreview = {
|
||||
event,
|
||||
title: getArticleTitle(event) || 'Untitled',
|
||||
summary: getArticleSummary(event),
|
||||
image: getArticleImage(event),
|
||||
published: getArticlePublished(event),
|
||||
author: event.pubkey
|
||||
}
|
||||
onPost(post)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
console.log('[NOSTRVERSE] 📊 Blog post events fetched (unique):', uniqueEvents.size)
|
||||
|
||||
// Convert to blog post previews and sort by published date (most recent first)
|
||||
const blogPosts: BlogPostPreview[] = Array.from(uniqueEvents.values())
|
||||
@@ -67,7 +79,6 @@ export const fetchNostrverseBlogPosts = async (
|
||||
return timeB - timeA // Most recent first
|
||||
})
|
||||
|
||||
console.log('[NOSTRVERSE] 📰 Processed', blogPosts.length, 'unique blog posts')
|
||||
|
||||
return blogPosts
|
||||
} catch (error) {
|
||||
@@ -89,9 +100,10 @@ export const fetchNostrverseHighlights = async (
|
||||
eventStore?: IEventStore
|
||||
): Promise<Highlight[]> => {
|
||||
try {
|
||||
console.log('[NOSTRVERSE] 💡 Fetching highlights (kind 9802), limit:', limit)
|
||||
|
||||
const seenIds = new Set<string>()
|
||||
// Collect but do not block callers awaiting network completion
|
||||
const collected: NostrEvent[] = []
|
||||
const rawEvents = await queryEvents(
|
||||
relayPool,
|
||||
{ kinds: [9802], limit },
|
||||
@@ -104,6 +116,7 @@ export const fetchNostrverseHighlights = async (
|
||||
if (eventStore) {
|
||||
eventStore.add(event)
|
||||
}
|
||||
collected.push(event)
|
||||
}
|
||||
}
|
||||
)
|
||||
@@ -113,10 +126,9 @@ export const fetchNostrverseHighlights = async (
|
||||
rawEvents.forEach(evt => eventStore.add(evt))
|
||||
}
|
||||
|
||||
const uniqueEvents = dedupeHighlights(rawEvents)
|
||||
const uniqueEvents = dedupeHighlights([...collected, ...rawEvents])
|
||||
const highlights = uniqueEvents.map(eventToHighlight)
|
||||
|
||||
console.log('[NOSTRVERSE] 💡 Processed', highlights.length, 'unique highlights')
|
||||
|
||||
return sortHighlights(highlights)
|
||||
} catch (error) {
|
||||
|
||||
169
src/services/nostrverseWritingsController.ts
Normal file
169
src/services/nostrverseWritingsController.ts
Normal file
@@ -0,0 +1,169 @@
|
||||
import { RelayPool } from 'applesauce-relay'
|
||||
import { IEventStore, Helpers } from 'applesauce-core'
|
||||
import { NostrEvent } from 'nostr-tools'
|
||||
import { KINDS } from '../config/kinds'
|
||||
import { queryEvents } from './dataFetch'
|
||||
import { BlogPostPreview } from './exploreService'
|
||||
|
||||
const { getArticleTitle, getArticleSummary, getArticleImage, getArticlePublished } = Helpers
|
||||
|
||||
type WritingsCallback = (posts: BlogPostPreview[]) => void
|
||||
type LoadingCallback = (loading: boolean) => void
|
||||
|
||||
const LAST_SYNCED_KEY = 'nostrverse_writings_last_synced'
|
||||
|
||||
function toPreview(event: NostrEvent): BlogPostPreview {
|
||||
return {
|
||||
event,
|
||||
title: getArticleTitle(event) || 'Untitled',
|
||||
summary: getArticleSummary(event),
|
||||
image: getArticleImage(event),
|
||||
published: getArticlePublished(event),
|
||||
author: event.pubkey
|
||||
}
|
||||
}
|
||||
|
||||
function sortPosts(posts: BlogPostPreview[]): BlogPostPreview[] {
|
||||
return posts.slice().sort((a, b) => {
|
||||
const timeA = a.published || a.event.created_at
|
||||
const timeB = b.published || b.event.created_at
|
||||
return timeB - timeA
|
||||
})
|
||||
}
|
||||
|
||||
class NostrverseWritingsController {
|
||||
private writingsListeners: WritingsCallback[] = []
|
||||
private loadingListeners: LoadingCallback[] = []
|
||||
|
||||
private currentPosts: BlogPostPreview[] = []
|
||||
private loaded = false
|
||||
private generation = 0
|
||||
|
||||
onWritings(cb: WritingsCallback): () => void {
|
||||
this.writingsListeners.push(cb)
|
||||
return () => {
|
||||
this.writingsListeners = this.writingsListeners.filter(l => l !== cb)
|
||||
}
|
||||
}
|
||||
|
||||
onLoading(cb: LoadingCallback): () => void {
|
||||
this.loadingListeners.push(cb)
|
||||
return () => {
|
||||
this.loadingListeners = this.loadingListeners.filter(l => l !== cb)
|
||||
}
|
||||
}
|
||||
|
||||
private setLoading(loading: boolean): void {
|
||||
this.loadingListeners.forEach(cb => cb(loading))
|
||||
}
|
||||
|
||||
private emitWritings(posts: BlogPostPreview[]): void {
|
||||
this.writingsListeners.forEach(cb => cb(posts))
|
||||
}
|
||||
|
||||
getWritings(): BlogPostPreview[] {
|
||||
return [...this.currentPosts]
|
||||
}
|
||||
|
||||
isLoaded(): boolean {
|
||||
return this.loaded
|
||||
}
|
||||
|
||||
private getLastSyncedAt(): number | null {
|
||||
try {
|
||||
const raw = localStorage.getItem(LAST_SYNCED_KEY)
|
||||
if (!raw) return null
|
||||
const parsed = JSON.parse(raw)
|
||||
return typeof parsed?.ts === 'number' ? parsed.ts : null
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
private setLastSyncedAt(ts: number): void {
|
||||
try { localStorage.setItem(LAST_SYNCED_KEY, JSON.stringify({ ts })) } catch { /* ignore */ }
|
||||
}
|
||||
|
||||
async start(options: {
|
||||
relayPool: RelayPool
|
||||
eventStore: IEventStore
|
||||
force?: boolean
|
||||
}): Promise<void> {
|
||||
const { relayPool, eventStore, force = false } = options
|
||||
|
||||
if (!force && this.loaded) {
|
||||
this.emitWritings(this.currentPosts)
|
||||
return
|
||||
}
|
||||
|
||||
this.generation++
|
||||
const currentGeneration = this.generation
|
||||
this.setLoading(true)
|
||||
|
||||
try {
|
||||
const seenIds = new Set<string>()
|
||||
const uniqueByReplaceable = new Map<string, BlogPostPreview>()
|
||||
|
||||
const lastSyncedAt = force ? null : this.getLastSyncedAt()
|
||||
const filter: { kinds: number[]; since?: number } = { kinds: [KINDS.BlogPost] }
|
||||
if (lastSyncedAt) filter.since = lastSyncedAt
|
||||
|
||||
const events = await queryEvents(
|
||||
relayPool,
|
||||
filter,
|
||||
{
|
||||
onEvent: (evt) => {
|
||||
if (currentGeneration !== this.generation) return
|
||||
if (seenIds.has(evt.id)) return
|
||||
seenIds.add(evt.id)
|
||||
|
||||
eventStore.add(evt)
|
||||
|
||||
const dTag = evt.tags.find(t => t[0] === 'd')?.[1] || ''
|
||||
const key = `${evt.pubkey}:${dTag}`
|
||||
const preview = toPreview(evt)
|
||||
const existing = uniqueByReplaceable.get(key)
|
||||
if (!existing || evt.created_at > existing.event.created_at) {
|
||||
uniqueByReplaceable.set(key, preview)
|
||||
const sorted = sortPosts(Array.from(uniqueByReplaceable.values()))
|
||||
this.currentPosts = sorted
|
||||
this.emitWritings(sorted)
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
if (currentGeneration !== this.generation) return
|
||||
|
||||
events.forEach(evt => eventStore.add(evt))
|
||||
|
||||
events.forEach(evt => {
|
||||
const dTag = evt.tags.find(t => t[0] === 'd')?.[1] || ''
|
||||
const key = `${evt.pubkey}:${dTag}`
|
||||
const existing = uniqueByReplaceable.get(key)
|
||||
if (!existing || evt.created_at > existing.event.created_at) {
|
||||
uniqueByReplaceable.set(key, toPreview(evt))
|
||||
}
|
||||
})
|
||||
|
||||
const sorted = sortPosts(Array.from(uniqueByReplaceable.values()))
|
||||
this.currentPosts = sorted
|
||||
this.loaded = true
|
||||
this.emitWritings(sorted)
|
||||
|
||||
if (sorted.length > 0) {
|
||||
const newest = Math.max(...sorted.map(p => p.event.created_at))
|
||||
this.setLastSyncedAt(newest)
|
||||
}
|
||||
} catch {
|
||||
this.currentPosts = []
|
||||
this.emitWritings(this.currentPosts)
|
||||
} finally {
|
||||
if (currentGeneration === this.generation) this.setLoading(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const nostrverseWritingsController = new NostrverseWritingsController()
|
||||
|
||||
|
||||
@@ -20,7 +20,6 @@ const syncStateListeners: Array<(eventId: string, isSyncing: boolean) => void> =
|
||||
*/
|
||||
export function markEventAsOfflineCreated(eventId: string): void {
|
||||
offlineCreatedEvents.add(eventId)
|
||||
console.log(`📝 Marked event ${eventId.slice(0, 8)} as offline-created. Total: ${offlineCreatedEvents.size}`)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -57,49 +56,35 @@ export async function syncLocalEventsToRemote(
|
||||
eventStore: IEventStore
|
||||
): Promise<void> {
|
||||
if (isSyncing) {
|
||||
console.log('⏳ Sync already in progress, skipping...')
|
||||
return
|
||||
}
|
||||
|
||||
console.log('🔄 Coming back online - syncing local events to remote relays...')
|
||||
console.log(`📦 Offline events tracked: ${offlineCreatedEvents.size}`)
|
||||
isSyncing = true
|
||||
|
||||
try {
|
||||
const remoteRelays = RELAYS.filter(url => !isLocalRelay(url))
|
||||
|
||||
console.log(`📡 Remote relays: ${remoteRelays.length}`)
|
||||
|
||||
if (remoteRelays.length === 0) {
|
||||
console.log('⚠️ No remote relays available for sync')
|
||||
isSyncing = false
|
||||
return
|
||||
}
|
||||
|
||||
if (offlineCreatedEvents.size === 0) {
|
||||
console.log('✅ No offline events to sync')
|
||||
isSyncing = false
|
||||
return
|
||||
}
|
||||
|
||||
// Get events from EventStore using the tracked IDs
|
||||
const eventsToSync: NostrEvent[] = []
|
||||
console.log(`🔍 Querying EventStore for ${offlineCreatedEvents.size} offline events...`)
|
||||
|
||||
for (const eventId of offlineCreatedEvents) {
|
||||
const event = eventStore.getEvent(eventId)
|
||||
if (event) {
|
||||
console.log(`📥 Found event ${eventId.slice(0, 8)} (kind ${event.kind}) in EventStore`)
|
||||
eventsToSync.push(event)
|
||||
} else {
|
||||
console.warn(`⚠️ Event ${eventId.slice(0, 8)} not found in EventStore`)
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`📊 Total events to sync: ${eventsToSync.length}`)
|
||||
|
||||
if (eventsToSync.length === 0) {
|
||||
console.log('✅ No events found in EventStore to sync')
|
||||
isSyncing = false
|
||||
offlineCreatedEvents.clear()
|
||||
return
|
||||
@@ -110,8 +95,6 @@ export async function syncLocalEventsToRemote(
|
||||
new Map(eventsToSync.map(e => [e.id, e])).values()
|
||||
)
|
||||
|
||||
console.log(`📤 Syncing ${uniqueEvents.length} event(s) to remote relays...`)
|
||||
|
||||
// Mark all events as syncing
|
||||
uniqueEvents.forEach(event => {
|
||||
syncingEvents.add(event.id)
|
||||
@@ -119,21 +102,16 @@ export async function syncLocalEventsToRemote(
|
||||
})
|
||||
|
||||
// Publish to remote relays
|
||||
let successCount = 0
|
||||
const successfulIds: string[] = []
|
||||
|
||||
for (const event of uniqueEvents) {
|
||||
try {
|
||||
await relayPool.publish(remoteRelays, event)
|
||||
successCount++
|
||||
successfulIds.push(event.id)
|
||||
console.log(`✅ Synced event ${event.id.slice(0, 8)}`)
|
||||
} catch (error) {
|
||||
console.warn(`⚠️ Failed to sync event ${event.id.slice(0, 8)}:`, error)
|
||||
// Silently fail for individual events
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`✅ Synced ${successCount}/${uniqueEvents.length} events to remote relays`)
|
||||
|
||||
// Clear syncing state and offline tracking for successful events
|
||||
successfulIds.forEach(eventId => {
|
||||
@@ -150,7 +128,7 @@ export async function syncLocalEventsToRemote(
|
||||
}
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('❌ Error during offline sync:', error)
|
||||
// Silently fail
|
||||
} finally {
|
||||
isSyncing = false
|
||||
}
|
||||
|
||||
@@ -22,7 +22,6 @@ export const fetchProfiles = async (
|
||||
}
|
||||
|
||||
const uniquePubkeys = Array.from(new Set(pubkeys))
|
||||
console.log('👤 Fetching profiles (kind:0) for', uniquePubkeys.length, 'authors')
|
||||
|
||||
const relayUrls = Array.from(relayPool.relays.values()).map(relay => relay.url)
|
||||
const prioritized = prioritizeLocalRelays(relayUrls)
|
||||
@@ -65,7 +64,6 @@ export const fetchProfiles = async (
|
||||
await lastValueFrom(merge(local$, remote$).pipe(toArray()))
|
||||
|
||||
const profiles = Array.from(profilesByPubkey.values())
|
||||
console.log('✅ Fetched', profiles.length, 'unique profiles')
|
||||
|
||||
// Rebroadcast profiles to local/all relays based on settings
|
||||
if (profiles.length > 0) {
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
import { EventFactory } from 'applesauce-factory'
|
||||
import { RelayPool, completeOnEose, onlyEvents } from 'applesauce-relay'
|
||||
import { IAccount } from 'applesauce-accounts'
|
||||
import { NostrEvent } from 'nostr-tools'
|
||||
import { lastValueFrom, takeUntil, timer, toArray } from 'rxjs'
|
||||
import { RELAYS } from '../config/relays'
|
||||
import { EventFactory } from 'applesauce-factory'
|
||||
import { getActiveRelayUrls } from './relayManager'
|
||||
|
||||
const MARK_AS_READ_EMOJI = '📚'
|
||||
const ARCHIVE_EMOJI = '📚'
|
||||
|
||||
export { MARK_AS_READ_EMOJI }
|
||||
export { ARCHIVE_EMOJI }
|
||||
|
||||
/**
|
||||
* Creates a kind:7 reaction to a nostr event (for nostr-native articles)
|
||||
@@ -23,7 +23,8 @@ export async function createEventReaction(
|
||||
eventAuthor: string,
|
||||
eventKind: number,
|
||||
account: IAccount,
|
||||
relayPool: RelayPool
|
||||
relayPool: RelayPool,
|
||||
options?: { aCoord?: string }
|
||||
): Promise<NostrEvent> {
|
||||
const factory = new EventFactory({ signer: account })
|
||||
|
||||
@@ -32,22 +33,23 @@ export async function createEventReaction(
|
||||
['p', eventAuthor],
|
||||
['k', eventKind.toString()]
|
||||
]
|
||||
if (options?.aCoord) {
|
||||
tags.push(['a', options.aCoord])
|
||||
}
|
||||
|
||||
const draft = await factory.create(async () => ({
|
||||
kind: 7, // Reaction
|
||||
content: MARK_AS_READ_EMOJI,
|
||||
content: ARCHIVE_EMOJI,
|
||||
tags,
|
||||
created_at: Math.floor(Date.now() / 1000)
|
||||
}))
|
||||
|
||||
const signed = await factory.sign(draft)
|
||||
|
||||
console.log('📚 Created kind:7 reaction (mark as read) for event:', eventId.slice(0, 8))
|
||||
|
||||
// Publish to relays
|
||||
await relayPool.publish(RELAYS, signed)
|
||||
await relayPool.publish(getActiveRelayUrls(relayPool), signed)
|
||||
|
||||
console.log('✅ Reaction published to', RELAYS.length, 'relay(s)')
|
||||
|
||||
return signed
|
||||
}
|
||||
@@ -87,23 +89,42 @@ export async function createWebsiteReaction(
|
||||
|
||||
const draft = await factory.create(async () => ({
|
||||
kind: 17, // Reaction to a website
|
||||
content: MARK_AS_READ_EMOJI,
|
||||
content: ARCHIVE_EMOJI,
|
||||
tags,
|
||||
created_at: Math.floor(Date.now() / 1000)
|
||||
}))
|
||||
|
||||
const signed = await factory.sign(draft)
|
||||
|
||||
console.log('📚 Created kind:17 reaction (mark as read) for URL:', normalizedUrl)
|
||||
|
||||
// Publish to relays
|
||||
await relayPool.publish(RELAYS, signed)
|
||||
await relayPool.publish(getActiveRelayUrls(relayPool), signed)
|
||||
|
||||
console.log('✅ Website reaction published to', RELAYS.length, 'relay(s)')
|
||||
|
||||
return signed
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a deletion request (NIP-09) for a reaction event to effectively un-archive.
|
||||
* The caller must know the reaction event id to delete.
|
||||
*/
|
||||
export async function deleteReaction(
|
||||
reactionEventId: string,
|
||||
account: IAccount,
|
||||
relayPool: RelayPool
|
||||
): Promise<NostrEvent> {
|
||||
const factory = new EventFactory({ signer: account })
|
||||
const draft = await factory.create(async () => ({
|
||||
kind: 5, // Deletion per NIP-09
|
||||
content: 'unarchive',
|
||||
tags: [['e', reactionEventId]],
|
||||
created_at: Math.floor(Date.now() / 1000)
|
||||
}))
|
||||
const signed = await factory.sign(draft)
|
||||
await relayPool.publish(getActiveRelayUrls(relayPool), signed)
|
||||
return signed
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the user has already marked a nostr event as read
|
||||
* @param eventId The ID of the event to check
|
||||
@@ -124,7 +145,7 @@ export async function hasMarkedEventAsRead(
|
||||
}
|
||||
|
||||
const events$ = relayPool
|
||||
.req(RELAYS, filter)
|
||||
.req(getActiveRelayUrls(relayPool), filter)
|
||||
.pipe(
|
||||
onlyEvents(),
|
||||
completeOnEose(),
|
||||
@@ -134,8 +155,8 @@ export async function hasMarkedEventAsRead(
|
||||
|
||||
const events: NostrEvent[] = await lastValueFrom(events$)
|
||||
|
||||
// Check if any reaction has our mark-as-read emoji
|
||||
const hasReadReaction = events.some((event: NostrEvent) => event.content === MARK_AS_READ_EMOJI)
|
||||
// Check if any reaction has our archive emoji
|
||||
const hasReadReaction = events.some((event: NostrEvent) => event.content === ARCHIVE_EMOJI)
|
||||
|
||||
return hasReadReaction
|
||||
} catch (error) {
|
||||
@@ -177,7 +198,7 @@ export async function hasMarkedWebsiteAsRead(
|
||||
}
|
||||
|
||||
const events$ = relayPool
|
||||
.req(RELAYS, filter)
|
||||
.req(getActiveRelayUrls(relayPool), filter)
|
||||
.pipe(
|
||||
onlyEvents(),
|
||||
completeOnEose(),
|
||||
@@ -187,8 +208,8 @@ export async function hasMarkedWebsiteAsRead(
|
||||
|
||||
const events: NostrEvent[] = await lastValueFrom(events$)
|
||||
|
||||
// Check if any reaction has our mark-as-read emoji
|
||||
const hasReadReaction = events.some((event: NostrEvent) => event.content === MARK_AS_READ_EMOJI)
|
||||
// Check if any reaction has our archive emoji
|
||||
const hasReadReaction = events.some((event: NostrEvent) => event.content === ARCHIVE_EMOJI)
|
||||
|
||||
return hasReadReaction
|
||||
} catch (error) {
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import { NostrEvent } from 'nostr-tools'
|
||||
import { NostrEvent, nip19 } from 'nostr-tools'
|
||||
import { ReadItem } from './readsService'
|
||||
import { fallbackTitleFromUrl } from '../utils/readItemMerge'
|
||||
import { KINDS } from '../config/kinds'
|
||||
|
||||
const READING_POSITION_PREFIX = 'boris:reading-position:'
|
||||
const READING_PROGRESS_KIND = KINDS.ReadingProgress // 39802 - NIP-85
|
||||
|
||||
interface ReadArticle {
|
||||
id: string
|
||||
@@ -13,44 +14,81 @@ interface ReadArticle {
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes reading position events into ReadItems
|
||||
* Processes reading progress events (kind 39802) into ReadItems
|
||||
*
|
||||
* Test scenarios:
|
||||
* - Kind 39802 with d="30023:..." → article ReadItem with naddr id
|
||||
* - Kind 39802 with d="url:..." → external ReadItem with decoded URL
|
||||
* - Newer event.created_at overwrites older timestamp
|
||||
* - Invalid d tag format → skip event
|
||||
* - Malformed JSON content → skip event
|
||||
*/
|
||||
export function processReadingPositions(
|
||||
export function processReadingProgress(
|
||||
events: NostrEvent[],
|
||||
readsMap: Map<string, ReadItem>
|
||||
): void {
|
||||
|
||||
for (const event of events) {
|
||||
if (event.kind !== READING_PROGRESS_KIND) {
|
||||
continue
|
||||
}
|
||||
|
||||
const dTag = event.tags.find(t => t[0] === 'd')?.[1]
|
||||
if (!dTag || !dTag.startsWith(READING_POSITION_PREFIX)) continue
|
||||
|
||||
const identifier = dTag.replace(READING_POSITION_PREFIX, '')
|
||||
if (!dTag) {
|
||||
continue
|
||||
}
|
||||
|
||||
try {
|
||||
const positionData = JSON.parse(event.content)
|
||||
const position = positionData.position
|
||||
const timestamp = positionData.timestamp
|
||||
const content = JSON.parse(event.content)
|
||||
const position = content.progress || 0
|
||||
|
||||
// Validate progress is between 0 and 1 (NIP-85 requirement)
|
||||
if (position < 0 || position > 1) {
|
||||
continue
|
||||
}
|
||||
|
||||
// Use event.created_at as authoritative timestamp (NIP-85 spec)
|
||||
const timestamp = event.created_at
|
||||
|
||||
let itemId: string
|
||||
let itemUrl: string | undefined
|
||||
let itemType: 'article' | 'external' = 'external'
|
||||
|
||||
// Check if it's a nostr article (naddr format)
|
||||
if (identifier.startsWith('naddr1')) {
|
||||
itemId = identifier
|
||||
itemType = 'article'
|
||||
} else {
|
||||
// It's a base64url-encoded URL
|
||||
// Check if d tag is a coordinate (30023:pubkey:identifier)
|
||||
if (dTag.startsWith('30023:')) {
|
||||
// It's a nostr article coordinate
|
||||
const parts = dTag.split(':')
|
||||
if (parts.length === 3) {
|
||||
// Convert to naddr for consistency with the rest of the app
|
||||
try {
|
||||
const naddr = nip19.naddrEncode({
|
||||
kind: parseInt(parts[0]),
|
||||
pubkey: parts[1],
|
||||
identifier: parts[2]
|
||||
})
|
||||
itemId = naddr
|
||||
itemType = 'article'
|
||||
} catch (e) {
|
||||
continue
|
||||
}
|
||||
} else {
|
||||
continue
|
||||
}
|
||||
} else if (dTag.startsWith('url:')) {
|
||||
// It's a URL with base64url encoding
|
||||
const encoded = dTag.replace('url:', '')
|
||||
try {
|
||||
itemUrl = atob(identifier.replace(/-/g, '+').replace(/_/g, '/'))
|
||||
itemUrl = atob(encoded.replace(/-/g, '+').replace(/_/g, '/'))
|
||||
itemId = itemUrl
|
||||
itemType = 'external'
|
||||
} catch (e) {
|
||||
console.warn('Failed to decode URL identifier:', identifier)
|
||||
continue
|
||||
}
|
||||
} else {
|
||||
continue
|
||||
}
|
||||
|
||||
// Add or update the item
|
||||
// Add or update the item, preferring newer timestamps
|
||||
const existing = readsMap.get(itemId)
|
||||
if (!existing || !existing.readingTimestamp || timestamp > existing.readingTimestamp) {
|
||||
readsMap.set(itemId, {
|
||||
@@ -64,7 +102,7 @@ export function processReadingPositions(
|
||||
})
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn('Failed to parse reading position:', error)
|
||||
// Silently fail
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
import { IEventStore, mapEventsToStore } from 'applesauce-core'
|
||||
import { EventFactory } from 'applesauce-factory'
|
||||
import { RelayPool, onlyEvents } from 'applesauce-relay'
|
||||
import { NostrEvent } from 'nostr-tools'
|
||||
import { NostrEvent, nip19 } from 'nostr-tools'
|
||||
import { firstValueFrom } from 'rxjs'
|
||||
import { publishEvent } from './writeService'
|
||||
import { RELAYS } from '../config/relays'
|
||||
import { KINDS } from '../config/kinds'
|
||||
|
||||
const APP_DATA_KIND = 30078 // NIP-78 Application Data
|
||||
const READING_POSITION_PREFIX = 'boris:reading-position:'
|
||||
const READING_PROGRESS_KIND = KINDS.ReadingProgress // 39802 - NIP-85 Reading Progress
|
||||
|
||||
export interface ReadingPosition {
|
||||
position: number // 0-1 scroll progress
|
||||
@@ -15,16 +15,79 @@ export interface ReadingPosition {
|
||||
scrollTop?: number // Optional: pixel position
|
||||
}
|
||||
|
||||
// Helper to extract and parse reading position from an event
|
||||
function getReadingPositionContent(event: NostrEvent): ReadingPosition | undefined {
|
||||
export interface ReadingProgressContent {
|
||||
progress: number // 0-1 scroll progress
|
||||
ts?: number // Unix timestamp (optional, for display)
|
||||
loc?: number // Optional: pixel position
|
||||
ver?: string // Schema version
|
||||
}
|
||||
|
||||
// Helper to extract and parse reading progress from event (kind 39802)
|
||||
function getReadingProgressContent(event: NostrEvent): ReadingPosition | undefined {
|
||||
if (!event.content || event.content.length === 0) return undefined
|
||||
try {
|
||||
return JSON.parse(event.content) as ReadingPosition
|
||||
const content = JSON.parse(event.content) as ReadingProgressContent
|
||||
return {
|
||||
position: content.progress,
|
||||
timestamp: content.ts || event.created_at,
|
||||
scrollTop: content.loc
|
||||
}
|
||||
} catch {
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
|
||||
// Generate d tag for kind 39802 based on target
|
||||
// Test cases:
|
||||
// - naddr1... → "30023:<pubkey>:<identifier>"
|
||||
// - https://example.com/post → "url:<base64url>"
|
||||
// - Invalid naddr → "url:<base64url>" (fallback)
|
||||
function generateDTag(naddrOrUrl: string): string {
|
||||
// If it's a nostr article (naddr format), decode and build coordinate
|
||||
if (naddrOrUrl.startsWith('naddr1')) {
|
||||
try {
|
||||
const decoded = nip19.decode(naddrOrUrl)
|
||||
if (decoded.type === 'naddr') {
|
||||
const dTag = `${decoded.data.kind}:${decoded.data.pubkey}:${decoded.data.identifier || ''}`
|
||||
return dTag
|
||||
}
|
||||
} catch (e) {
|
||||
// Ignore decode errors
|
||||
}
|
||||
}
|
||||
|
||||
// For URLs, use url: prefix with base64url encoding
|
||||
const base64url = btoa(naddrOrUrl)
|
||||
.replace(/\+/g, '-')
|
||||
.replace(/\//g, '_')
|
||||
.replace(/=+$/, '')
|
||||
return `url:${base64url}`
|
||||
}
|
||||
|
||||
// Generate tags for kind 39802 event
|
||||
function generateProgressTags(naddrOrUrl: string): string[][] {
|
||||
const dTag = generateDTag(naddrOrUrl)
|
||||
const tags: string[][] = [['d', dTag]]
|
||||
|
||||
// Add 'a' tag for nostr articles
|
||||
if (naddrOrUrl.startsWith('naddr1')) {
|
||||
try {
|
||||
const decoded = nip19.decode(naddrOrUrl)
|
||||
if (decoded.type === 'naddr') {
|
||||
const coordinate = `${decoded.data.kind}:${decoded.data.pubkey}:${decoded.data.identifier || ''}`
|
||||
tags.push(['a', coordinate])
|
||||
}
|
||||
} catch (e) {
|
||||
// Ignore decode errors
|
||||
}
|
||||
} else {
|
||||
// Add 'r' tag for URLs
|
||||
tags.push(['r', naddrOrUrl])
|
||||
}
|
||||
|
||||
return tags
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a unique identifier for an article
|
||||
* For Nostr articles: use the naddr directly
|
||||
@@ -43,7 +106,7 @@ export function generateArticleIdentifier(naddrOrUrl: string): string {
|
||||
}
|
||||
|
||||
/**
|
||||
* Save reading position to Nostr (Kind 30078)
|
||||
* Save reading position to Nostr (kind 39802)
|
||||
*/
|
||||
export async function saveReadingPosition(
|
||||
relayPool: RelayPool,
|
||||
@@ -52,36 +115,31 @@ export async function saveReadingPosition(
|
||||
articleIdentifier: string,
|
||||
position: ReadingPosition
|
||||
): Promise<void> {
|
||||
console.log('💾 [ReadingPosition] Saving position:', {
|
||||
identifier: articleIdentifier.slice(0, 32) + '...',
|
||||
position: position.position,
|
||||
positionPercent: Math.round(position.position * 100) + '%',
|
||||
timestamp: position.timestamp,
|
||||
scrollTop: position.scrollTop
|
||||
})
|
||||
|
||||
const dTag = `${READING_POSITION_PREFIX}${articleIdentifier}`
|
||||
const now = Math.floor(Date.now() / 1000)
|
||||
|
||||
const progressContent: ReadingProgressContent = {
|
||||
progress: position.position,
|
||||
ts: position.timestamp,
|
||||
loc: position.scrollTop,
|
||||
ver: '1'
|
||||
}
|
||||
|
||||
const tags = generateProgressTags(articleIdentifier)
|
||||
|
||||
const draft = await factory.create(async () => ({
|
||||
kind: APP_DATA_KIND,
|
||||
content: JSON.stringify(position),
|
||||
tags: [
|
||||
['d', dTag],
|
||||
['client', 'boris']
|
||||
],
|
||||
created_at: Math.floor(Date.now() / 1000)
|
||||
kind: READING_PROGRESS_KIND,
|
||||
content: JSON.stringify(progressContent),
|
||||
tags,
|
||||
created_at: now
|
||||
}))
|
||||
|
||||
const signed = await factory.sign(draft)
|
||||
|
||||
// Use unified write service
|
||||
|
||||
await publishEvent(relayPool, eventStore, signed)
|
||||
|
||||
console.log('✅ [ReadingPosition] Position saved successfully, event ID:', signed.id.slice(0, 8))
|
||||
}
|
||||
|
||||
/**
|
||||
* Load reading position from Nostr
|
||||
* Load reading position from Nostr (kind 39802)
|
||||
*/
|
||||
export async function loadReadingPosition(
|
||||
relayPool: RelayPool,
|
||||
@@ -89,32 +147,20 @@ export async function loadReadingPosition(
|
||||
pubkey: string,
|
||||
articleIdentifier: string
|
||||
): Promise<ReadingPosition | null> {
|
||||
const dTag = `${READING_POSITION_PREFIX}${articleIdentifier}`
|
||||
const dTag = generateDTag(articleIdentifier)
|
||||
|
||||
console.log('📖 [ReadingPosition] Loading position:', {
|
||||
pubkey: pubkey.slice(0, 8) + '...',
|
||||
identifier: articleIdentifier.slice(0, 32) + '...',
|
||||
dTag: dTag.slice(0, 50) + '...'
|
||||
})
|
||||
|
||||
// First, check if we already have the position in the local event store
|
||||
// Check local event store first
|
||||
try {
|
||||
const localEvent = await firstValueFrom(
|
||||
eventStore.replaceable(APP_DATA_KIND, pubkey, dTag)
|
||||
eventStore.replaceable(READING_PROGRESS_KIND, pubkey, dTag)
|
||||
)
|
||||
if (localEvent) {
|
||||
const content = getReadingPositionContent(localEvent)
|
||||
const content = getReadingProgressContent(localEvent)
|
||||
if (content) {
|
||||
console.log('✅ [ReadingPosition] Loaded from local store:', {
|
||||
position: content.position,
|
||||
positionPercent: Math.round(content.position * 100) + '%',
|
||||
timestamp: content.timestamp
|
||||
})
|
||||
|
||||
// Still fetch from relays in the background to get any updates
|
||||
// Fetch from relays in background to get any updates
|
||||
relayPool
|
||||
.subscription(RELAYS, {
|
||||
kinds: [APP_DATA_KIND],
|
||||
kinds: [READING_PROGRESS_KIND],
|
||||
authors: [pubkey],
|
||||
'#d': [dTag]
|
||||
})
|
||||
@@ -125,23 +171,43 @@ export async function loadReadingPosition(
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.log('📭 No cached reading position found, fetching from relays...')
|
||||
// Ignore errors and fetch from relays
|
||||
}
|
||||
|
||||
// If not in local store, fetch from relays
|
||||
// Fetch from relays
|
||||
const result = await fetchFromRelays(
|
||||
relayPool,
|
||||
eventStore,
|
||||
pubkey,
|
||||
READING_PROGRESS_KIND,
|
||||
dTag,
|
||||
getReadingProgressContent
|
||||
)
|
||||
|
||||
return result || null
|
||||
}
|
||||
|
||||
// Helper function to fetch from relays with timeout
|
||||
async function fetchFromRelays(
|
||||
relayPool: RelayPool,
|
||||
eventStore: IEventStore,
|
||||
pubkey: string,
|
||||
kind: number,
|
||||
dTag: string,
|
||||
parser: (event: NostrEvent) => ReadingPosition | undefined
|
||||
): Promise<ReadingPosition | null> {
|
||||
return new Promise((resolve) => {
|
||||
let hasResolved = false
|
||||
const timeout = setTimeout(() => {
|
||||
if (!hasResolved) {
|
||||
console.log('⏱️ Reading position load timeout - no position found')
|
||||
hasResolved = true
|
||||
resolve(null)
|
||||
}
|
||||
}, 3000) // Shorter timeout for reading positions
|
||||
}, 3000)
|
||||
|
||||
const sub = relayPool
|
||||
.subscription(RELAYS, {
|
||||
kinds: [APP_DATA_KIND],
|
||||
kinds: [kind],
|
||||
authors: [pubkey],
|
||||
'#d': [dTag]
|
||||
})
|
||||
@@ -153,33 +219,20 @@ export async function loadReadingPosition(
|
||||
hasResolved = true
|
||||
try {
|
||||
const event = await firstValueFrom(
|
||||
eventStore.replaceable(APP_DATA_KIND, pubkey, dTag)
|
||||
eventStore.replaceable(kind, pubkey, dTag)
|
||||
)
|
||||
if (event) {
|
||||
const content = getReadingPositionContent(event)
|
||||
if (content) {
|
||||
console.log('✅ [ReadingPosition] Loaded from relays:', {
|
||||
position: content.position,
|
||||
positionPercent: Math.round(content.position * 100) + '%',
|
||||
timestamp: content.timestamp
|
||||
})
|
||||
resolve(content)
|
||||
} else {
|
||||
console.log('⚠️ [ReadingPosition] Event found but no valid content')
|
||||
resolve(null)
|
||||
}
|
||||
const content = parser(event)
|
||||
resolve(content || null)
|
||||
} else {
|
||||
console.log('📭 [ReadingPosition] No position found on relays')
|
||||
resolve(null)
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('❌ Error loading reading position:', err)
|
||||
resolve(null)
|
||||
}
|
||||
}
|
||||
},
|
||||
error: (err) => {
|
||||
console.error('❌ Reading position subscription error:', err)
|
||||
error: () => {
|
||||
clearTimeout(timeout)
|
||||
if (!hasResolved) {
|
||||
hasResolved = true
|
||||
|
||||
374
src/services/readingProgressController.ts
Normal file
374
src/services/readingProgressController.ts
Normal file
@@ -0,0 +1,374 @@
|
||||
import { RelayPool } from 'applesauce-relay'
|
||||
import { IEventStore } from 'applesauce-core'
|
||||
import { NostrEvent } from 'nostr-tools'
|
||||
import { queryEvents } from './dataFetch'
|
||||
import { KINDS } from '../config/kinds'
|
||||
import { processReadingProgress } from './readingDataProcessor'
|
||||
import { ReadItem } from './readsService'
|
||||
import { ARCHIVE_EMOJI } from './reactionService'
|
||||
import { nip19 } from 'nostr-tools'
|
||||
|
||||
|
||||
type ProgressMapCallback = (progressMap: Map<string, number>) => void
|
||||
type LoadingCallback = (loading: boolean) => void
|
||||
|
||||
const LAST_SYNCED_KEY = 'reading_progress_last_synced'
|
||||
const PROGRESS_CACHE_KEY = 'reading_progress_cache_v1'
|
||||
|
||||
/**
|
||||
* Shared reading progress controller
|
||||
* Manages the user's reading progress (kind:39802) centrally
|
||||
*/
|
||||
class ReadingProgressController {
|
||||
private progressListeners: ProgressMapCallback[] = []
|
||||
private loadingListeners: LoadingCallback[] = []
|
||||
private markedAsReadListeners: (() => void)[] = []
|
||||
|
||||
private currentProgressMap: Map<string, number> = new Map()
|
||||
private markedAsReadIds: Set<string> = new Set()
|
||||
private lastLoadedPubkey: string | null = null
|
||||
private generation = 0
|
||||
private timelineSubscription: { unsubscribe: () => void } | null = null
|
||||
private isLoading = false
|
||||
|
||||
onProgress(cb: ProgressMapCallback): () => void {
|
||||
this.progressListeners.push(cb)
|
||||
return () => {
|
||||
this.progressListeners = this.progressListeners.filter(l => l !== cb)
|
||||
}
|
||||
}
|
||||
|
||||
onLoading(cb: LoadingCallback): () => void {
|
||||
this.loadingListeners.push(cb)
|
||||
return () => {
|
||||
this.loadingListeners = this.loadingListeners.filter(l => l !== cb)
|
||||
}
|
||||
}
|
||||
|
||||
onMarkedAsReadChanged(cb: () => void): () => void {
|
||||
this.markedAsReadListeners.push(cb)
|
||||
return () => {
|
||||
this.markedAsReadListeners = this.markedAsReadListeners.filter(l => l !== cb)
|
||||
}
|
||||
}
|
||||
|
||||
private setLoading(loading: boolean): void {
|
||||
this.loadingListeners.forEach(cb => cb(loading))
|
||||
}
|
||||
|
||||
private emitProgress(progressMap: Map<string, number>): void {
|
||||
this.progressListeners.forEach(cb => cb(new Map(progressMap)))
|
||||
}
|
||||
|
||||
private emitMarkedAsReadChanged(): void {
|
||||
this.markedAsReadListeners.forEach(cb => cb())
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current reading progress map without triggering a reload
|
||||
*/
|
||||
getProgressMap(): Map<string, number> {
|
||||
return new Map(this.currentProgressMap)
|
||||
}
|
||||
|
||||
/**
|
||||
* Load cached progress from localStorage for a pubkey
|
||||
*/
|
||||
private loadCachedProgress(pubkey: string): Map<string, number> {
|
||||
try {
|
||||
const raw = localStorage.getItem(PROGRESS_CACHE_KEY)
|
||||
if (!raw) return new Map()
|
||||
const parsed = JSON.parse(raw) as Record<string, Record<string, number>>
|
||||
const forUser = parsed[pubkey] || {}
|
||||
return new Map(Object.entries(forUser))
|
||||
} catch {
|
||||
return new Map()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Save current progress map to localStorage for the active pubkey
|
||||
*/
|
||||
private persistProgress(pubkey: string, progressMap: Map<string, number>): void {
|
||||
try {
|
||||
const raw = localStorage.getItem(PROGRESS_CACHE_KEY)
|
||||
const parsed: Record<string, Record<string, number>> = raw ? JSON.parse(raw) : {}
|
||||
parsed[pubkey] = Object.fromEntries(progressMap.entries())
|
||||
localStorage.setItem(PROGRESS_CACHE_KEY, JSON.stringify(parsed))
|
||||
} catch (err) {
|
||||
// Silently fail cache persistence
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get progress for a specific article by naddr
|
||||
*/
|
||||
getProgress(naddr: string): number | undefined {
|
||||
return this.currentProgressMap.get(naddr)
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if article is marked as read
|
||||
*/
|
||||
isMarkedAsRead(naddr: string): boolean {
|
||||
return this.markedAsReadIds.has(naddr)
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all marked as read IDs (for debugging)
|
||||
*/
|
||||
getMarkedAsReadIds(): string[] {
|
||||
return Array.from(this.markedAsReadIds)
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if reading progress is loaded for a specific pubkey
|
||||
*/
|
||||
isLoadedFor(pubkey: string): boolean {
|
||||
return this.lastLoadedPubkey === pubkey
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset state (for logout or manual refresh)
|
||||
*/
|
||||
reset(): void {
|
||||
this.generation++
|
||||
// Unsubscribe from any active timeline subscription
|
||||
if (this.timelineSubscription) {
|
||||
try {
|
||||
this.timelineSubscription.unsubscribe()
|
||||
} catch (err) {
|
||||
// Silently fail on unsubscribe
|
||||
}
|
||||
this.timelineSubscription = null
|
||||
}
|
||||
this.currentProgressMap = new Map()
|
||||
this.markedAsReadIds = new Set()
|
||||
this.lastLoadedPubkey = null
|
||||
this.emitProgress(this.currentProgressMap)
|
||||
}
|
||||
|
||||
/**
|
||||
* Update last synced timestamp
|
||||
*/
|
||||
private updateLastSyncedAt(pubkey: string, timestamp: number): void {
|
||||
try {
|
||||
const data = localStorage.getItem(LAST_SYNCED_KEY)
|
||||
const parsed = data ? JSON.parse(data) : {}
|
||||
parsed[pubkey] = timestamp
|
||||
localStorage.setItem(LAST_SYNCED_KEY, JSON.stringify(parsed))
|
||||
} catch (err) {
|
||||
// Silently fail
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load and watch reading progress for a user
|
||||
*/
|
||||
async start(params: {
|
||||
relayPool: RelayPool
|
||||
eventStore: IEventStore
|
||||
pubkey: string
|
||||
force?: boolean
|
||||
}): Promise<void> {
|
||||
const { relayPool, eventStore, pubkey, force = false } = params
|
||||
const startGeneration = this.generation
|
||||
|
||||
|
||||
// Skip if already loaded for this pubkey and not forcing
|
||||
if (!force && this.isLoadedFor(pubkey)) {
|
||||
return
|
||||
}
|
||||
|
||||
// Prevent concurrent starts
|
||||
if (this.isLoading) {
|
||||
return
|
||||
}
|
||||
|
||||
this.setLoading(true)
|
||||
this.isLoading = true
|
||||
|
||||
try {
|
||||
// Seed from local cache immediately (survives refresh/flight mode)
|
||||
const cached = this.loadCachedProgress(pubkey)
|
||||
if (cached.size > 0) {
|
||||
this.currentProgressMap = cached
|
||||
this.emitProgress(this.currentProgressMap)
|
||||
}
|
||||
|
||||
// Subscribe to local eventStore timeline for immediate and reactive updates
|
||||
// This handles both local writes and synced events from relays
|
||||
if (this.timelineSubscription) {
|
||||
try {
|
||||
this.timelineSubscription.unsubscribe()
|
||||
} catch (err) {
|
||||
// Silently fail
|
||||
}
|
||||
this.timelineSubscription = null
|
||||
}
|
||||
|
||||
const timeline$ = eventStore.timeline({
|
||||
kinds: [KINDS.ReadingProgress],
|
||||
authors: [pubkey]
|
||||
})
|
||||
const generationAtSubscribe = this.generation
|
||||
this.timelineSubscription = timeline$.subscribe((localEvents: NostrEvent[]) => {
|
||||
if (generationAtSubscribe !== this.generation) return
|
||||
if (!Array.isArray(localEvents) || localEvents.length === 0) return
|
||||
this.processEvents(localEvents)
|
||||
})
|
||||
|
||||
// Mark as loaded immediately - queries run in background non-blocking
|
||||
this.lastLoadedPubkey = pubkey
|
||||
|
||||
// Query reading progress from relays in background (non-blocking, fire-and-forget)
|
||||
queryEvents(relayPool, {
|
||||
kinds: [KINDS.ReadingProgress],
|
||||
authors: [pubkey]
|
||||
})
|
||||
.then((relayEvents) => {
|
||||
if (startGeneration !== this.generation) return
|
||||
if (relayEvents.length > 0) {
|
||||
relayEvents.forEach(e => eventStore.add(e))
|
||||
this.processEvents(relayEvents)
|
||||
const now = Math.floor(Date.now() / 1000)
|
||||
this.updateLastSyncedAt(pubkey, now)
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
console.warn('[readingProgress] Background reading progress query failed:', err)
|
||||
})
|
||||
|
||||
// Load mark-as-read reactions in background (non-blocking, streaming)
|
||||
this.loadMarkAsReadReactions(relayPool, eventStore, pubkey, startGeneration)
|
||||
.then(() => {
|
||||
})
|
||||
.catch((err) => {
|
||||
console.warn('[readingProgress] Mark-as-read reactions loading failed:', err)
|
||||
})
|
||||
|
||||
} catch (err) {
|
||||
console.error('📊 [ReadingProgress] Failed to setup:', err)
|
||||
} finally {
|
||||
if (startGeneration === this.generation) {
|
||||
this.setLoading(false)
|
||||
}
|
||||
this.isLoading = false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Process events and update progress map
|
||||
*/
|
||||
private processEvents(events: NostrEvent[]): void {
|
||||
const readsMap = new Map<string, ReadItem>()
|
||||
|
||||
// Merge with existing progress
|
||||
for (const [id, progress] of this.currentProgressMap.entries()) {
|
||||
readsMap.set(id, {
|
||||
id,
|
||||
source: 'reading-progress',
|
||||
type: 'article',
|
||||
readingProgress: progress
|
||||
})
|
||||
}
|
||||
|
||||
// Process new events
|
||||
processReadingProgress(events, readsMap)
|
||||
|
||||
// Convert back to progress map (naddr -> progress)
|
||||
const newProgressMap = new Map<string, number>()
|
||||
for (const [id, item] of readsMap.entries()) {
|
||||
if (item.readingProgress !== undefined && item.type === 'article') {
|
||||
newProgressMap.set(id, item.readingProgress)
|
||||
}
|
||||
}
|
||||
|
||||
this.currentProgressMap = newProgressMap
|
||||
this.emitProgress(this.currentProgressMap)
|
||||
|
||||
// Persist for current user so it survives refresh/flight mode
|
||||
if (this.lastLoadedPubkey) {
|
||||
this.persistProgress(this.lastLoadedPubkey, this.currentProgressMap)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load mark-as-read reactions in background (non-blocking)
|
||||
*/
|
||||
private async loadMarkAsReadReactions(
|
||||
relayPool: RelayPool,
|
||||
_eventStore: IEventStore,
|
||||
pubkey: string,
|
||||
generation: number
|
||||
): Promise<void> {
|
||||
try {
|
||||
// Stream kind:17 (URL reactions) and kind:7 (event reactions) in parallel
|
||||
const seenReactionIds = new Set<string>()
|
||||
|
||||
const handleUrlReaction = (evt: NostrEvent) => {
|
||||
if (seenReactionIds.has(evt.id)) return
|
||||
seenReactionIds.add(evt.id)
|
||||
if (evt.content !== ARCHIVE_EMOJI) return
|
||||
const rTag = evt.tags.find(t => t[0] === 'r')?.[1]
|
||||
if (!rTag) return
|
||||
this.markedAsReadIds.add(rTag)
|
||||
this.emitMarkedAsReadChanged()
|
||||
}
|
||||
|
||||
const pendingEventIds = new Set<string>()
|
||||
const handleEventReaction = (evt: NostrEvent) => {
|
||||
if (seenReactionIds.has(evt.id)) return
|
||||
seenReactionIds.add(evt.id)
|
||||
if (evt.content !== ARCHIVE_EMOJI) return
|
||||
const eTag = evt.tags.find(t => t[0] === 'e')?.[1]
|
||||
if (!eTag) return
|
||||
pendingEventIds.add(eTag)
|
||||
}
|
||||
|
||||
// Fire queries with onEvent callbacks for streaming behavior
|
||||
const [kind17Events, kind7Events] = await Promise.all([
|
||||
queryEvents(relayPool, { kinds: [17], authors: [pubkey] }, { onEvent: handleUrlReaction }),
|
||||
queryEvents(relayPool, { kinds: [7], authors: [pubkey] }, { onEvent: handleEventReaction })
|
||||
])
|
||||
|
||||
if (generation !== this.generation) return
|
||||
|
||||
// Include any reactions that arrived only at EOSE
|
||||
kind17Events.forEach(handleUrlReaction)
|
||||
kind7Events.forEach(handleEventReaction)
|
||||
|
||||
if (pendingEventIds.size > 0) {
|
||||
// Fetch referenced 30023 events, streaming not required here
|
||||
const ids = Array.from(pendingEventIds)
|
||||
const articleEvents = await queryEvents(relayPool, { kinds: [KINDS.BlogPost], ids })
|
||||
const eventIdToNaddr = new Map<string, string>()
|
||||
for (const article of articleEvents) {
|
||||
const dTag = article.tags.find(t => t[0] === 'd')?.[1]
|
||||
if (!dTag) continue
|
||||
try {
|
||||
const naddr = nip19.naddrEncode({ kind: KINDS.BlogPost, pubkey: article.pubkey, identifier: dTag })
|
||||
eventIdToNaddr.set(article.id, naddr)
|
||||
} catch (e) {
|
||||
console.warn('[readingProgress] Failed to encode naddr for article:', article.id)
|
||||
}
|
||||
}
|
||||
|
||||
// Map pending event IDs to naddrs and emit
|
||||
for (const eId of pendingEventIds) {
|
||||
const naddr = eventIdToNaddr.get(eId)
|
||||
if (naddr) {
|
||||
this.markedAsReadIds.add(naddr)
|
||||
}
|
||||
}
|
||||
this.emitMarkedAsReadChanged()
|
||||
}
|
||||
|
||||
} catch (err) {
|
||||
console.warn('[readingProgress] Failed to load mark-as-read reactions:', err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const readingProgressController = new ReadingProgressController()
|
||||
|
||||
276
src/services/readsController.ts
Normal file
276
src/services/readsController.ts
Normal file
@@ -0,0 +1,276 @@
|
||||
import { RelayPool } from 'applesauce-relay'
|
||||
import { Helpers, IEventStore } from 'applesauce-core'
|
||||
import { createAddressLoader } from 'applesauce-loaders/loaders'
|
||||
import { NostrEvent } from 'nostr-tools'
|
||||
import { nip19 } from 'nostr-tools'
|
||||
import { merge } from 'rxjs'
|
||||
import { KINDS } from '../config/kinds'
|
||||
import { RELAYS } from '../config/relays'
|
||||
import { readingProgressController } from './readingProgressController'
|
||||
import { archiveController } from './archiveController'
|
||||
|
||||
const { getArticleTitle, getArticleSummary, getArticleImage, getArticlePublished } = Helpers
|
||||
|
||||
export interface ReadItem {
|
||||
id: string // naddr coordinate
|
||||
source: 'reading-progress' | 'marked-as-read' | 'bookmark'
|
||||
type: 'article' | 'external'
|
||||
|
||||
// Article data
|
||||
event?: NostrEvent
|
||||
url?: string
|
||||
title?: string
|
||||
summary?: string
|
||||
image?: string
|
||||
published?: number
|
||||
author?: string
|
||||
|
||||
// Reading metadata
|
||||
readingProgress?: number // 0-1
|
||||
readingTimestamp?: number // Unix timestamp of last reading activity
|
||||
markedAsRead?: boolean
|
||||
markedAt?: number
|
||||
}
|
||||
|
||||
type ReadsCallback = (reads: ReadItem[]) => void
|
||||
type LoadingCallback = (loading: boolean) => void
|
||||
|
||||
/**
|
||||
* Reads controller - manages read articles with progressive hydration
|
||||
* Follows the same pattern as bookmarkController
|
||||
*/
|
||||
class ReadsController {
|
||||
private readsListeners: ReadsCallback[] = []
|
||||
private loadingListeners: LoadingCallback[] = []
|
||||
|
||||
private currentReads: Map<string, ReadItem> = new Map()
|
||||
private isLoading = false
|
||||
private hydrationGeneration = 0
|
||||
|
||||
// Address loader for efficient batching
|
||||
private addressLoader: ReturnType<typeof createAddressLoader> | null = null
|
||||
private eventStore: IEventStore | null = null
|
||||
|
||||
onReads(cb: ReadsCallback): () => void {
|
||||
this.readsListeners.push(cb)
|
||||
return () => {
|
||||
this.readsListeners = this.readsListeners.filter(l => l !== cb)
|
||||
}
|
||||
}
|
||||
|
||||
onLoading(cb: LoadingCallback): () => void {
|
||||
this.loadingListeners.push(cb)
|
||||
return () => {
|
||||
this.loadingListeners = this.loadingListeners.filter(l => l !== cb)
|
||||
}
|
||||
}
|
||||
|
||||
reset(): void {
|
||||
this.hydrationGeneration++
|
||||
this.currentReads.clear()
|
||||
this.setLoading(false)
|
||||
}
|
||||
|
||||
private setLoading(loading: boolean): void {
|
||||
if (this.isLoading !== loading) {
|
||||
this.isLoading = loading
|
||||
this.loadingListeners.forEach(cb => cb(loading))
|
||||
}
|
||||
}
|
||||
|
||||
getReads(): ReadItem[] {
|
||||
return Array.from(this.currentReads.values())
|
||||
}
|
||||
|
||||
/**
|
||||
* Hydrate article events by coordinates using AddressLoader (auto-batching, streaming)
|
||||
*/
|
||||
private hydrateArticles(
|
||||
coordinates: string[],
|
||||
onProgress: () => void,
|
||||
generation: number
|
||||
): void {
|
||||
if (!this.addressLoader) {
|
||||
return
|
||||
}
|
||||
|
||||
if (coordinates.length === 0) return
|
||||
|
||||
// Parse coordinates into pointers
|
||||
const pointers: Array<{ kind: number; pubkey: string; identifier: string }> = []
|
||||
|
||||
for (const coord of coordinates) {
|
||||
try {
|
||||
// Decode naddr to get article coordinates
|
||||
if (coord.startsWith('naddr1')) {
|
||||
const decoded = nip19.decode(coord)
|
||||
if (decoded.type === 'naddr' && decoded.data.kind === KINDS.BlogPost) {
|
||||
pointers.push({
|
||||
kind: decoded.data.kind,
|
||||
pubkey: decoded.data.pubkey,
|
||||
identifier: decoded.data.identifier || ''
|
||||
})
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('Failed to decode article coordinate:', coord)
|
||||
}
|
||||
}
|
||||
|
||||
if (pointers.length === 0) return
|
||||
|
||||
// Use AddressLoader - it auto-batches and streams results
|
||||
merge(...pointers.map(this.addressLoader)).subscribe({
|
||||
next: (event) => {
|
||||
// Check if hydration was cancelled
|
||||
if (this.hydrationGeneration !== generation) return
|
||||
|
||||
const dTag = event.tags?.find((t: string[]) => t[0] === 'd')?.[1] || ''
|
||||
|
||||
// Build naddr from event
|
||||
try {
|
||||
const naddr = nip19.naddrEncode({
|
||||
kind: event.kind,
|
||||
pubkey: event.pubkey,
|
||||
identifier: dTag
|
||||
})
|
||||
|
||||
const item = this.currentReads.get(naddr)
|
||||
if (item) {
|
||||
// Enrich the item with article data
|
||||
item.event = event
|
||||
item.title = getArticleTitle(event) || 'Untitled'
|
||||
item.summary = getArticleSummary(event)
|
||||
item.image = getArticleImage(event)
|
||||
item.published = getArticlePublished(event)
|
||||
item.author = event.pubkey
|
||||
|
||||
// Store in event store if available
|
||||
if (this.eventStore) {
|
||||
this.eventStore.add(event)
|
||||
}
|
||||
|
||||
onProgress()
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('Failed to encode naddr for event:', event.id)
|
||||
}
|
||||
},
|
||||
error: () => {
|
||||
// Silent error - AddressLoader handles retries
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Build ReadItems from reading progress and emit them
|
||||
*/
|
||||
private buildAndEmitReads(): void {
|
||||
const progressMap = readingProgressController.getProgressMap()
|
||||
const markedIds = Array.from(new Set([
|
||||
...readingProgressController.getMarkedAsReadIds(),
|
||||
...archiveController.getMarkedIds()
|
||||
]))
|
||||
|
||||
// Build read items from progress map
|
||||
const readItems: ReadItem[] = []
|
||||
|
||||
for (const [id, progress] of progressMap.entries()) {
|
||||
const existing = this.currentReads.get(id)
|
||||
const item: ReadItem = existing || {
|
||||
id,
|
||||
source: 'reading-progress',
|
||||
type: 'article',
|
||||
readingProgress: progress,
|
||||
readingTimestamp: Math.floor(Date.now() / 1000)
|
||||
}
|
||||
|
||||
// Update progress
|
||||
item.readingProgress = progress
|
||||
item.markedAsRead = markedIds.includes(id)
|
||||
|
||||
readItems.push(item)
|
||||
this.currentReads.set(id, item)
|
||||
}
|
||||
|
||||
// Include items that are only marked-as-read (no progress event yet)
|
||||
for (const id of markedIds) {
|
||||
if (!this.currentReads.has(id) && id.startsWith('naddr1')) {
|
||||
const item: ReadItem = {
|
||||
id,
|
||||
source: 'marked-as-read',
|
||||
type: 'article',
|
||||
markedAsRead: true,
|
||||
readingTimestamp: Math.floor(Date.now() / 1000)
|
||||
}
|
||||
readItems.push(item)
|
||||
this.currentReads.set(id, item)
|
||||
}
|
||||
}
|
||||
|
||||
// Emit current state (items without article data yet)
|
||||
this.readsListeners.forEach(cb => cb(Array.from(this.currentReads.values())))
|
||||
|
||||
// Fetch missing articles in background (progressive hydration)
|
||||
const generation = this.hydrationGeneration
|
||||
const onProgress = () => {
|
||||
this.readsListeners.forEach(cb => cb(Array.from(this.currentReads.values())))
|
||||
}
|
||||
|
||||
const coordinatesToFetch = readItems
|
||||
.filter(item => !item.event && item.type === 'article')
|
||||
.map(item => item.id)
|
||||
|
||||
this.hydrateArticles(coordinatesToFetch, onProgress, generation)
|
||||
}
|
||||
|
||||
async start(options: {
|
||||
relayPool: RelayPool
|
||||
eventStore: IEventStore
|
||||
pubkey: string
|
||||
}): Promise<void> {
|
||||
const { relayPool, eventStore } = options
|
||||
|
||||
// Increment generation to cancel any in-flight hydration
|
||||
this.hydrationGeneration++
|
||||
this.eventStore = eventStore
|
||||
|
||||
// Initialize loader for this session
|
||||
this.addressLoader = createAddressLoader(relayPool, {
|
||||
eventStore,
|
||||
extraRelays: RELAYS
|
||||
})
|
||||
|
||||
this.setLoading(true)
|
||||
|
||||
try {
|
||||
// Subscribe to reading progress changes
|
||||
const unsubProgress = readingProgressController.onProgress(() => {
|
||||
this.buildAndEmitReads()
|
||||
})
|
||||
|
||||
const unsubMarked = archiveController.onMarked(() => {
|
||||
this.buildAndEmitReads()
|
||||
})
|
||||
|
||||
// Build initial reads
|
||||
this.buildAndEmitReads()
|
||||
|
||||
// Cleanup subscriptions on next start
|
||||
setTimeout(() => {
|
||||
unsubProgress()
|
||||
unsubMarked()
|
||||
}, 0)
|
||||
|
||||
} catch (error) {
|
||||
console.error('Failed to load reads:', error)
|
||||
this.readsListeners.forEach(cb => cb([]))
|
||||
} finally {
|
||||
this.setLoading(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Singleton instance
|
||||
export const readsController = new ReadsController()
|
||||
|
||||
@@ -1,43 +1,25 @@
|
||||
import { RelayPool } from 'applesauce-relay'
|
||||
import { NostrEvent } from 'nostr-tools'
|
||||
import { Helpers } from 'applesauce-core'
|
||||
import { Bookmark } from '../types/bookmarks'
|
||||
import { fetchReadArticles } from './libraryService'
|
||||
import { queryEvents } from './dataFetch'
|
||||
import { RELAYS } from '../config/relays'
|
||||
import { KINDS } from '../config/kinds'
|
||||
import { classifyBookmarkType } from '../utils/bookmarkTypeClassifier'
|
||||
import { nip19 } from 'nostr-tools'
|
||||
import { processReadingPositions, processMarkedAsRead, filterValidItems, sortByReadingActivity } from './readingDataProcessor'
|
||||
import { AddressPointer } from 'nostr-tools/nip19'
|
||||
import { processReadingProgress, processMarkedAsRead, filterValidItems, sortByReadingActivity } from './readingDataProcessor'
|
||||
import { mergeReadItem } from '../utils/readItemMerge'
|
||||
import type { ReadItem } from './readsController'
|
||||
|
||||
const { getArticleTitle, getArticleImage, getArticlePublished, getArticleSummary } = Helpers
|
||||
|
||||
export interface ReadItem {
|
||||
id: string // event ID or URL or coordinate
|
||||
source: 'bookmark' | 'reading-progress' | 'marked-as-read'
|
||||
type: 'article' | 'external' // article=kind:30023, external=URL
|
||||
|
||||
// Article data
|
||||
event?: NostrEvent
|
||||
url?: string
|
||||
title?: string
|
||||
summary?: string
|
||||
image?: string
|
||||
published?: number
|
||||
author?: string
|
||||
|
||||
// Reading metadata
|
||||
readingProgress?: number // 0-1
|
||||
readingTimestamp?: number // Unix timestamp of last reading activity
|
||||
markedAsRead?: boolean
|
||||
markedAt?: number
|
||||
}
|
||||
// Re-export ReadItem from readsController for consistency
|
||||
export type { ReadItem } from './readsController'
|
||||
|
||||
/**
|
||||
* Fetches all reads from multiple sources:
|
||||
* - Bookmarked articles (kind:30023) and article/website URLs
|
||||
* - Articles/URLs with reading progress (kind:30078)
|
||||
* - Articles/URLs with reading progress (kind:39802)
|
||||
* - Manually marked as read articles/URLs (kind:7, kind:17)
|
||||
*/
|
||||
export async function fetchAllReads(
|
||||
@@ -46,7 +28,6 @@ export async function fetchAllReads(
|
||||
bookmarks: Bookmark[],
|
||||
onItem?: (item: ReadItem) => void
|
||||
): Promise<ReadItem[]> {
|
||||
console.log('📚 [Reads] Fetching all reads for user:', userPubkey.slice(0, 8))
|
||||
|
||||
const readsMap = new Map<string, ReadItem>()
|
||||
|
||||
@@ -61,30 +42,19 @@ export async function fetchAllReads(
|
||||
|
||||
try {
|
||||
// Fetch all data sources in parallel
|
||||
const [readingPositionEvents, markedAsReadArticles] = await Promise.all([
|
||||
queryEvents(relayPool, { kinds: [KINDS.AppData], authors: [userPubkey] }, { relayUrls: RELAYS }),
|
||||
const [progressEvents, markedAsReadArticles] = await Promise.all([
|
||||
queryEvents(relayPool, { kinds: [KINDS.ReadingProgress], authors: [userPubkey] }),
|
||||
fetchReadArticles(relayPool, userPubkey)
|
||||
])
|
||||
|
||||
console.log('📊 [Reads] Data fetched:', {
|
||||
readingPositions: readingPositionEvents.length,
|
||||
markedAsRead: markedAsReadArticles.length,
|
||||
bookmarks: bookmarks.length
|
||||
})
|
||||
|
||||
// Process reading positions and emit items
|
||||
processReadingPositions(readingPositionEvents, readsMap)
|
||||
if (onItem) {
|
||||
readsMap.forEach(item => {
|
||||
if (item.type === 'article') onItem(item)
|
||||
})
|
||||
}
|
||||
// Process reading progress events (kind 39802)
|
||||
processReadingProgress(progressEvents, readsMap)
|
||||
|
||||
// Process marked-as-read and emit items
|
||||
processMarkedAsRead(markedAsReadArticles, readsMap)
|
||||
if (onItem) {
|
||||
readsMap.forEach(item => {
|
||||
if (item.type === 'article') onItem(item)
|
||||
onItem(item)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -120,7 +90,6 @@ export async function fetchAllReads(
|
||||
.map(item => item.id)
|
||||
|
||||
if (articleCoordinates.length > 0) {
|
||||
console.log('📖 [Reads] Fetching article events for', articleCoordinates.length, 'articles')
|
||||
|
||||
// Parse coordinates and fetch events
|
||||
const articlesToFetch: Array<{ pubkey: string; identifier: string }> = []
|
||||
@@ -130,11 +99,14 @@ export async function fetchAllReads(
|
||||
// Try to decode as naddr
|
||||
if (coord.startsWith('naddr1')) {
|
||||
const decoded = nip19.decode(coord)
|
||||
if (decoded.type === 'naddr' && decoded.data.kind === KINDS.BlogPost) {
|
||||
articlesToFetch.push({
|
||||
pubkey: decoded.data.pubkey,
|
||||
identifier: decoded.data.identifier || ''
|
||||
})
|
||||
if (decoded.type === 'naddr') {
|
||||
const data = decoded.data as AddressPointer
|
||||
if (data.kind === KINDS.BlogPost) {
|
||||
articlesToFetch.push({
|
||||
pubkey: data.pubkey,
|
||||
identifier: data.identifier || ''
|
||||
})
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Try coordinate format (kind:pubkey:identifier)
|
||||
@@ -157,8 +129,7 @@ export async function fetchAllReads(
|
||||
|
||||
const events = await queryEvents(
|
||||
relayPool,
|
||||
{ kinds: [KINDS.BlogPost], authors, '#d': identifiers },
|
||||
{ relayUrls: RELAYS }
|
||||
{ kinds: [KINDS.BlogPost], authors, '#d': identifiers }
|
||||
)
|
||||
|
||||
// Merge event data into ReadItems and emit
|
||||
@@ -187,7 +158,6 @@ export async function fetchAllReads(
|
||||
const validArticles = filterValidItems(articles)
|
||||
const sortedReads = sortByReadingActivity(validArticles)
|
||||
|
||||
console.log('✅ [Reads] Processed', sortedReads.length, 'total reads')
|
||||
return sortedReads
|
||||
|
||||
} catch (error) {
|
||||
|
||||
@@ -34,7 +34,6 @@ export async function rebroadcastEvents(
|
||||
|
||||
// If we're in flight mode (only local relays connected) and user wants to broadcast to all relays, skip
|
||||
if (broadcastToAll && !hasRemoteConnection) {
|
||||
console.log('✈️ Flight mode: skipping rebroadcast to remote relays')
|
||||
return
|
||||
}
|
||||
|
||||
@@ -50,7 +49,6 @@ export async function rebroadcastEvents(
|
||||
}
|
||||
|
||||
if (targetRelays.length === 0) {
|
||||
console.log('📡 No target relays for rebroadcast')
|
||||
return
|
||||
}
|
||||
|
||||
@@ -58,7 +56,6 @@ export async function rebroadcastEvents(
|
||||
const rebroadcastPromises = events.map(async (event) => {
|
||||
try {
|
||||
await relayPool.publish(targetRelays, event)
|
||||
console.log('📡 Rebroadcast event', event.id?.slice(0, 8), 'to', targetRelays.length, 'relay(s)')
|
||||
} catch (error) {
|
||||
console.warn('⚠️ Failed to rebroadcast event', event.id?.slice(0, 8), error)
|
||||
}
|
||||
@@ -68,11 +65,5 @@ export async function rebroadcastEvents(
|
||||
Promise.all(rebroadcastPromises).catch((err) => {
|
||||
console.warn('⚠️ Some rebroadcasts failed:', err)
|
||||
})
|
||||
|
||||
console.log(`📡 Rebroadcasting ${events.length} event(s) to ${targetRelays.length} relay(s)`, {
|
||||
broadcastToAll,
|
||||
useLocalCache,
|
||||
targetRelays
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
194
src/services/relayListService.ts
Normal file
194
src/services/relayListService.ts
Normal file
@@ -0,0 +1,194 @@
|
||||
import { RelayPool } from 'applesauce-relay'
|
||||
import { NostrEvent } from 'nostr-tools'
|
||||
import { queryEvents } from './dataFetch'
|
||||
|
||||
export interface UserRelayInfo {
|
||||
url: string
|
||||
mode?: 'read' | 'write' | 'both'
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads user's relay list from kind 10002 (NIP-65)
|
||||
*/
|
||||
export async function loadUserRelayList(
|
||||
relayPool: RelayPool,
|
||||
pubkey: string,
|
||||
options?: {
|
||||
onUpdate?: (relays: UserRelayInfo[]) => void
|
||||
}
|
||||
): Promise<UserRelayInfo[]> {
|
||||
try {
|
||||
console.log('[relayListService] Loading user relay list for pubkey:', pubkey.slice(0, 16) + '...')
|
||||
console.log('[relayListService] Available relays:', Array.from(relayPool.relays.keys()))
|
||||
|
||||
console.log('[relayListService] Starting query for kind 10002...')
|
||||
const startTime = Date.now()
|
||||
|
||||
// Try querying with streaming callback for faster results
|
||||
const events: NostrEvent[] = []
|
||||
const eventsMap = new Map<string, NostrEvent>()
|
||||
|
||||
const result = await queryEvents(relayPool, {
|
||||
kinds: [10002],
|
||||
authors: [pubkey],
|
||||
limit: 10
|
||||
}, {
|
||||
onEvent: (evt) => {
|
||||
// Deduplicate by id and keep most recent
|
||||
const existing = eventsMap.get(evt.id)
|
||||
if (!existing || evt.created_at > existing.created_at) {
|
||||
eventsMap.set(evt.id, evt)
|
||||
// Update events array with deduplicated events
|
||||
events.length = 0
|
||||
events.push(...Array.from(eventsMap.values()))
|
||||
|
||||
// Stream immediate updates to caller using the newest event
|
||||
if (options?.onUpdate) {
|
||||
const tags = evt.tags || []
|
||||
const relays: UserRelayInfo[] = []
|
||||
for (const tag of tags) {
|
||||
if (tag[0] === 'r' && tag[1]) {
|
||||
const url = tag[1]
|
||||
const mode = (tag[2] as 'read' | 'write' | undefined) || 'both'
|
||||
relays.push({ url, mode })
|
||||
}
|
||||
}
|
||||
if (relays.length > 0) {
|
||||
options.onUpdate(relays)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
// Use the streaming results if we got any, otherwise fall back to the full result
|
||||
const finalEvents = events.length > 0 ? events : result
|
||||
|
||||
const queryTime = Date.now() - startTime
|
||||
console.log('[relayListService] Query completed in', queryTime, 'ms')
|
||||
|
||||
// Also try a broader query to see if we get any events at all
|
||||
console.log('[relayListService] Trying broader query for any kind 10002 events...')
|
||||
const allEvents = await queryEvents(relayPool, {
|
||||
kinds: [10002],
|
||||
limit: 5
|
||||
})
|
||||
console.log('[relayListService] Found', allEvents.length, 'total kind 10002 events from any author')
|
||||
|
||||
|
||||
console.log('[relayListService] Found', finalEvents.length, 'kind 10002 events')
|
||||
if (finalEvents.length > 0) {
|
||||
console.log('[relayListService] Event details:', finalEvents.map(e => ({ id: e.id, created_at: e.created_at, tags: e.tags.length })))
|
||||
}
|
||||
|
||||
if (finalEvents.length === 0) return []
|
||||
|
||||
// Get most recent event
|
||||
const sortedEvents = finalEvents.sort((a, b) => b.created_at - a.created_at)
|
||||
const relayListEvent = sortedEvents[0]
|
||||
|
||||
const relays: UserRelayInfo[] = []
|
||||
for (const tag of relayListEvent.tags) {
|
||||
if (tag[0] === 'r' && tag[1]) {
|
||||
const url = tag[1]
|
||||
const mode = tag[2] as 'read' | 'write' | undefined
|
||||
relays.push({
|
||||
url,
|
||||
mode: mode || 'both'
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
console.log('[relayListService] Parsed', relays.length, 'relays from event')
|
||||
return relays
|
||||
} catch (error) {
|
||||
console.error('Failed to load user relay list:', error)
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads blocked relays from kind 10006 (NIP-51 mute list)
|
||||
*/
|
||||
export async function loadBlockedRelays(
|
||||
relayPool: RelayPool,
|
||||
pubkey: string
|
||||
): Promise<string[]> {
|
||||
try {
|
||||
const events = await queryEvents(relayPool, {
|
||||
kinds: [10006],
|
||||
authors: [pubkey]
|
||||
})
|
||||
|
||||
if (events.length === 0) return []
|
||||
|
||||
// Get most recent event
|
||||
const sortedEvents = events.sort((a, b) => b.created_at - a.created_at)
|
||||
const muteListEvent = sortedEvents[0]
|
||||
|
||||
const blocked: string[] = []
|
||||
for (const tag of muteListEvent.tags) {
|
||||
if (tag[0] === 'r' && tag[1]) {
|
||||
blocked.push(tag[1])
|
||||
}
|
||||
}
|
||||
|
||||
return blocked
|
||||
} catch (error) {
|
||||
console.error('Failed to load blocked relays:', error)
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes final relay set by merging inputs and removing blocked relays
|
||||
*/
|
||||
export function computeRelaySet(params: {
|
||||
hardcoded: string[]
|
||||
bunker?: string[]
|
||||
userList?: UserRelayInfo[]
|
||||
blocked?: string[]
|
||||
alwaysIncludeLocal: string[]
|
||||
}): string[] {
|
||||
const {
|
||||
hardcoded,
|
||||
bunker = [],
|
||||
userList = [],
|
||||
blocked = [],
|
||||
alwaysIncludeLocal
|
||||
} = params
|
||||
|
||||
const relaySet = new Set<string>()
|
||||
const blockedSet = new Set(blocked)
|
||||
|
||||
// Helper to check if relay should be included
|
||||
const shouldInclude = (url: string): boolean => {
|
||||
// Always include local relays
|
||||
if (alwaysIncludeLocal.includes(url)) return true
|
||||
// Otherwise check if blocked
|
||||
return !blockedSet.has(url)
|
||||
}
|
||||
|
||||
// Add hardcoded relays
|
||||
for (const url of hardcoded) {
|
||||
if (shouldInclude(url)) relaySet.add(url)
|
||||
}
|
||||
|
||||
// Add bunker relays
|
||||
for (const url of bunker) {
|
||||
if (shouldInclude(url)) relaySet.add(url)
|
||||
}
|
||||
|
||||
// Add user relays (treating 'both' and 'read' as applicable for queries)
|
||||
for (const relay of userList) {
|
||||
if (shouldInclude(relay.url)) relaySet.add(relay.url)
|
||||
}
|
||||
|
||||
// Always ensure local relays are present
|
||||
for (const url of alwaysIncludeLocal) {
|
||||
relaySet.add(url)
|
||||
}
|
||||
|
||||
return Array.from(relaySet)
|
||||
}
|
||||
|
||||
86
src/services/relayManager.ts
Normal file
86
src/services/relayManager.ts
Normal file
@@ -0,0 +1,86 @@
|
||||
import { RelayPool } from 'applesauce-relay'
|
||||
import { prioritizeLocalRelays } from '../utils/helpers'
|
||||
|
||||
/**
|
||||
* Local relays that are always included
|
||||
*/
|
||||
export const ALWAYS_LOCAL_RELAYS = [
|
||||
'ws://localhost:10547',
|
||||
'ws://localhost:4869'
|
||||
]
|
||||
|
||||
/**
|
||||
* Gets active relay URLs from the relay pool
|
||||
*/
|
||||
export function getActiveRelayUrls(relayPool: RelayPool): string[] {
|
||||
const urls = Array.from(relayPool.relays.keys())
|
||||
return prioritizeLocalRelays(urls)
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalizes a relay URL to match what applesauce-relay stores internally
|
||||
* Adds trailing slash for URLs without a path
|
||||
*/
|
||||
function normalizeRelayUrl(url: string): string {
|
||||
try {
|
||||
const parsed = new URL(url)
|
||||
// If the pathname is empty or just "/", ensure it ends with "/"
|
||||
if (parsed.pathname === '' || parsed.pathname === '/') {
|
||||
return url.endsWith('/') ? url : url + '/'
|
||||
}
|
||||
return url
|
||||
} catch {
|
||||
// If URL parsing fails, return as-is
|
||||
return url
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies a new relay set to the pool: adds missing relays, removes extras
|
||||
*/
|
||||
export function applyRelaySetToPool(
|
||||
relayPool: RelayPool,
|
||||
finalUrls: string[]
|
||||
): void {
|
||||
// Normalize all URLs to match pool's internal format
|
||||
const currentUrls = new Set(Array.from(relayPool.relays.keys()))
|
||||
const normalizedTargetUrls = new Set(finalUrls.map(normalizeRelayUrl))
|
||||
|
||||
console.log('[relayManager] applyRelaySetToPool called')
|
||||
console.log('[relayManager] Current pool has:', currentUrls.size, 'relays')
|
||||
console.log('[relayManager] Target has:', finalUrls.length, 'relays')
|
||||
|
||||
// Add new relays (use original URLs for adding, not normalized)
|
||||
const toAdd = finalUrls.filter(url => !currentUrls.has(normalizeRelayUrl(url)))
|
||||
console.log('[relayManager] Will add:', toAdd.length, 'relays', toAdd)
|
||||
if (toAdd.length > 0) {
|
||||
relayPool.group(toAdd)
|
||||
}
|
||||
|
||||
// Remove relays not in target (but always keep local relays)
|
||||
const toRemove: string[] = []
|
||||
for (const url of currentUrls) {
|
||||
// Check if this normalized URL is in the target set
|
||||
if (!normalizedTargetUrls.has(url)) {
|
||||
// Also check if it's a local relay (check both normalized and original forms)
|
||||
const isLocal = ALWAYS_LOCAL_RELAYS.some(localUrl =>
|
||||
normalizeRelayUrl(localUrl) === url || localUrl === url
|
||||
)
|
||||
if (!isLocal) {
|
||||
toRemove.push(url)
|
||||
}
|
||||
}
|
||||
}
|
||||
console.log('[relayManager] Will remove:', toRemove.length, 'relays', toRemove)
|
||||
|
||||
for (const url of toRemove) {
|
||||
const relay = relayPool.relays.get(url)
|
||||
if (relay) {
|
||||
relay.close()
|
||||
relayPool.relays.delete(url)
|
||||
}
|
||||
}
|
||||
|
||||
console.log('[relayManager] After apply, pool has:', relayPool.relays.size, 'relays')
|
||||
}
|
||||
|
||||
@@ -40,11 +40,7 @@ export function updateAndGetRelayStatuses(relayPool: RelayPool): RelayStatus[] {
|
||||
const connectedCount = statuses.filter(s => s.isInPool).length
|
||||
const disconnectedCount = statuses.filter(s => !s.isInPool).length
|
||||
if (connectedCount === 0 || disconnectedCount > 0) {
|
||||
console.log(`🔌 Relay status: ${connectedCount} connected, ${disconnectedCount} disconnected`)
|
||||
const connected = statuses.filter(s => s.isInPool).map(s => s.url.replace(/^wss?:\/\//, ''))
|
||||
const disconnected = statuses.filter(s => !s.isInPool).map(s => s.url.replace(/^wss?:\/\//, ''))
|
||||
if (connected.length > 0) console.log('✅ Connected:', connected.join(', '))
|
||||
if (disconnected.length > 0) console.log('❌ Disconnected:', disconnected.join(', '))
|
||||
// Debug: relay status changed, but we're not logging it
|
||||
}
|
||||
|
||||
// Add recently seen relays that are no longer connected
|
||||
|
||||
@@ -60,6 +60,9 @@ export interface UserSettings {
|
||||
paragraphAlignment?: 'left' | 'justify' // default: justify
|
||||
// Reading position sync
|
||||
syncReadingPosition?: boolean // default: false (opt-in)
|
||||
autoMarkAsReadOnCompletion?: boolean // default: false (opt-in)
|
||||
// Bookmark filtering
|
||||
hideBookmarksWithoutCreationDate?: boolean // default: false
|
||||
}
|
||||
|
||||
export async function loadSettings(
|
||||
@@ -68,7 +71,6 @@ export async function loadSettings(
|
||||
pubkey: string,
|
||||
relays: string[]
|
||||
): Promise<UserSettings | null> {
|
||||
console.log('⚙️ Loading settings from nostr...', { pubkey: pubkey.slice(0, 8) + '...', relays })
|
||||
|
||||
// First, check if we already have settings in the local event store
|
||||
try {
|
||||
@@ -77,7 +79,6 @@ export async function loadSettings(
|
||||
)
|
||||
if (localEvent) {
|
||||
const content = getAppDataContent<UserSettings>(localEvent)
|
||||
console.log('✅ Settings loaded from local store (cached):', content)
|
||||
|
||||
// Still fetch from relays in the background to get any updates
|
||||
relayPool
|
||||
@@ -91,8 +92,8 @@ export async function loadSettings(
|
||||
|
||||
return content || null
|
||||
}
|
||||
} catch (err) {
|
||||
console.log('📭 No cached settings found, fetching from relays...')
|
||||
} catch (_err) {
|
||||
// Ignore local store errors
|
||||
}
|
||||
|
||||
// If not in local store, fetch from relays
|
||||
@@ -124,10 +125,8 @@ export async function loadSettings(
|
||||
)
|
||||
if (event) {
|
||||
const content = getAppDataContent<UserSettings>(event)
|
||||
console.log('✅ Settings loaded from relays:', content)
|
||||
resolve(content || null)
|
||||
} else {
|
||||
console.log('📭 No settings event found - using defaults')
|
||||
resolve(null)
|
||||
}
|
||||
} catch (err) {
|
||||
@@ -158,7 +157,6 @@ export async function saveSettings(
|
||||
factory: EventFactory,
|
||||
settings: UserSettings
|
||||
): Promise<void> {
|
||||
console.log('💾 Saving settings to nostr:', settings)
|
||||
|
||||
// Create NIP-78 application data event manually
|
||||
// Note: AppDataBlueprint is not available in the npm package
|
||||
@@ -174,7 +172,6 @@ export async function saveSettings(
|
||||
// Use unified write service
|
||||
await publishEvent(relayPool, eventStore, signed)
|
||||
|
||||
console.log('✅ Settings published successfully')
|
||||
}
|
||||
|
||||
export function watchSettings(
|
||||
|
||||
121
src/services/unarchiveService.ts
Normal file
121
src/services/unarchiveService.ts
Normal file
@@ -0,0 +1,121 @@
|
||||
import { RelayPool, completeOnEose, onlyEvents } from 'applesauce-relay'
|
||||
import { IAccount } from 'applesauce-accounts'
|
||||
import { NostrEvent } from 'nostr-tools'
|
||||
import { lastValueFrom, takeUntil, timer, toArray } from 'rxjs'
|
||||
import { RELAYS } from '../config/relays'
|
||||
import { ARCHIVE_EMOJI, deleteReaction } from './reactionService'
|
||||
|
||||
/**
|
||||
* Returns the user's archive reactions (kind:7) for a given event id.
|
||||
*/
|
||||
export async function findArchiveReactionsForEvent(
|
||||
eventId: string,
|
||||
userPubkey: string,
|
||||
relayPool: RelayPool
|
||||
): Promise<NostrEvent[]> {
|
||||
try {
|
||||
const filter = {
|
||||
kinds: [7],
|
||||
authors: [userPubkey],
|
||||
'#e': [eventId]
|
||||
}
|
||||
|
||||
const events$ = relayPool
|
||||
.req(RELAYS, filter)
|
||||
.pipe(
|
||||
onlyEvents(),
|
||||
completeOnEose(),
|
||||
takeUntil(timer(2000)),
|
||||
toArray()
|
||||
)
|
||||
|
||||
const events: NostrEvent[] = await lastValueFrom(events$)
|
||||
return events.filter(evt => evt.content === ARCHIVE_EMOJI)
|
||||
} catch (error) {
|
||||
console.error('[unarchive] findArchiveReactionsForEvent error:', error)
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the user's archive reactions (kind:17) for a given website URL.
|
||||
*/
|
||||
export async function findArchiveReactionsForWebsite(
|
||||
url: string,
|
||||
userPubkey: string,
|
||||
relayPool: RelayPool
|
||||
): Promise<NostrEvent[]> {
|
||||
try {
|
||||
// Normalize URL same as creation
|
||||
let normalizedUrl = url
|
||||
try {
|
||||
const parsed = new URL(url)
|
||||
parsed.hash = ''
|
||||
normalizedUrl = parsed.toString()
|
||||
if (normalizedUrl.endsWith('/')) normalizedUrl = normalizedUrl.slice(0, -1)
|
||||
} catch (e) {
|
||||
console.warn('[unarchive] URL normalize failed:', e)
|
||||
}
|
||||
|
||||
const filter = {
|
||||
kinds: [17],
|
||||
authors: [userPubkey],
|
||||
'#r': [normalizedUrl]
|
||||
}
|
||||
|
||||
const events$ = relayPool
|
||||
.req(RELAYS, filter)
|
||||
.pipe(
|
||||
onlyEvents(),
|
||||
completeOnEose(),
|
||||
takeUntil(timer(2000)),
|
||||
toArray()
|
||||
)
|
||||
|
||||
const events: NostrEvent[] = await lastValueFrom(events$)
|
||||
return events.filter(evt => evt.content === ARCHIVE_EMOJI)
|
||||
} catch (error) {
|
||||
console.error('[unarchive] findArchiveReactionsForWebsite error:', error)
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends deletion requests for all of the user's archive reactions to an event.
|
||||
* Returns the number of deletion requests published.
|
||||
*/
|
||||
export async function unarchiveEvent(
|
||||
eventId: string,
|
||||
account: IAccount,
|
||||
relayPool: RelayPool
|
||||
): Promise<number> {
|
||||
try {
|
||||
const reactions = await findArchiveReactionsForEvent(eventId, account.pubkey, relayPool)
|
||||
await Promise.all(reactions.map(r => deleteReaction(r.id, account, relayPool)))
|
||||
return reactions.length
|
||||
} catch (error) {
|
||||
console.error('[unarchive] unarchiveEvent error:', error)
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends deletion requests for all of the user's archive reactions to a website URL.
|
||||
* Returns the number of deletion requests published.
|
||||
*/
|
||||
export async function unarchiveWebsite(
|
||||
url: string,
|
||||
account: IAccount,
|
||||
relayPool: RelayPool
|
||||
): Promise<number> {
|
||||
try {
|
||||
const reactions = await findArchiveReactionsForWebsite(url, account.pubkey, relayPool)
|
||||
await Promise.all(reactions.map(r => deleteReaction(r.id, account, relayPool)))
|
||||
return reactions.length
|
||||
} catch (error) {
|
||||
console.error('[unarchive] unarchiveWebsite error:', error)
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -78,7 +78,6 @@ export async function createWebBookmark(
|
||||
// Publish to relays in the background (don't block UI)
|
||||
relayPool.publish(relays, signedEvent)
|
||||
.then(() => {
|
||||
console.log('✅ Web bookmark published to', relays.length, 'relays:', signedEvent)
|
||||
})
|
||||
.catch((err) => {
|
||||
console.warn('⚠️ Some relays failed to publish bookmark:', err)
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { RelayPool } from 'applesauce-relay'
|
||||
import { NostrEvent } from 'nostr-tools'
|
||||
import { IEventStore } from 'applesauce-core'
|
||||
import { RELAYS } from '../config/relays'
|
||||
import { isLocalRelay, areAllRelaysLocal } from '../utils/helpers'
|
||||
import { markEventAsOfflineCreated } from './offlineSyncService'
|
||||
import { getActiveRelayUrls } from './relayManager'
|
||||
|
||||
/**
|
||||
* Unified write helper: add event to EventStore, detect connectivity,
|
||||
@@ -14,9 +14,11 @@ export async function publishEvent(
|
||||
eventStore: IEventStore,
|
||||
event: NostrEvent
|
||||
): Promise<void> {
|
||||
const isProgressEvent = event.kind === 39802
|
||||
const logPrefix = isProgressEvent ? '[progress]' : ''
|
||||
|
||||
// Store the event in the local EventStore FIRST for immediate UI display
|
||||
eventStore.add(event)
|
||||
console.log('💾 Stored event in EventStore:', event.id.slice(0, 8), `(kind ${event.kind})`)
|
||||
|
||||
// Check current connection status - are we online or in flight mode?
|
||||
const connectedRelays = Array.from(relayPool.relays.values())
|
||||
@@ -25,20 +27,17 @@ export async function publishEvent(
|
||||
|
||||
const hasRemoteConnection = connectedRelays.some(url => !isLocalRelay(url))
|
||||
|
||||
// Get active relay URLs from the pool
|
||||
const activeRelays = getActiveRelayUrls(relayPool)
|
||||
|
||||
// Determine which relays we expect to succeed
|
||||
const expectedSuccessRelays = hasRemoteConnection
|
||||
? RELAYS
|
||||
: RELAYS.filter(isLocalRelay)
|
||||
? activeRelays
|
||||
: activeRelays.filter(isLocalRelay)
|
||||
|
||||
const isLocalOnly = areAllRelaysLocal(expectedSuccessRelays)
|
||||
|
||||
console.log('📍 Event relay status:', {
|
||||
targetRelays: RELAYS.length,
|
||||
expectedSuccessRelays: expectedSuccessRelays.length,
|
||||
isLocalOnly,
|
||||
hasRemoteConnection,
|
||||
eventId: event.id.slice(0, 8)
|
||||
})
|
||||
// Publishing event
|
||||
|
||||
// If we're in local-only mode, mark this event for later sync
|
||||
if (isLocalOnly) {
|
||||
@@ -46,12 +45,11 @@ export async function publishEvent(
|
||||
}
|
||||
|
||||
// Publish to all configured relays in the background (non-blocking)
|
||||
relayPool.publish(RELAYS, event)
|
||||
relayPool.publish(activeRelays, event)
|
||||
.then(() => {
|
||||
console.log('✅ Event published to', RELAYS.length, 'relay(s):', event.id.slice(0, 8))
|
||||
})
|
||||
.catch((error) => {
|
||||
console.warn('⚠️ Failed to publish event to relays (event still saved locally):', error)
|
||||
console.warn(`${logPrefix} ⚠️ Failed to publish event to relays (event still saved locally):`, error)
|
||||
|
||||
// Surface common bunker signing errors for debugging
|
||||
if (error instanceof Error && error.message.includes('permission')) {
|
||||
|
||||
245
src/services/writingsController.ts
Normal file
245
src/services/writingsController.ts
Normal file
@@ -0,0 +1,245 @@
|
||||
import { RelayPool } from 'applesauce-relay'
|
||||
import { IEventStore, Helpers } from 'applesauce-core'
|
||||
import { NostrEvent } from 'nostr-tools'
|
||||
import { KINDS } from '../config/kinds'
|
||||
import { queryEvents } from './dataFetch'
|
||||
import { BlogPostPreview } from './exploreService'
|
||||
|
||||
const { getArticleTitle, getArticleSummary, getArticleImage, getArticlePublished } = Helpers
|
||||
|
||||
type WritingsCallback = (posts: BlogPostPreview[]) => void
|
||||
type LoadingCallback = (loading: boolean) => void
|
||||
|
||||
const LAST_SYNCED_KEY = 'writings_last_synced'
|
||||
|
||||
/**
|
||||
* Shared writings controller
|
||||
* Manages the user's nostr-native long-form articles (kind:30023) centrally,
|
||||
* similar to highlightsController
|
||||
*/
|
||||
class WritingsController {
|
||||
private writingsListeners: WritingsCallback[] = []
|
||||
private loadingListeners: LoadingCallback[] = []
|
||||
|
||||
private currentPosts: BlogPostPreview[] = []
|
||||
private lastLoadedPubkey: string | null = null
|
||||
private generation = 0
|
||||
|
||||
onWritings(cb: WritingsCallback): () => void {
|
||||
this.writingsListeners.push(cb)
|
||||
return () => {
|
||||
this.writingsListeners = this.writingsListeners.filter(l => l !== cb)
|
||||
}
|
||||
}
|
||||
|
||||
onLoading(cb: LoadingCallback): () => void {
|
||||
this.loadingListeners.push(cb)
|
||||
return () => {
|
||||
this.loadingListeners = this.loadingListeners.filter(l => l !== cb)
|
||||
}
|
||||
}
|
||||
|
||||
private setLoading(loading: boolean): void {
|
||||
this.loadingListeners.forEach(cb => cb(loading))
|
||||
}
|
||||
|
||||
private emitWritings(posts: BlogPostPreview[]): void {
|
||||
this.writingsListeners.forEach(cb => cb(posts))
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current writings without triggering a reload
|
||||
*/
|
||||
getWritings(): BlogPostPreview[] {
|
||||
return [...this.currentPosts]
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if writings are loaded for a specific pubkey
|
||||
*/
|
||||
isLoadedFor(pubkey: string): boolean {
|
||||
return this.lastLoadedPubkey === pubkey && this.currentPosts.length >= 0
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset state (for logout or manual refresh)
|
||||
*/
|
||||
reset(): void {
|
||||
this.generation++
|
||||
this.currentPosts = []
|
||||
this.lastLoadedPubkey = null
|
||||
this.emitWritings(this.currentPosts)
|
||||
}
|
||||
|
||||
/**
|
||||
* Get last synced timestamp for incremental loading
|
||||
*/
|
||||
private getLastSyncedAt(pubkey: string): number | null {
|
||||
try {
|
||||
const data = localStorage.getItem(LAST_SYNCED_KEY)
|
||||
if (!data) return null
|
||||
const parsed = JSON.parse(data)
|
||||
return parsed[pubkey] || null
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update last synced timestamp
|
||||
*/
|
||||
private setLastSyncedAt(pubkey: string, timestamp: number): void {
|
||||
try {
|
||||
const data = localStorage.getItem(LAST_SYNCED_KEY)
|
||||
const parsed = data ? JSON.parse(data) : {}
|
||||
parsed[pubkey] = timestamp
|
||||
localStorage.setItem(LAST_SYNCED_KEY, JSON.stringify(parsed))
|
||||
} catch (err) {
|
||||
console.warn('[writings] Failed to save last synced timestamp:', err)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert NostrEvent to BlogPostPreview using applesauce Helpers
|
||||
*/
|
||||
private toPreview(event: NostrEvent): BlogPostPreview {
|
||||
return {
|
||||
event,
|
||||
title: getArticleTitle(event) || 'Untitled',
|
||||
summary: getArticleSummary(event),
|
||||
image: getArticleImage(event),
|
||||
published: getArticlePublished(event),
|
||||
author: event.pubkey
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sort posts by published/created date (most recent first)
|
||||
*/
|
||||
private sortPosts(posts: BlogPostPreview[]): BlogPostPreview[] {
|
||||
return posts.slice().sort((a, b) => {
|
||||
const timeA = a.published || a.event.created_at
|
||||
const timeB = b.published || b.event.created_at
|
||||
return timeB - timeA
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Load writings for a user (kind:30023)
|
||||
* Streams results and stores in event store
|
||||
*/
|
||||
async start(options: {
|
||||
relayPool: RelayPool
|
||||
eventStore: IEventStore
|
||||
pubkey: string
|
||||
force?: boolean
|
||||
}): Promise<void> {
|
||||
const { relayPool, eventStore, pubkey, force = false } = options
|
||||
|
||||
// Skip if already loaded for this pubkey (unless forced)
|
||||
if (!force && this.isLoadedFor(pubkey)) {
|
||||
this.emitWritings(this.currentPosts)
|
||||
return
|
||||
}
|
||||
|
||||
// Increment generation to cancel any in-flight work
|
||||
this.generation++
|
||||
const currentGeneration = this.generation
|
||||
|
||||
this.setLoading(true)
|
||||
|
||||
try {
|
||||
const seenIds = new Set<string>()
|
||||
const uniqueByReplaceable = new Map<string, BlogPostPreview>()
|
||||
|
||||
// Get last synced timestamp for incremental loading
|
||||
const lastSyncedAt = force ? null : this.getLastSyncedAt(pubkey)
|
||||
const filter: { kinds: number[]; authors: string[]; since?: number } = {
|
||||
kinds: [KINDS.BlogPost],
|
||||
authors: [pubkey]
|
||||
}
|
||||
if (lastSyncedAt) {
|
||||
filter.since = lastSyncedAt
|
||||
}
|
||||
|
||||
const events = await queryEvents(
|
||||
relayPool,
|
||||
filter,
|
||||
{
|
||||
onEvent: (evt) => {
|
||||
// Check if this generation is still active
|
||||
if (currentGeneration !== this.generation) return
|
||||
|
||||
if (seenIds.has(evt.id)) return
|
||||
seenIds.add(evt.id)
|
||||
|
||||
// Store in event store immediately
|
||||
eventStore.add(evt)
|
||||
|
||||
// Dedupe by replaceable key (author + d-tag)
|
||||
const dTag = evt.tags.find(t => t[0] === 'd')?.[1] || ''
|
||||
const key = `${evt.pubkey}:${dTag}`
|
||||
|
||||
const preview = this.toPreview(evt)
|
||||
const existing = uniqueByReplaceable.get(key)
|
||||
|
||||
// Keep the newest version for replaceable events
|
||||
if (!existing || evt.created_at > existing.event.created_at) {
|
||||
uniqueByReplaceable.set(key, preview)
|
||||
|
||||
// Stream to listeners
|
||||
const sortedPosts = this.sortPosts(Array.from(uniqueByReplaceable.values()))
|
||||
this.currentPosts = sortedPosts
|
||||
this.emitWritings(sortedPosts)
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
// Check if still active after async operation
|
||||
if (currentGeneration !== this.generation) {
|
||||
return
|
||||
}
|
||||
|
||||
// Store all events in event store
|
||||
events.forEach(evt => eventStore.add(evt))
|
||||
|
||||
// Final processing - ensure we have the latest version of each replaceable
|
||||
events.forEach(evt => {
|
||||
const dTag = evt.tags.find(t => t[0] === 'd')?.[1] || ''
|
||||
const key = `${evt.pubkey}:${dTag}`
|
||||
const existing = uniqueByReplaceable.get(key)
|
||||
|
||||
if (!existing || evt.created_at > existing.event.created_at) {
|
||||
uniqueByReplaceable.set(key, this.toPreview(evt))
|
||||
}
|
||||
})
|
||||
|
||||
const sorted = this.sortPosts(Array.from(uniqueByReplaceable.values()))
|
||||
|
||||
this.currentPosts = sorted
|
||||
this.lastLoadedPubkey = pubkey
|
||||
this.emitWritings(sorted)
|
||||
|
||||
// Update last synced timestamp
|
||||
if (sorted.length > 0) {
|
||||
const newestTimestamp = Math.max(...sorted.map(p => p.event.created_at))
|
||||
this.setLastSyncedAt(pubkey, newestTimestamp)
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('[writings] ❌ Failed to load writings:', error)
|
||||
this.currentPosts = []
|
||||
this.emitWritings(this.currentPosts)
|
||||
} finally {
|
||||
// Only clear loading if this generation is still active
|
||||
if (currentGeneration === this.generation) {
|
||||
this.setLoading(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Singleton instance
|
||||
export const writingsController = new WritingsController()
|
||||
|
||||
@@ -22,7 +22,6 @@ export async function fetchBorisZappers(
|
||||
relayPool: RelayPool
|
||||
): Promise<ZapSender[]> {
|
||||
try {
|
||||
console.log('⚡ Fetching zap receipts for Boris...', BORIS_PUBKEY)
|
||||
|
||||
// Use all configured relays plus specific zap-heavy relays
|
||||
const zapRelays = [
|
||||
@@ -63,23 +62,18 @@ export async function fetchBorisZappers(
|
||||
merge(local$, remote$).pipe(toArray())
|
||||
)
|
||||
|
||||
console.log(`📊 Fetched ${zapReceipts.length} raw zap receipts`)
|
||||
|
||||
// Dedupe by event ID and validate
|
||||
const uniqueReceipts = new Map<string, NostrEvent>()
|
||||
let invalidCount = 0
|
||||
|
||||
zapReceipts.forEach(receipt => {
|
||||
if (!uniqueReceipts.has(receipt.id)) {
|
||||
if (isValidZap(receipt)) {
|
||||
uniqueReceipts.set(receipt.id, receipt)
|
||||
} else {
|
||||
invalidCount++
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
console.log(`✅ ${uniqueReceipts.size} valid zap receipts (${invalidCount} invalid)`)
|
||||
|
||||
// Aggregate by sender using applesauce helpers
|
||||
const senderTotals = new Map<string, { totalSats: number; zapCount: number }>()
|
||||
@@ -102,7 +96,6 @@ export async function fetchBorisZappers(
|
||||
})
|
||||
}
|
||||
|
||||
console.log(`👥 Found ${senderTotals.size} unique senders`)
|
||||
|
||||
// Filter >= 2100 sats, mark whales >= 69420 sats, sort by total desc
|
||||
const zappers: ZapSender[] = Array.from(senderTotals.entries())
|
||||
@@ -115,7 +108,6 @@ export async function fetchBorisZappers(
|
||||
}))
|
||||
.sort((a, b) => b.totalSats - a.totalSats)
|
||||
|
||||
console.log(`✅ Found ${zappers.length} supporters (${zappers.filter(z => z.isWhale).length} whales)`)
|
||||
|
||||
return zappers
|
||||
} catch (error) {
|
||||
|
||||
@@ -32,7 +32,7 @@
|
||||
.individual-bookmarks h4 { margin: 0 0 1rem 0; font-size: 1rem; color: var(--color-text); }
|
||||
|
||||
.bookmarks-grid { display: flex; flex-direction: column; gap: 1rem; width: 100%; max-width: 100%; }
|
||||
.bookmarks-grid.bookmarks-compact { gap: 0.5rem; }
|
||||
.bookmarks-grid.bookmarks-compact { gap: 0.25rem; }
|
||||
.bookmarks-grid.bookmarks-large { gap: 1.5rem; }
|
||||
@media (max-width: 768px) {
|
||||
.bookmarks-grid { gap: 0.75rem; }
|
||||
@@ -44,9 +44,9 @@
|
||||
.individual-bookmark:hover { border-color: var(--color-border); background: var(--color-bg-elevated); }
|
||||
|
||||
/* Compact view */
|
||||
.individual-bookmark.compact { padding: 0.5rem 0.5rem; background: transparent; border: none !important; border-radius: 0; box-shadow: none; width: 100%; max-width: 100%; overflow: hidden; }
|
||||
.individual-bookmark.compact { padding: 0.25rem 0.5rem; background: transparent; border: none !important; border-radius: 0; box-shadow: none; width: 100%; max-width: 100%; overflow: hidden; }
|
||||
.individual-bookmark.compact:hover { background: var(--color-bg-elevated); transform: none; box-shadow: none; border: none !important; }
|
||||
.compact-row { display: flex; align-items: center; gap: 0.5rem; height: 28px; width: 100%; min-width: 0; overflow: hidden; }
|
||||
.compact-row { display: flex; align-items: center; gap: 0.5rem; height: 24px; width: 100%; min-width: 0; overflow: hidden; }
|
||||
.compact-thumbnail { width: 24px; height: 24px; flex-shrink: 0; border-radius: 4px; overflow: hidden; background: var(--color-bg-elevated); display: flex; align-items: center; justify-content: center; }
|
||||
.compact-thumbnail img { width: 100%; height: 100%; object-fit: cover; }
|
||||
.compact-row.clickable { cursor: pointer; }
|
||||
|
||||
@@ -109,6 +109,16 @@
|
||||
background: var(--color-bg-elevated) !important;
|
||||
}
|
||||
|
||||
.bookmarks-list .individual-bookmark.compact {
|
||||
border: none !important;
|
||||
background: transparent !important;
|
||||
}
|
||||
|
||||
.bookmarks-list .individual-bookmark.compact:hover {
|
||||
border-color: transparent !important;
|
||||
background: var(--color-bg-elevated) !important;
|
||||
}
|
||||
|
||||
.bookmark-item {
|
||||
padding: 1rem;
|
||||
background: var(--color-bg);
|
||||
|
||||
@@ -14,6 +14,31 @@
|
||||
50% { opacity: 1; transform: scale(1.1); }
|
||||
}
|
||||
|
||||
/* Subtle success burst used for archive action */
|
||||
@keyframes success-burst {
|
||||
0% {
|
||||
box-shadow: 0 0 0 0 rgba(16, 185, 129, 0.45), 0 0 0 0 rgba(16, 185, 129, 0.25);
|
||||
transform: scale(1);
|
||||
}
|
||||
60% {
|
||||
box-shadow: 0 0 0 10px rgba(16, 185, 129, 0), 0 0 0 0 rgba(16, 185, 129, 0);
|
||||
transform: scale(1.05);
|
||||
}
|
||||
100% {
|
||||
box-shadow: 0 0 0 0 rgba(16, 185, 129, 0), 0 0 0 0 rgba(16, 185, 129, 0);
|
||||
transform: scale(1);
|
||||
}
|
||||
}
|
||||
|
||||
/* Apply archive animation when button enters animating state */
|
||||
.mark-as-read-btn.animating {
|
||||
animation: success-burst 600ms ease-out 1;
|
||||
}
|
||||
|
||||
.mark-as-read-btn.animating svg {
|
||||
animation: pulse 600ms ease-in-out 1;
|
||||
}
|
||||
|
||||
@keyframes highlight-pulse-animation {
|
||||
0%, 100% { box-shadow: 0 0 8px rgba(var(--highlight-rgb, 255, 255, 0), 0.2); transform: scale(1); }
|
||||
25% { box-shadow: 0 0 20px rgba(var(--highlight-rgb, 255, 255, 0), 0.6); transform: scale(1.02); }
|
||||
|
||||
@@ -22,7 +22,6 @@ cleanupOutdatedCaches()
|
||||
sw.skipWaiting()
|
||||
clientsClaim()
|
||||
|
||||
console.log('[SW] Boris service worker loaded')
|
||||
|
||||
// Runtime cache: Cross-origin images
|
||||
// This preserves the existing image caching behavior
|
||||
|
||||
@@ -2,7 +2,7 @@ import React from 'react'
|
||||
import { formatDistanceToNow, differenceInSeconds, differenceInMinutes, differenceInHours, differenceInDays, differenceInMonths, differenceInYears } from 'date-fns'
|
||||
import { ParsedContent, ParsedNode, IndividualBookmark } from '../types/bookmarks'
|
||||
import ResolvedMention from '../components/ResolvedMention'
|
||||
// Note: ContentWithResolvedProfiles is imported by components directly to keep this file component-only for fast refresh
|
||||
// Note: RichContent is imported by components directly to keep this file component-only for fast refresh
|
||||
|
||||
export const formatDate = (timestamp: number) => {
|
||||
const date = new Date(timestamp * 1000)
|
||||
@@ -118,6 +118,16 @@ export function hasContent(bookmark: IndividualBookmark): boolean {
|
||||
return hasValidContent || hasId
|
||||
}
|
||||
|
||||
// Check if bookmark has a real creation date (not "Now" / current time)
|
||||
export function hasCreationDate(bookmark: IndividualBookmark): boolean {
|
||||
if (!bookmark.created_at) return false
|
||||
// If timestamp is missing or equals current time (within 1 second), consider it invalid
|
||||
const now = Math.floor(Date.now() / 1000)
|
||||
const createdAt = Math.floor(bookmark.created_at)
|
||||
// If created_at is within 1 second of now, it's likely missing/placeholder
|
||||
return Math.abs(createdAt - now) > 1
|
||||
}
|
||||
|
||||
// Bookmark sets helpers (kind 30003)
|
||||
export interface BookmarkSet {
|
||||
name: string
|
||||
|
||||
@@ -16,18 +16,15 @@ const loadingFonts = new Map<string, Promise<void>>()
|
||||
|
||||
export async function loadFont(fontKey: string): Promise<void> {
|
||||
if (fontKey === 'system') {
|
||||
console.log('📝 Using system font')
|
||||
return Promise.resolve()
|
||||
}
|
||||
|
||||
if (loadedFonts.has(fontKey)) {
|
||||
console.log('✅ Font already loaded:', fontKey)
|
||||
return Promise.resolve()
|
||||
}
|
||||
|
||||
// If font is currently loading, return the existing promise
|
||||
if (loadingFonts.has(fontKey)) {
|
||||
console.log('⏳ Font already loading:', fontKey)
|
||||
return loadingFonts.get(fontKey)!
|
||||
}
|
||||
|
||||
@@ -37,7 +34,6 @@ export async function loadFont(fontKey: string): Promise<void> {
|
||||
return Promise.resolve()
|
||||
}
|
||||
|
||||
console.log('🔤 Loading font:', fontFamily)
|
||||
|
||||
// Create a promise for this font loading
|
||||
const loadPromise = new Promise<void>((resolve) => {
|
||||
@@ -48,7 +44,6 @@ export async function loadFont(fontKey: string): Promise<void> {
|
||||
|
||||
// Wait for the stylesheet to load
|
||||
link.onload = () => {
|
||||
console.log('📄 Stylesheet loaded for:', fontFamily)
|
||||
|
||||
// Use Font Loading API to wait for the actual font to be ready
|
||||
if ('fonts' in document) {
|
||||
@@ -56,7 +51,6 @@ export async function loadFont(fontKey: string): Promise<void> {
|
||||
document.fonts.load(`400 16px "${fontFamily}"`),
|
||||
document.fonts.load(`700 16px "${fontFamily}"`)
|
||||
]).then(() => {
|
||||
console.log('✅ Font ready:', fontFamily)
|
||||
loadedFonts.add(fontKey)
|
||||
loadingFonts.delete(fontKey)
|
||||
resolve()
|
||||
@@ -69,7 +63,6 @@ export async function loadFont(fontKey: string): Promise<void> {
|
||||
} else {
|
||||
// Fallback: just wait a bit for older browsers
|
||||
setTimeout(() => {
|
||||
console.log('✅ Font assumed ready (no Font Loading API):', fontFamily)
|
||||
loadedFonts.add(fontKey)
|
||||
loadingFonts.delete(fontKey)
|
||||
resolve()
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
// Extract pubkeys from nprofile strings in content
|
||||
import { READING_PROGRESS } from '../config/kinds'
|
||||
|
||||
export const extractNprofilePubkeys = (content: string): string[] => {
|
||||
const nprofileRegex = /nprofile1[a-z0-9]+/gi
|
||||
const matches = content.match(nprofileRegex) || []
|
||||
@@ -123,3 +125,14 @@ export function createParallelReqStreams(
|
||||
return { local$, remote$ }
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if content is long enough to track reading progress
|
||||
* Minimum 1000 characters (roughly 150 words)
|
||||
*/
|
||||
export const shouldTrackReadingProgress = (html: string | undefined, markdown: string | undefined): boolean => {
|
||||
const content = (html || markdown || '').trim()
|
||||
// Strip HTML tags to get character count
|
||||
const plainText = content.replace(/<[^>]*>/g, '').trim()
|
||||
return plainText.length >= READING_PROGRESS.MIN_CONTENT_LENGTH
|
||||
}
|
||||
|
||||
|
||||
@@ -10,14 +10,9 @@ export function applyHighlightsToHTML(
|
||||
highlightStyle: 'marker' | 'underline' = 'marker'
|
||||
): string {
|
||||
if (!html || highlights.length === 0) {
|
||||
console.log('⚠️ applyHighlightsToHTML: No HTML or highlights', {
|
||||
htmlLength: html?.length,
|
||||
highlightsCount: highlights.length
|
||||
})
|
||||
return html
|
||||
}
|
||||
|
||||
console.log('🔨 applyHighlightsToHTML: Processing', highlights.length, 'highlights')
|
||||
|
||||
const tempDiv = document.createElement('div')
|
||||
tempDiv.innerHTML = html
|
||||
@@ -31,9 +26,6 @@ export function applyHighlightsToHTML(
|
||||
mark.parentNode?.replaceChild(textNode, mark)
|
||||
})
|
||||
|
||||
console.log('🧹 Removed', existingMarks.length, 'existing highlight marks')
|
||||
|
||||
let appliedCount = 0
|
||||
|
||||
for (const highlight of highlights) {
|
||||
const searchText = highlight.content.trim()
|
||||
@@ -42,7 +34,6 @@ export function applyHighlightsToHTML(
|
||||
continue
|
||||
}
|
||||
|
||||
console.log('🔍 Searching for highlight:', searchText.substring(0, 50) + '...')
|
||||
|
||||
// Collect all text nodes
|
||||
const walker = document.createTreeWalker(tempDiv, NodeFilter.SHOW_TEXT, null)
|
||||
@@ -50,21 +41,16 @@ export function applyHighlightsToHTML(
|
||||
let node: Node | null
|
||||
while ((node = walker.nextNode())) textNodes.push(node as Text)
|
||||
|
||||
console.log('📄 Found', textNodes.length, 'text nodes to search')
|
||||
|
||||
// Try exact match first, then normalized match
|
||||
const found = tryMarkInTextNodes(textNodes, searchText, highlight, false, highlightStyle) ||
|
||||
tryMarkInTextNodes(textNodes, searchText, highlight, true, highlightStyle)
|
||||
|
||||
if (found) {
|
||||
appliedCount++
|
||||
console.log('✅ Highlight applied successfully')
|
||||
} else {
|
||||
if (!found) {
|
||||
console.warn('❌ Could not find match for highlight:', searchText.substring(0, 50))
|
||||
}
|
||||
}
|
||||
|
||||
console.log('🎉 Applied', appliedCount, '/', highlights.length, 'highlights')
|
||||
|
||||
return tempDiv.innerHTML
|
||||
}
|
||||
|
||||
@@ -1,26 +1,74 @@
|
||||
import { ReadItem } from '../services/readsService'
|
||||
import { ReadingProgressFilterType } from '../components/ReadingProgressFilters'
|
||||
import { Highlight } from '../types/highlights'
|
||||
import { nip19 } from 'nostr-tools'
|
||||
|
||||
/**
|
||||
* Filters ReadItems by reading progress
|
||||
*/
|
||||
export function filterByReadingProgress(
|
||||
items: ReadItem[],
|
||||
filter: ReadingProgressFilterType
|
||||
filter: ReadingProgressFilterType,
|
||||
highlights?: Highlight[]
|
||||
): ReadItem[] {
|
||||
// Build a map of article references to highlight count
|
||||
// Normalize both coordinate and naddr formats for matching
|
||||
const articleHighlightCount = new Map<string, number>()
|
||||
if (highlights) {
|
||||
highlights.forEach(h => {
|
||||
if (h.eventReference) {
|
||||
// eventReference could be a hex ID or a coordinate (30023:pubkey:identifier)
|
||||
let normalizedRef = h.eventReference
|
||||
|
||||
// If it's a coordinate, convert to naddr format for matching
|
||||
if (h.eventReference.includes(':')) {
|
||||
const parts = h.eventReference.split(':')
|
||||
if (parts.length === 3) {
|
||||
const [kind, pubkey, identifier] = parts
|
||||
try {
|
||||
normalizedRef = nip19.naddrEncode({
|
||||
kind: parseInt(kind),
|
||||
pubkey,
|
||||
identifier
|
||||
})
|
||||
} catch {
|
||||
// If conversion fails, use the original reference
|
||||
normalizedRef = h.eventReference
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const count = articleHighlightCount.get(normalizedRef) || 0
|
||||
articleHighlightCount.set(normalizedRef, count + 1)
|
||||
}
|
||||
if (h.urlReference) {
|
||||
const count = articleHighlightCount.get(h.urlReference) || 0
|
||||
articleHighlightCount.set(h.urlReference, count + 1)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
return items.filter((item) => {
|
||||
const progress = item.readingProgress || 0
|
||||
const isMarked = item.markedAsRead || false
|
||||
// Reading progress filters MUST ignore emoji/archive reactions
|
||||
const hasHighlights = (articleHighlightCount.get(item.id) || 0) > 0 ||
|
||||
(item.url && (articleHighlightCount.get(item.url) || 0) > 0)
|
||||
|
||||
switch (filter) {
|
||||
case 'unopened':
|
||||
return progress === 0 && !isMarked
|
||||
return progress === 0
|
||||
case 'started':
|
||||
return progress > 0 && progress <= 0.10 && !isMarked
|
||||
return progress > 0 && progress <= 0.10
|
||||
case 'reading':
|
||||
return progress > 0.10 && progress <= 0.94 && !isMarked
|
||||
return progress > 0.10 && progress <= 0.94
|
||||
case 'completed':
|
||||
return progress >= 0.95 || isMarked
|
||||
// Completed is 95%+ progress only (no emoji fallback)
|
||||
return progress >= 0.95
|
||||
case 'archive':
|
||||
// Archive filter handled upstream; keep fallback as false to avoid mixing
|
||||
return false
|
||||
case 'highlighted':
|
||||
return hasHighlights
|
||||
case 'all':
|
||||
default:
|
||||
return true
|
||||
|
||||
@@ -43,8 +43,7 @@ export function applyTheme(
|
||||
root.classList.add(`light-${lightColorTheme}`)
|
||||
|
||||
// Listen for system theme changes
|
||||
mediaQueryListener = (e: MediaQueryListEvent) => {
|
||||
console.log('🎨 System theme changed to:', e.matches ? 'dark' : 'light')
|
||||
mediaQueryListener = () => {
|
||||
// The CSS media query handles the color changes automatically
|
||||
}
|
||||
|
||||
@@ -59,5 +58,4 @@ export function applyTheme(
|
||||
}
|
||||
}
|
||||
|
||||
console.log('🎨 Applied theme:', theme, 'with colors:', { dark: darkColorTheme, light: lightColorTheme })
|
||||
}
|
||||
|
||||
15
src/utils/toBlogPostPreview.ts
Normal file
15
src/utils/toBlogPostPreview.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { NostrEvent } from 'nostr-tools'
|
||||
import { Helpers } from 'applesauce-core'
|
||||
import { BlogPostPreview } from '../services/exploreService'
|
||||
|
||||
const { getArticleTitle, getArticleImage, getArticlePublished, getArticleSummary } = Helpers
|
||||
|
||||
export const toBlogPostPreview = (event: NostrEvent): BlogPostPreview => ({
|
||||
event,
|
||||
title: getArticleTitle(event) || 'Untitled',
|
||||
summary: getArticleSummary(event),
|
||||
image: getArticleImage(event),
|
||||
published: getArticlePublished(event),
|
||||
author: event.pubkey
|
||||
})
|
||||
|
||||
@@ -11,26 +11,21 @@ export function normalizeUrl(url: string): string {
|
||||
|
||||
export function filterHighlightsByUrl(highlights: Highlight[], selectedUrl: string | undefined): Highlight[] {
|
||||
if (!selectedUrl || highlights.length === 0) {
|
||||
console.log('🔍 filterHighlightsByUrl: No URL or highlights', { selectedUrl, count: highlights.length })
|
||||
return []
|
||||
}
|
||||
|
||||
console.log('🔍 filterHighlightsByUrl:', { selectedUrl, totalHighlights: highlights.length })
|
||||
|
||||
// For Nostr articles, we already fetched highlights specifically for this article
|
||||
// So we don't need to filter them - they're all relevant
|
||||
if (selectedUrl.startsWith('nostr:')) {
|
||||
console.log('📌 Nostr article - returning all', highlights.length, 'highlights')
|
||||
return highlights
|
||||
}
|
||||
|
||||
// For web URLs, filter by URL matching
|
||||
const normalizedSelected = normalizeUrl(selectedUrl)
|
||||
console.log('🔗 Normalized selected URL:', normalizedSelected)
|
||||
|
||||
const filtered = highlights.filter(h => {
|
||||
if (!h.urlReference) {
|
||||
console.log('⚠️ Highlight has no urlReference:', h.id, 'eventReference:', h.eventReference)
|
||||
return false
|
||||
}
|
||||
const normalizedRef = normalizeUrl(h.urlReference)
|
||||
@@ -39,14 +34,13 @@ export function filterHighlightsByUrl(highlights: Highlight[], selectedUrl: stri
|
||||
normalizedRef.includes(normalizedSelected)
|
||||
|
||||
if (matches) {
|
||||
console.log('✅ URL match:', normalizedRef)
|
||||
// URLs match
|
||||
} else {
|
||||
console.log('❌ URL mismatch:', normalizedRef, 'vs', normalizedSelected)
|
||||
// URLs do not match
|
||||
}
|
||||
|
||||
return matches
|
||||
})
|
||||
|
||||
console.log('📊 Filtered to', filtered.length, 'highlights')
|
||||
return filtered
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user