All files / webview/contexts BoltzHubContext.tsx

82.35% Statements 14/17
50% Branches 1/2
80% Functions 4/5
87.5% Lines 14/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48                      4x     37x 37x 37x   37x   1x 1x         37x               37x 37x       37x       37x 37x 37x    
import { createContext, type ReactNode, useCallback, useContext, useMemo } from 'react'
import { useBoltzHubState } from '../hooks/useBoltzHubState'
import { vscode } from '../lib/vscode'
import { useChat } from './ChatContext'
import { useRouter } from './MessageRouterContext'
 
type BoltzHubStateType = ReturnType<typeof useBoltzHubState> & {
  handleReleaseNotesApprove: (notes: string, updateType: 'major' | 'minor' | 'patch' | undefined) => void
  handleReleaseNotesDeny: (feedback: string) => void
}
 
const BoltzHubCtx = createContext<BoltzHubStateType | null>(null)
 
export function BoltzHubProvider({ children }: { children: ReactNode }) {
  const router = useRouter()
  const { setMessages } = useChat()
  const boltzHub = useBoltzHubState(router, setMessages)
 
  const handleReleaseNotesApprove = useCallback(
    (notes: string, updateType: 'major' | 'minor' | 'patch' | undefined) => {
      boltzHub.setPendingReleaseNotesDraft(null)
      vscode.postMessage({ type: 'pushProjectConfirmed', releaseNotes: notes, updateType })
    },
    [boltzHub.setPendingReleaseNotesDraft],
  )
 
  const handleReleaseNotesDeny = useCallback(
    (feedback: string) => {
      boltzHub.setPendingReleaseNotesDraft(null)
      vscode.postMessage({ type: 'generateReleaseNotes', feedback })
    },
    [boltzHub.setPendingReleaseNotesDraft],
  )
 
  const value = useMemo(
    () => ({ ...boltzHub, handleReleaseNotesApprove, handleReleaseNotesDeny }),
    [boltzHub, handleReleaseNotesApprove, handleReleaseNotesDeny],
  )
 
  return <BoltzHubCtx.Provider value={value}>{children}</BoltzHubCtx.Provider>
}
 
export function useBoltzHub(): BoltzHubStateType {
  const ctx = useContext(BoltzHubCtx)
  Iif (!ctx) throw new Error('useBoltzHub must be used within BoltzHubProvider')
  return ctx
}