diff --git a/packages/app/src/ui.ts b/packages/app/src/ui.ts index 9e2f9cc8..23b73cdf 100644 --- a/packages/app/src/ui.ts +++ b/packages/app/src/ui.ts @@ -49,11 +49,11 @@ const card = "rounded-2xl border border-stone-200 bg-white shadow-sm dark:border const uiHelpers = ` const $=s=>document.querySelector(s),esc=v=>String(v??'').replace(/[&<>"']/g,c=>({'&':'&','<':'<','>':'>','"':'"',"'":'''}[c])); ${ansiToHtmlBrowserSource} -const request=async(url,options)=>{const requestUrl=String(url).startsWith('/api/v1/runs?jobId=')&&!String(url).includes('limit=')?String(url)+'&limit=20':url,r=await fetch(requestUrl,options),body=await r.json().catch(()=>({}));if(r.status===401){location.href='/auth/linear';throw new Error('Unauthorized')}if(!r.ok)throw new Error(body.error?.message||body.error||'Request failed');if(String(requestUrl).startsWith('/api/v1/runs?jobId=')){window.__factorizeLastRuns=body}return body}; -const jobRunsPageSize=20,jobRunsState={page:1,cursors:[null],runs:null}; +const request=async(url,options)=>{const requestUrl=String(url).startsWith('/api/v1/runs?jobId=')&&!String(url).includes('limit=')?String(url)+'&limit=20':url,r=await fetch(requestUrl,options),body=await r.json().catch(()=>({}));if(r.status===401){location.href='/auth/linear';throw new Error('Unauthorized')}if(!r.ok)throw new Error(body.error?.message||body.error||'Request failed');if(String(requestUrl).startsWith('/api/v1/runs?jobId='))window.__factorizeLastRuns=body;return body}; +const jobRunsPageSize=20,jobRunsState={page:1,cursors:[null],runs:null,loading:false}; const jobRunsMarkup=r=>r.items?.length?'
No runs yet.
'; const jobRunsControls=(r,page)=>{if(page===1&&!r.nextCursor)return'';const previous=page>1?'':' disabled aria-disabled="true"',next=r.nextCursor?'':' disabled aria-disabled="true"';return''}; -const refreshJobRunsPagination=()=>{const section=document.querySelector('#job-detail section:last-of-type'),r=jobRunsState.runs;if(!section||!r)return;section.querySelectorAll('[data-job-runs-pagination]').forEach(x=>x.remove());const list=section.querySelector('[data-job-runs-list]');if(list)list.outerHTML=jobRunsMarkup(r);const header=section.firstElementChild;header?.insertAdjacentHTML('afterend',jobRunsControls(r,jobRunsState.page));section.insertAdjacentHTML('beforeend',jobRunsControls(r,jobRunsState.page));section.querySelectorAll('[data-run-page]').forEach(button=>button.addEventListener('click',async()=>{const direction=button.dataset.runPage;if(direction==='next'&&r.nextCursor){jobRunsState.cursors[jobRunsState.page]=r.nextCursor;jobRunsState.page++}else if(direction==='previous'&&jobRunsState.page>1){jobRunsState.page--}else return;const query='/api/v1/runs?jobId='+encodeURIComponent(document.querySelector('#job-detail').dataset.id)+'&limit='+jobRunsPageSize+(jobRunsState.cursors[jobRunsState.page]?'&cursor='+encodeURIComponent(jobRunsState.cursors[jobRunsState.page]):'');jobRunsState.runs=await request(query);refreshJobRunsPagination()}))}; +const refreshJobRunsPagination=()=>{const section=document.querySelector('[data-job-runs-section]')||document.querySelector('#job-detail section:last-of-type'),r=jobRunsState.runs;if(!section||!r)return;section.querySelectorAll('[data-job-runs-pagination]').forEach(x=>x.remove());const list=section.querySelector('[data-job-runs-list]')||section.querySelector('ul,section>p');if(list)list.outerHTML=jobRunsMarkup(r);const header=section.firstElementChild;header?.insertAdjacentHTML('afterend',jobRunsControls(r,jobRunsState.page));section.insertAdjacentHTML('beforeend',jobRunsControls(r,jobRunsState.page));section.querySelectorAll('[data-run-page]').forEach(button=>button.addEventListener('click',async()=>{if(jobRunsState.loading)return;const direction=button.dataset.runPage,currentPage=jobRunsState.page,targetPage=direction==='next'?currentPage+1:currentPage-1;if(targetPage<1||targetPage>currentPage+1)return;const cursor=targetPage===currentPage+1?r.nextCursor:jobRunsState.cursors[targetPage];if(direction==='next'&&!cursor)return;jobRunsState.loading=true;try{const query='/api/v1/runs?jobId='+encodeURIComponent(document.querySelector('#job-detail').dataset.id)+'&limit='+jobRunsPageSize+(cursor?'&cursor='+encodeURIComponent(cursor):'');const nextRuns=await request(query);jobRunsState.cursors[targetPage]=cursor;jobRunsState.page=targetPage;jobRunsState.runs=nextRuns;refreshJobRunsPagination()}finally{jobRunsState.loading=false}}))}; const jobDetailRoot=document.querySelector('#job-detail');if(jobDetailRoot)new MutationObserver(()=>{const r=window.__factorizeLastRuns;if(r&&r!==jobRunsState.runs){jobRunsState.runs=r;refreshJobRunsPagination()}}).observe(jobDetailRoot,{childList:true}); const pairs=v=>Object.entries(v||{}).map(([k,x])=>k+'='+x).join('\\n'); const record=v=>Object.fromEntries(String(v||'').split(/\\n/).map(x=>x.trim()).filter(Boolean).map(x=>{const i=x.indexOf('=');if(i<1)throw new Error('Parameters must use name=value, one per line.');return[x.slice(0,i).trim(),x.slice(i+1)]})); diff --git a/packages/app/test/ui.test.ts b/packages/app/test/ui.test.ts index 41d9c2f6..14546c23 100644 --- a/packages/app/test/ui.test.ts +++ b/packages/app/test/ui.test.ts @@ -35,6 +35,9 @@ describe("pages", () => { expect(html).toContain("data-run-page=\"previous\""); expect(html).toContain("data-run-page=\"next\""); expect(html).toContain("&limit='+jobRunsPageSize"); + expect(html).toContain("jobRunsState.cursors[targetPage]=cursor"); + expect(html).toContain("const cursor=targetPage===currentPage+1?r.nextCursor:jobRunsState.cursors[targetPage]"); + expect(html).not.toContain("jobRunsState.cursors[jobRunsState.page]=r.nextCursor"); expectInlineScriptsToParse(html); });