"use client"; import { useImperativeHandle, useRef } from "react"; import { Alert, Button, Center, Loader, Stack, Text } from "@mantine/core"; import { IconInfoCircle } from "@tabler/icons-react"; import { clientApi } from "@homarr/api/client"; import type { AppFormHandle } from "@homarr/forms-collection"; import { AppForm } from "@homarr/forms-collection"; import { showErrorNotification, showSuccessNotification } from "@homarr/notifications"; import { useI18n } from "@homarr/translation/client"; export interface EmbeddedAppEditFormHandle { submitIfDirty: () => Promise; } interface EmbeddedAppEditFormProps { appId: string; handleRef: React.Ref; } export const EmbeddedAppEditForm = ({ appId, handleRef }: EmbeddedAppEditFormProps) => { const t = useI18n(); const utils = clientApi.useUtils(); const appFormRef = useRef(null); const { data: app, isPending: isLoadingApp, isError, refetch } = clientApi.app.byId.useQuery({ id: appId }); const { mutateAsync, isPending: isMutating } = clientApi.app.update.useMutation({ onSuccess: async () => { await utils.app.invalidate(); showSuccessNotification({ title: t("app.page.edit.notification.success.title"), message: t("app.page.edit.notification.success.message"), }); }, onError: () => { showErrorNotification({ title: t("app.page.edit.notification.error.title"), message: t("app.page.edit.notification.error.message"), }); }, }); useImperativeHandle( handleRef, () => ({ submitIfDirty: async () => { if (!appFormRef.current?.isDirty()) { return true; } return appFormRef.current.submit(); }, }), [], ); if (isLoadingApp) { return (
); } if (isError || !app) { return ( {t("app.action.select.notFound")} ); } return ( } color="blue" variant="light"> {t("item.edit.app.propagationNotice")} { await mutateAsync({ id: appId, ...values }); }} isPending={isMutating} /> ); };