diff --git a/dashboard/e2e/specs/schedule-autofill.spec.js b/dashboard/e2e/specs/schedule-autofill.spec.js new file mode 100644 index 0000000..9c34a4e --- /dev/null +++ b/dashboard/e2e/specs/schedule-autofill.spec.js @@ -0,0 +1,53 @@ +// Global schedule start date (results header) auto-fills each clip's post modal +// date/time from that clip's VIRALITY RANK (rank 1 = most viral = top card). +// +// The mocked session clips are: +// clip index 0 "First viral clip" score 91 -> rank 1 +// clip index 1 "Second viral clip" score 77 -> rank 2 +// Per the agreed cadence (odd rank on the picked date, even rank the next day, +// shared time cycles 18:00 -> 21:00 -> 13:00): +// rank 1 -> start date @ 18:00 +// rank 2 -> start date + 1 day @ 18:00 +import { test, expect } from '@playwright/test'; +import { openApp, completedSession } from '../fixtures/app.js'; + +const START = '2030-01-02'; +const seed = { + session: completedSession(), + postizUrl: 'http://postiz.e2e.local', + postizKey: 'postiz-e2e-key', +}; + +test.describe('global schedule auto-fill', () => { + test('prefills each clip post time from its virality rank', async ({ page }) => { + await openApp(page, { seed }); + await expect(page.getByRole('heading', { name: 'First viral clip' })).toBeVisible(); + + // Pick the global start date in the results header. + await page.getByTestId('schedule-start-date').fill(START); + + // Rank 1 (top card): start date @ 18:00, scheduling auto-enabled so the + // prefilled time is visible. + await page.getByRole('button', { name: 'post', exact: true }).nth(0).click(); + let dialog = page.getByRole('dialog'); + await expect(dialog.getByLabel('date', { exact: true })).toHaveValue(START); + await expect(dialog.getByLabel('time (HH:MM)', { exact: true })).toHaveValue('18:00'); + await expect(dialog.getByRole('checkbox', { name: /schedule post/i })).toBeChecked(); + await dialog.getByRole('button', { name: 'close' }).click(); + + // Rank 2: next day, same first time slot. + await page.getByRole('button', { name: 'post', exact: true }).nth(1).click(); + dialog = page.getByRole('dialog'); + await expect(dialog.getByLabel('date', { exact: true })).toHaveValue('2030-01-03'); + await expect(dialog.getByLabel('time (HH:MM)', { exact: true })).toHaveValue('18:00'); + }); + + test('with no start date the schedule stays off', async ({ page }) => { + await openApp(page, { seed }); + await expect(page.getByRole('heading', { name: 'First viral clip' })).toBeVisible(); + + await page.getByRole('button', { name: 'post', exact: true }).nth(0).click(); + const dialog = page.getByRole('dialog'); + await expect(dialog.getByRole('checkbox', { name: /schedule post/i })).not.toBeChecked(); + }); +}); diff --git a/dashboard/src/App.jsx b/dashboard/src/App.jsx index 502a97a..097885b 100644 --- a/dashboard/src/App.jsx +++ b/dashboard/src/App.jsx @@ -14,6 +14,7 @@ import Modal from './components/ui/Modal'; import { useAuth } from './contexts/AuthContext'; import { apiFetch } from './lib/api'; import { track } from './lib/analytics'; +import { scheduleForRank } from './lib/schedule'; // Enhanced "Encryption" using XOR + Base64 with a Salt // This is better than plain Base64 but still client-side. @@ -199,6 +200,14 @@ const readableError = (raw) => { // already purged server-side fails gracefully and clears the saved session. const SESSION_MAX_AGE = 86400000; // 24 hours +// Today as YYYY-MM-DD in the user's own timezone, for the schedule-start date +// input's `min`. Built from local parts (not toISOString, which would shift the +// day across the UTC boundary for users west of Greenwich). +const todayInputValue = () => { + const n = new Date(); + return `${n.getFullYear()}-${String(n.getMonth() + 1).padStart(2, '0')}-${String(n.getDate()).padStart(2, '0')}`; +}; + // Mock polling function const pollJob = async (jobId) => { const res = await apiFetch(`/api/status/${jobId}`); @@ -238,6 +247,12 @@ function App() { const [jobId, setJobId] = useState(null); const [status, setStatus] = useState('idle'); // idle, processing, complete, error const [results, setResults] = useState(null); + // Global publish start date for the whole results panel. When set, each clip's + // modal auto-fills its post date/time from this date and the clip's virality + // rank (rank 1 = most viral = first card). Empty means the feature is off. + const [scheduleStartDate, setScheduleStartDate] = useState(() => { + try { return localStorage.getItem('cw_schedule_start') || ''; } catch (_) { return ''; } + }); // Best clips first. The backend hands them back in transcript order, which // buries the strongest one wherever it happens to fall in the video — and // the first card is the one people actually watch and publish. @@ -259,6 +274,14 @@ function App() { return sb - sa || a.index - b.index; }); }, [results]); + // One suggested publish slot per virality rank, taken from the shared start + // date. Memoized so a card's `suggestedSchedule` keeps a stable identity + // across unrelated re-renders — otherwise the modal-open reset (which the + // card keys off this prop) would clobber a date/time the user just picked. + const suggestedSchedules = useMemo( + () => rankedClips.map((_, rank) => scheduleForRank(scheduleStartDate, rank + 1)), + [rankedClips, scheduleStartDate] + ); // Bulk subtitles: apply one style to every clip of the job (triggered from // within a clip's subtitle modal via "apply to all"). const [bulkSub, setBulkSub] = useState({ running: false, current: 0, total: 0, errors: 0 }); @@ -464,6 +487,13 @@ function App() { } catch (_) { /* ignore */ } }, [postizUrl]); + useEffect(() => { + try { + if (scheduleStartDate) localStorage.setItem('cw_schedule_start', scheduleStartDate); + else localStorage.removeItem('cw_schedule_start'); + } catch (_) { /* ignore */ } + }, [scheduleStartDate]); + useEffect(() => { if (postizKey && postizUrl && postizChannels.length === 0) { fetchPostizChannels({ silent: true }); @@ -1360,7 +1390,23 @@ function App() { )} {results?.clips?.length > 0 && status === 'complete' && ( -
clips fill in by virality rank — 6pm, 9pm, 1pm
+