routes: use nip19 as routes

so we need no guessing logic and know what to subscribe too. in a
later step it add more to the view i.e. show profile meta data for
npub.
pull/72/head
OFF0 2 years ago
parent b0e190fd22
commit 309367852a
Signed by: offbyn
GPG Key ID: 94A2F643C51F37FA

@ -98,6 +98,7 @@ const containers = [
]; ];
let activeContainerIndex = null; let activeContainerIndex = null;
const textNoteList = []; // could use indexDB const textNoteList = []; // could use indexDB
const eventRelayMap = {}; // eventId: [relay1, relay2] const eventRelayMap = {}; // eventId: [relay1, relay2]
@ -331,9 +332,9 @@ function createTextNote(evt, relay) {
${evt.tags.length ? `\nTags ${JSON.stringify(evt.tags)}\n` : ''} ${evt.tags.length ? `\nTags ${JSON.stringify(evt.tags)}\n` : ''}
${evt.content}` ${evt.content}`
}, [ }, [
elem('a', {className: `mbox-username${name ? ' mbox-kind0-name' : ''}`, href: `/${evt.nip19.npub}`, data: {nav: '/[profile]'}}, name || userName), elem('a', {className: `mbox-username${name ? ' mbox-kind0-name' : ''}`, href: `/${evt.nip19.npub}`, data: {nav: true}}, name || userName),
' ', ' ',
elem('a', {href: `/${evt.nip19.note}`, data: {nav: '/[note]'}}, formatTime(time)), elem('a', {href: `/${evt.nip19.note}`, data: {nav: true}}, formatTime(time)),
]), ]),
elem('div', {/* data: isLongContent ? {append: evt.content.slice(280)} : null*/}, [ elem('div', {/* data: isLongContent ? {append: evt.content.slice(280)} : null*/}, [
...content, ...content,
@ -833,68 +834,36 @@ function view(route) {
}); });
} }
function navigate(route) { // subscribe and change view
if (typeof route === 'string') { function route(path) {
view(route); unsubAll();
history.pushState({}, '', route); if (path === '/') {
return;
}
if (route.pubkey) {
view(`/${route.pubkey}`);
history.pushState(route, '', `/${route.pubkey}`);
return;
}
if (route.note) {
view(`/${route.note}`);
history.pushState(route, '', `/${route.note}`);
return;
}
if (route.pubOrNote) {
view(`/${route.pubOrNote}`);
history.pushState(route, '', `/${route.pubOrNote}`);
return;
}
console.warn('unhandeleded', route);
}
// onload
switch(location.pathname) {
case '/':
sub24hFeed(); sub24hFeed();
navigate('/'); view('/');
} else if (path.length === 64 && path.match(/^\/[0-9a-z]+$/)) {
const {type, data} = nip19.decode(path.slice(1));
switch(type) {
case 'note':
subNote(data);
view(path);
break;
case 'npub':
subProfile(data);
view(path);
break; break;
default: default:
const pubOrNote = location.pathname.slice(1); console.warn(`type ${type} not yet supported`);
if (pubOrNote.length === 64 && pubOrNote.match(/^[0-9a-f]+$/)) {
navigate({pubOrNote});
subNoteAndProfile(pubOrNote);
} }
break;
} }
}
// onload
route(location.pathname);
history.pushState({}, '', location.pathname);
window.addEventListener('popstate', (event) => { window.addEventListener('popstate', (event) => {
// console.log(`popstate: ${location.pathname}, state: ${JSON.stringify(event.state)}`); // console.log(`popstate: ${location.pathname}, state: ${JSON.stringify(event.state)}`);
unsubAll(); route(location.pathname);
if (event.state?.pubkey) {
subProfile(event.state.pubkey);
view(`/${event.state.pubkey}`);
return;
}
if (event.state?.pubOrNote) {
subNoteAndProfile(event.state.pubOrNote);
view(`/${event.state.pubOrNote}`); // assuming note
return;
}
if (event.state?.note) {
subTextNote(event.state.note);
view(`/${event.state.note}`); // assuming note
return;
}
if (location.pathname === '/') {
sub24hFeed();
view('/');
return;
}
}); });
const settingsView = document.querySelector('#settings'); const settingsView = document.querySelector('#settings');
@ -914,29 +883,8 @@ document.body.addEventListener('click', (e) => {
publishView.hidden = true; publishView.hidden = true;
} }
const href = a.getAttribute('href'); const href = a.getAttribute('href');
switch(href) { route(href);
case '/': history.pushState({}, null, href);
navigate('/');
unsubAll();
sub24hFeed();
break;
default:
switch(a.dataset.nav) {
case '/[profile]':
unsubAll();
subProfile(pubkey);
navigate({pubkey});
break;
case '/[note]':
unsubAll();
subTextNote(id)
navigate({note: id});
break;
default:
console.warn('what route is that', href);
}
break;
}
e.preventDefault(); e.preventDefault();
} }
return; return;

Loading…
Cancel
Save