import { escapeHtml as esc } from './html.mjs'; import { studioLink, studioForm, studioUrl } from './studio-links.mjs'; export const studioTaskStages = { backlog: 'Backlog', active: 'In progress', review: 'In review', done: 'Done' }; const owners = ['Maya Chen', 'Jon Bell', 'Eli Rivera']; const priorities = ['Normal', 'High', 'Low']; export function createStudioTasks() { return [ ['ST-101', 'Map the flagship customer journey', 'cinder', 'backlog', 'Jon Bell', 'Normal', 'Walk the shop floor with Sofia and capture the touchpoints between arrival and checkout.'], ['ST-102', 'Specify the acoustic panels', 'tandem', 'backlog', 'Eli Rivera', 'Low', 'Compare the two acoustic samples against the brief for the member lounge.'], ['ST-103', 'Price the first retail rollout', 'forma', 'active', 'Maya Chen', 'High', 'Prepare a cost breakdown for the first three stores, including delivery and installation.'], ['ST-104', 'Refine the identity lockups', 'arc', 'active', 'Jon Bell', 'Normal', 'Prepare the horizontal and stacked lockups for the next review with Theo.'], ['ST-105', 'Check the fixture clearances', 'stillwater', 'active', 'Eli Rivera', 'Normal', 'Check the showroom measurements against the latest joinery drawings.'], ['ST-106', 'Sign off the material samples', 'union', 'review', 'Eli Rivera', 'High', 'Review the hospitality sample board with purchasing before releasing the order.'], ['ST-107', 'Approve the storefront elevations', 'forma', 'review', 'Maya Chen', 'Normal', 'Collect the retail team’s final approval on the revised street-facing elevations.'], ['ST-108', 'Issue the showroom survey', 'stillwater', 'done', 'Maya Chen', 'Normal', 'Send the measured survey and annotated photos to the installation team.'], ['ST-109', 'Deliver the concept deck', 'arc', 'done', 'Jon Bell', 'Low', 'Share the agreed creative direction and workshop notes with the client.'], ].map(([id, title, companyId, status, owner, priority, description]) => ({ id, title, companyId, status, owner, priority, description })); } function getValue(values, key) { return values instanceof URLSearchParams ? values.get(key) ?? '' : values[key] ?? ''; } function options(entries, value) { return entries.map(([id, label]) => ``).join(''); } function avatar(owner) { const initials = owner.split(' ').map(word => word.charAt(0)).join(''); return ``; } function stage(status) { return ``; } function priorityLabel(priority) { return `${esc(priority)}`; } function card(task, companyName, scope, subheading, params) { return `
${esc(task.id)}${priorityLabel(task.priority)}
<${subheading} class="stb-card-title">${esc(task.title)}
`; } function list(tasks, names, scope, params) { if (!tasks.length) return '

No tasks

'; return `
`; } function scopeFields(scope) { return ``; } function readout(studio, task, scope, params) { const company = studio.companies.find(item => item.id === task.companyId); return `
${esc(task.id)}${priorityLabel(task.priority)}

${esc(task.title)}

${task.description ? `

${esc(task.description)}

` : ''}
${scopeFields(scope)}
Back to ${scope.display === 'list' ? 'list' : 'board'}
`; } function composer(studio, scope, params, values, error) { const value = key => getValue(values, key); const company = value('companyId') || scope.company || ''; const pairs = items => items.map(item => [item, item]); return `
${scopeFields(scope)}
Cancel
`; } function lanes(tasks, names, scope, params, laneHeading, cardHeading, newTask) { return `
${Object.entries(studioTaskStages).map(([id, label]) => { const items = tasks.filter(task => task.status === id); return `
<${laneHeading}>${stage(id)}${label} ${items.length}+
${items.length ? items.map(task => card(task, names.get(task.companyId), scope, cardHeading, params)).join('') : '

No tasks

'}
`; }).join('')}
${scopeFields(scope)}
`; } export function renderStudioBoard(studio, params, { error = '', values = {}, pageHeading = true } = {}) { const heading = pageHeading ? 'h1' : 'h2'; const laneHeading = pageHeading ? 'h2' : 'h3'; const cardHeading = pageHeading ? 'h3' : 'h4'; const company = studio.companies.find(item => item.id === params.get('company')); const listView = params.get('display') === 'list'; const display = { display: listView ? 'list' : 'board' }; const scope = company ? { ...display, company: company.id } : display; const tasks = company ? studio.tasks.filter(task => task.companyId === company.id) : studio.tasks; const selected = tasks.find(task => task.id === params.get('task')); const compose = params.get('compose') === 'task'; const stageId = params.get('stage'); const stageName = Object.hasOwn(studioTaskStages, stageId) ? studioTaskStages[stageId] : ''; const names = new Map(studio.companies.map(item => [item.id, item.name])); const newTask = (stageId = '') => studioLink('board', { ...scope, compose: 'task', ...(stageId ? { stage: stageId } : {}) }, params); const selection = selected ? { task: selected.id } : {}; const viewLink = choice => studioLink('board', { ...scope, ...selection, display: choice }, params); const implicit = !['board', 'list'].includes(params.get('display')); const alternate = implicit && !error && studioUrl('board', { ...scope, ...selection, ...(compose ? { compose: 'task', stage: stageName && stageId } : {}), display: 'list', }, params); return `
<${heading}>${compose ? 'New task' : 'Task board'}

${compose ? (company ? esc(company.name) + ' · ' : '') + esc(stageName || 'Studio projects') : company ? esc(company.name) : 'Studio projects'}${compose ? '' : ' · ' + tasks.length + (tasks.length === 1 ? ' task' : ' tasks')}

${compose ? '' : `New task`}
${compose ? composer(studio, scope, params, values, error) : ` ${error ? `` : ''} ${selected ? readout(studio, selected, scope, params) : ''}
${esc(company?.name || 'All companies')}
All companies${studio.companies.map(item => `${esc(item.name)}`).join('')}
${listView ? list(tasks, names, scope, params) : lanes(tasks, names, scope, params, laneHeading, cardHeading, newTask)}`}
`; } export function mutateStudioTask(studio, form) { const get = key => getValue(form, key).trim(); const company = studio.companies.find(item => item.id === get('company')); const choice = get('display'); const scope = ['board', 'list'].includes(choice) ? { display: choice } : {}; if (company) scope.company = company.id; const status = get('status'); if (get('operation') === 'move') { const task = studio.tasks.find(item => item.id === get('id')); if (!task) return { error: 'This task could not be found.', params: scope }; const params = get('openTask') === '1' ? { ...scope, task: task.id } : scope; if (!Object.hasOwn(studioTaskStages, status)) return { error: 'Choose a valid task status.', params }; task.status = status; return { params }; } if (get('operation') !== 'create') return { error: 'Choose a task action.', params: scope }; const failure = message => ({ error: message, params: { ...scope, compose: 'task' } }); const title = get('title'); const description = get('description'); const companyId = get('companyId'); const owner = get('owner'); const priority = get('priority'); if (!title || title.length > 100) return failure('Enter a task title of 1–100 characters.'); if (!studio.companies.some(item => item.id === companyId)) return failure('Choose a company for this task.'); if (!owners.includes(owner) || !priorities.includes(priority) || !Object.hasOwn(studioTaskStages, status)) return failure('Choose a valid owner, priority and status.'); if (description.length > 600) return failure('Keep the description within 600 characters.'); const number = studio.tasks.reduce((n, task) => Math.max(n, Number(task.id.slice(3)) || 0), 100) + 1; const id = 'ST-' + number; studio.tasks.unshift({ id, title, description, companyId, owner, priority, status }); return { params: { ...scope, ...(company ? { company: companyId } : {}), task: id } }; }