This repository has been archived by the owner on Jan 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 398
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #221 from openchatai/falta/widget-enhancments-2
Falta/widget enhancments 2
- Loading branch information
Showing
10 changed files
with
167 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { useState, useEffect } from "react"; | ||
|
||
type CopyFn = (text: any) => Promise<boolean>; | ||
|
||
export function useCopyToClipboard(): [boolean, CopyFn] { | ||
const [copied, setCopied] = useState(false); | ||
const copy: CopyFn = async (text) => { | ||
if (!navigator?.clipboard) { | ||
console.warn("Clipboard not supported"); | ||
return false; | ||
} | ||
|
||
// Try to save to clipboard then update the state if it worked | ||
try { | ||
await navigator.clipboard.writeText(text); | ||
setCopied(true); | ||
return true; | ||
} catch (error) { | ||
console.warn("Copy failed", error); | ||
setCopied(false); | ||
return false; | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
if (copied) { | ||
const timeout = setTimeout(() => { | ||
setCopied(false); | ||
}, 5000); | ||
|
||
return () => clearTimeout(timeout); | ||
} | ||
}, [copied]); | ||
|
||
return [copied, copy]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.