2022-08-13 10:56:07 +02:00
import { t , Trans } from "@lingui/macro"
import { Box , Button , FileInput , Group , Stack } from "@mantine/core"
import { useForm } from "@mantine/form"
import { client , errorToStrings } from "app/client"
import { redirectToSelectedSource } from "app/slices/redirect"
import { reloadTree } from "app/slices/tree"
import { useAppDispatch } from "app/store"
import { Alert } from "components/Alert"
2022-08-13 18:38:11 +02:00
import { useAsyncCallback } from "react-async-hook"
2022-08-13 10:56:07 +02:00
import { TbFileImport } from "react-icons/tb"
export function ImportOpml() {
const dispatch = useAppDispatch ( )
const form = useForm < { file : File } > ( {
validate : {
file : v = > ( v ? null : t ` file is required ` ) ,
} ,
} )
2022-08-13 18:38:11 +02:00
const importOpml = useAsyncCallback ( client . feed . importOpml , {
2022-08-13 10:56:07 +02:00
onSuccess : ( ) = > {
dispatch ( reloadTree ( ) )
dispatch ( redirectToSelectedSource ( ) )
} ,
} )
return (
< >
2022-08-13 18:38:11 +02:00
{ importOpml . error && (
2022-08-13 10:56:07 +02:00
< Box mb = "md" >
2022-08-13 18:38:11 +02:00
< Alert messages = { errorToStrings ( importOpml . error ) } / >
2022-08-13 10:56:07 +02:00
< / Box >
) }
2022-08-13 18:38:11 +02:00
< form onSubmit = { form . onSubmit ( v = > importOpml . execute ( v . file ) ) } >
2022-08-13 10:56:07 +02:00
< Stack >
< FileInput
label = { t ` OPML file ` }
placeholder = { t ` OPML file ` }
description = { t ` An opml file is an XML file containing feed URLs and categories. You can get an OPML file by exporting your data from other feed reading services. ` }
{ . . . form . getInputProps ( "file" ) }
required
accept = "application/xml"
/ >
< Group position = "center" >
< Button variant = "default" onClick = { ( ) = > dispatch ( redirectToSelectedSource ( ) ) } >
< Trans > Cancel < / Trans >
< / Button >
2022-08-13 18:38:11 +02:00
< Button type = "submit" leftIcon = { < TbFileImport size = { 16 } / > } loading = { importOpml . loading } >
2022-08-13 10:56:07 +02:00
< Trans > Import < / Trans >
< / Button >
< / Group >
< / Stack >
< / form >
< / >
)
}