/* * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ import React, { useState } from "react"; import { useHistory } from "react-router-dom"; import { useForm } from "react-hook-form"; import { useSettings, useLogo } from "../hooks"; import BackendService from "../services/BackendService"; import StorageService from "../services/StorageService"; import FileInput from "../components/FileInput"; import Button from "../components/Button"; import Breadcrumbs from "../components/Breadcrumbs"; import Spinner from "../components/Spinner"; import defaultLogo from "../logo.svg"; import { useTranslation } from "react-i18next"; import Header from "../components/Header"; import TextField from "../components/TextField"; function EditLogo() { const { t } = useTranslation(); const history = useHistory(); const { settings, reloadSettings, loadingSettings } = useSettings(true); const { loadingFile, logo, logoFileName } = useLogo(settings.customLogoS3Key); const { register, errors, handleSubmit } = useForm(); const [currentLogo, setCurrentLogo] = useState(logo); const [imageUploading, setImageUploading] = useState(false); const [altText, setAltText] = useState(""); const onSubmit = async () => { let refreshSettings = false; if (altText) { try { await BackendService.updateSetting("customLogoAltText", altText, new Date()); refreshSettings = true; } catch (err) { history.push("/admin/settings/brandingandstyling", { alert: { type: "error", message: t("SettingsLogoEditFailed"), }, }); } } if (currentLogo) { try { setImageUploading(true); const s3Key = await StorageService.uploadLogo( currentLogo, settings.customLogoS3Key, ); await BackendService.updateSetting("customLogoS3Key", s3Key, new Date()); setImageUploading(false); refreshSettings = true; } catch (err) { setImageUploading(false); history.push("/admin/settings/brandingandstyling", { alert: { type: "error", message: t("SettingsLogoEditFailed"), }, }); } } if (refreshSettings) { try { await reloadSettings(); history.push("/admin/settings/brandingandstyling", { alert: { type: "success", message: t("SettingsLogoEditSuccess"), }, }); } catch (err) { history.push("/admin/settings/brandingandstyling", { alert: { type: "error", message: t("SettingsLogoEditFailed"), }, }); } } else { history.push("/admin/settings/brandingandstyling"); } }; const onCancel = () => { history.push("/admin/settings/brandingandstyling"); }; const crumbs = [ { label: t("Settings"), url: "/admin/settings", }, { label: t("BrandingAndStyle"), url: "/admin/settings/brandingandstyling", }, { label: t("SettingsLogoEdit"), }, ]; const onFileProcessed = (data: File) => { if (data) { setCurrentLogo(data); } }; const handleAltTextChange = (event: React.FormEvent) => { setAltText((event.target as HTMLInputElement).value); }; return (

{t("SettingsLogoEdit")}

{t("SettingsLogoDescription")}

{loadingSettings || loadingFile ? ( ) : ( <>
{t("SettingsLogoFileUploadHint")}} fileName={ logoFileName ? logoFileName : defaultLogo.replace(/^.*[\\/]/, "") } onFileProcessed={onFileProcessed} />

{t("SettingsLogoPreview")}

{currentLogo && ( {altText )} {!currentLogo && ( {altText )}
{settings.navbarTitle}
)}
); } export default EditLogo;