Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/app/src/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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=>({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[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?'<ul data-job-runs-list class="divide-y divide-stone-200 dark:divide-slate-800">'+r.items.map(x=>'<li><a class="flex items-center justify-between gap-4 p-5 hover:bg-stone-50 dark:hover:bg-slate-800" href="/job-runs/'+encodeURIComponent(x.id)+'"><span><span class="font-medium">'+esc(x.provider||x.agent_kind||'Factorize')+'</span><span class="mt-1 block text-xs text-slate-500">'+new Date(x.created_at).toLocaleString()+(activeRunStates.includes(x.state)?' · <span data-run-started="'+esc(x.created_at)+'">'+elapsedSince(x.created_at)+'</span>':'')+'</span></span>'+stateBadge(x.state)+'</a></li>').join('')+'</ul>':'<p data-job-runs-list class="p-8 text-sm text-slate-500">No runs yet.</p>';
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'<nav data-job-runs-pagination class="flex items-center justify-between gap-3 border-b border-stone-200 px-6 py-3 text-sm dark:border-slate-800" aria-label="Runs pagination"><button type="button" data-run-page="previous"'+previous+' class="rounded-lg border border-stone-300 px-3 py-2 font-semibold disabled:cursor-not-allowed disabled:opacity-50 dark:border-slate-700">Previous</button><span class="text-slate-500">Page '+page+'</span><button type="button" data-run-page="next"'+next+' class="rounded-lg border border-stone-300 px-3 py-2 font-semibold disabled:cursor-not-allowed disabled:opacity-50 dark:border-slate-700">Next</button></nav>'};
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)]}));
Expand Down
3 changes: 3 additions & 0 deletions packages/app/test/ui.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand Down