feat(ui): shot status, projects and client portal keep their place
Deploy / deploy (push) Successful in 3m31s

Coming back from a shot left the shot status page at the top with its
sections reset. The scroll restore saved window.scrollY, but dashboard pages
scroll the layout's <main>, so it always saved 0; the Projects page had the
same bug. Only episode expansion was remembered — scene and group sections
came back fully collapsed.

A shared useScrollMemory hook saves the real scroll container's position as
the user scrolls (so any way of leaving keeps it) and restores it once the
content that sets the page height is back. It ignores the scroll the shared
<main> fires when the next page replaces this one, which otherwise
overwrote the saved position.

Shot status now remembers episode, scene and group expansion per project.
The client portal already kept its filters and collapsed episodes but
restored scroll before they were applied; it now uses the hook too.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
twotalesanimation
2026-09-18 05:52:13 +02:00
parent 532ebd82d7
commit 33bc6a935c
4 changed files with 170 additions and 59 deletions
@@ -4,6 +4,7 @@ import { useState, useTransition, useCallback, useEffect, useRef } from "react";
import { useRouter } from "next/navigation";
import Image from "next/image";
import Link from "next/link";
import { useScrollMemory } from "@/hooks/use-scroll-memory";
import {
Select,
SelectContent,
@@ -101,7 +102,25 @@ interface ShotStatusClientProps {
canManage: boolean;
}
const SCROLL_KEY = 'shot-status-scroll';
function readStored(key: string): string | null {
try {
return localStorage.getItem(key);
} catch {
return null;
}
}
/** A saved list of open sections, or null when none was saved (or it is unreadable). */
function readStoredList(key: string): string[] | null {
const raw = readStored(key);
if (!raw) return null;
try {
const parsed = JSON.parse(raw);
return Array.isArray(parsed) ? parsed.filter((v): v is string => typeof v === "string") : null;
} catch {
return null;
}
}
const STATUS_CONFIG: Record<string, { label: string; color: string; icon: React.ElementType }> = {
WAITING: { label: "Waiting", color: "bg-zinc-500/10 text-zinc-400 border-zinc-500/20", icon: Clock },
@@ -780,7 +799,6 @@ function ShotTable({
<Link
href={`/projects/${projectId}/shots/${shot.id}`}
className="font-mono text-sm text-white hover:text-blue-400 transition-colors"
onClick={() => sessionStorage.setItem(SCROLL_KEY, String(window.scrollY))}
>
{shot.shotCode}
</Link>
@@ -879,15 +897,6 @@ export function ShotStatusClient({
const router = useRouter();
const { toast } = useToast();
// Restore scroll position when shots load
useEffect(() => {
const saved = sessionStorage.getItem(SCROLL_KEY);
if (saved) {
sessionStorage.removeItem(SCROLL_KEY);
requestAnimationFrame(() => window.scrollTo(0, parseInt(saved, 10)));
}
}, [shots]);
// Restore last selected project if no project is currently selected
useEffect(() => {
if (!selectedProjectId) {
@@ -905,22 +914,39 @@ export function ShotStatusClient({
const [groupMode, setGroupMode] = useState<"flat" | "scene" | "group">("flat");
const [selectedIds, setSelectedIds] = useState<Set<string>>(new Set());
// Load saved episode expand state when project changes
// Which sections were open is restored before the scroll position: until
// they are, the page is shorter and the saved offset would not fit.
const [expandRestored, setExpandRestored] = useState(false);
const pageRef = useRef<HTMLDivElement>(null);
useScrollMemory(selectedProjectId ? `shotStatus:${selectedProjectId}` : null, pageRef, expandRestored);
// Load saved expand state and grouping when the project changes
useEffect(() => {
if (!selectedProjectId) return;
try {
const saved = localStorage.getItem(`shotStatus:${selectedProjectId}:episodeExpand`);
setExpandedEpisodes(saved ? new Set(JSON.parse(saved) as string[]) : new Set());
} catch {
setExpandedEpisodes(new Set());
}
try {
const savedGroup = localStorage.getItem(`shotStatus:${selectedProjectId}:groupMode`);
setGroupMode((savedGroup as "flat" | "scene" | "group") ?? "flat");
} catch {
setGroupMode("flat");
}
setExpandRestored(false);
const mode = (readStored(`shotStatus:${selectedProjectId}:groupMode`) ?? "flat") as "flat" | "scene" | "group";
setGroupMode(mode);
setExpandedEpisodes(new Set(readStoredList(`shotStatus:${selectedProjectId}:episodeExpand`) ?? []));
// Scenes and groups open fully when their mode is first chosen, so with
// nothing saved yet they come back that way rather than all collapsed.
setExpandedScenes(
new Set(readStoredList(`shotStatus:${selectedProjectId}:sceneExpand`) ?? shots.map((s) => s.scene))
);
setExpandedGroups(
new Set(readStoredList(`shotStatus:${selectedProjectId}:groupExpand`) ?? shots.map((s) => s.shotGroup?.id ?? ""))
);
setExpandRestored(true);
// Shots are read only for the defaults; a refresh after an edit must not
// reset what the user has opened.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [selectedProjectId]);
const saveExpanded = (kind: "episodeExpand" | "sceneExpand" | "groupExpand", open: Set<string>) => {
if (!selectedProjectId) return;
try {
localStorage.setItem(`shotStatus:${selectedProjectId}:${kind}`, JSON.stringify([...open]));
} catch {}
};
const [dueDateDialogOpen, setDueDateDialogOpen] = useState(false);
const [assignDialogOpen, setAssignDialogOpen] = useState(false);
const [deleteDialogOpen, setDeleteDialogOpen] = useState(false);
@@ -930,11 +956,7 @@ export function ShotStatusClient({
setExpandedEpisodes((prev) => {
const next = new Set(prev);
next.has(ep) ? next.delete(ep) : next.add(ep);
if (selectedProjectId) {
try {
localStorage.setItem(`shotStatus:${selectedProjectId}:episodeExpand`, JSON.stringify([...next]));
} catch {}
}
saveExpanded("episodeExpand", next);
return next;
});
@@ -942,6 +964,7 @@ export function ShotStatusClient({
setExpandedScenes((prev) => {
const next = new Set(prev);
next.has(sc) ? next.delete(sc) : next.add(sc);
saveExpanded("sceneExpand", next);
return next;
});
@@ -949,13 +972,22 @@ export function ShotStatusClient({
setExpandedGroups((prev) => {
const next = new Set(prev);
next.has(g) ? next.delete(g) : next.add(g);
saveExpanded("groupExpand", next);
return next;
});
const handleGroupMode = (mode: "flat" | "scene" | "group") => {
setGroupMode(mode);
if (mode === "scene") setExpandedScenes(new Set(shots.map((s) => s.scene)));
if (mode === "group") setExpandedGroups(new Set(shots.map((s) => s.shotGroup?.id ?? "")));
if (mode === "scene") {
const all = new Set(shots.map((s) => s.scene));
setExpandedScenes(all);
saveExpanded("sceneExpand", all);
}
if (mode === "group") {
const all = new Set(shots.map((s) => s.shotGroup?.id ?? ""));
setExpandedGroups(all);
saveExpanded("groupExpand", all);
}
if (selectedProjectId) {
try { localStorage.setItem(`shotStatus:${selectedProjectId}:groupMode`, mode); } catch {}
}
@@ -1092,7 +1124,7 @@ export function ShotStatusClient({
const selectionCount = selectedIds.size;
return (
<div className="p-8 space-y-6 max-w-[1600px] mx-auto">
<div ref={pageRef} className="p-8 space-y-6 max-w-[1600px] mx-auto">
{/* Header */}
<div>
<h1 className="text-3xl font-bold text-white">Shot Status</h1>