13 KiB
Study Desk - AI Contributor Guide
This file is the operating guide for AI agents and human contributors working in this repository.
Project summary
Study Desk is a self-hosted, single-user study application built with Next.js, React, TypeScript, Prisma, and SQLite.
Core capabilities:
- Flashcard decks with Markdown content, self-grading, shuffle persistence, resume state, and card management
- Practice quizzes with multiple-choice and SATA questions, partial-credit scoring, rationales, category breakdowns, retakes, and attempt history
- JSON import, repair, validation, and generation workflows
- Material groups for organizing decks and quizzes
- Password-protected private pages
- Public, read-only share links for decks, quizzes, and groups
- Responsive light and dark themes
This is intentionally a single-user container, not a multi-tenant SaaS application. There is no User model. Content ownership is implicit in the local installation.
When documentation conflicts with implementation, prefer:
- Current source code
prisma/schema.prisma- Applied migrations
- This file
- Older planning documents and the stock README
Mandatory working rules
- Inspect
git status --shortbefore editing. - Preserve unrelated user changes.
- Keep changes scoped to the requested behavior.
- Do not use destructive commands such as
git reset --hardorgit checkout --unless explicitly requested. - Prefer narrow, patch-based edits over rewriting entire files.
- Do not commit secrets,
.envfiles, database contents, generated credentials, or private study material. - Do not manually edit generated Prisma files under
src/generated/prisma/. - Do not modify an already-applied migration. Change
prisma/schema.prisma, then create a new migration. - Prefer TypeScript types over
any. - Add dependencies only when the existing stack cannot reasonably solve the problem.
- When dependencies change, update
package-lock.jsonand verify the build. - Separate UI, route handling, services, and low-level utilities by responsibility.
- Ask a clarifying question only when missing information would materially change the implementation or create a risky assumption.
Stack and source of truth
Confirm exact versions in package.json. The project currently uses:
- Next.js 16 App Router
- React 19
- TypeScript in strict mode
- Tailwind CSS v4
- Prisma 7 with SQLite and
@prisma/adapter-better-sqlite3 iron-sessionfor encrypted session cookies- Argon2 password hashing
- Zod validation
react-markdownwithremark-gfm@dnd-kitfor sortable UI- Node 22 in Docker
Important files:
package.json- scripts and dependency versionsprisma/schema.prisma- database source of truthsrc/lib/db.ts- shared Prisma clientsrc/lib/auth.ts- session helperssrc/services/- business logic and database accesssrc/app/api/- route handlerssrc/app/globals.css- design tokens and global stylingsrc/config/studyModes.ts- shared study-mode navigationSKILL.md- project workflow for adding API routesstudy-app-implementation-plan.md- historical architectural context onlyREADME.md- currently not authoritative
For framework-sensitive changes involving routing, layouts, dynamic params, middleware, server/client boundaries, or framework APIs, follow nearby repository patterns and consult version-matched Next.js documentation. Do not perform unrelated framework migrations.
Local development
For a clean checkout:
npm ci
npx prisma generate
npm run dev
On Windows PowerShell, use npm.cmd and npx.cmd if script execution policy blocks the default shims.
Useful scripts:
npm run dev
npm run build
npm run start
npm run lint
There is currently no configured test runner. Use focused manual verification plus npm run build. Run lint as well, but distinguish pre-existing failures from failures introduced by the current change.
Environment and authentication
Common environment variables:
DATABASE_URL- SQLite URLSESSION_SECRET-iron-sessionsecret, at least 32 characters outside throwaway local developmentSECURE_COOKIES- marks the session cookie secure when set totrueADMIN_PASSWORD_HASH- present in.env.example, but currently not used by the login flow
Current password behavior:
- On first login, the submitted password is Argon2-hashed.
- The hash is stored in the
Settingrowadmin_password_hash. - Later logins verify against that stored hash.
Do not change password provisioning casually. Any change must update login, setup-status behavior, deployment documentation, and security handling together.
Authentication state is stored in the study-app-session cookie.
Public paths include:
/login/api/auth/*/shared/*- Static assets and Next internals
Everything else requires authentication unless explicitly designed and reviewed as public.
Do not log passwords, secrets, full share tokens, or private study content.
Database and migration safety
The local dev.db may contain real study data.
- Do not delete, reset, replace, or recreate it during normal work.
- Back up the database before destructive migration work.
- Use the shared Prisma client from
@/lib/db. - Do not create additional
PrismaClientinstances in request handlers or components. - Use transactions when partial writes would leave inconsistent state.
- Return 404 responses for missing records instead of exposing raw Prisma errors.
After changing the Prisma schema:
npx prisma generate
npx prisma migrate dev --name describe_the_change
Use npx prisma migrate deploy for deployed environments.
Architecture conventions
Server and client components
- Use Server Components by default.
- Add
"use client"only when state, effects, event handlers, browser APIs, drag and drop, or client-side fetching are required. - Keep database and authentication modules out of client import graphs.
- Do not access
window,localStorage,navigator, or clipboard APIs from Server Components. - Prefer
Linkfor ordinary internal navigation. - Use
useRouterwhen navigation follows an action or must be imperative. - Follow nearby Next.js 16 patterns for dynamic params and route context types.
API routes and services
Preferred route-handler flow:
- Parse and validate external input.
- Delegate business logic to focused service or utility functions.
- Return a useful status and JSON response.
Business logic and Prisma calls normally belong in src/services/ or a focused src/lib/ module. Reuse existing services before creating new abstractions.
Some older routes still contain direct Prisma calls or loose input handling. Improve the requested path without turning a small task into a broad refactor.
Validation
- Reuse existing Zod schemas where available.
- Never rely only on client-side validation.
- Validate IDs, required strings, enums, arrays, and import payloads at the API boundary.
- Keep stored JSON parseable.
- Use
jsonRepair.tsonly for user- or LLM-generated import text, not to hide malformed database state.
UI and accessibility
Use the existing design system in src/app/globals.css.
Common tokens include:
bg-bg-basebg-bg-surfacebg-bg-surface-alttext-text-headingtext-text-secondarytext-text-mutedprimaryprimary-hoverborderborder-lightsuccesserror
Preserve:
- Light and dark themes
- Responsive mobile layouts
- Existing card, modal, shadow, and rounded-corner language
editorial-titlefor major serif headings- Manrope for interface text
- Existing Markdown rendering through
ReactMarkdown,remarkGfm, and.markdown-content - Existing
@dnd-kitpatterns for sortable content
Accessibility requirements:
- Use semantic buttons and links.
- Preserve visible keyboard focus states.
- Give icon-only controls an accessible name with visible text or
aria-label. - Keep interactive controls keyboard accessible.
- Avoid horizontal overflow on study pages, library cards, modals, and action rows.
- Do not attach drag listeners to nested action buttons.
Domain invariants
Flashcards
- A deck contains ordered
Flashcardrows withfrontandback. - Study progress stores order and results as JSON.
- Study mode may be sequential or shuffled.
- Saved progress may contain stale card IDs after deletion. Resume helpers must filter and clamp stale IDs safely.
- Preserve both study and manage views.
- Preserve existing sharing entry points when adding new ones.
Quizzes
- A quiz contains ordered questions with ordered answer options.
MULTIPLE_CHOICEquestions must have exactly one correct option.SATAquestions may have multiple correct options.- SATA uses partial-credit scoring in
src/lib/scoring.ts. - Partial retakes score only questions present in the submitted answer set.
- Attempts persist score, maximum score, submitted answers, retake state, and completion time.
- In-progress answers and position persist through
/api/progress.
Imports and generation
Flashcard import shape:
{
"type": "flashcards",
"deckName": "Deck name",
"description": "Optional description",
"cards": [{ "front": "...", "back": "..." }]
}
Quiz import shape:
{
"type": "quiz",
"quizName": "Quiz name",
"description": "Optional description",
"questions": [
{
"type": "multiple_choice",
"prompt": "...",
"category": "topic",
"options": [{ "text": "...", "correct": true }],
"rationale": "..."
}
]
}
The app does not call an LLM directly. The Generate UI prepares instructions and accepts generated JSON from the user.
Preserve Markdown inside JSON string values and render it through existing Markdown components.
Keep the current separation between ImportModal, CreateTab, ImportTab, and GenerateTab.
Sharing
ShareLinkmay target aDECK,QUIZ, orGROUP.- Reuse
src/components/ui/ShareMenu.tsx. - Public routes are
/shared/<classSlug>/<type>/<token>. - Public viewers are read-only.
- Public viewers must not write authenticated progress.
- Validate that token, type, and class slug agree before returning shared content.
- A deck or quiz may appear publicly shared through its group. Preserve inherited group-share behavior and
?itemId=...handling.
Verification checklist
Before editing:
- Read
git status --short. - Inspect the target component, route, service, and schema paths relevant to the requested behavior.
- Check whether the change affects authentication, public sharing, migrations, generated Prisma output, or real study data.
During implementation:
- Keep route, service, utility, and component responsibilities clear.
- Add validation for new external input.
- Reuse existing design tokens, components, and services.
- Consider loading, error, empty, mobile, keyboard, light-theme, and dark-theme states.
- Avoid unrelated cleanup.
After editing:
npm run lint
npm run build
If lint already fails, report baseline failures separately.
For UI work, manually verify:
- The affected authenticated route
- The corresponding public/shared route when applicable
- Loading, error, and empty states
- Study resume and restart behavior when relevant
- Drag and drop when relevant
- Light and dark themes
- A narrow mobile viewport
- No horizontal overflow
- Keyboard access for changed controls
Known repository conditions
README.mdis still mostly stock create-next-app documentation.study-app-implementation-plan.mdcontains useful historical context but may be stale.task.mdrecords completed implementation phases.SKILL.mddescribes the expected workflow for new API routes.- The current lint baseline includes existing errors such as
anyusage and React effect-rule violations. - A successful
next buildis the primary repository-wide compile and type check. - The current middleware convention produces a known deprecation warning.
docker-compose.ymlmay publish port3000, while the production Docker image uses port3726. Verify the mapping before relying on production Compose.- Do not silently fix known baseline issues as unrelated cleanup.
Extending the application
For a genuinely new study mode:
- Add or update the Prisma model and migration when persistence is required.
- Add a focused service in
src/services/. - Add API routes under
src/app/api/. - Add page routes and client components.
- Add the mode to
src/config/studyModes.tswhen it belongs in shared navigation. - Extend progress, import, scoring, or sharing only when required.
- Verify authentication, public/private boundaries, mobile layout, and both themes.
Do not add a new mode by placing unrelated logic into deckService.ts, quizService.ts, or a large page component.