Files
rustfs-console/hooks/use-import-export.ts
安正超 ca87e9e007 fix: status home redirect (#47)
* feat: move status route and format repo

* fix: resolve lint warnings

* fix: load bucket usage asynchronously

* fix: sign version config request

* fix: show spinner for bucket usage loading

* fix: prevent object list fetch loop

* fix: dedupe list requests

* feat: add sidebar version footer
2026-02-04 21:42:11 +08:00

77 lines
2.1 KiB
TypeScript

"use client"
import { useState, useCallback } from "react"
import { useTranslation } from "react-i18next"
import { useApi } from "@/contexts/api-context"
import { useMessage } from "@/lib/feedback/message"
import { exportFile } from "@/lib/export-file"
export function useImportExport() {
const api = useApi()
const message = useMessage()
const { t } = useTranslation()
const [isLoading, setIsLoading] = useState(false)
const exportIamConfig = useCallback(async () => {
try {
setIsLoading(true)
const response = await api.request(
"/export-iam",
{
method: "GET",
headers: { Accept: "application/zip" },
},
false,
)
if (!response || !(response instanceof Response)) {
throw new Error("No response")
}
const timestamp = new Date().toISOString().slice(0, 19).replace(/[:-]/g, "")
const fileName = `iam-config-export-${timestamp}.zip`
const blob = await response.blob()
const exportResponse = {
data: blob,
headers: {
"content-type": "application/zip",
filename: encodeURIComponent(fileName),
},
}
exportFile(exportResponse, fileName)
message.success(t("IAM configuration exported successfully"))
} catch (error) {
console.error("IAM export error:", error)
message.error((error as Error).message || t("Failed to export IAM configuration"))
} finally {
setIsLoading(false)
}
}, [api, message, t])
const importIamConfig = useCallback(
async (file: File) => {
try {
setIsLoading(true)
await api.put("/import-iam", file, {
headers: { "Content-Type": "application/zip" },
})
message.success(t("IAM configuration imported successfully"))
} catch (error) {
console.error("IAM import error:", error)
message.error((error as Error).message || t("Failed to import IAM configuration"))
throw error
} finally {
setIsLoading(false)
}
},
[api, message, t],
)
return { isLoading, exportIamConfig, importIamConfig }
}