import './vault-chip.js';
export class DkaKeyCard extends HTMLElement {
static get observedAttributes() {
return ['name', 'key-id', 'service', 'rkey', 'uri', 'created-at'];
}
connectedCallback() {
this._editing = false;
this._showingQR = false;
this._qrLoaded = false;
this._qrImg = null;
this.render();
}
attributeChangedCallback() {
if (this.isConnected) this.render();
}
get data() {
return {
name: this.getAttribute('name') || 'unnamed',
keyId: this.getAttribute('key-id') || '',
service: this.getAttribute('service') || '*',
rkey: this.getAttribute('rkey') || '',
uri: this.getAttribute('uri') || '',
createdAt: this.getAttribute('created-at') || '',
};
}
render() {
const d = this.data;
const truncated = d.keyId.length > 28 ? d.keyId.slice(0, 28) + '…' + d.keyId.slice(-5) : d.keyId;
const dateStr = d.createdAt ? `Created: ${new Date(d.createdAt).toLocaleDateString()}` : '';
this.innerHTML = `
${this._editing
? `
`
: `${this._escape(d.name)}
`
}
${dateStr ? `
${dateStr}` : ''}
${this._escape(truncated)}
⧉ copy
${this._editing ? '' : 'Rename'}
Share QR
pdsls
Delete
`;
this._wire();
}
_wire() {
if (this._editing) {
this.querySelector('#save-btn').addEventListener('click', () => {
const input = this.querySelector('#edit-input');
const newName = input.value.trim() || 'unnamed';
this.dispatchEvent(new CustomEvent('dka:rename', {
bubbles: true,
detail: { rkey: this.data.rkey, name: newName },
}));
this._editing = false;
this.setAttribute('name', newName);
this.render();
});
this.querySelector('#cancel-btn').addEventListener('click', () => {
this._editing = false;
this.render();
});
const input = this.querySelector('#edit-input');
input.focus();
input.addEventListener('keydown', (e) => {
if (e.key === 'Enter') this.querySelector('#save-btn').click();
if (e.key === 'Escape') this.querySelector('#cancel-btn').click();
});
} else {
this.querySelector('#rename-btn').addEventListener('click', () => {
this._editing = true;
this.render();
});
}
this.querySelector('#copy-btn').addEventListener('click', () => {
navigator.clipboard.writeText(this.data.keyId).catch(() => {});
const btn = this.querySelector('#copy-btn');
btn.textContent = 'copied!';
setTimeout(() => { btn.textContent = '⧉ copy'; }, 1500);
});
this.querySelector('#qr-btn').addEventListener('click', async () => {
if (!this._showingQR) {
this._showingQR = true;
this.render();
const container = this.querySelector('#qr-inline');
if (!this._qrLoaded) {
const { generateQrImg } = await import('../main.js');
container.appendChild(await generateQrImg(this.data.keyId));
this._qrLoaded = true;
}
} else {
this._showingQR = false;
this.render();
}
});
this.querySelector('#delete-btn').addEventListener('click', () => {
if (!confirm(`Delete association for key "${this.data.name}"?`)) return;
this.dispatchEvent(new CustomEvent('dka:delete', {
bubbles: true,
detail: { rkey: this.data.rkey },
}));
});
}
_escape(s) {
const el = document.createElement('span');
el.textContent = s;
return el.innerHTML;
}
}
if (!customElements.get('dka-key-card')) {
customElements.define('dka-key-card', DkaKeyCard);
}