33 lines
1.1 KiB
TypeScript
33 lines
1.1 KiB
TypeScript
import type { MediaRef } from '@/types/nostr'
|
|
import { uploadNip95Media } from '@/lib/nip95'
|
|
import { t } from '@/lib/i18n'
|
|
|
|
export function createImageUploadHandler(params: {
|
|
setError: (value: string | null) => void
|
|
setUploading: (value: boolean) => void
|
|
onMediaAdd: ((media: MediaRef) => void) | undefined
|
|
onBannerChange: ((url: string) => void) | undefined
|
|
onSetPageImageUrl: (pageNumber: number, url: string) => void
|
|
}): (args: { file: File; pageNumber?: number }) => Promise<void> {
|
|
return async (args): Promise<void> => {
|
|
params.setError(null)
|
|
params.setUploading(true)
|
|
try {
|
|
const media = await uploadNip95Media(args.file)
|
|
if (media.type !== 'image') {
|
|
return
|
|
}
|
|
if (args.pageNumber !== undefined) {
|
|
params.onSetPageImageUrl(args.pageNumber, media.url)
|
|
} else {
|
|
params.onBannerChange?.(media.url)
|
|
}
|
|
params.onMediaAdd?.(media)
|
|
} catch (e) {
|
|
params.setError(e instanceof Error ? e.message : t('upload.error.failed'))
|
|
} finally {
|
|
params.setUploading(false)
|
|
}
|
|
}
|
|
}
|