Skip to content

Commit

Permalink
copy block cid (#255)
Browse files Browse the repository at this point in the history
  • Loading branch information
LexLuthr authored Oct 9, 2024
1 parent 9243d8a commit defbf78
Showing 1 changed file with 74 additions and 2 deletions.
76 changes: 74 additions & 2 deletions web/static/win-stats.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,71 @@ class WinStats extends LitElement {
this.requestUpdate();
}

async copyToClipboard(block) {
if (navigator.clipboard && navigator.clipboard.writeText) {
try {
await navigator.clipboard.writeText(block);
// Show notification
this.showNotification('Block copied to clipboard');
} catch (err) {
console.error('Failed to copy using Clipboard API', err);
this.fallbackCopyTextToClipboard(block);
}
} else {
// Fallback method
this.fallbackCopyTextToClipboard(block);
}
}

fallbackCopyTextToClipboard(text) {
const textArea = document.createElement('textarea');
textArea.value = text;

// Avoid scrolling to bottom
textArea.style.position = 'fixed';
textArea.style.top = '0';
textArea.style.left = '0';
textArea.style.width = '1px';
textArea.style.height = '1px';
textArea.style.opacity = '0';

document.body.appendChild(textArea);
textArea.focus();
textArea.select();

try {
const successful = document.execCommand('copy');
if (successful) {
this.showNotification('Block copied to clipboard');
} else {
this.showNotification('Failed to copy block');
}
} catch (err) {
console.error('Fallback: Oops, unable to copy', err);
this.showNotification('Failed to copy block');
}

document.body.removeChild(textArea);
}

showNotification(message) {
// Simple notification logic; you might want to customize this
const notification = document.createElement('div');
notification.textContent = message;
notification.style.position = 'fixed';
notification.style.bottom = '20px';
notification.style.right = '20px';
notification.style.background = 'rgba(0,0,0,0.7)';
notification.style.color = 'white';
notification.style.padding = '10px';
notification.style.borderRadius = '5px';
notification.style.zIndex = '1000';
document.body.appendChild(notification);
setTimeout(() => {
document.body.removeChild(notification);
}, 2000);
}

render() {
return html`
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
Expand All @@ -35,7 +100,14 @@ class WinStats extends LitElement {
<tr>
<td>f0${entry.Actor}</td>
<td>${entry.Epoch}</td>
<td><abbr title="${entry.Block}">...${entry.Block.slice(-10)}</abbr></td>
<td>
<span
@click=${() => this.copyToClipboard(entry.Block)}
style="cursor:pointer; text-decoration:underline;"
title="${entry.Block}">
...${entry.Block.slice(-10)}
</span>
</td>
<td>${entry.TaskSuccess}</td>
<td>${entry.SubmittedAtStr}</td>
<td>${entry.ComputeTime}</td>
Expand All @@ -48,4 +120,4 @@ class WinStats extends LitElement {
}
}

customElements.define('win-stats', WinStats);
customElements.define('win-stats', WinStats);

0 comments on commit defbf78

Please sign in to comment.