MCPcopy Create free account
hub / github.com/dailydotdev/apps / usePatchyIdle

Function usePatchyIdle

packages/shared/src/features/patchy/patchyIdle.ts:96–193  ·  view source on GitHub ↗
({
  isEnabled,
  isResting,
  isReading,
  prefersReducedMotion,
  canBlink = true,
}: UsePatchyIdleProps)

Source from the content-addressed store, hash-verified

94const still: PatchyIdleState = { face: null, motion: null, key: 0 };
95
96export const usePatchyIdle = ({
97 isEnabled,
98 isResting,
99 isReading,
100 prefersReducedMotion,
101 canBlink = true,
102}: UsePatchyIdleProps): PatchyIdleState => {
103 const [state, setState] = useState<PatchyIdleState>(still);
104 const keyRef = useRef(0);
105 const readingRef = useRef(isReading);
106 readingRef.current = isReading;
107
108 useEffect(() => {
109 if (!isEnabled || isResting) {
110 setState(still);
111 return undefined;
112 }
113 const timers = new Set<ReturnType<typeof setTimeout>>();
114 let isBusy = false;
115 const later = (fn: () => void, ms: number) => {
116 const id = setTimeout(() => {
117 timers.delete(id);
118 fn();
119 }, ms);
120 timers.add(id);
121 };
122 const play = (name: PatchyIdleActionName) => {
123 if (document.hidden) {
124 return;
125 }
126 const action: PatchyIdleAction = patchyIdleActions[name];
127 isBusy = true;
128 keyRef.current += 1;
129 setState({
130 face: action.face,
131 // Animated faces bring their own motion.
132 motion:
133 prefersReducedMotion || isPatchyFaceAnimated(action.face)
134 ? null
135 : (action.motion ?? null),
136 key: keyRef.current,
137 });
138 later(() => {
139 isBusy = false;
140 setState((current) => ({ ...current, face: null, motion: null }));
141 }, action.ms);
142 };
143 const blinkLoop = () => {
144 later(() => {
145 if (!isBusy) {
146 play('blink');
147 }
148 blinkLoop();
149 }, between(BLINK_EVERY_MS));
150 };
151 const actionLoop = () => {
152 later(() => {
153 if (!isBusy && !prefersReducedMotion) {

Callers 1

usePatchyCompanionFunction · 0.90

Calls 2

blinkLoopFunction · 0.85
actionLoopFunction · 0.85

Tested by

no test coverage detected