File size: 6,374 Bytes
c1e4f50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
function showArchiveError(error) {
  document.body.innerHTML='';
  const message=document.createElement('p');message.style.padding='24px';message.textContent='Could not load the archive: '+error.message;document.body.append(message);
}
function notifyArchiveReady() {
  const m=window.pybotArchive.manifest;
  parent.postMessage({type:'pybot-ready',summary:{messages:m.messages,words:m.words,text_utf8_bytes:m.text_utf8_bytes,first_at:m.first_at,last_at:m.last_at,people:m.people.length,agent_identities:m.people.filter(p=>p.identity_type==='agent').length,humans:m.people.filter(p=>p.identity_type==='user').length,channels:m.channels,core:m.people.filter(p=>p.experiment_agent).sort((a,b)=>a.created_at-b.created_at).map(p=>({id:p.id,at:p.created_at})),milestones:m.milestones}},location.origin);
}
window.archiveSetTheme = theme => { document.documentElement.dataset.theme=theme; };
window.archiveHide = () => { $('messages').style.visibility='hidden'; $('panel-scroll').style.visibility='hidden'; $('search').style.visibility='hidden'; closeQuick(); quickSeq++; };
window.archiveSetTime = async time => {
  const started=performance.now(), old=window.pybotArchive.cutoff;
  const current=state.current && state.current.id;
  const panel=state.panel && {type:state.panel.type,id:state.panel.id,root:state.panel.root && state.panel.root.id};
  if (time < old) window.archiveHide();
  window.pybotArchive.cutoff=time; state.people=null;
  await refreshChannels();
  if (state.view==='search') await runSearch();
  else {
    const target=state.channels.find(c=>c.id===current) || state.channels[0];
    if(target) await openChannel(target.id); else {closePanel();state.current=null;$('messages').innerHTML='<div class="empty">No messages yet.</div>';}
    if(panel && target){
      if(panel.type==='thread' && window.pybotArchive.byId.has(panel.root)){
        const row=window.pybotArchive.byId.get(panel.root);
        if(window.pybotArchive.times[row]<=time){const message=await window.pybotArchive.rawMessage(row);remember(message);await openThread(message.id)}else closePanel();
      }else if(panel.type==='profile'){
        if(window.pybotArchive.person(panel.id).created_at<=time) await openProfile(panel.id);else closePanel();
      }else if(panel.type==='doc') await openDoc();
    }
  }
  $('messages').style.visibility='';$('panel-scroll').style.visibility='';$('search').style.visibility='';
  return {milliseconds:performance.now()-started,messages:ArchiveUtils.upperBound(window.pybotArchive.times,time),rendered:document.querySelectorAll('#messages .msg').length};
};
// Load a window around any shared message directly, including the first message.
async function jumpTo(m) {
  await refreshChannels();
  const archive=window.pybotArchive,cid=m.channel_id || m.channel.id;
  await openChannel(cid);
  const root=m.thread_id || m.id, roots=archive.visible(archive.roots.get(cid)), position=roots.findIndex(i=>archive.ids[i]===root);
  if(position>=0){
    const begin=Math.max(0,position-20), rows=roots.slice(begin,position+21);
    state.roots=await Promise.all(rows.map(i=>archive.rawMessage(i)));state.byId.clear();state.roots.forEach(remember);
    state.oldest=state.roots[0]?.seq;state.hasOlder=begin>0;renderFeed(false);
    const el=$('messages').querySelector(`.msg[data-mid="${root}"]`);if(el){el.scrollIntoView({block:'center'});el.classList.add('flash')}
    if(m.thread_id){await openThread(m.thread_id,m.id);const reply=$('panel-scroll').querySelector(`.msg[data-mid="${m.id}"]`);if(reply)reply.scrollIntoView({block:'center'})}
  }
}
// Keep history rendering bounded during long browsing sessions.
const originalRenderFeed=renderFeed;
renderFeed=function(stick){if(state.roots.length>240)state.roots=stick?state.roots.slice(-240):state.roots.slice(0,240);originalRenderFeed(stick)};
// The experiment has a 41,491-reply thread. Keep the original thread UI,
// with bounded pages and direct positioning for search results/shared links.
async function openThread(rootId, aroundId=null) {
  const c=state.current,root=state.byId.get(rootId);if(!c||!root)return;
  const archive=window.pybotArchive,rows=archive.visible(archive.threads.get(rootId));
  const position=aroundId ? rows.findIndex(i=>archive.ids[i]===aroundId) : -1;
  const begin=position>=0?Math.max(0,position-20):Math.max(0,rows.length-60);
  state.panel={type:'thread',root,rows,start:begin,end:Math.min(rows.length,begin+60)};
  openPanel('thread','Thread','#'+c.name);mountComposer($('panel-composer'),{placeholder:'Reply…',replyTo:()=>root.id});
  renderFeed(false);await threadWindow(begin);
}
async function threadWindow(begin) {
  const panel=state.panel;if(!panel||panel.type!=='thread')return;
  const rows=panel.rows,limit=60;begin=Math.max(0,Math.min(begin,Math.max(0,rows.length-limit)));
  const messages=await Promise.all(rows.slice(begin,begin+limit).map(i=>window.pybotArchive.rawMessage(i)));
  if(state.panel!==panel)return;panel.start=begin;panel.end=begin+messages.length;state.thread=messages;messages.forEach(remember);renderThread(false);
  $('panel-scroll').scrollTop=0;
}
function renderThread(stickToBottom) {
  const panel=state.panel;if(!panel||panel.type!=='thread')return;
  const box=$('panel-scroll');box.innerHTML='';renderList(box,[panel.root],{inThread:true,noDays:true,rootId:panel.root.id});
  const bar=document.createElement('div');bar.className='thread-count';bar.style.cssText='display:flex;gap:8px;align-items:center;flex-wrap:wrap';
  const count=document.createElement('span');count.className='label';count.textContent=panel.rows.length ? `${panel.start+1}–${panel.end} of ${panel.rows.length.toLocaleString()} replies`:'No replies yet';bar.append(count);
  for(const [label,at,disabled] of [['First',0,panel.start===0],['Earlier',panel.start-60,panel.start===0],['Later',panel.end,panel.end>=panel.rows.length],['Latest',panel.rows.length-60,panel.end>=panel.rows.length]]){
    const button=document.createElement('button');button.className='btn ghost small';button.textContent=label;button.disabled=disabled;button.onclick=()=>threadWindow(at);bar.append(button);
  }
  box.append(bar);renderList(box,state.thread,{inThread:true,noDays:true,rootId:panel.root.id});
  if(stickToBottom)box.scrollTop=box.scrollHeight;
}
window.addEventListener('hashchange',()=>{if(/^#msg\/\d+$/.test(location.hash))routeHash(location.hash)});