Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

copy feedback

Manav Rathiec30ace82211
diff --git a/web/apps/photos/src/components/Collections/CollectionShare.tsx b/web/apps/photos/src/components/Collections/CollectionShare.tsx
index 0b1e1ade79..538a4425cc 100644
--- a/web/apps/photos/src/components/Collections/CollectionShare.tsx
+++ b/web/apps/photos/src/components/Collections/CollectionShare.tsx
@@ -39,6 +39,7 @@ import {
RowSwitch,
} from "ente-base/components/RowButton";
import { Titlebar } from "ente-base/components/Titlebar";
+import { useClipboardCopy } from "ente-base/components/utils/hooks";
import { useModalVisibility } from "ente-base/components/utils/modal";
import { useBaseContext } from "ente-base/context";
import { sharedCryptoWorker } from "ente-base/crypto";
@@ -1209,7 +1210,6 @@ const PublicShare: React.FC<PublicShareProps> = ({
collection={collection}
publicShareUrl={publicShareUrl}
onRootClose={onRootClose}
- copyToClipboardHelper={copyToClipboardHelper}
/>
) : (
<EnablePublicShareOptions
@@ -1232,7 +1232,6 @@ interface ManagePublicShareProps {
setPublicShareProp: SetPublicShareProp;
onRootClose: () => void;
publicShareUrl: string;
- copyToClipboardHelper: () => void;
}
const ManagePublicShare: React.FC<ManagePublicShareProps> = ({
@@ -1241,11 +1240,16 @@ const ManagePublicShare: React.FC<ManagePublicShareProps> = ({
collection,
onRootClose,
publicShareUrl,
- copyToClipboardHelper,
}) => {
const [manageShareView, setManageShareView] = useState(false);
+
+ const [copied, handleCopyLink] = useClipboardCopy(publicShareUrl);
+
+ const CopyableIcon = copied ? DoneIcon : ContentCopyIcon;
+
const closeManageShare = () => setManageShareView(false);
const openManageShare = () => setManageShareView(true);
+
return (
<>
<Stack>
@@ -1263,8 +1267,8 @@ const ManagePublicShare: React.FC<ManagePublicShareProps> = ({
/>
) : (
<RowButton
- startIcon={<ContentCopyIcon />}
- onClick={copyToClipboardHelper}
+ startIcon={<CopyableIcon />}
+ onClick={handleCopyLink}
disabled={isLinkExpired(publicShareProp.validTill)}
label={t("copy_link")}
/>
diff --git a/web/packages/base/components/utils/hooks.ts b/web/packages/base/components/utils/hooks.ts
index 909fbbd179..4515cd7ad7 100644
--- a/web/packages/base/components/utils/hooks.ts
+++ b/web/packages/base/components/utils/hooks.ts
@@ -1,4 +1,5 @@
import { useMediaQuery, useTheme } from "@mui/material";
+import { useCallback, useState } from "react";
/**
* Return true if the screen width is classified as a small size. This is often
@@ -25,3 +26,27 @@ export const useIsSmallWidth = () =>
*/
export const useIsTouchscreen = () =>
useMediaQuery("(hover: none) and (pointer: coarse)", { noSsr: true });
+
+/**
+ * A hook that manages a transient "copied" state.
+ *
+ * @param text The text to copy.
+ *
+ * @returns a tuple containing a boolean {@link copied} indicating if a copy has
+ * just successfully happened, and a function {@link onCopy} to trigger the
+ * copy.
+ */
+export const useClipboardCopy = (
+ text: string,
+): [copied: boolean, onCopy: () => void] => {
+ const [copied, setCopied] = useState(false);
+
+ const handleCopyLink = useCallback(() => {
+ void navigator.clipboard.writeText(text).then(() => {
+ setCopied(true);
+ setTimeout(() => setCopied(false), 1000);
+ });
+ }, [text]);
+
+ return [copied, handleCopyLink];
+};