Merge pull request #268 from rowboatlabs/dev

Fix copy URL bug
This commit is contained in:
arkml 2025-09-17 00:35:42 +05:30 committed by GitHub
commit b2c809b1bb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -11,7 +11,7 @@ import { Button } from "@/components/ui/button";
import { ChevronLeftIcon, ChevronRightIcon } from "lucide-react"; import { ChevronLeftIcon, ChevronRightIcon } from "lucide-react";
import { TextareaWithSend } from "@/app/components/ui/textarea-with-send"; import { TextareaWithSend } from "@/app/components/ui/textarea-with-send";
import { Workflow } from '../../lib/types/workflow_types'; import { Workflow } from '../../lib/types/workflow_types';
import { loadSharedWorkflow } from '@/app/actions/shared-workflow.actions'; import { loadSharedWorkflow, createSharedWorkflowFromJson } from '@/app/actions/shared-workflow.actions';
import { PictureImg } from '@/components/ui/picture-img'; import { PictureImg } from '@/components/ui/picture-img';
import { Tabs, Tab } from "@/components/ui/tabs"; import { Tabs, Tab } from "@/components/ui/tabs";
import { Project } from "@/src/entities/models/project"; import { Project } from "@/src/entities/models/project";
@ -220,18 +220,43 @@ export function BuildAssistantSection() {
// Handle template share (for both library and community) // Handle template share (for both library and community)
const handleTemplateShare = async (template: any) => { const handleTemplateShare = async (template: any) => {
try { try {
// Fetch workflow for the template and create a shared snapshot // Robust copy helper: tries async clipboard first, then falls back to execCommand
const data = await getAssistantTemplate(template.id); const copyTextToClipboard = async (text: string): Promise<boolean> => {
try {
if (navigator.clipboard && window.isSecureContext) {
await navigator.clipboard.writeText(text);
return true;
}
} catch (_e) {
// fall through to fallback
}
try {
const textarea = document.createElement('textarea');
textarea.value = text;
textarea.setAttribute('readonly', '');
textarea.style.position = 'fixed';
textarea.style.opacity = '0';
textarea.style.left = '-9999px';
document.body.appendChild(textarea);
textarea.focus();
textarea.select();
const successful = document.execCommand('copy');
document.body.removeChild(textarea);
return successful;
} catch (_e) {
return false;
}
};
const shareResp = await fetch('/api/shared-workflow', { // Fetch workflow for the template and create a shared snapshot via server action
method: 'POST', const data = await getAssistantTemplate(template.id);
headers: { 'content-type': 'application/json' }, const { id } = await createSharedWorkflowFromJson(JSON.stringify(data.workflow));
body: JSON.stringify({ workflow: data.workflow }), const url = `${window.location.origin}/projects?shared=${id}`;
}); const copied = await copyTextToClipboard(url);
if (!shareResp.ok) throw new Error('Failed to create shared workflow'); if (!copied) {
const shareData = await shareResp.json(); throw new Error('Clipboard write failed');
const url = `${window.location.origin}/projects?shared=${shareData.id}`; }
await navigator.clipboard.writeText(url); // Optional debug log
console.log('URL copied to clipboard'); console.log('URL copied to clipboard');
} catch (err) { } catch (err) {
console.error('Failed to copy shared URL:', err); console.error('Failed to copy shared URL:', err);