Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(mobile): pull to refresh improvement (#6085)

Peter Chinman5f1b0cca64bf
diff --git a/apps/web/src/components/app/mobile/PullToRefresh.tsx b/apps/web/src/components/app/mobile/PullToRefresh.tsx
index f49885d3e7..774d74524e 100644
--- a/apps/web/src/components/app/mobile/PullToRefresh.tsx
+++ b/apps/web/src/components/app/mobile/PullToRefresh.tsx
@@ -1,6 +1,7 @@
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 Spinner from '@phosphor-icons/core/bold/spinner-bold.svg';
import { cn } from '@ui';
import {
@@ -30,11 +31,10 @@ const OVERDRAG_FADE = 40;
const SETTLE_MS = 250;
/** Spin floor so near-instant refreshes still read as a completed refresh. */
const MIN_REFRESH_SPIN_MS = 200;
-/** Refreshes running longer than this are treated as failed. Bounds the
- * spinner when connectivity is gone: refetches can pause or hang
- * indefinitely rather than reject (navigator.onLine is unreliable in
- * WKWebView, so the offline case can't be detected up front). */
-const REFRESH_TIMEOUT_MS = 8000;
+/** 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';
@@ -48,7 +48,8 @@ type PullGesture = {
/**
* 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. Renders nothing off mobile.
+ * 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,
@@ -76,6 +77,22 @@ export function PullToRefresh(props: {
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;
+ void started.then(() => {
+ if (attempt === started) attempt = undefined;
+ });
+ return started;
+ };
+
const triggerRefresh = () => {
setPhase('refreshing');
setPull(PULL_THRESHOLD);
@@ -83,21 +100,33 @@ export function PullToRefresh(props: {
const minSpin = new Promise((resolve) =>
window.setTimeout(resolve, MIN_REFRESH_SPIN_MS)
);
- const timeout = new Promise((_, reject) =>
- window.setTimeout(
- () => reject(new Error('Refresh timed out')),
- REFRESH_TIMEOUT_MS
- )
+ const cap = new Promise<'pending'>((resolve) =>
+ window.setTimeout(() => resolve('pending'), REFRESH_SPIN_CAP_MS)
);
- void Promise.allSettled([
- Promise.race([props.onRefresh(), timeout]),
- minSpin,
- ]).then(([refreshResult]) => {
- if (refreshResult.status === 'rejected') {
- toast.failure('Failed to refresh');
+ // 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();
+
+ void 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();
}
- retract();
- });
+ );
};
const onTouchStart = (e: TouchEvent) => {
@@ -224,7 +253,7 @@ export function PullToRefresh(props: {
<div
class="pointer-events-none absolute inset-x-0 flex justify-center"
style={{
- top: 'var(--mobile-content-inset-top, 0px)',
+ top: 'calc(var(--mobile-content-inset-top, 0px) + var(--mobile-chrome-gutter, 12px) + 4px)',
}}
aria-hidden
>
diff --git a/apps/web/src/features/next-soup/soup-view/create-search-state.ts b/apps/web/src/features/next-soup/soup-view/create-search-state.ts
index 60338ea71e..e341879a3b 100644
--- a/apps/web/src/features/next-soup/soup-view/create-search-state.ts
+++ b/apps/web/src/features/next-soup/soup-view/create-search-state.ts
@@ -305,8 +305,9 @@ export const createSearchState = ({
return {
searchText,
setSearchText,
- isSearching: search.isSearching,
localFuzzyResults: search.localFuzzyResults,
+ refresh: search.refresh,
+ isSearching: search.isSearching,
serviceSearchResults: search.serviceSearchResults,
featuredIds: search.featuredIds,
searchQuery: search.searchQuery,
diff --git a/apps/web/src/features/next-soup/soup-view/soup-view-context.tsx b/apps/web/src/features/next-soup/soup-view/soup-view-context.tsx
index 0e3e31bea0..e77d1ebefc 100644
--- a/apps/web/src/features/next-soup/soup-view/soup-view-context.tsx
+++ b/apps/web/src/features/next-soup/soup-view/soup-view-context.tsx
@@ -118,8 +118,11 @@ type DataSource<T> = {
hasNextPage: Accessor<boolean>;
fetchNextPage: VoidFunction;
/**
- * Full refresh (e.g. mobile pull-to-refresh): invalidate every soup query
- * plus notification state. Resolves once the active refetches settle.
+ * Full refresh (e.g. mobile pull-to-refresh): starts invalidation of every
+ * soup query plus notification state, then resolves once the refetch of the
+ * list currently on screen settles — rejecting when that refetch fails. The
+ * invalidations are not awaited, so another panel's queries can neither
+ * delay this refresh nor report it as failed.
*/
refresh: () => Promise<void>;
};
@@ -1493,19 +1496,31 @@ export const SoupViewContextProvider: FlowComponent<
resetToInitialPage();
- await Promise.all([
- queryClient.invalidateQueries(
- { queryKey: soupKeys._def },
- // Reject on refetch failure so pull-to-refresh can surface it
- // instead of retracting as if the refresh succeeded.
- { throwOnError: true }
- ),
- // urql pages are outside the TanStack cache invalidation above.
- itemsQuery.transport === 'graphql'
- ? itemsQuery.refresh()
- : Promise.resolve(),
- invalidateUserNotifications(),
- ]);
+ // Reconcile everything else in the background. Awaiting it would hold
+ // the caller (mobile pull-to-refresh) hostage to every active soup
+ // query in the app — other mounted panels refetch their whole page
+ // chain one request at a time — and would let an unrelated view's
+ // failure report the visible refresh as failed.
+ void queryClient
+ .invalidateQueries({ queryKey: soupKeys._def })
+ .catch(() => undefined);
+ void invalidateUserNotifications().catch(() => undefined);
+
+ // Only the visible list decides the outcome. It throws on refetch
+ // failure, so pull-to-refresh can tell "nothing came back" apart from
+ // "still on its way" instead of retracting as if it had succeeded.
+
+ // Search renders its own results and disables the items query, whose
+ // refresh then no-ops — awaiting that would settle as success without
+ // anything on screen having been refetched.
+ if (search.isSearching()) {
+ await search.refresh();
+ return;
+ }
+
+ // This covers both transports: urql pages sit outside the TanStack
+ // invalidation above, and the REST refetch dedupes against it.
+ await itemsQuery.refresh();
},
},
items,
diff --git a/apps/web/src/features/soup/search/create-search-state.ts b/apps/web/src/features/soup/search/create-search-state.ts
index 4371767dcb..ae45862192 100644
--- a/apps/web/src/features/soup/search/create-search-state.ts
+++ b/apps/web/src/features/soup/search/create-search-state.ts
@@ -178,6 +178,14 @@ export function createSearchState(options: CreateSearchStateOptions) {
isFetchingNextPage: () => searchQuery.isFetchingNextPage,
fetchNextPage: () => searchQuery.fetchNextPage(),
refetch: () => searchQuery.refetch(),
+ /** Refetches the service results, throwing on failure so a caller (mobile
+ * pull-to-refresh) can report the outcome. Resolves without a request
+ * when the service query is disabled — a short or paused query renders
+ * local fuzzy matches, which have no network source to refetch. */
+ refresh: async () => {
+ if (!searchQuery.isEnabled) return;
+ await searchQuery.refetch({ throwOnError: true });
+ },
};
}