Download static/js/wizard.js from ag235772/Workhub: direct link, hf CLI and curl.
- Browser
- Download file 16.1 kB
-
https://huggingface.co/spaces/ag235772/Workhub/resolve/main/static/js/wizard.js
- Command line
-
hf download hf://spaces/ag235772/Workhub/static/js/wizard.js
-
curl -L -o wizard.js https://huggingface.co/spaces/ag235772/Workhub/resolve/main/static/js/wizard.js
16.1 kB
| // Import wizard: upload old sheet -> confirm structure -> answer questions about unclear records -> save. | |
| import { $, $$, esc, api, toast, guard, table } from "./ui.js"; | |
| import { show } from "./gate.js"; | |
| import * as S from "./store.js"; | |
| import * as I from "./importer.js"; | |
| const FIELDS = [["date", "Date"], ["workType", "Type (Ticket/POC)"], ["product", "Product (WAF/WAS)"], ["ticketId", "Ticket ID"], ["pocId", "POC ID"], ["itemId", "Other ID column"], ["title", "Title / issue"], ["description", "Description / notes"]]; | |
| const STEPS = ["1 · Upload", "2 · Check structure", "3 · Review", "4 · Done"]; | |
| const steps = n => `<div class="steps">${STEPS.map((s, i) => `<span class="${i === n ? "on" : ""}">${s}</span>`).join("")}</div>`; | |
| const colName = (an, i) => (an.headers[i] || "").trim() || "Column " + (i + 1); | |
| export function openWizard({ onDone, onCancel, ai = false }) { | |
| const W = { sheets: [], an: [], use: [], recs: [], useAI: ai, ai, showGuessed: false, page: 0, flagged: [] }; | |
| upload(); | |
| // ------------------------------------------------------------ step 1 | |
| function upload() { | |
| const c = show(`<h1>Import past records</h1>${steps(0)} | |
| <p class="muted">Choose your old sheet(s): <b>.xlsx</b> or <b>.csv</b>. It can be a neat table, loose notes, or a mix. The file is read <b>in your browser</b>.</p> | |
| <input type="file" id="file" accept=".xlsx,.csv,.txt" multiple> | |
| <div id="found"></div> | |
| <div class="row"><button class="primary" id="next" disabled>Next</button><button class="ghost" id="cancel">Cancel</button></div>`, true); | |
| $("#cancel", c).onclick = () => onCancel(); | |
| $("#file", c).onchange = guard(async e => { | |
| W.sheets = []; for (const f of e.target.files) for (const sh of await I.readSheets(f)) W.sheets.push(sh); | |
| W.an = W.sheets.map(I.analyze); W.use = W.sheets.map(() => true); | |
| $("#found", c).innerHTML = W.sheets.length ? `<p><b>${W.sheets.length}</b> sheet(s) found:</p>` + W.sheets.map((s, i) => `<label style="display:block;margin:4px 0"><input type="checkbox" data-i="${i}" checked style="width:auto"> ${esc(s.name)} <span class="pill">${s.rows.length} rows</span> <span class="pill">${W.an[i].structured ? "table with headers" : "free-form notes"}</span></label>`).join("") : '<div class="err">No data found in that file.</div>'; | |
| $("#next", c).disabled = !W.sheets.length; | |
| }); | |
| $("#found", c).onchange = e => { if (e.target.dataset.i) W.use[+e.target.dataset.i] = e.target.checked; }; | |
| $("#next", c).onclick = () => { if (!W.use.some(Boolean)) return toast("Select at least one sheet", true); structure(); }; | |
| } | |
| // ------------------------------------------------------------ step 2 | |
| function structure() { | |
| const blocks = W.sheets.map((sh, i) => W.use[i] ? block(sh, W.an[i], i) : "").join(""); | |
| const c = show(`<h1>Check how I read your sheet</h1>${steps(1)} | |
| <p class="muted">I guessed which column is which. Fix anything that's wrong – you'll be asked about unclear <i>records</i> in the next step.</p>${blocks} | |
| <div class="row"><button class="primary" id="go">Read my records</button><button id="back">Back</button><button class="ghost" id="cancel">Cancel</button></div>`, true); | |
| $("#cancel", c).onclick = () => onCancel(); $("#back", c).onclick = upload; | |
| c.onchange = e => { | |
| const i = +e.target.dataset.i, an = W.an[i]; if (isNaN(i)) return; | |
| if (e.target.dataset.map) { const v = e.target.value; if (v === "") delete an.mapping[e.target.dataset.map]; else an.mapping[e.target.dataset.map] = +v; } | |
| if (e.target.dataset.order) { an.dateOrder = e.target.value; an.dateOrderCertain = true; } | |
| if (e.target.dataset.hdr) { const r = +e.target.value; if (r < 0) Object.assign(an, { structured: false, headerRow: -1, mapping: {}, headers: [] }); else { const row = W.sheets[i].rows[r] || [], m = I.mapHeaders(row); Object.assign(an, { structured: true, headerRow: r, mapping: m.mapping, headers: row.map(x => String(x ?? "").trim()) }); } structure(); } | |
| }; | |
| $("#go", c).onclick = guard(read); | |
| } | |
| function block(sh, an, i) { | |
| const hdrOpts = `<option value="-1" ${!an.structured ? "selected" : ""}>No headers – free-form notes</option>` + sh.rows.slice(0, 15).map((r, k) => `<option value="${k}" ${an.structured && an.headerRow === k ? "selected" : ""}>Row ${k + 1}: ${esc((r || []).filter(x => x !== null && x !== "").slice(0, 4).join(" | ").slice(0, 60))}</option>`).join(""); | |
| let body = `<label>Which row has the column headings? <select data-i="${i}" data-hdr="1">${hdrOpts}</select></label>`; | |
| if (an.structured) { | |
| const first = sh.rows.slice(an.headerRow + 1).find(r => (r || []).some(x => x !== null && String(x).trim() !== "")) || []; | |
| const opts = sel => `<option value="">— none —</option>` + an.headers.map((h, k) => `<option value="${k}" ${sel === k ? "selected" : ""}>${esc(colName(an, k))}</option>`).join(""); | |
| body += `<table class="maptable"><thead><tr><th>Field</th><th>Column in your sheet</th><th>Example</th></tr></thead><tbody>` + FIELDS.map(([f, label]) => `<tr><td>${label}</td><td><select data-i="${i}" data-map="${f}">${opts(an.mapping[f])}</select></td><td class="muted">${esc(String(an.mapping[f] !== undefined ? (first[an.mapping[f]] instanceof Date ? first[an.mapping[f]].toISOString().slice(0, 10) : first[an.mapping[f]] ?? "") : "").slice(0, 40))}</td></tr>`).join("") + `</tbody></table>`; | |
| } else body += `<div class="note">I'll read each non-empty row as a free-text note and pull out the date, ID, type and product. Lines that are only a date (like “5 Oct 2026”) set the date for the lines below.</div>`; | |
| body += `<label>Dates like 03/04/2026 mean <select data-i="${i}" data-order="1"><option value="dmy" ${an.dateOrder === "dmy" ? "selected" : ""}>3 April (day first)</option><option value="mdy" ${an.dateOrder === "mdy" ? "selected" : ""}>March 4 (month first)</option></select>${an.dateOrderCertain ? "" : ' <span class="pill">please confirm</span>'}</label>`; | |
| return `<div class="card"><h3>${esc(sh.name)}</h3>${body}</div>`; | |
| } | |
| // ------------------------------------------------------------ step 3 | |
| async function read() { | |
| const c = show(`<h1>Reading your records…</h1>${steps(2)}<p class="muted" id="st">Working…</p>`, true); | |
| W.recs = []; | |
| W.sheets.forEach((sh, i) => { if (W.use[i]) W.recs.push(...I.extract(sh, W.an[i], { today: new Date() })); }); | |
| if (!W.recs.length) { show(`<h1>Nothing to import</h1><div class="err">I couldn't find any records with those settings.</div><div class="row"><button id="b">Back</button></div>`).querySelector("#b").onclick = structure; return; } | |
| const need = W.recs.filter(r => r.issues.length); | |
| if (W.ai && need.length) { | |
| const c2 = show(`<h1>Some rows need a closer look</h1>${steps(2)}<p>${need.length} of ${W.recs.length} records have missing details. The AI can try to understand them from their text.</p> | |
| <div class="note">If you use AI, the <b>text of those ${need.length} rows only</b> is sent through the company server to the AI service. Nothing else from your file leaves this device. Choose “No” to answer everything yourself.</div> | |
| <div class="row"><button class="primary" id="y">Yes, use AI for those rows</button><button id="n">No, I'll answer them myself</button></div>`, true); | |
| await new Promise(res => { $("#y", c2).onclick = async () => { W.useAI = true; await runAI(need, c2); res(); }; $("#n", c2).onclick = () => { W.useAI = false; res(); }; }); | |
| } | |
| review(); | |
| } | |
| async function runAI(need, c) { | |
| c.innerHTML = `<h1>Asking the AI…</h1><p class="muted" id="p">0 / ${need.length}</p>`; | |
| for (let k = 0; k < need.length; k += 25) { | |
| const batch = need.slice(k, k + 25); | |
| try { | |
| const res = (await api("/api/ai/import-extract", "POST", { items: batch.map((r, j) => ({ i: k + j, text: r.src.text })) })).result || []; | |
| for (const a of res) { const r = need[a.i]; if (r) I.mergeAI(r, a); } | |
| } catch (e) { toast("AI couldn't read a batch – you'll be asked about those rows instead.", true); } | |
| $("#p", c).textContent = `${Math.min(k + 25, need.length)} / ${need.length}`; | |
| } | |
| } | |
| // ------------------------------------------------------------ step 3b: review & questions | |
| function review() { | |
| W.flagged = W.recs.map((r, i) => i).filter(i => W.recs[i].issues.length || (W.showGuessed && Object.keys(W.recs[i].assumed).length)); | |
| const ok = W.recs.filter(r => !r.issues.length).length, guessed = W.recs.filter(r => !r.issues.length && Object.keys(r.assumed).length).length; | |
| const pages = Math.max(1, Math.ceil(W.flagged.length / 20)); W.page = Math.min(W.page, pages - 1); | |
| const slice = W.flagged.slice(W.page * 20, W.page * 20 + 20); | |
| const c = show(`<h1>Review your records</h1>${steps(2)} | |
| <p><b>${W.recs.length}</b> records read · <b id="okc">${ok}</b> complete · <b id="needc">${W.recs.length - ok}</b> need your answer · ${guessed} complete but with a guessed value</p> | |
| <label style="display:inline-block"><input type="checkbox" id="sg" ${W.showGuessed ? "checked" : ""} style="width:auto"> Also ask me to confirm records where I guessed a value</label> | |
| <div class="note">Highlighted fields are what I need. Dashed fields are my guesses. Records you skip are not imported.</div> | |
| <div id="cards">${slice.map(i => card(i)).join("") || '<p class="muted">Nothing needs your attention 🎉</p>'}</div> | |
| ${pages > 1 ? `<div class="row"><button id="prev" ${W.page === 0 ? "disabled" : ""}>← Previous</button><span class="muted">Page ${W.page + 1} of ${pages}</span><button id="nextp" ${W.page >= pages - 1 ? "disabled" : ""}>Next →</button></div>` : ""} | |
| <details><summary class="muted">See all ${W.recs.length} records</summary>${table(["Sheet/row", "Date", "Type", "Product", "ID", "Title", "Status"], W.recs.map(r => [`${r.src.sheet} #${r.src.row}`, r.date || "—", r.workType || "—", r.product || "—", r.itemId || "—", r.title || "—", r.skip ? "skipped" : r.issues.length ? "needs: " + r.issues.join(", ") : Object.keys(r.assumed).length ? "guessed" : "ok"]), { scroll: true })}</details> | |
| <div class="row"><button class="primary" id="save">Add to my Excel file</button><button id="back">Back</button><button class="ghost" id="cancel">Cancel</button></div>`, true); | |
| $("#cancel", c).onclick = () => onCancel(); $("#back", c).onclick = structure; | |
| $("#sg", c).onchange = e => { W.showGuessed = e.target.checked; W.page = 0; review(); }; | |
| if ($("#prev", c)) { $("#prev", c).onclick = () => { W.page--; review(); }; $("#nextp", c).onclick = () => { W.page++; review(); }; } | |
| const upd = () => { const bad = W.recs.filter(r => r.issues.length && !r.skip).length; $("#needc", c).textContent = W.recs.filter(r => r.issues.length).length; $("#okc", c).textContent = W.recs.filter(r => !r.issues.length).length; $("#save", c).textContent = `Add ${W.recs.filter(r => !r.issues.length && !r.skip).length} records to my Excel file` + (bad ? ` (${bad} incomplete will be left out)` : ""); }; | |
| upd(); | |
| c.oninput = c.onchange = e => { | |
| const el = e.target.closest("[data-f]"); if (!el) return; const box = el.closest(".rev"), r = W.recs[+box.dataset.ri], f = el.dataset.f; | |
| if (f === "skip") r.skip = el.checked; else { r[f] = el.value.trim() || (f === "description" ? "" : null); delete r.assumed[f]; } | |
| I.finishIssues(r); paint(box, r); upd(); | |
| }; | |
| c.onclick = e => { | |
| const b = e.target.closest("[data-bulk]"); if (!b) return; const f = b.dataset.bulk, src = W.recs[+b.closest(".rev").dataset.ri]; | |
| if (!src[f]) return toast("Fill this field on this record first, then I can apply it to the others.", true); | |
| const targets = W.recs.filter(r => !r[f] && !r.skip); if (!targets.length || !confirm(`Set ${f === "workType" ? "Type" : f} = “${src[f]}” on ${targets.length} other record(s) that are missing it?`)) return; | |
| targets.forEach(r => { r[f] = src[f]; I.finishIssues(r); }); review(); | |
| }; | |
| $("#save", c).onclick = guard(save); | |
| } | |
| const L = { date: "Date", workType: "Type", product: "Product", itemId: "ID", title: "Title" }; | |
| function cls(r, f) { return !r[f] ? "miss" : r.assumed[f] ? "guess" : ""; } | |
| function card(i) { | |
| const r = W.recs[i], opt = (v, list) => `<option value="">— choose —</option>` + list.map(x => `<option ${v === x ? "selected" : ""}>${x}</option>`).join(""); | |
| return `<div class="rev ${r.skip ? "ok" : ""}" data-ri="${i}"><div class="src">${esc(r.src.sheet)} · row ${r.src.row}: ${esc(r.src.text.slice(0, 300))}</div> | |
| <div class="f"> | |
| <label class="${cls(r, "date")}">Date<input type="date" data-f="date" value="${esc(r.date || "")}"></label> | |
| <label class="${cls(r, "workType")}">Type<select data-f="workType">${opt(r.workType, ["Ticket", "POC"])}</select></label> | |
| <label class="${cls(r, "product")}">Product<select data-f="product">${opt(r.product, ["WAF", "WAS"])}</select></label> | |
| <label class="${cls(r, "itemId")}">Ticket / POC ID<input data-f="itemId" value="${esc(r.itemId || "")}"></label> | |
| </div> | |
| <label class="${cls(r, "title")}" style="margin-top:8px">Title<input data-f="title" value="${esc(r.title || "")}"></label> | |
| <label>Description<textarea data-f="description" rows="2">${esc(r.description || "")}</textarea></label> | |
| <div class="q" data-q>${hint(r)}</div> | |
| <div class="row"><label style="margin:0"><input type="checkbox" data-f="skip" ${r.skip ? "checked" : ""} style="width:auto"> Skip this record</label> | |
| ${["workType", "product", "date"].map(f => `<button class="linkbtn" data-bulk="${f}">apply ${L[f]} to all others missing it</button>`).join("")}</div></div>`; | |
| } | |
| function hint(r) { | |
| const need = r.issues.map(f => L[f]), g = Object.entries(r.assumed).map(([f, why]) => `${L[f] || f} (${why})`); | |
| return (need.length ? `Please tell me: <b>${need.join(", ")}</b>. ` : "") + (r.aiQuestions.length ? "AI asks: " + esc(r.aiQuestions.join(" ")) + " " : "") + (g.length ? `<span class="muted">Guessed: ${esc(g.join("; "))}</span>` : ""); | |
| } | |
| function paint(box, r) { | |
| box.classList.toggle("ok", !!r.skip || (!r.issues.length && !Object.keys(r.assumed).length)); | |
| const set = { date: 0, workType: 1, product: 2, itemId: 3 }; const labels = $$("label", box); | |
| for (const f of ["date", "workType", "product", "itemId", "title"]) { const l = box.querySelector(`[data-f="${f}"]`).closest("label"); l.classList.remove("miss", "guess"); const k = cls(r, f); if (k) l.classList.add(k); } | |
| box.querySelector("[data-q]").innerHTML = hint(r); | |
| } | |
| // ------------------------------------------------------------ step 4 | |
| async function save() { | |
| const bad = W.recs.filter(r => r.issues.length && !r.skip).length; | |
| if (bad && !confirm(`${bad} record(s) are still incomplete and will be left out. Continue?`)) return; | |
| let result; | |
| await S.commit(st => { | |
| const next = Math.max(0, ...st.entries.map(e => e.entryNo)) + 1; | |
| result = I.finalize(W.recs, st.entries, next); | |
| st.entries.push(...result.entries); | |
| st.entries.sort((a, b) => a.date < b.date ? -1 : a.date > b.date ? 1 : a.entryNo - b.entryNo); | |
| }); | |
| const months = [...new Set(result.entries.map(e => e.date.slice(0, 7)))].sort(); | |
| const c = show(`<h1>Import finished ✔</h1>${steps(3)} | |
| <p><b>${result.entries.length}</b> records were added to your Excel file, across <b>${months.length}</b> monthly sheet(s)${months.length ? ": " + months.join(", ") : ""}. The Summary and Weekly sheets now include them.</p> | |
| ${result.skipped.length ? `<details><summary>${result.skipped.length} record(s) not imported</summary>${table(["Source", "Text", "Reason"], result.skipped.map(s => [`${s.rec.src.sheet} #${s.rec.src.row}`, s.rec.src.text.slice(0, 80), s.why]), { scroll: true })}</details>` : ""} | |
| <div class="row"><button class="primary" id="done">Open my records</button></div>`, true); | |
| $("#done", c).onclick = () => onDone(); | |
| } | |
| } | |