Files
boris/src/hooks/useHighlightCreation.ts
Gigi 239ab5763d feat: add configurable zap split for highlights on nostr-native content
- Add zapSplitPercentage setting (default 50%) to UserSettings
- Implement NIP-57 Appendix G zap tags for highlight events
- Add zap tags when creating highlights of nostr-native content
- Split zaps between highlighter and article author based on setting
- Add UI slider in settings to configure split percentage
- Include relay URL in zap tags for metadata lookup
- Only add author zap tag if different from highlighter
2025-10-08 06:32:02 +01:00

85 lines
2.5 KiB
TypeScript

import { useCallback, useRef } from 'react'
import { RelayPool } from 'applesauce-relay'
import { NostrEvent } from 'nostr-tools'
import { Highlight } from '../types/highlights'
import { ReadableContent } from '../services/readerService'
import { createHighlight, eventToHighlight } from '../services/highlightCreationService'
import { HighlightButtonRef } from '../components/HighlightButton'
import { UserSettings } from '../services/settingsService'
interface UseHighlightCreationParams {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
activeAccount: any
relayPool: RelayPool | null
currentArticle: NostrEvent | undefined
selectedUrl: string | undefined
readerContent: ReadableContent | undefined
onHighlightCreated: (highlight: Highlight) => void
settings?: UserSettings
}
export const useHighlightCreation = ({
activeAccount,
relayPool,
currentArticle,
selectedUrl,
readerContent,
onHighlightCreated,
settings
}: UseHighlightCreationParams) => {
const highlightButtonRef = useRef<HighlightButtonRef>(null)
const handleTextSelection = useCallback((text: string) => {
highlightButtonRef.current?.updateSelection(text)
}, [])
const handleClearSelection = useCallback(() => {
highlightButtonRef.current?.clearSelection()
}, [])
const handleCreateHighlight = useCallback(async (text: string) => {
if (!activeAccount || !relayPool) {
console.error('Missing requirements for highlight creation')
return
}
if (!currentArticle && !selectedUrl) {
console.error('No source available for highlight creation')
return
}
try {
const source = currentArticle || selectedUrl!
const contentForContext = currentArticle
? currentArticle.content
: readerContent?.markdown || readerContent?.html
const signedEvent = await createHighlight(
text,
source,
activeAccount,
relayPool,
contentForContext,
undefined,
settings
)
console.log('✅ Highlight created successfully!')
highlightButtonRef.current?.clearSelection()
const newHighlight = eventToHighlight(signedEvent)
onHighlightCreated(newHighlight)
} catch (error) {
console.error('Failed to create highlight:', error)
}
}, [activeAccount, relayPool, currentArticle, selectedUrl, readerContent, onHighlightCreated, settings])
return {
highlightButtonRef,
handleTextSelection,
handleClearSelection,
handleCreateHighlight
}
}