url params

main
jerl 2024-06-16 02:02:15 -05:00
parent 62816d255f
commit 043a68874b
2 changed files with 46 additions and 20 deletions

View File

@ -126,7 +126,7 @@ function instanceClicked(event) {
document.getElementById('instancename').textContent = instance.url;
let userlist = document.getElementById('userlist');
userlist.innerHTML = '';
instance.emit('list_users', {}, msg =>
instance.emit?.('list_users', {}, msg =>
userlist.replaceChildren(...msg.users.map(user => {
let p = document.createElement('p');
p.append(window.makeUser(user, instance.url));

View File

@ -17,6 +17,7 @@ function setVisibility() {
function chooseThread() {
const edit = document.getElementById('edit');
let thread = this.thread;
let url = new URL(location);
if (window.currentThread) {
window.currentThread.div.classList.remove('active');
let editform = document.getElementById('editthread');
@ -24,6 +25,10 @@ function chooseThread() {
editform.remove();
edit.textContent = 'edit';
}
url.searchParams.delete('tab');
if (window.currentThread.id === thread.id)
url.searchParams.delete('thread');
window.history.pushState(null, '', url.toString());
if (window.currentThread.id === thread.id) {
document.getElementById('thread').classList.add('hidden');
window.currentThread = null;
@ -40,36 +45,25 @@ function chooseThread() {
this.classList.add('active');
window.currentThread = thread;
window.currentInstance = thread.instance;
if (this.tab) {
let tab = url.searchParams.get('tab');
if (['message', 'space', 'stream'].indexOf(tab) === -1)
tab = null;
if (tab || this.tab) {
if (tab)
this.tab = tab + 'tab';
switchTab(document.getElementById(this.tab));
if (this.tab === 'messagetab')
loadMessages();
else if (this.tab === 'spacetab')
loadSpace();
}
else // load first tab that has any content
loadMessages(messages => {
if (messages.length)
switchTab(document.getElementById(this.tab = 'messagetab'));
else
loadSpace(spans => {
if (spans.length)
switchTab(document.getElementById(this.tab = 'spacetab'));
else if (window.currentThread.streams.length)
switchTab(document.getElementById(this.tab = 'streamtab'));
else
switchTab(document.getElementById(
this.tab = spans.length ? 'spacetab' :
window.currentThread.streams.length ? 'streamtab' : 'messagetab'));
});
});
let div = this;
window.currentInstance.emit('get_thread', { thread: thread.id }, async msg => {
if (!msg.success) {
console.log('get_thread failed:', msg.message);
return;
}
Object.assign(thread, msg.thread);
loadStreams();
setVisibility();
document.getElementById('memberlist').replaceChildren(
...await Promise.all(msg.thread.members.map(async member => {
@ -79,6 +73,31 @@ function chooseThread() {
return p;
}))
);
loadStreams();
if (!div.tab)
await new Promise(resolve => {
// load first tab that has any content
loadMessages(messages => {
if (messages.length) {
switchTab(document.getElementById(div.tab = 'messagetab'));
resolve();
} else
loadSpace(spans => {
if (spans.length)
switchTab(document.getElementById(div.tab = 'spacetab'));
else if (window.currentThread.streams.length)
switchTab(document.getElementById(div.tab = 'streamtab'));
else
switchTab(document.getElementById(
div.tab = spans.length ? 'spacetab' :
window.currentThread.streams.length ? 'streamtab' : 'messagetab'));
resolve();
});
});
});
url.searchParams.set('thread', thread.id);
url.searchParams.set('tab', div.tab.substring(0, div.tab.length - 3));
window.history.pushState(null, '', url.toString());
});
}
@ -282,6 +301,9 @@ function clickedTab(event) {
loadMessages();
else if (this.id === 'spacetab')
loadSpace();
let url = new URL(location);
url.searchParams.set('tab', this.id.substring(0, this.id.length - 3));
window.history.pushState(null, '', url.toString());
}
async function loadThreads(instancediv, select) {
@ -339,7 +361,11 @@ async function loadThreads(instancediv, select) {
instancediv.instance.emit('list_threads', {}, msg => {
threadlist.replaceChildren(...msg.threads.map(makeThread));
if (select && msg.threads.length)
let thread = msg.threads.find(thread =>
thread.id == (new URLSearchParams(location.search)).get('thread'))?.div;
if (!window.currentThread && thread)
chooseThread.call(thread);
else if (select && msg.threads.length)
chooseThread.call(threadlist.firstChild);
});