yaver-feedback-react-native 0.7.16 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,332 @@
1
+ import React, { useCallback, useEffect, useRef, useState } from 'react';
2
+ import {
3
+ Animated,
4
+ DeviceEventEmitter,
5
+ Dimensions,
6
+ NativeModules,
7
+ PanResponder,
8
+ Platform,
9
+ Pressable,
10
+ StyleSheet,
11
+ Text,
12
+ View,
13
+ } from 'react-native';
14
+ import { YaverFeedback } from './YaverFeedback';
15
+ import { getQuickIconDisabled, setQuickIconDisabled } from './preferences';
16
+
17
+ // Mirror the suppression rule used by YaverFeedback + ShakeDetector:
18
+ // when loaded through Yaver's super-host Hermes bundle, the host owns
19
+ // shake + reload UX. We must not render a second action surface.
20
+ function isRunningInsideYaverHost(): boolean {
21
+ try {
22
+ return !!(NativeModules as any)?.YaverInfo;
23
+ } catch {
24
+ return false;
25
+ }
26
+ }
27
+
28
+ const DEFAULT_SIZE = 44;
29
+ const DEFAULT_COLOR = '#6366f1';
30
+ const LONG_PRESS_MS = 550;
31
+
32
+ export interface QuickActionIconProps {
33
+ /** Override the color from FeedbackConfig.quickIconColor. */
34
+ color?: string;
35
+ /** Override the initial position from FeedbackConfig.quickIconInitialPosition. */
36
+ initialPosition?: { x: number; y: number };
37
+ /** Override the icon diameter. Default 44. */
38
+ size?: number;
39
+ }
40
+
41
+ /**
42
+ * Small tap-to-open icon for the Yaver Feedback SDK.
43
+ *
44
+ * Default UX:
45
+ * - **Tap** opens the feedback modal (same as shake).
46
+ * - **Long-press** (~550ms) opens a menu with "Open feedback" and
47
+ * "Hide icon". Hiding is persisted to AsyncStorage so the user's
48
+ * decision survives app relaunches.
49
+ * - **Drag** repositions the icon.
50
+ *
51
+ * Shake always keeps working independently — even when the icon is
52
+ * hidden the user can still shake to open feedback.
53
+ *
54
+ * Visibility is controlled by `FeedbackConfig.quickIcon`:
55
+ * - `'auto'` (default) → `'always'` on iOS/Android, `'off'` on web.
56
+ * - `'always'` → visible from first render.
57
+ * - `'after-shake'` → hidden until `yaverFeedback:firstShake` fires.
58
+ * - `'off'` → never rendered.
59
+ *
60
+ * Suppressed entirely when the SDK is loaded inside Yaver's super-host
61
+ * (the Yaver mobile app owns the shake gesture + overlay in that case).
62
+ */
63
+ export const QuickActionIcon: React.FC<QuickActionIconProps> = ({
64
+ color: colorProp,
65
+ initialPosition: initialPositionProp,
66
+ size = DEFAULT_SIZE,
67
+ }) => {
68
+ const config = YaverFeedback.getConfig();
69
+
70
+ const mode: 'always' | 'after-shake' | 'off' = (() => {
71
+ const raw = config?.quickIcon ?? 'auto';
72
+ if (raw === 'auto') {
73
+ return Platform.OS === 'web' ? 'off' : 'always';
74
+ }
75
+ return raw;
76
+ })();
77
+
78
+ const color = colorProp ?? config?.quickIconColor ?? DEFAULT_COLOR;
79
+
80
+ const { width, height } = Dimensions.get('window');
81
+ const defaultStart =
82
+ initialPositionProp ??
83
+ config?.quickIconInitialPosition ?? {
84
+ x: Math.max(width - size - 14, 0),
85
+ y: Math.max(Math.floor(height * 0.35), 80),
86
+ };
87
+
88
+ const pan = useRef(new Animated.ValueXY(defaultStart)).current;
89
+ const lastPos = useRef(defaultStart);
90
+ const dragStart = useRef<{ x: number; y: number } | null>(null);
91
+ const didDrag = useRef(false);
92
+
93
+ const [userDisabled, setUserDisabled] = useState<boolean | null>(null);
94
+ const [shakenThisSession, setShakenThisSession] = useState(false);
95
+ const [menuOpen, setMenuOpen] = useState(false);
96
+ const [hostSuppressed] = useState<boolean>(() => isRunningInsideYaverHost());
97
+
98
+ // Load the persisted disable flag once on mount. Until it resolves we
99
+ // render nothing — a one-frame flash of the icon before hiding would
100
+ // be worse than a tiny delayed appearance.
101
+ useEffect(() => {
102
+ let alive = true;
103
+ getQuickIconDisabled().then((v) => {
104
+ if (alive) setUserDisabled(v);
105
+ });
106
+ return () => {
107
+ alive = false;
108
+ };
109
+ }, []);
110
+
111
+ // `after-shake` mode waits for the first shake before revealing
112
+ // itself. YaverFeedback emits this event from its shake callback.
113
+ useEffect(() => {
114
+ const sub = DeviceEventEmitter.addListener(
115
+ 'yaverFeedback:firstShake',
116
+ () => setShakenThisSession(true),
117
+ );
118
+ return () => sub.remove();
119
+ }, []);
120
+
121
+ // Programmatic control: host apps can call
122
+ // `YaverFeedback.setQuickIconVisible(true)` to re-surface the icon
123
+ // after the user hid it (e.g. from a settings screen).
124
+ useEffect(() => {
125
+ const showSub = DeviceEventEmitter.addListener(
126
+ 'yaverFeedback:quickIconShow',
127
+ () => {
128
+ setUserDisabled(false);
129
+ void setQuickIconDisabled(false);
130
+ },
131
+ );
132
+ const hideSub = DeviceEventEmitter.addListener(
133
+ 'yaverFeedback:quickIconHide',
134
+ () => {
135
+ setUserDisabled(true);
136
+ void setQuickIconDisabled(true);
137
+ setMenuOpen(false);
138
+ },
139
+ );
140
+ return () => {
141
+ showSub.remove();
142
+ hideSub.remove();
143
+ };
144
+ }, []);
145
+
146
+ const panResponder = useRef(
147
+ PanResponder.create({
148
+ onStartShouldSetPanResponder: () => true,
149
+ onMoveShouldSetPanResponder: (_, g) =>
150
+ Math.abs(g.dx) > 3 || Math.abs(g.dy) > 3,
151
+ onPanResponderGrant: () => {
152
+ didDrag.current = false;
153
+ dragStart.current = { ...lastPos.current };
154
+ pan.setOffset({ x: lastPos.current.x, y: lastPos.current.y });
155
+ pan.setValue({ x: 0, y: 0 });
156
+ },
157
+ onPanResponderMove: (_, g) => {
158
+ if (Math.abs(g.dx) > 3 || Math.abs(g.dy) > 3) {
159
+ didDrag.current = true;
160
+ }
161
+ Animated.event([null, { dx: pan.x, dy: pan.y }], {
162
+ useNativeDriver: false,
163
+ })(_, g);
164
+ },
165
+ onPanResponderRelease: (_, g) => {
166
+ pan.flattenOffset();
167
+ const start = dragStart.current ?? lastPos.current;
168
+ const maxX = Math.max(width - size, 0);
169
+ const maxY = Math.max(height - size, 0);
170
+ const nextX = Math.max(0, Math.min(maxX, start.x + g.dx));
171
+ const nextY = Math.max(0, Math.min(maxY, start.y + g.dy));
172
+ lastPos.current = { x: nextX, y: nextY };
173
+ Animated.spring(pan, {
174
+ toValue: { x: nextX, y: nextY },
175
+ useNativeDriver: false,
176
+ friction: 7,
177
+ }).start();
178
+ },
179
+ }),
180
+ ).current;
181
+
182
+ const openFeedback = useCallback(() => {
183
+ setMenuOpen(false);
184
+ void YaverFeedback.startReport();
185
+ }, []);
186
+
187
+ const hideForever = useCallback(() => {
188
+ setMenuOpen(false);
189
+ setUserDisabled(true);
190
+ void setQuickIconDisabled(true);
191
+ }, []);
192
+
193
+ if (hostSuppressed) return null;
194
+ if (mode === 'off') return null;
195
+ if (userDisabled === null) return null;
196
+ if (userDisabled) return null;
197
+ if (mode === 'after-shake' && !shakenThisSession) return null;
198
+ if (!YaverFeedback.isEnabled()) return null;
199
+
200
+ const visualSize = size;
201
+ const radius = visualSize / 2;
202
+
203
+ return (
204
+ <Animated.View
205
+ pointerEvents="box-none"
206
+ style={[
207
+ StyleSheet.absoluteFill,
208
+ { zIndex: 9998 },
209
+ ]}
210
+ >
211
+ <Animated.View
212
+ {...panResponder.panHandlers}
213
+ style={[
214
+ styles.container,
215
+ {
216
+ transform: [{ translateX: pan.x }, { translateY: pan.y }],
217
+ },
218
+ ]}
219
+ >
220
+ <Pressable
221
+ onPress={() => {
222
+ if (didDrag.current) {
223
+ didDrag.current = false;
224
+ return;
225
+ }
226
+ openFeedback();
227
+ }}
228
+ onLongPress={() => {
229
+ if (didDrag.current) return;
230
+ setMenuOpen((m) => !m);
231
+ }}
232
+ delayLongPress={LONG_PRESS_MS}
233
+ hitSlop={6}
234
+ accessibilityRole="button"
235
+ accessibilityLabel="Open Yaver feedback"
236
+ style={({ pressed }) => [
237
+ styles.icon,
238
+ {
239
+ width: visualSize,
240
+ height: visualSize,
241
+ borderRadius: radius,
242
+ backgroundColor: color,
243
+ opacity: pressed ? 0.85 : 1,
244
+ },
245
+ ]}
246
+ >
247
+ <Text style={[styles.iconLabel, { fontSize: Math.round(visualSize * 0.5) }]}>y</Text>
248
+ </Pressable>
249
+ {menuOpen ? (
250
+ <View style={styles.menu}>
251
+ <Pressable
252
+ onPress={openFeedback}
253
+ style={({ pressed }) => [
254
+ styles.menuItem,
255
+ pressed && styles.menuItemPressed,
256
+ ]}
257
+ >
258
+ <Text style={styles.menuItemText}>Open feedback</Text>
259
+ </Pressable>
260
+ <View style={styles.menuDivider} />
261
+ <Pressable
262
+ onPress={hideForever}
263
+ style={({ pressed }) => [
264
+ styles.menuItem,
265
+ pressed && styles.menuItemPressed,
266
+ ]}
267
+ >
268
+ <Text style={[styles.menuItemText, styles.menuItemDanger]}>
269
+ Hide icon
270
+ </Text>
271
+ </Pressable>
272
+ </View>
273
+ ) : null}
274
+ </Animated.View>
275
+ </Animated.View>
276
+ );
277
+ };
278
+
279
+ const styles = StyleSheet.create({
280
+ container: {
281
+ position: 'absolute',
282
+ top: 0,
283
+ left: 0,
284
+ alignItems: 'flex-start',
285
+ },
286
+ icon: {
287
+ alignItems: 'center',
288
+ justifyContent: 'center',
289
+ shadowColor: '#000',
290
+ shadowOffset: { width: 0, height: 2 },
291
+ shadowOpacity: 0.25,
292
+ shadowRadius: 4,
293
+ elevation: 4,
294
+ },
295
+ iconLabel: {
296
+ color: '#ffffff',
297
+ fontWeight: '700',
298
+ includeFontPadding: false,
299
+ },
300
+ menu: {
301
+ marginTop: 6,
302
+ minWidth: 150,
303
+ backgroundColor: '#1f1f23',
304
+ borderRadius: 10,
305
+ paddingVertical: 4,
306
+ shadowColor: '#000',
307
+ shadowOffset: { width: 0, height: 2 },
308
+ shadowOpacity: 0.3,
309
+ shadowRadius: 6,
310
+ elevation: 6,
311
+ },
312
+ menuItem: {
313
+ paddingHorizontal: 14,
314
+ paddingVertical: 10,
315
+ },
316
+ menuItemPressed: {
317
+ backgroundColor: '#2a2a30',
318
+ },
319
+ menuItemText: {
320
+ color: '#f4f4f5',
321
+ fontSize: 14,
322
+ fontWeight: '500',
323
+ },
324
+ menuItemDanger: {
325
+ color: '#f97316',
326
+ },
327
+ menuDivider: {
328
+ height: StyleSheet.hairlineWidth,
329
+ backgroundColor: '#3f3f46',
330
+ marginHorizontal: 8,
331
+ },
332
+ });
@@ -13,6 +13,10 @@ import {
13
13
  clearSelectedDeviceId,
14
14
  DEFAULT_CONVEX_SITE_URL,
15
15
  } from './auth';
16
+ import {
17
+ getQuickIconDisabled,
18
+ setQuickIconDisabled,
19
+ } from './preferences';
16
20
 
17
21
  // Is this JS runtime the Yaver mobile app's super-host bridge? The
18
22
  // YaverInfo native module is only registered inside Yaver's container
@@ -49,6 +53,14 @@ let maxErrors = 5;
49
53
  /** Track whether BlackBox was running before disable (to restart on enable). */
50
54
  let blackBoxWasStreaming = false;
51
55
 
56
+ /**
57
+ * Tracks whether the user has already shaken once in this process.
58
+ * Consumed by QuickActionIcon's `'after-shake'` mode so the icon
59
+ * appears the first time a user discovers shake and stays around
60
+ * thereafter.
61
+ */
62
+ let firstShakeFired = false;
63
+
52
64
  /**
53
65
  * Flag evaluation cache — 30s TTL per `userId|key`. Prevents a
54
66
  * tight render loop from hammering /flags/eval when the dev calls
@@ -134,6 +146,7 @@ export class YaverFeedback {
134
146
  if (enabled && config.trigger === 'shake') {
135
147
  shakeDetector = new ShakeDetector();
136
148
  shakeDetector.start(() => {
149
+ YaverFeedback.notifyShake();
137
150
  if (config?.reportingOnly) {
138
151
  YaverFeedback.sendAutoReport();
139
152
  } else {
@@ -464,6 +477,7 @@ export class YaverFeedback {
464
477
  if (config?.trigger === 'shake' && !shakeDetector) {
465
478
  shakeDetector = new ShakeDetector();
466
479
  shakeDetector.start(() => {
480
+ YaverFeedback.notifyShake();
467
481
  if (config?.reportingOnly) {
468
482
  YaverFeedback.sendAutoReport();
469
483
  } else {
@@ -796,6 +810,59 @@ export class YaverFeedback {
796
810
  }
797
811
  }
798
812
 
813
+ /**
814
+ * Internal: fired from every shake path (dev-menu + accelerometer)
815
+ * before the feedback modal opens. Emits `yaverFeedback:firstShake`
816
+ * exactly once per process so QuickActionIcon's `'after-shake'` mode
817
+ * can surface itself on first shake and stay visible for the rest of
818
+ * the session.
819
+ */
820
+ static notifyShake(): void {
821
+ if (firstShakeFired) return;
822
+ firstShakeFired = true;
823
+ try {
824
+ const { DeviceEventEmitter } = require('react-native');
825
+ DeviceEventEmitter.emit('yaverFeedback:firstShake');
826
+ } catch {
827
+ // emitter unavailable (e.g. jsdom unit test) — safe to ignore
828
+ }
829
+ }
830
+
831
+ /**
832
+ * Show / hide the QuickActionIcon programmatically and persist the
833
+ * choice across launches. Host apps can call this from a settings
834
+ * screen so the user has a second way to re-enable the icon after
835
+ * hiding it via the icon's own long-press menu — shake is always the
836
+ * third back-door because it never depends on a visible control.
837
+ */
838
+ static async setQuickIconVisible(visible: boolean): Promise<void> {
839
+ await setQuickIconDisabled(!visible);
840
+ try {
841
+ const { DeviceEventEmitter } = require('react-native');
842
+ DeviceEventEmitter.emit(
843
+ visible ? 'yaverFeedback:quickIconShow' : 'yaverFeedback:quickIconHide',
844
+ );
845
+ } catch {
846
+ // emitter unavailable — preference is still persisted
847
+ }
848
+ }
849
+
850
+ /**
851
+ * Returns `true` when the user has chosen to hide the QuickActionIcon
852
+ * (via its long-press menu or `setQuickIconVisible(false)`).
853
+ * FeedbackModal uses this to surface a one-tap "Show quick icon"
854
+ * control so the user can bring the icon back without having to know
855
+ * about the programmatic API.
856
+ */
857
+ static async isQuickIconHidden(): Promise<boolean> {
858
+ return getQuickIconDisabled();
859
+ }
860
+
861
+ /** Clear the persisted "user hid the icon" flag. */
862
+ static async resetQuickIconPreference(): Promise<void> {
863
+ await YaverFeedback.setQuickIconVisible(true);
864
+ }
865
+
799
866
  /** Tear down the SDK (stop shake detector, clear state). */
800
867
  static destroy(): void {
801
868
  if (shakeDetector) {
package/src/index.ts CHANGED
@@ -45,7 +45,14 @@ export { AuthOverlay } from './AuthOverlay';
45
45
  export { ShakeDetector } from './ShakeDetector';
46
46
  export { FloatingButton } from './FloatingButton';
47
47
  export { FeedbackModal } from './FeedbackModal';
48
+ export { QuickActionIcon } from './QuickActionIcon';
49
+ export type { QuickActionIconProps } from './QuickActionIcon';
48
50
  export { FixReport } from './FixReport';
51
+ export {
52
+ getQuickIconDisabled,
53
+ setQuickIconDisabled,
54
+ clearQuickIconDisabled,
55
+ } from './preferences';
49
56
  export {
50
57
  configureAuthEndpoints,
51
58
  getConvexSiteUrl,
@@ -0,0 +1,55 @@
1
+ /**
2
+ * SDK user preferences persisted across launches.
3
+ *
4
+ * Currently only the quick-action icon's user-level dismiss flag
5
+ * lives here: the dev enables the icon via `FeedbackConfig.quickIcon`,
6
+ * but the *user* can long-press → Hide to opt out, and we remember
7
+ * that choice across launches so their next app session still
8
+ * respects it.
9
+ *
10
+ * AsyncStorage is an optional peer dep — if it's not installed the
11
+ * getters return `false` and the setters silently no-op, so the icon
12
+ * still works (it just can't remember the disable beyond the
13
+ * in-memory session).
14
+ */
15
+
16
+ let AsyncStorage: {
17
+ getItem: (key: string) => Promise<string | null>;
18
+ setItem: (key: string, value: string) => Promise<void>;
19
+ removeItem: (key: string) => Promise<void>;
20
+ } | null = null;
21
+ try {
22
+ AsyncStorage = require('@react-native-async-storage/async-storage').default;
23
+ } catch {
24
+ // not installed — degrade gracefully
25
+ }
26
+
27
+ const QUICK_ICON_DISABLED_KEY = 'yaver_feedback_quickicon_disabled';
28
+
29
+ /** True if the user has long-pressed the icon and chosen "Hide". */
30
+ export async function getQuickIconDisabled(): Promise<boolean> {
31
+ if (!AsyncStorage) return false;
32
+ try {
33
+ const v = await AsyncStorage.getItem(QUICK_ICON_DISABLED_KEY);
34
+ return v === '1';
35
+ } catch {
36
+ return false;
37
+ }
38
+ }
39
+
40
+ export async function setQuickIconDisabled(disabled: boolean): Promise<void> {
41
+ if (!AsyncStorage) return;
42
+ try {
43
+ if (disabled) {
44
+ await AsyncStorage.setItem(QUICK_ICON_DISABLED_KEY, '1');
45
+ } else {
46
+ await AsyncStorage.removeItem(QUICK_ICON_DISABLED_KEY);
47
+ }
48
+ } catch {
49
+ // best-effort
50
+ }
51
+ }
52
+
53
+ export async function clearQuickIconDisabled(): Promise<void> {
54
+ await setQuickIconDisabled(false);
55
+ }
package/src/types.ts CHANGED
@@ -42,6 +42,34 @@ export interface FeedbackConfig {
42
42
  preferredDeviceId?: string;
43
43
  /** How feedback collection is triggered */
44
44
  trigger?: 'shake' | 'floating-button' | 'manual';
45
+ /**
46
+ * Small tap-to-open icon that floats above the app so the user
47
+ * doesn't have to shake every time they want to open feedback.
48
+ * Single tap → open the feedback modal; long-press → menu with
49
+ * "Hide icon" (persisted across launches via AsyncStorage).
50
+ *
51
+ * - `'auto'` (default) → `'always'` on iOS/Android, `'off'` on web.
52
+ * - `'always'` → visible from app launch.
53
+ * - `'after-shake'` → hidden until the first shake this session.
54
+ * - `'off'` → never rendered. Shake still works.
55
+ *
56
+ * The user can always override `'always'` / `'auto'` by long-pressing
57
+ * → Hide. Devs can clear that override via
58
+ * `YaverFeedback.resetQuickIconPreference()`.
59
+ */
60
+ quickIcon?: 'auto' | 'always' | 'after-shake' | 'off';
61
+ /**
62
+ * Background color for the quick-action icon. Default: '#6366f1'
63
+ * (indigo). Pick something distinctive so the icon never visually
64
+ * collides with your own FAB.
65
+ */
66
+ quickIconColor?: string;
67
+ /**
68
+ * Initial position of the quick-action icon, in pixels from the
69
+ * top-left. Default: near the top-right corner of the screen. The
70
+ * icon is draggable at runtime — this is only the first mount.
71
+ */
72
+ quickIconInitialPosition?: { x: number; y: number };
45
73
  /** Enable/disable the SDK. Defaults to __DEV__ */
46
74
  enabled?: boolean;
47
75
  /**