analytics.showViewed({...}) reads better than track('show_viewed', {...}) and
drops the track_ alias Captions needed, where vidstack already exports track.
The methods are derived from the event catalogue with a mapped type plus a
Proxy rather than written out, so the catalogue stays the only place an event
is declared and the two cannot drift. Names stay snake_case on the wire.
Emitters are cached so a method is referentially stable and safe to pass to a
hook or use as an effect dependency.
Events with no payload are typed void and take no arguments.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
32 lines
1021 B
TypeScript
32 lines
1021 B
TypeScript
import { useEffect, useRef } from 'react';
|
|
|
|
// Stands in for TypeScript 5.4's NoInfer. Without it the payload widens literal unions such as
|
|
// `'page' | 'modal'` to `string` instead of being checked against the emitter's type.
|
|
type NoInfer<Type> = [Type][Type extends unknown ? 0 : never];
|
|
|
|
/**
|
|
* Fires an emitter once per distinct `key`, skipping while it is nullish. Keeps view style
|
|
* events (a show opened, a search resolved) to a single line at the call site instead of an
|
|
* effect plus a ref in every component that needs one.
|
|
*/
|
|
const useTrackOnce = <Properties>(
|
|
emit: (properties: Properties) => void,
|
|
key: string | number | null | undefined,
|
|
payload: () => NoInfer<Properties>
|
|
) => {
|
|
const payloadRef = useRef(payload);
|
|
const sentRef = useRef<string | number | null>(null);
|
|
|
|
payloadRef.current = payload;
|
|
|
|
useEffect(() => {
|
|
if (key == null || sentRef.current === key) return;
|
|
|
|
sentRef.current = key;
|
|
|
|
emit(payloadRef.current());
|
|
}, [emit, key]);
|
|
};
|
|
|
|
export default useTrackOnce;
|