fix admin copy text functions

This commit is contained in:
Chris Lu
2026-01-08 18:44:36 -08:00
parent abfa64456b
commit 6bf0c16862
3 changed files with 75 additions and 25 deletions

View File

@@ -270,7 +270,7 @@ templ ServiceAccounts(data dash.ServiceAccountsData) {
<label class="form-label fw-bold">AWS_ACCESS_KEY_ID</label>
<div class="input-group">
<input type="text" class="form-control font-monospace" id="displayAccessKey" readonly>
<button class="btn btn-outline-secondary" type="button" onclick="copyToClipboard(event, 'displayAccessKey')">
<button class="btn btn-outline-secondary" type="button" onclick="copyCredentialToClipboard(this, 'displayAccessKey')">
<i class="fas fa-copy"></i> Copy
</button>
</div>
@@ -280,7 +280,7 @@ templ ServiceAccounts(data dash.ServiceAccountsData) {
<label class="form-label fw-bold">AWS_SECRET_ACCESS_KEY</label>
<div class="input-group">
<input type="text" class="form-control font-monospace" id="displaySecretKey" readonly>
<button class="btn btn-outline-secondary" type="button" onclick="copyToClipboard(event, 'displaySecretKey')">
<button class="btn btn-outline-secondary" type="button" onclick="copyCredentialToClipboard(this, 'displaySecretKey')">
<i class="fas fa-copy"></i> Copy
</button>
</div>
@@ -474,27 +474,25 @@ templ ServiceAccounts(data dash.ServiceAccountsData) {
setTimeout(() => window.location.reload(), 500);
}
function copyToClipboard(event, elementId) {
function copyCredentialToClipboard(button, elementId) {
const element = document.getElementById(elementId);
const textToCopy = element.value;
// Use modern Clipboard API if available
if (navigator.clipboard) {
navigator.clipboard.writeText(element.value).then(() => {
// Success feedback could be added here
if (navigator.clipboard && navigator.clipboard.writeText) {
navigator.clipboard.writeText(textToCopy).then(() => {
showSuccessMessage('Copied to clipboard!');
}).catch(err => {
console.warn('Clipboard API failed:', err);
// Fallback
element.select();
document.execCommand('copy');
fallbackCopyTextToClipboard(element);
});
} else {
// Fallback for older browsers
element.select();
document.execCommand('copy');
// Fallback for older browsers or non-secure contexts
fallbackCopyTextToClipboard(element);
}
// Visual feedback
const button = event.target.closest('button');
const originalHTML = button.innerHTML;
button.innerHTML = '<i class="fas fa-check"></i>';
@@ -508,6 +506,26 @@ templ ServiceAccounts(data dash.ServiceAccountsData) {
}, 1000);
}
function fallbackCopyTextToClipboard(element) {
element.select();
element.setSelectionRange(0, 99999); // For mobile devices
try {
const successful = document.execCommand('copy');
if (successful) {
showSuccessMessage('Copied to clipboard!');
} else {
showErrorMessage('Failed to copy to clipboard');
}
} catch (err) {
console.error('Fallback copy failed:', err);
showErrorMessage('Failed to copy to clipboard');
}
// Clear selection
window.getSelection().removeAllRanges();
}
function createSADetailsContent(sa) {
// Create DOM elements safely to prevent XSS

File diff suppressed because one or more lines are too long