mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 11:07:40 +01:00
feat(mobile): pull-to-refresh on /today + /control
~60-line client component that wraps a route's children, attaches window-level touch listeners, and fires router.refresh() when the user swipes down past 80px while at scrollTop=0. Mobile-only via md:hidden indicator + scrollTop guard so desktop sees nothing. Behaviors: - Damped pull (0.5x finger travel) — feels natural, max-clamped at 120px. - Indicator: small RefreshCw chip that fades in, rotates with pull progress, then animate-spins once armed (≥80px). - Commit haptic at 20ms (deeper than the 10ms create/edit tick — confirms "you triggered the refresh"). - 700ms hold after refresh so the user sees the spin, then snap back. - Window listeners (not container-scoped) so the gesture works regardless of which scroll container started receiving touches. Mounted on the two status surfaces the original Elon-led mobile review called out (item #13). Other routes can opt in by wrapping their content. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
3 files changed
+118
-3
No files matched your search
@@ -1,12 +1,15 @@
|
||||
import { PageLayout } from "@/components/ui/page-layout";
|
||||
import { ControlPanel } from "@/components/control/ControlPanel";
|
||||
import { PullToRefresh } from "@/components/shared/PullToRefresh";
|
||||
|
||||
export const metadata = { title: "Control" };
|
||||
|
||||
export default function ControlPage() {
|
||||
return (
|
||||
<PageLayout title="Control" subtitle="Live agent state, project readiness, and next actions">
|
||||
<ControlPanel />
|
||||
</PageLayout>
|
||||
<PullToRefresh>
|
||||
<PageLayout title="Control" subtitle="Live agent state, project readiness, and next actions">
|
||||
<ControlPanel />
|
||||
</PageLayout>
|
||||
</PullToRefresh>
|
||||
);
|
||||
}
|
||||
@@ -22,6 +22,7 @@ import { IvyDispatchButton } from "@/components/shared/IvyDispatchButton";
|
||||
import { requirePageUserId, getCurrentUserName } from "@/lib/session";
|
||||
import { getUserProjects, getOrgProjects } from "@/db/queries/user-projects";
|
||||
import { PLAN_DAY_PROMPT, WRAP_UP_PROMPT } from "@/lib/constants/today";
|
||||
import { PullToRefresh } from "@/components/shared/PullToRefresh";
|
||||
|
||||
export const metadata = { title: "Today" };
|
||||
|
||||
@@ -35,6 +36,7 @@ export default async function TodayPage() {
|
||||
const hour = new Date().getHours();
|
||||
const isEvening = hour >= 17;
|
||||
return (
|
||||
<PullToRefresh>
|
||||
<div className="app-page max-w-4xl space-y-6">
|
||||
<div>
|
||||
<Greeting name={name} />
|
||||
@@ -121,5 +123,6 @@ export default async function TodayPage() {
|
||||
</Suspense>
|
||||
</div>
|
||||
</div>
|
||||
</PullToRefresh>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { RefreshCw } from "lucide-react";
|
||||
import { haptic } from "@/lib/haptics";
|
||||
|
||||
/**
|
||||
* Mobile-only pull-to-refresh. Swipe-down past 80px at scrollTop=0 fires
|
||||
* router.refresh() — useful for status surfaces (/today, /control) where the
|
||||
* user wants to manually pull the latest server data without a hard reload.
|
||||
*
|
||||
* Wraps children with no layout impact: the indicator is a fixed-position
|
||||
* overlay that fades in as the user pulls, snaps back if the threshold isn't
|
||||
* reached, and locks at the threshold position during the actual refresh.
|
||||
*
|
||||
* Touch listeners are attached to `window` so the gesture works no matter
|
||||
* which scroll container the user starts from. Bails out cleanly on
|
||||
* non-touch devices — desktop users see nothing.
|
||||
*/
|
||||
const THRESHOLD = 80;
|
||||
const DAMPING = 0.5;
|
||||
|
||||
export function PullToRefresh({ children }: { children: React.ReactNode }) {
|
||||
const router = useRouter();
|
||||
const [pull, setPull] = useState(0);
|
||||
const [refreshing, setRefreshing] = useState(false);
|
||||
const startY = useRef<number | null>(null);
|
||||
const pullRef = useRef(0);
|
||||
|
||||
useEffect(() => {
|
||||
pullRef.current = pull;
|
||||
}, [pull]);
|
||||
|
||||
useEffect(() => {
|
||||
const scrollContainer = document.querySelector(".app-main") as HTMLElement | null;
|
||||
const getScrollTop = () =>
|
||||
scrollContainer ? scrollContainer.scrollTop : window.scrollY;
|
||||
|
||||
const onTouchStart = (e: TouchEvent) => {
|
||||
if (refreshing) return;
|
||||
if (getScrollTop() > 0) { startY.current = null; return; }
|
||||
startY.current = e.touches[0]!.clientY;
|
||||
};
|
||||
|
||||
const onTouchMove = (e: TouchEvent) => {
|
||||
if (startY.current === null || refreshing) return;
|
||||
const dy = e.touches[0]!.clientY - startY.current;
|
||||
if (dy <= 0) { setPull(0); return; }
|
||||
setPull(Math.min(dy * DAMPING, THRESHOLD * 1.5));
|
||||
};
|
||||
|
||||
const onTouchEnd = () => {
|
||||
if (startY.current === null) { setPull(0); return; }
|
||||
const triggered = pullRef.current >= THRESHOLD;
|
||||
startY.current = null;
|
||||
if (triggered) {
|
||||
haptic(20);
|
||||
setRefreshing(true);
|
||||
setPull(THRESHOLD);
|
||||
router.refresh();
|
||||
// Hold the indicator briefly so the user sees the refresh, then snap back.
|
||||
setTimeout(() => {
|
||||
setRefreshing(false);
|
||||
setPull(0);
|
||||
}, 700);
|
||||
} else {
|
||||
setPull(0);
|
||||
}
|
||||
};
|
||||
|
||||
window.addEventListener("touchstart", onTouchStart, { passive: true });
|
||||
window.addEventListener("touchmove", onTouchMove, { passive: true });
|
||||
window.addEventListener("touchend", onTouchEnd);
|
||||
window.addEventListener("touchcancel", onTouchEnd);
|
||||
return () => {
|
||||
window.removeEventListener("touchstart", onTouchStart);
|
||||
window.removeEventListener("touchmove", onTouchMove);
|
||||
window.removeEventListener("touchend", onTouchEnd);
|
||||
window.removeEventListener("touchcancel", onTouchEnd);
|
||||
};
|
||||
}, [refreshing, router]);
|
||||
|
||||
const indicatorVisible = pull > 8;
|
||||
const armed = pull >= THRESHOLD;
|
||||
|
||||
return (
|
||||
<>
|
||||
{indicatorVisible && (
|
||||
<div
|
||||
className="pointer-events-none fixed inset-x-0 top-0 z-30 flex justify-center md:hidden"
|
||||
style={{
|
||||
transform: `translateY(${Math.max(0, pull - 16)}px)`,
|
||||
opacity: Math.min(1, pull / THRESHOLD),
|
||||
transition: refreshing || pull === 0 ? "transform 200ms ease, opacity 200ms ease" : "none",
|
||||
}}
|
||||
>
|
||||
<div className="mt-2 rounded-full border border-border-subtle bg-surface-overlay p-2 shadow-panel">
|
||||
<RefreshCw
|
||||
className={`h-4 w-4 text-text-secondary ${refreshing || armed ? "animate-spin" : ""}`}
|
||||
style={refreshing || armed ? undefined : { transform: `rotate(${pull * 4}deg)` }}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
{children}
|
||||
</>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user