mirror of
https://github.com/johndoe6345789/metabuilder.git
synced 2026-04-24 13:54:57 +00:00
- Added @metabuilder/hooks workspace package at root
- Consolidated 30 React hooks from across codebase into single module
- Implemented conditional exports for tree-shaking support
- Added comprehensive package.json with build/lint/typecheck scripts
- Created README.md documenting hook categories and usage patterns
- Updated root package.json workspaces array to include hooks
- Supports multi-version peer dependencies (React 18/19, Redux 8/9)
Usage:
import { useDashboardLogic } from '@metabuilder/hooks'
import { useLoginLogic } from '@metabuilder/hooks/useLoginLogic'
Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
58 lines
1.6 KiB
TypeScript
58 lines
1.6 KiB
TypeScript
import { useCallback, useState } from 'react'
|
|
import { toast } from 'sonner'
|
|
import { createJsonFileInput, downloadJson, formatStorageError } from './storageSettingsUtils'
|
|
import { storageSettingsCopy } from './storageSettingsConfig'
|
|
|
|
type DataHandlers = {
|
|
exportData: () => Promise<unknown>
|
|
importData: (data: unknown) => Promise<void>
|
|
exportFilename: () => string
|
|
importAccept: string
|
|
}
|
|
|
|
export const useStorageDataHandlers = ({
|
|
exportData,
|
|
importData,
|
|
exportFilename,
|
|
importAccept,
|
|
}: DataHandlers) => {
|
|
const [isExporting, setIsExporting] = useState(false)
|
|
const [isImporting, setIsImporting] = useState(false)
|
|
|
|
const handleExport = useCallback(async () => {
|
|
setIsExporting(true)
|
|
try {
|
|
const data = await exportData()
|
|
downloadJson(data, exportFilename())
|
|
toast.success(storageSettingsCopy.toasts.success.export)
|
|
} catch (error) {
|
|
toast.error(`${storageSettingsCopy.toasts.failure.export}: ${formatStorageError(error)}`)
|
|
} finally {
|
|
setIsExporting(false)
|
|
}
|
|
}, [exportData, exportFilename])
|
|
|
|
const handleImport = useCallback(() => {
|
|
createJsonFileInput(importAccept, async (file) => {
|
|
setIsImporting(true)
|
|
try {
|
|
const text = await file.text()
|
|
const data = JSON.parse(text)
|
|
await importData(data)
|
|
toast.success(storageSettingsCopy.toasts.success.import)
|
|
} catch (error) {
|
|
toast.error(`${storageSettingsCopy.toasts.failure.import}: ${formatStorageError(error)}`)
|
|
} finally {
|
|
setIsImporting(false)
|
|
}
|
|
})
|
|
}, [importAccept, importData])
|
|
|
|
return {
|
|
isExporting,
|
|
isImporting,
|
|
handleExport,
|
|
handleImport,
|
|
}
|
|
}
|