68 lines
2.0 KiB
TypeScript
68 lines
2.0 KiB
TypeScript
"use client";
|
|
import React from "react";
|
|
import { useTranslation } from "react-i18next";
|
|
import { toast } from "sonner";
|
|
|
|
import { SaveToast } from "~/components/notifications/SaveToast";
|
|
import { COPY_TOAST_DURATION_MS } from "~/hooks/use-copy-toast";
|
|
|
|
import { CopyToClipboardButton as CopyToClipboardButtonComponent } from "./CopyToClipboardButton";
|
|
|
|
/**
|
|
* Add wrapper components and business logic here.
|
|
* If you modify this file, disable Subframe sync for it to prevent overwrites.
|
|
* Learn more: https://docs.subframe.com/concepts/syncing-components#wrapping-components
|
|
*/
|
|
|
|
type GeneratedProps = React.ComponentProps<typeof CopyToClipboardButtonComponent>;
|
|
|
|
export type CopyToClipboardButtonProps = Omit<GeneratedProps, "onCopy"> & {
|
|
successKey?: string;
|
|
errorKey?: string;
|
|
onCopy?: (copied: boolean) => void;
|
|
};
|
|
|
|
export const CopyToClipboardButton = React.forwardRef<HTMLDivElement, CopyToClipboardButtonProps>(function CopyToClipboardButton(
|
|
{ successKey, errorKey, onCopy, ...props },
|
|
ref
|
|
) {
|
|
const { t } = useTranslation();
|
|
|
|
return (
|
|
<CopyToClipboardButtonComponent
|
|
{...props}
|
|
ref={ref}
|
|
onCopy={(copied) => {
|
|
onCopy?.(copied);
|
|
if (copied) {
|
|
toast.custom(
|
|
() => (
|
|
<SaveToast
|
|
variant="success"
|
|
title={t(successKey ?? "shared.copyToast.successTitle", {
|
|
defaultValue: t("shared.copyToast.successTitle"),
|
|
})}
|
|
/>
|
|
),
|
|
{ duration: COPY_TOAST_DURATION_MS }
|
|
);
|
|
return;
|
|
}
|
|
|
|
toast.custom(
|
|
() => (
|
|
<SaveToast
|
|
variant="error"
|
|
title={t("shared.copyToast.errorTitle")}
|
|
description={t(errorKey ?? "shared.copyToast.errorDescription", {
|
|
defaultValue: t("shared.copyToast.errorDescription"),
|
|
})}
|
|
/>
|
|
),
|
|
{ duration: COPY_TOAST_DURATION_MS }
|
|
);
|
|
}}
|
|
/>
|
|
);
|
|
});
|