Skip to content

Commit

Permalink
fix: improve ShareUrlButton functionality and design
Browse files Browse the repository at this point in the history
gisce/webclient#1612
- Refactored the copyToClipboard function to use a temporary textarea for better compatibility across browsers and avoid SSL restrictions
- Updated the ShareUrlButton component to include new icons and improved styling.
- Added new translations for "Copy to clipboard" in ca_ES, en_US, and es_ES locale files.
- Adjusted the popover content padding for a cleaner UI.
  • Loading branch information
mguellsegarra committed Jan 14, 2025
1 parent 86aec55 commit dcf74f1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 31 deletions.
79 changes: 48 additions & 31 deletions src/actionbar/ShareUrlButton.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import { useState, useCallback } from "react";
import { Button, Input, message, Space, Popover } from "antd";
import {
ShareAltOutlined,
CopyOutlined,
CheckOutlined,
LinkOutlined,
} from "@ant-design/icons";
import { Button, Input, message, Space, Popover, theme } from "antd";
import { CopyOutlined, CheckOutlined } from "@ant-design/icons";
import { useLocale } from "@gisce/react-formiga-components";
import { createShareOpenUrl } from "@/helpers/shareUrlHelper";
import { ViewType } from "@/types";
import ActionButton from "./ActionButton";
import { IconExternalLink, IconShare2 } from "@tabler/icons-react";

export type ShareUrlButtonProps = {
action_id?: number;
Expand All @@ -22,6 +18,7 @@ export function ShareUrlButton({
view_type,
res_id,
}: ShareUrlButtonProps) {
const { token } = theme.useToken();
const { t } = useLocale();
const [isCopied, setIsCopied] = useState(false);

Expand All @@ -38,20 +35,39 @@ export function ShareUrlButton({
moreDataNeededForCopying = !action_id || !res_id;
}

const copyToClipboard = useCallback(async () => {
const copyToClipboard = useCallback(() => {
try {
await navigator.clipboard.writeText(shareUrl);
setIsCopied(true);
message.success(t("urlCopiedToClipboard"));
setTimeout(() => setIsCopied(false), 2000);
// Create a temporary textarea element
const tempInput = document.createElement("textarea");
tempInput.value = shareUrl;
document.body.appendChild(tempInput);

// Select the text in the textarea
tempInput.select();
tempInput.setSelectionRange(0, 99999); // For mobile devices

// Copy the text using execCommand
const successful = document.execCommand("copy");

// Clean up the temporary element
document.body.removeChild(tempInput);

// Handle success or failure
if (successful) {
setIsCopied(true);
message.success(t("urlCopiedToClipboard"));
setTimeout(() => setIsCopied(false), 2000);
} else {
throw new Error("Copy command was unsuccessful.");
}
} catch (err) {
console.error("Error copying to clipboard:", err);
message.error(t("errorCopyingToClipboard"));
}
}, [shareUrl, setIsCopied, t]);

const popoverContent = (
<div style={{ padding: 8 }}>
<div style={{ padding: 2 }}>
<Space.Compact style={{ width: "100%" }}>
<Input
value={shareUrl}
Expand All @@ -63,25 +79,26 @@ export function ShareUrlButton({
minWidth: 300,
}}
/>
{isSecureContext && (
<Button
type="text"
style={{
marginRight: 8,
}}
icon={
isCopied ? (
<CheckOutlined style={{ color: "#52c41a" }} />
) : (
<CopyOutlined />
)
}
onClick={copyToClipboard}
/>
)}
<Button
title={t("copyToClipboard")}
type="text"
style={{
marginRight: 8,
}}
icon={
isCopied ? (
<CheckOutlined style={{ color: token.colorSuccess }} />
) : (
<CopyOutlined style={{ color: token.colorTextSecondary }} />
)
}
onClick={copyToClipboard}
/>
<Button
title={t("openInNewTab")}
style={{ height: 28 }}
type="text"
icon={<LinkOutlined />}
icon={<IconExternalLink size={18} color={token.colorTextSecondary} />}
onClick={() => window.open(shareUrl, "_blank", "noopener,noreferrer")}
/>
</Space.Compact>
Expand All @@ -91,7 +108,7 @@ export function ShareUrlButton({
return (
<Popover content={popoverContent} trigger="click" placement="bottom">
<ActionButton
icon={<ShareAltOutlined />}
icon={<IconShare2 size={16} color={token.colorTextSecondary} />}
disabled={moreDataNeededForCopying}
tooltip={t("share")}
/>
Expand Down
1 change: 1 addition & 0 deletions src/locales/ca_ES.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,6 @@ export default {
loading: "Carregant...",
pendingToCalculate: "Pendent de calcular",
share: "Compartir URL",
copyToClipboard: "Copiar al porta-retalls",
urlCopiedToClipboard: "URL copiada al porta-retalls",
};
1 change: 1 addition & 0 deletions src/locales/en_US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,5 @@ export default {
pendingToCalculate: "Pending to calculate",
share: "Compartir URL",
urlCopiedToClipboard: "URL copied to clipboard",
copyToClipboard: "Copy to clipboard",
};
1 change: 1 addition & 0 deletions src/locales/es_ES.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,5 @@ export default {
pendingToCalculate: "Pendiente de calcular",
share: "Compartir URL",
urlCopiedToClipboard: "URL copiada al portapapeles",
copyToClipboard: "Copiar al portapapeles",
};

0 comments on commit dcf74f1

Please sign in to comment.