|
| 1 | +import { useState, useCallback } from "react"; |
| 2 | +import { Button, Input, message, Space, Popover, theme } from "antd"; |
| 3 | +import { CopyOutlined, CheckOutlined } from "@ant-design/icons"; |
| 4 | +import { useLocale } from "@gisce/react-formiga-components"; |
| 5 | +import { createShareOpenUrl } from "@/helpers/shareUrlHelper"; |
| 6 | +import ActionButton from "./ActionButton"; |
| 7 | +import { IconExternalLink, IconShare2 } from "@tabler/icons-react"; |
| 8 | +import { useTabs } from "@/context/TabManagerContext"; |
| 9 | +import { useActionViewContext } from "@/context/ActionViewContext"; |
| 10 | +import { ActionInfo } from "@/types"; |
| 11 | + |
| 12 | +export type ShareUrlButtonProps = { |
| 13 | + res_id?: number; |
| 14 | + searchParams?: any[]; |
| 15 | +}; |
| 16 | + |
| 17 | +export function ShareUrlButton({ res_id, searchParams }: ShareUrlButtonProps) { |
| 18 | + const { currentView } = useActionViewContext(); |
| 19 | + const initialView = { |
| 20 | + id: currentView.view_id, |
| 21 | + type: currentView.type, |
| 22 | + }; |
| 23 | + const { token } = theme.useToken(); |
| 24 | + const { t } = useLocale(); |
| 25 | + const [isCopied, setIsCopied] = useState(false); |
| 26 | + const { currentTab } = useTabs(); |
| 27 | + |
| 28 | + const copyToClipboard = useCallback( |
| 29 | + (url: string) => { |
| 30 | + try { |
| 31 | + // Create a temporary textarea element |
| 32 | + const tempInput = document.createElement("textarea"); |
| 33 | + tempInput.value = url; |
| 34 | + document.body.appendChild(tempInput); |
| 35 | + |
| 36 | + // Select the text in the textarea |
| 37 | + tempInput.select(); |
| 38 | + tempInput.setSelectionRange(0, 99999); // For mobile devices |
| 39 | + |
| 40 | + // Copy the text using execCommand |
| 41 | + |
| 42 | + const successful = document.execCommand("copy"); |
| 43 | + |
| 44 | + // Clean up the temporary element |
| 45 | + document.body.removeChild(tempInput); |
| 46 | + |
| 47 | + // Handle success or failure |
| 48 | + if (successful) { |
| 49 | + setIsCopied(true); |
| 50 | + message.success(t("urlCopiedToClipboard")); |
| 51 | + setTimeout(() => setIsCopied(false), 2000); |
| 52 | + } else { |
| 53 | + throw new Error("Copy command was unsuccessful."); |
| 54 | + } |
| 55 | + } catch (err) { |
| 56 | + console.error("Error copying to clipboard:", err); |
| 57 | + message.error(t("errorCopyingToClipboard")); |
| 58 | + } |
| 59 | + }, |
| 60 | + [setIsCopied, t], |
| 61 | + ); |
| 62 | + |
| 63 | + if (!currentTab?.action) return null; |
| 64 | + const { action_id } = currentTab?.action || {}; |
| 65 | + const finalActionData: ActionInfo = { |
| 66 | + ...currentTab.action, |
| 67 | + ...(initialView && { initialView }), |
| 68 | + ...(searchParams && { searchParams }), |
| 69 | + ...(res_id && { res_id }), |
| 70 | + }; |
| 71 | + const shareUrl = createShareOpenUrl(finalActionData); |
| 72 | + const { type } = initialView || {}; |
| 73 | + |
| 74 | + let moreDataNeededForCopying = !action_id; |
| 75 | + if (type === "form") { |
| 76 | + moreDataNeededForCopying = !action_id || !res_id; |
| 77 | + } |
| 78 | + |
| 79 | + const popoverContent = ( |
| 80 | + <div style={{ padding: 2 }}> |
| 81 | + <Space.Compact style={{ width: "100%" }}> |
| 82 | + <Input |
| 83 | + value={shareUrl} |
| 84 | + readOnly |
| 85 | + style={{ |
| 86 | + borderRadius: 6, |
| 87 | + flex: 1, |
| 88 | + marginRight: 8, |
| 89 | + minWidth: 300, |
| 90 | + }} |
| 91 | + /> |
| 92 | + <Button |
| 93 | + title={t("copyToClipboard")} |
| 94 | + type="text" |
| 95 | + style={{ |
| 96 | + marginRight: 8, |
| 97 | + }} |
| 98 | + icon={ |
| 99 | + isCopied ? ( |
| 100 | + <CheckOutlined style={{ color: token.colorSuccess }} /> |
| 101 | + ) : ( |
| 102 | + <CopyOutlined style={{ color: token.colorTextSecondary }} /> |
| 103 | + ) |
| 104 | + } |
| 105 | + onClick={() => copyToClipboard(shareUrl)} |
| 106 | + /> |
| 107 | + <Button |
| 108 | + title={t("openInNewTab")} |
| 109 | + style={{ height: 28 }} |
| 110 | + type="text" |
| 111 | + icon={<IconExternalLink size={18} color={token.colorTextSecondary} />} |
| 112 | + onClick={() => window.open(shareUrl, "_blank", "noopener,noreferrer")} |
| 113 | + /> |
| 114 | + </Space.Compact> |
| 115 | + </div> |
| 116 | + ); |
| 117 | + |
| 118 | + return ( |
| 119 | + <div style={{ maxHeight: 28 }}> |
| 120 | + <Popover content={popoverContent} trigger="click" placement="bottom"> |
| 121 | + <ActionButton |
| 122 | + style={{ height: 28 }} |
| 123 | + icon={<IconShare2 size={16} color={token.colorTextSecondary} />} |
| 124 | + disabled={moreDataNeededForCopying} |
| 125 | + tooltip={t("share")} |
| 126 | + /> |
| 127 | + </Popover> |
| 128 | + </div> |
| 129 | + ); |
| 130 | +} |
0 commit comments