跪拜 Guibai
← All articles
Next.js

From Excel and localStorage to MySQL: Seven Reworks That Shaped an Esports Venue's Next.js 16 Scheduling and Payroll System

By 甘旻 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Small-business admin software is a real workload; the hard problems are data ownership, temporal boundaries, and payroll rules, and this build shows how they break a full-stack app. The fixes, especially protecting manual edits from generated writes and making payroll a pure function, transfer directly to any team tool where humans and automation share data.

Summary

A Next.js 16 + React 19 + MySQL system replaced Excel scheduling and hand-calculated payroll for a dozen-person esports venue. The first scheduler assigned shifts day by day and produced chaotic early/middle/late rotations, so the final design splits each month into two half-month blocks and gives every employee one fixed shift type per block. New hires and mid-month departures go into a floating pool that only covers absent fixed-role staff, in late > early > middle order, and late-to-day transitions get a bridging rest day in the 14-18 window.

Payroll is the second hard part. Eight components stack together: base, performance, attendance, holiday triple pay, trial-period pay, overnight allowance, recharge commission, overtime, and manual fields. The engine is a pure function that takes employee, month data, settings, and holidays, reads no database, calls no new Date(), and changes no state. That makes rule changes diffable and lets historical months be recomputed as regression checks. Two measurement rules matter: payable attendance excludes trial shifts, and the daily-wage denominator is fixed at days in month minus rest days.

Seven pitfalls cost real rework. DDL ran against a different MySQL instance than the app used; whole-row snapshot writes silently zeroed hand-entered overtime; payroll rules changed five times before settling; React 19 hydration broke on localStorage and new Date(); date-string parsing shifted months in some time zones; the first auto-scheduler overwrote the manager's manual shift edits; and localStorage as the source of truth made data vanish across computers. The fixes: verify schema changes with the app's own connection config, separate engine-derived from manual fields, snapshot and audit every write, gate browser-only rendering behind a hydrated flag, pass YYYY-MM-DD strings and avoid toISOString(), and make auto-generation fill only blank cells. The code is open-sourced under MIT.

Takeaways
— The stack is Next.js 16 (App Router), React 19, TypeScript 5 strict, Tailwind CSS 4, shadcn/ui, MySQL via mysql2/promise, and SheetJS for Excel export.
— Scheduling uses half-month fixed blocks: the month splits into 1-15 and 16-end, and each employee stays on one shift type for the whole block.
— Standard slots are assigned by stable name sort, not hire date; front desk has early/middle/late, servers have early/middle/floating/late/late.
— New hires and mid-month leavers are pushed into floating slots; floating slots only cover for absent fixed-role staff, in late > early > middle order.
— Employees moving from late shift in the first block to early/middle in the second get a transition rest day in the 14-18 window, outside weekends and holidays.
— Payroll is a pure function: computePayroll(employee, monthData, monthKey, settings, holidays) reads no database, calls no new Date(), and changes no state; the same function serves UI, Excel export, and snapshots.
— Payable attendance excludes trial shifts; trial pay is per day and is not folded into base-salary proration.
— The daily-rate denominator for attendance and holiday pay is fixed at days in month minus rest days, so the same date pays the same daily rate across months.
— Employment boundaries are normalized in two layers: normalizeForView forces not-yet-employed/departed in memory; normalizeEmploymentPeriods fills only blank cells when persisting, preserving manual schedules.
— A CREATE TABLE can succeed in a SQL tool pointed at a different MySQL instance than the app uses; verify DDL with the app's own connection config.
— Whole-row snapshot writes can silently zero manual fields; engine-derived and manual fields must be stored and passed through separately.
— Payroll rules changed five times while the system was in real use; snapshots plus an audit trail on every write made recomputation and verification survivable.
— React 19/App Router hydration breaks when render logic uses localStorage or new Date(); gate browser-only rendering behind a hydrated flag set in useEffect.
— Dates are passed as YYYY-MM-DD strings with pure helpers; never use toISOString(), because new Date('YYYY-MM-DD') can shift to the previous month in some time zones.
— Auto-scheduling must only fill blank cells; the first version rewrote the month and erased the manager's manual adjustments.
— localStorage as a source of truth loses data across computers and lets concurrent managers overwrite each other; after MySQL, it only stores the currently viewed month.
Conclusions

A pure-function payroll engine is the load-bearing decision here; after five rule changes in real use, being able to rerun history and diff outputs is what kept the system trustworthy.

The only-fill-blank-cells rule is a general answer to a common failure: when automated logic and human override share the same data, generated writes must treat manual edits as a protected layer, not a conflict to resolve.

Choosing a direct MySQL connection with a thin repository layer over an ORM is a scale-appropriate call for a dozen-person domain, but it only pays off because the model is small enough that SQL stays transparent.

The bugs that cost the most time cluster around data ownership and time boundaries. Framework features are not where the complexity lived.

The build's own conclusion is that small-but-complex business software succeeds or fails on domain rules; once a rule is wrong, clean code does not save it.

Concepts & terms
Half-month fixed block
A scheduling pattern that splits each month into two segments (1-15 and 16-end) and keeps each employee on one shift type for the whole segment, instead of assigning day by day.
Flexible slot
A reserve position in the schedule, usually filled by new hires or mid-month departures, that only steps in when a fixed-role employee is absent; the substitution order is late > early > middle.
Payable attendance
The attendance count used for payroll calculations. It is not raw attendance: trial-period shifts are excluded because they are paid separately per day.
Hydration
In React, the client-side process of attaching state and event handlers to server-rendered HTML. A hydration mismatch occurs when server and client render different output, for example because render logic reads localStorage or new Date().
From the discussion
Featured comments
去吧波波鸟

That part where auto-scheduling rewrote the entire month made me sweat—the boss thought he'd just misremembered. Brutal lesson.

甘旻

Good thing I'd snapped a photo back then, otherwise my whole salary wouldn't have been enough to cover it. [crying]

See top comments, translated →
Source: juejin.cn ↗ Google Translate ↗ Backup ↗