mirror of
https://github.com/johndoe6345789/low-code-react-app-b.git
synced 2026-04-27 15:14:55 +00:00
35 lines
923 B
TypeScript
35 lines
923 B
TypeScript
export const formatStorageError = (error: unknown) => {
|
|
if (error instanceof Error) {
|
|
return error.message
|
|
}
|
|
|
|
return String(error)
|
|
}
|
|
|
|
export const downloadJson = (data: unknown, filename: string) => {
|
|
const json = JSON.stringify(data, null, 2)
|
|
const blob = new Blob([json], { type: 'application/json' })
|
|
const url = URL.createObjectURL(blob)
|
|
const anchor = document.createElement('a')
|
|
|
|
anchor.href = url
|
|
anchor.download = filename
|
|
document.body.appendChild(anchor)
|
|
anchor.click()
|
|
document.body.removeChild(anchor)
|
|
URL.revokeObjectURL(url)
|
|
}
|
|
|
|
export const createJsonFileInput = (accept: string, onFileLoaded: (file: File) => void) => {
|
|
const input = document.createElement('input')
|
|
input.type = 'file'
|
|
input.accept = accept
|
|
input.onchange = (event) => {
|
|
const file = (event.target as HTMLInputElement).files?.[0]
|
|
if (file) {
|
|
onFileLoaded(file)
|
|
}
|
|
}
|
|
input.click()
|
|
}
|