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 | 3x 3x 3x 3x 3x | import { useState } from 'react'
import { toast } from 'sonner'
import { api, type Source } from '../api/client'
export default function useSourceToggle(
sources: Source[],
onSourcesChange: (sources: Source[]) => void
) {
const [togglingSourceId, setTogglingSourceId] = useState<number | null>(null)
const [bulkToggling, setBulkToggling] = useState(false)
const handleToggleSource = async (sourceId: number) => {
setTogglingSourceId(sourceId)
try {
const result = await api.sources.toggle(sourceId)
onSourcesChange(
sources.map((s) =>
s.id === sourceId ? { ...s, is_enabled: result.is_enabled } : s
)
)
} catch (error) {
console.error('Failed to toggle source:', error)
toast.error('Failed to toggle source')
} finally {
setTogglingSourceId(null)
}
}
const handleBulkToggle = async (enable: boolean) => {
setBulkToggling(true)
try {
await api.sources.bulkToggle(enable)
onSourcesChange(sources.map((s) => ({ ...s, is_enabled: enable })))
toast.success(enable ? 'All sources enabled' : 'All sources disabled')
} catch (error) {
console.error('Failed to bulk toggle sources:', error)
toast.error('Failed to update sources')
} finally {
setBulkToggling(false)
}
}
return { togglingSourceId, bulkToggling, handleToggleSource, handleBulkToggle }
}
|