Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
import { toast } from '@core/component/Toast/Toast';
import { hapticImpact } from '@core/mobile/haptics';
import { isTouchDevice } from '@core/mobile/isTouchDevice';
import { nativeNetworkStatus } from '@core/mobile/native-network-status';
import { fireAndForget } from '@core/util/fireAndForget';
import Spinner from '@icon/system/bold/spinner-bold.svg';
import { cn } from '@ui';
import {
type Accessor,
createEffect,
createMemo,
createSignal,
on,
onCleanup,
Show,
} from 'solid-js';
/** Movement (px) before a touch commits to a direction. Mirrors EntityRow. */
const DIRECTIONALITY_THRESHOLD = 5;
/** Finger-to-indicator damping while pulling. */
const PULL_RESISTANCE = 0.5;
/** Damped pull distance (px) past which releasing triggers a refresh. */
const PULL_THRESHOLD = 60;
/** Slope the overdrag damping settles at — deep overdrag keeps moving at
* this fraction of the base rate instead of hitting a hard stop. */
const OVERDRAG_RESISTANCE = 0.25;
/** Damped px over which the overdrag resistance fades in: the pull's slope
* eases from 1 down to OVERDRAG_RESISTANCE across roughly this distance,
* so crossing the threshold has no felt kink. */
const OVERDRAG_FADE = 40;
/** Retract transition length (ms). */
const SETTLE_MS = 250;
/** Spin floor so near-instant refreshes still read as a completed refresh. */
const MIN_REFRESH_SPIN_MS = 200;
/** Spinner display cap. A refresh still in flight past this retracts the
* badge anyway — the list renders reactively, so rows that arrive later
* still arrive. A slow refresh is not a failed one. */
const REFRESH_SPIN_CAP_MS = 3000;
type PullPhase = 'idle' | 'pulling' | 'refreshing' | 'settling';
type PullGesture = {
startX: number;
startY: number;
/** null until the gesture commits to a direction. */
pulling: boolean | null;
};
/**
* Pull-to-refresh for a touch scroll container: dragging down from the top
* reveals a floating spinner badge that arms past a threshold and, once
* released, spins until `onRefresh` settles or the spin cap elapses.
* Renders nothing off mobile.
*
* Mount inside a `position: relative` wrapper of the scroll container. Pull translates the scroll container down, and the opaque rows sliding away
* are what reveal it. A transform leaves layout and scrollTop untouched,
* so the virtualizer's scroll math is unaffected.
*/
export function PullToRefresh(props: {
/** Element the gesture attaches to and that translates with the pull.
* Usually the scroll container, but a non-scrollable element (e.g. an
* empty state) works too — its scrollTop reads 0, so pulls always start
* from rest. */
scrollContainer: Accessor<HTMLElement | undefined>;
onRefresh: () => Promise<unknown>;
}) {
const [phase, setPhase] = createSignal<PullPhase>('idle');
const [pull, setPull] = createSignal(0);
let gesture: PullGesture | null = null;
let settleTimer: number | undefined;
onCleanup(() => window.clearTimeout(settleTimer));
const retract = () => {
setPhase('settling');
setPull(0);
window.clearTimeout(settleTimer);
settleTimer = window.setTimeout(() => setPhase('idle'), SETTLE_MS);
};
/** The refresh still running, if any. The spin cap retracts the badge
* without cancelling it, so the gesture can return to idle first. */
let attempt: Promise<'ok' | 'failed'> | undefined;
const startRefresh = () => {
const started = props.onRefresh().then(
() => 'ok' as const,
() => 'failed' as const
);
attempt = started;
fireAndForget(
started.then(() => {
if (attempt === started) attempt = undefined;
}),
'pull-to-refresh'
);
return started;
};
const triggerRefresh = () => {
setPhase('refreshing');
setPull(PULL_THRESHOLD);
const minSpin = new Promise((resolve) =>
window.setTimeout(resolve, MIN_REFRESH_SPIN_MS)
);
const cap = new Promise<'pending'>((resolve) =>
window.setTimeout(() => resolve('pending'), REFRESH_SPIN_CAP_MS)
);
// A pull released while the previous refresh is still running re-attaches
// the badge to it instead of starting a second one: overlapping calls
// would clear the caller's in-flight state (which the list reads to keep
// the empty state mounted) as soon as the first settled, swapping the
// gesture's own target element out from under it.
const pending = attempt ?? startRefresh();
fireAndForget(
Promise.all([Promise.race([pending, cap]), minSpin]).then(([outcome]) => {
// Report a failure only when nothing reached the wire. A refresh
// still running at the cap says nothing either way, so it stays
// quiet unless the native monitor is certain there is no path out
// — that case is indistinguishable from success otherwise.
if (outcome === 'failed') {
toast.failure('Failed to refresh');
} else if (
outcome === 'pending' &&
nativeNetworkStatus() === 'offline'
) {
toast.failure("You're offline");
}
retract();
}),
'pull-to-refresh'
);
};
const onTouchStart = (e: TouchEvent) => {
if (!isTouchDevice() || phase() !== 'idle') return;
if (e.touches.length !== 1) return;
const container = props.scrollContainer();
if (!container || container.scrollTop > 0) return;
const touch = e.touches[0];
gesture = { startX: touch.clientX, startY: touch.clientY, pulling: null };
};
const onTouchMove = (e: TouchEvent) => {
if (!gesture) return;
if (e.touches.length !== 1) {
onTouchCancel();
return;
}
const touch = e.touches[0];
const dx = touch.clientX - gesture.startX;
const dy = touch.clientY - gesture.startY;
if (gesture.pulling === null) {
if (
Math.abs(dx) < DIRECTIONALITY_THRESHOLD &&
Math.abs(dy) < DIRECTIONALITY_THRESHOLD
)
return;
// Only a downward, predominantly vertical drag becomes a pull —
// anything else stays with native scrolling / the row swipe handlers.
gesture.pulling = dy > Math.abs(dx);
if (!gesture.pulling) {
gesture = null;
return;
}
setPhase('pulling');
}
// Own the gesture: the indicator tracks the finger instead of the list
// scrolling (the container is already at its top).
if (e.cancelable) e.preventDefault();
const base = Math.max(dy - DIRECTIONALITY_THRESHOLD, 0) * PULL_RESISTANCE;
// Exponential blend past the threshold: slope starts at 1 and eases
// toward OVERDRAG_RESISTANCE, so the extra resistance fades in instead
// of kicking in abruptly.
const over = Math.max(base - PULL_THRESHOLD, 0);
const damped =
Math.min(base, PULL_THRESHOLD) +
OVERDRAG_RESISTANCE * over +
(1 - OVERDRAG_RESISTANCE) *
OVERDRAG_FADE *
(1 - Math.exp(-over / OVERDRAG_FADE));
const wasArmed = pull() >= PULL_THRESHOLD;
setPull(damped);
if (damped >= PULL_THRESHOLD !== wasArmed) hapticImpact('light');
};
const onTouchEnd = () => {
if (!gesture) return;
const wasPulling = gesture.pulling === true;
gesture = null;
if (!wasPulling) return;
if (pull() >= PULL_THRESHOLD) triggerRefresh();
else retract();
};
const onTouchCancel = () => {
if (!gesture) return;
const wasPulling = gesture.pulling === true;
gesture = null;
if (wasPulling) retract();
};
// Dedupe by element identity: the accessor may re-evaluate for reactive
// reasons that leave the element unchanged (e.g. a fallback target derived
// from fetch state), and re-running the effect would reset the transform
// mid-refresh — a visible bounce.
const scrollContainer = createMemo(() => props.scrollContainer());
createEffect(
on(scrollContainer, (el) => {
if (!el) return;
el.addEventListener('touchstart', onTouchStart, { passive: true });
el.addEventListener('touchmove', onTouchMove, { passive: false });
el.addEventListener('touchend', onTouchEnd, { passive: true });
el.addEventListener('touchcancel', onTouchCancel, { passive: true });
// The list follows the finger: translate the scroll container by the
// damped pull, tracking directly while pulling and easing back
// otherwise (mirroring the badge's settle transition).
createEffect(() => {
el.style.transform = pull() > 0 ? `translateY(${pull()}px)` : '';
el.style.transition =
phase() === 'pulling' ? '' : `transform ${SETTLE_MS}ms ease-out`;
});
onCleanup(() => {
// The target can swap mid-gesture (empty state ↔ list when a refresh
// lands rows); the old element takes its listeners with it, so drop
// the gesture here or the phase wedges in 'pulling'.
onTouchCancel();
el.style.transform = '';
el.style.transition = '';
el.removeEventListener('touchstart', onTouchStart);
el.removeEventListener('touchmove', onTouchMove);
el.removeEventListener('touchend', onTouchEnd);
el.removeEventListener('touchcancel', onTouchCancel);
});
})
);
const progress = () => Math.min(pull() / PULL_THRESHOLD, 1);
const isRefreshing = () => phase() === 'refreshing';
return (
<Show when={isTouchDevice()}>
<div
class="pointer-events-none absolute inset-x-0 flex justify-center"
style={{
top: 'calc(var(--mobile-content-inset-top, 0px) + var(--mobile-chrome-gutter, 12px) + 4px)',
}}
aria-hidden
>
<div
class="flex items-center justify-center rounded-full"
style={{
opacity: Math.min(progress() * 1.5, 1),
transition:
phase() === 'pulling'
? undefined
: `opacity ${SETTLE_MS}ms ease-out`,
}}
>
<Spinner
class={cn('size-7', isRefreshing() && 'animate-spin')}
style={
isRefreshing()
? undefined
: { transform: `rotate(${pull() * 3}deg)` }
}
/>
</div>
</div>
</Show>
);
}