Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class AutocompleteController extends AbstractController {
declare store: AutocompleteStore;
declare config: AutocompleteControllerConfig;
public storage: StorageStore;
private lastSearchQuery: string | undefined;
private lastSearchKey: string | undefined;

private events: {
[responseId: string]: {
Expand Down Expand Up @@ -773,8 +773,8 @@ export class AutocompleteController extends AbstractController {

const responseId = response.tracking.responseId;
this.events[responseId] = this.events[responseId] || { product: {}, banner: {} };

if (response.search?.query === this.lastSearchQuery) {
const currentSearchKey = response.search?.query + JSON.stringify(this.urlManager.state.filter || {});
if (currentSearchKey === this.lastSearchKey) {
const impressedResultIds = Object.keys(this.events[responseId].product || {}).filter(
(resultId) => this.events[responseId].product?.[resultId]?.impression
);
Expand All @@ -787,7 +787,7 @@ export class AutocompleteController extends AbstractController {
};
} else {
this.events[responseId] = { product: {}, banner: {} };
this.lastSearchQuery = response.search?.query;
this.lastSearchKey = currentSearchKey;
}

const afterSearchProfile = this.profiler.create({ type: 'event', name: 'afterSearch', context: params }).start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ export interface UseIntersectionOptions {
}

const VISIBILITY_POLL_INTERVAL = 250;
export const useIntersectionAdvanced = (ref: MutableRef<HTMLElement | null>, options: UseIntersectionOptions = {}): boolean => {
export const useIntersectionAdvanced = (
ref: MutableRef<HTMLElement | null>,
options: UseIntersectionOptions = {}
): { inViewport: boolean; updateRef: (el: HTMLElement | null) => void } => {
const { rootMargin = '0px', fireOnce = false, threshold = 0, minVisibleTime = 0, resetKey } = options;
// State and setter for storing whether element is visible
const [isIntersecting, setIntersecting] = useState<boolean>(false);
Expand All @@ -22,6 +25,15 @@ export const useIntersectionAdvanced = (ref: MutableRef<HTMLElement | null>, opt
// Track the last reset key to detect changes
const lastResetKeyRef = useRef<string | undefined>(resetKey);

const [counter, setCounter] = useState(0);
const updateRef = (el: HTMLElement | null) => {
// setting ref.current does not update useEffect, counter used to force useEffect
if (!ref.current || ref.current !== el) {
ref.current = el;
setCounter((c) => c + 1);
}
};
Comment on lines +29 to +35
Copy link

Copilot AI Feb 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updateRef is recreated on every render, which makes downstream memoization (e.g., the useCallback in withTracking) ineffective and can cause extra rerenders. Wrap updateRef in useCallback (and, if needed, switch counter to useReducer/useRef-based invalidation) so consumers can keep a stable function reference.

Copilot uses AI. Check for mistakes.

// Reset state if resetKey has changed
if (resetKey !== lastResetKeyRef.current) {
setIntersecting(false);
Copy link

Copilot AI Feb 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setIntersecting(false) is executed during render (inside the if (resetKey !== lastResetKeyRef.current) block). Updating state during render can cause render loops and React/Preact warnings. Move the reset logic into an effect that runs when resetKey changes (e.g., useEffect(() => { ... }, [resetKey])) and avoid returning early from the render path.

Copilot uses AI. Check for mistakes.
Expand All @@ -31,7 +43,7 @@ export const useIntersectionAdvanced = (ref: MutableRef<HTMLElement | null>, opt
}
visibleStartRef.current = null;
lastResetKeyRef.current = resetKey;
return false;
return { inViewport: false, updateRef };
}

useEffect(() => {
Expand Down Expand Up @@ -140,9 +152,9 @@ export const useIntersectionAdvanced = (ref: MutableRef<HTMLElement | null>, opt
observer.unobserve(ref.current);
}
};
}, [ref, resetKey]);
}, [ref, resetKey, counter]);

return isIntersecting;
return { inViewport: isIntersecting, updateRef };
};

function elementIsVisible(el: HTMLElement): boolean {
Expand Down
11 changes: 9 additions & 2 deletions packages/snap-preact-components/src/providers/withTracking.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export function withTracking<Props extends WithTrackingProps>(WrappedComponent:
});
}

const { ref, inViewport } = createImpressionObserver({ resetKey });
const { ref, inViewport, updateRef } = createImpressionObserver({ resetKey });

if (inViewport) {
// TODO: add support for disabling tracking events via config like in ResultTracker
Expand Down Expand Up @@ -90,7 +90,14 @@ export function withTracking<Props extends WithTrackingProps>(WrappedComponent:
banner,
type,
content,
trackingRef: ref,
trackingRef: useCallback(
(el: HTMLElement | null) => {
if (!ref.current) {
updateRef(el);
}
},
[ref, updateRef]
),
};

return <WrappedComponent {...(trackingProps as Props)} />;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ const IMPRESSION_MIN_VISIBLE_TIME = 1000;
export function createImpressionObserver(options?: UseIntersectionOptions): {
ref: Ref<HTMLElement | null>;
inViewport: boolean;
updateRef: (el: HTMLElement | null) => void;
} {
const ref = useRef<HTMLElement>(null);
const inViewport = useIntersectionAdvanced(ref, {
const ref = useRef<HTMLElement | null>(null);
const { inViewport, updateRef } = useIntersectionAdvanced(ref, {
...options,
fireOnce: true,
threshold: IMPRESSION_VISIBILITY_THRESHOLD,
Expand All @@ -17,5 +18,6 @@ export function createImpressionObserver(options?: UseIntersectionOptions): {
return {
ref,
inViewport,
updateRef,
};
}
4 changes: 2 additions & 2 deletions packages/snap-preact-demo/src/components/Results/Results.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ export class Results extends Component<ResultsProps> {
const infiniteEnabled = Boolean(controller.config.settings.infinite);
const infiniteRef = useRef(null);
if (infiniteEnabled) {
const atBottom = useIntersectionAdvanced(infiniteRef, {
const { inViewport } = useIntersectionAdvanced(infiniteRef, {
rootMargin: '0px',
threshold: 1,
minVisibleTime: 300,
});

if (atBottom && pagination.next && !loading && pagination.totalResults > 0) {
if (inViewport && pagination.next && !loading && pagination.totalResults > 0) {
setTimeout(() => {
pagination.next.url.go({ history: 'replace' });
});
Expand Down
Loading