import { Event } from 'nostr-tools';
import {
fetchCachedProfileEvent, fetchCachedProfileEventHistory, hadLatest, isUptodate,
} from './fetchEvents';
import LoadContactsPage from './LoadContactsPage';
import { LoadMetadataPage, MetadataFlex } from './LoadMetadataPage';
import LoadRelaysPage from './LoadRelaysPage';
export const generateLogoHero = () => (
'
'
);
const injectLoading = (loading:boolean = true) => `${loading ? 'aria-busy="true"' : ''}`;
const generateMetadataSummary = (e:Event | null, loading = false) => {
if (e == null) {
return `
`;
}
return `
`;
};
const generateContactsSummary = (e:Event | null, loading = false) => {
if (e == null) {
return ``;
}
return `
`;
};
const generateRelaysSummary = (e:Event | null, loading = false) => {
if (e == null) {
return `
`;
}
const read = e.tags.filter((t) => typeof t[2] === 'undefined' || t[2] === 'read').length;
const write = e.tags.filter((t) => typeof t[2] === 'undefined' || t[2] === 'write').length;
return ``;
};
const generateMetadataHeader = (e:Event) => {
const c = JSON.parse(e.content) as MetadataFlex;
// remove new lines from about
let about = c.about ? c.about.replace(/\r?\n|\r/, '') : '';
if (about.length > 50) about = `${about.substring(0, 47)}...`;
return `
${c.name ? c.name : ''} ${c.nip05 ? c.nip05 : ''}
${about}
`;
};
export const generateBackupHeroHeading = (
uptodate:boolean,
noprofileinfo:boolean,
hadlatest:boolean,
) => {
let content = '';
if (!uptodate) {
if (noprofileinfo) {
content = `
Finding Profile...
It's your first time here and we are backing up your metadata, contacts and relays to your offline browser data.
`;
} else {
content = `
Finding Latest Profile...
We backing up your latest metadata, contacts and relays to your offline browser data.
`;
}
} else if (noprofileinfo) {
content = `
No Profile Events Found
We didn't find any profile info for you. Either wedidn't look on the right relays or you have just created a key pair.
Only proceed if you are setting your profile up for the first time.
`;
} else if (hadlatest) {
content = `
Backup is up to date!
We already had backed up your profile to your offline browser data.
Download for safe keeping.
If your profile ever gets wiped by a nostr client, come back here on this device to restore. Come back from time to time to update your backup.
`;
} else {
content = `
Profile Backup Up!
We just backed up your latest profile to your offline browser data.
Download for safe keeping.
If your profile ever gets wiped by a nostr client, come back here on this device to restore. Come back from time to time to update your backup.
`;
}
return `${content}
{
const noprofileinfo = !fetchCachedProfileEvent(0) && !fetchCachedProfileEvent(3);
const uptodate = isUptodate();
const hadlatest = hadLatest();
const o:HTMLElement = document.getElementById('PM-container') as HTMLElement;
o.innerHTML = `
${noprofileinfo ? generateLogoHero() : `
${generateMetadataHeader(fetchCachedProfileEvent(0) as Event)}
${generateMetadataSummary(fetchCachedProfileEvent(0), !uptodate)}
${generateContactsSummary(fetchCachedProfileEvent(3), !uptodate)}
${generateRelaysSummary(fetchCachedProfileEvent(10002), !uptodate)}
`}
${generateBackupHeroHeading(uptodate, noprofileinfo, hadlatest)}
`;
const mbutton = document.getElementById('metadatabutton');
if (mbutton) mbutton.onclick = () => LoadMetadataPage();
const cbutton = document.getElementById('contactsbutton');
if (cbutton) cbutton.onclick = () => LoadContactsPage();
const rbutton = document.getElementById('relaysbutton');
if (rbutton) rbutton.onclick = () => LoadRelaysPage();
// enable download link
const donwloada = document.getElementById('downloadprofile');
if (donwloada) {
donwloada.onclick = (event) => {
event.preventDefault();
const jsonStr = JSON.stringify([
...(fetchCachedProfileEventHistory(0) || []),
...(fetchCachedProfileEventHistory(2) || []),
...(fetchCachedProfileEventHistory(10002) || []),
...(fetchCachedProfileEventHistory(3) || []),
]);
const element = document.createElement('a');
element.setAttribute('href', `data:text/plain;charset=utf-8,${encodeURIComponent(jsonStr)}`);
element.setAttribute('download', `my-nostr-profile-events-${Date.now()}.json`);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
};
}
};