@gemies/sdk 10.7.0 → 10.9.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,121 @@
1
+ import { Button } from '@oxy.so/bloom/button';
2
+ import { RiArrowDownSLine } from '@oxy.so/bloom/icons/RiArrowDownSLine';
3
+ import { RiArrowUpSLine } from '@oxy.so/bloom/icons/RiArrowUpSLine';
4
+ import { RiCloseLine } from '@oxy.so/bloom/icons/RiCloseLine';
5
+ import { Search } from '@oxy.so/bloom/search';
6
+ import { Muted } from '@oxy.so/bloom/typography';
7
+ import { useEffect, useRef } from 'react';
8
+ import { StyleSheet, type TextInputKeyPressEvent, View } from 'react-native';
9
+ import type { ThreadNavigation } from '../../hooks/useThreadNavigation';
10
+ import { useLabels } from '../../lib/labels';
11
+ import type { TextInputHandle } from '../../lib/text-input';
12
+ import { ThreadPanel } from './ThreadPanel';
13
+
14
+ /**
15
+ * Find in the open conversation: the words, which match of how many, and the way to the others.
16
+ *
17
+ * Not `ThreadSearch`, which asks the server for sentences across every conversation of a gemie's
18
+ * thread: this marks the messages on screen and moves between them. Enter goes to the match
19
+ * above (older), Shift+Enter to the one below, Escape closes.
20
+ */
21
+ export function ThreadFindBar({
22
+ find,
23
+ partial,
24
+ }: {
25
+ find: ThreadNavigation;
26
+ /** There is history the thread has not loaded, which find cannot see. */
27
+ partial: boolean;
28
+ }) {
29
+ const { t } = useLabels();
30
+ const field = useRef<TextInputHandle>(null);
31
+ const { query, total, current, focusRequest, older, newer, close, setQuery } = find;
32
+
33
+ // Asked for again while open (the shortcut): the caret comes back to the words.
34
+ useEffect(() => {
35
+ if (focusRequest > 0) field.current?.focus();
36
+ }, [focusRequest]);
37
+
38
+ const onKeyPress = (event: TextInputKeyPressEvent) => {
39
+ const { key, shiftKey } = event.nativeEvent as { key: string; shiftKey?: boolean };
40
+ if (key === 'Escape') {
41
+ event.preventDefault();
42
+ close();
43
+ } else if (key === 'Enter' && shiftKey) {
44
+ event.preventDefault();
45
+ newer();
46
+ }
47
+ };
48
+
49
+ const asked = query.trim() !== '';
50
+ const none = total === 0;
51
+
52
+ return (
53
+ <ThreadPanel>
54
+ <View style={styles.row}>
55
+ <View style={styles.field}>
56
+ <Search
57
+ ref={field}
58
+ label={t('chat.find.placeholder')}
59
+ value={query}
60
+ onChangeText={setQuery}
61
+ onClearText={() => setQuery('')}
62
+ onKeyPress={onKeyPress}
63
+ onSubmitEditing={older}
64
+ // Enter steps through the matches, so the field keeps the caret.
65
+ submitBehavior="submit"
66
+ blurOnSubmit={false}
67
+ autoFocus
68
+ returnKeyType="search"
69
+ />
70
+ </View>
71
+ {asked ? (
72
+ <View accessibilityLiveRegion="polite" aria-live="polite">
73
+ <Muted>
74
+ {none ? t('chat.find.none') : t('chat.find.count', { current: current + 1, total })}
75
+ </Muted>
76
+ </View>
77
+ ) : null}
78
+ <Button
79
+ size="sm"
80
+ appearance="plain"
81
+ tone="neutral"
82
+ iconOnly
83
+ leadingIcon={RiArrowUpSLine}
84
+ accessibilityLabel={t('chat.find.older')}
85
+ disabled={none}
86
+ onPress={older}
87
+ />
88
+ <Button
89
+ size="sm"
90
+ appearance="plain"
91
+ tone="neutral"
92
+ iconOnly
93
+ leadingIcon={RiArrowDownSLine}
94
+ accessibilityLabel={t('chat.find.newer')}
95
+ disabled={none}
96
+ onPress={newer}
97
+ />
98
+ <Button
99
+ size="sm"
100
+ appearance="plain"
101
+ tone="neutral"
102
+ iconOnly
103
+ leadingIcon={RiCloseLine}
104
+ accessibilityLabel={t('common.close')}
105
+ onPress={close}
106
+ />
107
+ </View>
108
+ {partial ? (
109
+ <View style={styles.note}>
110
+ <Muted>{t('chat.find.loadedOnly')}</Muted>
111
+ </View>
112
+ ) : null}
113
+ </ThreadPanel>
114
+ );
115
+ }
116
+
117
+ const styles = StyleSheet.create({
118
+ row: { flexDirection: 'row', alignItems: 'center', gap: 8 },
119
+ field: { flexGrow: 1, flexShrink: 1, flexBasis: '0%' },
120
+ note: { paddingHorizontal: 4, paddingTop: 8 },
121
+ });
@@ -0,0 +1,34 @@
1
+ import { useAiChatChromeInsets } from '@oxy.so/bloom/ai-chat';
2
+ import { Card, CardBody } from '@oxy.so/bloom/card';
3
+ import type { ReactNode } from 'react';
4
+ import { StyleSheet, View } from 'react-native';
5
+
6
+ /**
7
+ * A card floating over the top of the thread, in its 768 column: what the thread's search, its
8
+ * find bar and its list of turns stand on. The card is the surface, so nothing on it paints one.
9
+ *
10
+ * It starts under the container's header, which floats over the content on a layer above it.
11
+ */
12
+ export function ThreadPanel({ children }: { children: ReactNode }) {
13
+ const insets = useAiChatChromeInsets();
14
+ return (
15
+ <View style={[styles.overlay, { top: insets?.top ?? 0 }]} pointerEvents="box-none">
16
+ <View style={styles.column}>
17
+ <Card elevation="m">
18
+ <CardBody>{children}</CardBody>
19
+ </Card>
20
+ </View>
21
+ </View>
22
+ );
23
+ }
24
+
25
+ const styles = StyleSheet.create({
26
+ overlay: {
27
+ position: 'absolute',
28
+ insetInline: 0,
29
+ zIndex: 20,
30
+ paddingHorizontal: 16,
31
+ paddingTop: 8,
32
+ },
33
+ column: { width: '100%', maxWidth: 768, alignSelf: 'center' },
34
+ });
@@ -6,6 +6,7 @@ import {
6
6
  AiChatUserMessage,
7
7
  type AiChatTurnAction,
8
8
  } from '@oxy.so/bloom/ai-chat';
9
+ import { Button } from '@oxy.so/bloom/button';
9
10
  import { RiEditLine } from '@oxy.so/bloom/icons/RiEditLine';
10
11
  import { RiFileCopyLine } from '@oxy.so/bloom/icons/RiFileCopyLine';
11
12
  import { RiRefreshLine } from '@oxy.so/bloom/icons/RiRefreshLine';
@@ -16,7 +17,7 @@ import { RiVolumeUpLine } from '@oxy.so/bloom/icons/RiVolumeUpLine';
16
17
  import { TaskList } from '@oxy.so/bloom/task-list';
17
18
  import { Text } from '@oxy.so/bloom/typography';
18
19
  import { WebSearch } from '@oxy.so/bloom/web-search';
19
- import { memo } from 'react';
20
+ import { memo, useCallback } from 'react';
20
21
  import { type LayoutChangeEvent, View } from 'react-native';
21
22
  import Animated from 'react-native-reanimated';
22
23
  import type { ReadAloudState } from '../../hooks/useReadAloud';
@@ -44,6 +45,7 @@ import {
44
45
  turnFileChanges,
45
46
  } from '../../runtime/model';
46
47
  import type { OpenThought } from '../../hooks/useOpenThought';
48
+ import type { RowView } from '../../hooks/useRowJump';
47
49
  import { PlanPreviewCard } from '../PlanPreviewCard';
48
50
  import { AskedQuestions } from '../QuestionCard';
49
51
  import { CanvasComponent } from '../canvas/CanvasComponent';
@@ -100,6 +102,10 @@ export interface ThreadRowProps {
100
102
  conversationId: string | undefined;
101
103
  /** Where this row ended up, for the one row a jump is aimed at. */
102
104
  onLayout?: (event: LayoutChangeEvent) => void;
105
+ /** Hands the thread the row's view, which a jump measures. Stable across renders. */
106
+ registerRow?: (messageId: string, view: RowView | null) => void;
107
+ /** The row is the find bar's current match. */
108
+ highlighted?: boolean;
103
109
  actions: ThreadActions;
104
110
  /** This reply's read-aloud, as a primitive so only its own row re-renders. */
105
111
  readAloudState: ReadAloudState;
@@ -131,6 +137,8 @@ export const ThreadRow = memo(function ThreadRow({
131
137
  isLoading,
132
138
  conversationId,
133
139
  onLayout,
140
+ registerRow,
141
+ highlighted = false,
134
142
  actions,
135
143
  readAloudState,
136
144
  readAloudBlocked,
@@ -146,6 +154,11 @@ export const ThreadRow = memo(function ThreadRow({
146
154
  const attached = !isReply && hasAttachments(message.content);
147
155
  const storedIn = message.unsaved === true ? undefined : conversationId;
148
156
  const hasText = text.length > 0;
157
+ const rowRef = useCallback(
158
+ (view: RowView | null) => registerRow?.(message.id, view),
159
+ [registerRow, message.id],
160
+ );
161
+ const rowStyle = highlighted ? styles.highlighted : undefined;
149
162
 
150
163
  const copyAction: AiChatTurnAction | null = hasText
151
164
  ? {
@@ -175,7 +188,7 @@ export const ThreadRow = memo(function ThreadRow({
175
188
  };
176
189
  const questionActions = [copyAction, editAction].filter(isPresent);
177
190
  return (
178
- <Animated.View onLayout={onLayout}>
191
+ <Animated.View ref={rowRef} onLayout={onLayout} style={rowStyle}>
179
192
  {text.length === 0 && !attached ? null : (
180
193
  // Bloom's user turn, with the breathing room above it the thread has always had.
181
194
  <View style={styles.question}>
@@ -214,7 +227,7 @@ export const ThreadRow = memo(function ThreadRow({
214
227
  }
215
228
 
216
229
  return (
217
- <Animated.View onLayout={onLayout}>
230
+ <Animated.View ref={rowRef} onLayout={onLayout} style={rowStyle}>
218
231
  <ReplyTurn
219
232
  message={message}
220
233
  text={text}
@@ -411,13 +424,12 @@ function ReplyTurn({
411
424
  icon: RiThumbDownLine,
412
425
  onPress: () => actions.vote(message.id, 'down', storedIn),
413
426
  };
414
- const menuActions: AiChatTurnAction[] = hasFeedback
415
- ? [readAloudAction, copyAction, regenerateAction, likeAction, dislikeAction].filter(isPresent)
416
- : [];
417
- /** In a bubble: "react" is the emoji bar, "more" is the rest of what the turn offers, the votes included. */
427
+ /** A shared conversation is read, not judged: a finished reply still offers its words. */
428
+ const copyOnly = readOnly && !message.isStreaming ? copyAction : null;
429
+ /** The long-press menu, and a bubble's "more" (where "react" is the emoji bar beside it). */
418
430
  const moreActions = hasFeedback
419
431
  ? [readAloudAction, copyAction, regenerateAction, likeAction, dislikeAction].filter(isPresent)
420
- : [];
432
+ : [copyOnly].filter(isPresent);
421
433
  const reactions = actions.reactionsOf(
422
434
  message.id,
423
435
  message.reactions,
@@ -509,7 +521,7 @@ function ReplyTurn({
509
521
  ) : null}
510
522
  {/* Bloom's reply: its reveal and its own feedback row (like / dislike / copy), then read
511
523
  aloud and regenerate. Copy confirms only what the clipboard did. */}
512
- <TurnMenu actions={menuActions} label={t('chat.turnActions')}>
524
+ <TurnMenu actions={moreActions} label={t('chat.turnActions')}>
513
525
  <AiChatAssistantMessage
514
526
  animate={isNew}
515
527
  feedback={hasFeedback && !bubbled}
@@ -628,6 +640,20 @@ function ReplyTurn({
628
640
  ) : (
629
641
  replyBody
630
642
  )}
643
+ {/* Bloom's feedback row always votes, so a reader of a shared one gets copy alone. */}
644
+ {bubbled || copyOnly === null ? null : (
645
+ <View style={styles.copyOnly}>
646
+ <Button
647
+ iconOnly
648
+ size="sm"
649
+ appearance="plain"
650
+ tone="neutral"
651
+ leadingIcon={RiFileCopyLine}
652
+ accessibilityLabel={copyOnly.label}
653
+ onPress={copyOnly.onPress}
654
+ />
655
+ </View>
656
+ )}
631
657
  </AiChatAssistantMessage>
632
658
  </TurnMenu>
633
659
  </View>
@@ -637,8 +663,17 @@ function ReplyTurn({
637
663
  }
638
664
 
639
665
  const useStyles = createTokenStyles((t) => ({
666
+ // An outline takes no room, so marking a match moves nothing the reader is looking at.
667
+ highlighted: {
668
+ borderRadius: 16,
669
+ outlineWidth: 2,
670
+ outlineStyle: 'solid',
671
+ outlineColor: t.primary,
672
+ outlineOffset: 4,
673
+ },
640
674
  question: { marginTop: 8, width: '100%', flexDirection: 'column', alignItems: 'flex-end' },
641
675
  reply: { width: '100%', flexDirection: 'column' },
676
+ copyOnly: { alignSelf: 'flex-start' },
642
677
  replyIdentity: { marginBottom: 2 },
643
678
  canvas: { borderRadius: 16, borderWidth: 1, borderColor: t.border, padding: 16 },
644
679
  muted: { fontSize: 14, color: t.mutedForeground, lineHeight: 20 },
@@ -3,13 +3,13 @@ import type { GemieThreadAddress } from '../../runtime/routes';
3
3
  import { useThreadSearch } from '../../runtime/use-thread-search';
4
4
  import { useLabels } from '../../lib/labels';
5
5
  import { Button } from '@oxy.so/bloom/button';
6
- import { Card, CardBody } from '@oxy.so/bloom/card';
7
6
  import { RiCloseLine } from '@oxy.so/bloom/icons/RiCloseLine';
8
7
  import { ListItem } from '@oxy.so/bloom/list';
9
8
  import { Search } from '@oxy.so/bloom/search';
10
9
  import { Muted } from '@oxy.so/bloom/typography';
11
10
  import { useState } from 'react';
12
11
  import { ScrollView, View, StyleSheet } from 'react-native';
12
+ import { ThreadPanel } from './ThreadPanel';
13
13
 
14
14
  /**
15
15
  * Searching what was said in this thread, across every conversation in it.
@@ -24,9 +24,7 @@ import { ScrollView, View, StyleSheet } from 'react-native';
24
24
  * can read and not reach is a wall, so `onJump` is not an extra: it is what the
25
25
  * hit is for.
26
26
  *
27
- * Bloom's `Search` for the field and a `ListItem` per hit, on a `Card` floating
28
- * over the top of the thread in its 768 column: the card is the surface, so
29
- * nothing here paints one of its own.
27
+ * Bloom's `Search` for the field and a `ListItem` per hit, on a `ThreadPanel`.
30
28
  */
31
29
 
32
30
  interface ThreadSearchProps {
@@ -61,85 +59,70 @@ export const ThreadSearch = ({ thread, onJump, onClose }: ThreadSearchProps) =>
61
59
  const found = hits ?? [];
62
60
 
63
61
  return (
64
- <View style={styles.overlay} pointerEvents="box-none">
65
- <View style={styles.column}>
66
- <Card elevation="m">
67
- <CardBody>
68
- <View style={styles.searchRow}>
69
- <View style={styles.search}>
70
- <Search
71
- label={t('chat.searchThreadPlaceholder')}
72
- value={query}
73
- onChangeText={setQuery}
74
- onClearText={() => setQuery('')}
75
- autoFocus
76
- returnKeyType="search"
77
- />
78
- </View>
79
- <Button
80
- size="md"
81
- appearance="plain"
82
- tone="neutral"
83
- iconOnly
84
- leadingIcon={RiCloseLine}
85
- accessibilityLabel={t('common.close')}
86
- onPress={onClose}
87
- />
88
- </View>
89
-
90
- {!asked ? (
91
- <View style={styles.hint}>
92
- <Muted>{t('chat.searchThreadHint')}</Muted>
93
- </View>
94
- ) : isError ? (
95
- <View style={styles.hint}>
96
- <Muted>{t('chat.searchThreadFailed')}</Muted>
97
- </View>
98
- ) : found.length === 0 ? (
99
- <View style={styles.hint}>
100
- <Muted>
101
- {isFetching ? t('chat.searchThreadSearching') : t('chat.searchThreadEmpty')}
102
- </Muted>
103
- </View>
104
- ) : (
105
- <ScrollView
106
- // A height in px, not a percentage of a parent that has none:
107
- // measured in Chromium, the list otherwise sized itself to its
108
- // content and spilled the results over the thread behind it.
109
- style={styles.results}
110
- keyboardShouldPersistTaps="handled"
111
- >
112
- {found.map((hit) => (
113
- <ListItem
114
- // The cursor, never `messageId`: a message the server wrote
115
- // has no client id, and `null` is not a key.
116
- key={hit.cursor}
117
- title={hit.snippet}
118
- supporting={`${t(
119
- hit.role === 'user' ? 'chat.searchThreadYou' : 'chat.searchThreadAgent',
120
- )} · ${hitDay(hit.createdAt, locale)}`}
121
- onPress={() => onJump(hit)}
122
- />
123
- ))}
124
- </ScrollView>
125
- )}
126
- </CardBody>
127
- </Card>
62
+ <ThreadPanel>
63
+ <View style={styles.searchRow}>
64
+ <View style={styles.search}>
65
+ <Search
66
+ label={t('chat.searchThreadPlaceholder')}
67
+ value={query}
68
+ onChangeText={setQuery}
69
+ onClearText={() => setQuery('')}
70
+ autoFocus
71
+ returnKeyType="search"
72
+ />
73
+ </View>
74
+ <Button
75
+ size="md"
76
+ appearance="plain"
77
+ tone="neutral"
78
+ iconOnly
79
+ leadingIcon={RiCloseLine}
80
+ accessibilityLabel={t('common.close')}
81
+ onPress={onClose}
82
+ />
128
83
  </View>
129
- </View>
84
+
85
+ {!asked ? (
86
+ <View style={styles.hint}>
87
+ <Muted>{t('chat.searchThreadHint')}</Muted>
88
+ </View>
89
+ ) : isError ? (
90
+ <View style={styles.hint}>
91
+ <Muted>{t('chat.searchThreadFailed')}</Muted>
92
+ </View>
93
+ ) : found.length === 0 ? (
94
+ <View style={styles.hint}>
95
+ <Muted>
96
+ {isFetching ? t('chat.searchThreadSearching') : t('chat.searchThreadEmpty')}
97
+ </Muted>
98
+ </View>
99
+ ) : (
100
+ <ScrollView
101
+ // A height in px, not a percentage of a parent that has none:
102
+ // measured in Chromium, the list otherwise sized itself to its
103
+ // content and spilled the results over the thread behind it.
104
+ style={styles.results}
105
+ keyboardShouldPersistTaps="handled"
106
+ >
107
+ {found.map((hit) => (
108
+ <ListItem
109
+ // The cursor, never `messageId`: a message the server wrote
110
+ // has no client id, and `null` is not a key.
111
+ key={hit.cursor}
112
+ title={hit.snippet}
113
+ supporting={`${t(
114
+ hit.role === 'user' ? 'chat.searchThreadYou' : 'chat.searchThreadAgent',
115
+ )} · ${hitDay(hit.createdAt, locale)}`}
116
+ onPress={() => onJump(hit)}
117
+ />
118
+ ))}
119
+ </ScrollView>
120
+ )}
121
+ </ThreadPanel>
130
122
  );
131
123
  };
132
124
 
133
125
  const styles = StyleSheet.create({
134
- overlay: {
135
- position: 'absolute',
136
- insetInline: 0,
137
- top: 0,
138
- zIndex: 20,
139
- paddingHorizontal: 16,
140
- paddingTop: 16,
141
- },
142
- column: { width: '100%', maxWidth: 768, alignSelf: 'center' },
143
126
  searchRow: { flexDirection: 'row', alignItems: 'center', gap: 8 },
144
127
  search: { flexGrow: 1, flexShrink: 1, flexBasis: '0%' },
145
128
  hint: { paddingHorizontal: 4, paddingVertical: 16 },
@@ -0,0 +1,94 @@
1
+ import { useAiChatChromeInsets } from '@oxy.so/bloom/ai-chat';
2
+ import { Button } from '@oxy.so/bloom/button';
3
+ import { RiCloseLine } from '@oxy.so/bloom/icons/RiCloseLine';
4
+ import { RiListUnordered } from '@oxy.so/bloom/icons/RiListUnordered';
5
+ import { ListItem } from '@oxy.so/bloom/list';
6
+ import { Muted } from '@oxy.so/bloom/typography';
7
+ import { ScrollView, StyleSheet, View } from 'react-native';
8
+ import { useLabels } from '../../lib/labels';
9
+ import type { ThreadTurn } from '../../runtime/thread/thread-find';
10
+ import { ThreadPanel } from './ThreadPanel';
11
+
12
+ /** From how many turns a thread offers its list of them without being asked for it. */
13
+ export const LONG_THREAD_TURNS = 6;
14
+
15
+ /**
16
+ * The thread's turns, newest first, each named by the question that opened it: a press lands there.
17
+ *
18
+ * A list rather than a drawn minimap: the thread mounts only a window of its rows, so there are no
19
+ * heights to draw the rest from, and a map built on guesses would land in the wrong place.
20
+ */
21
+ export function ThreadTurnList({
22
+ turns,
23
+ onJump,
24
+ onLoadEarlier,
25
+ onClose,
26
+ }: {
27
+ turns: readonly ThreadTurn[];
28
+ onJump: (messageId: string) => void;
29
+ /** Page in the history above, whose turns then join the list. Absent where there is none. */
30
+ onLoadEarlier?: () => void;
31
+ onClose: () => void;
32
+ }) {
33
+ const { t } = useLabels();
34
+ return (
35
+ <ThreadPanel>
36
+ <View style={styles.header}>
37
+ <View style={styles.title}>
38
+ <Muted>{t('chat.turns.title')}</Muted>
39
+ </View>
40
+ <Button
41
+ size="sm"
42
+ appearance="plain"
43
+ tone="neutral"
44
+ iconOnly
45
+ leadingIcon={RiCloseLine}
46
+ accessibilityLabel={t('common.close')}
47
+ onPress={onClose}
48
+ />
49
+ </View>
50
+ <ScrollView style={styles.turns} keyboardShouldPersistTaps="handled">
51
+ {[...turns].reverse().map((turn) => (
52
+ <ListItem
53
+ key={turn.id}
54
+ title={turn.preview || t('chat.turns.attachment')}
55
+ onPress={() => onJump(turn.id)}
56
+ />
57
+ ))}
58
+ {onLoadEarlier === undefined ? null : (
59
+ <Button appearance="plain" tone="neutral" size="sm" onPress={onLoadEarlier}>
60
+ {t('chat.turns.loadEarlier')}
61
+ </Button>
62
+ )}
63
+ </ScrollView>
64
+ </ThreadPanel>
65
+ );
66
+ }
67
+
68
+ /** The way to the list from a long thread: a button at its edge, under the header. */
69
+ export function ThreadTurnsButton({ onPress }: { onPress: () => void }) {
70
+ const { t } = useLabels();
71
+ // The header floats over the transcript, so the edge starts under it.
72
+ const insets = useAiChatChromeInsets();
73
+ return (
74
+ <View style={[styles.edge, { top: (insets?.top ?? 0) + 8 }]} pointerEvents="box-none">
75
+ <Button
76
+ size="sm"
77
+ appearance="outline"
78
+ tone="neutral"
79
+ iconOnly
80
+ leadingIcon={RiListUnordered}
81
+ accessibilityLabel={t('chat.turns.title')}
82
+ onPress={onPress}
83
+ />
84
+ </View>
85
+ );
86
+ }
87
+
88
+ const styles = StyleSheet.create({
89
+ header: { flexDirection: 'row', alignItems: 'center', gap: 8 },
90
+ title: { flexGrow: 1, flexShrink: 1, flexBasis: '0%', paddingHorizontal: 4 },
91
+ // A height in px, not a percentage of a parent that has none (see `ThreadSearch`).
92
+ turns: { marginTop: 8, maxHeight: 384 },
93
+ edge: { position: 'absolute', insetInlineEnd: 12, zIndex: 10 },
94
+ });
@@ -31,7 +31,7 @@ export interface ShortcutKeyEvent {
31
31
  }
32
32
 
33
33
  /** Whether keys typed here are text: a field, or anything the person can edit. */
34
- function isEditing(target: unknown): boolean {
34
+ export function isEditing(target: unknown): boolean {
35
35
  const el = target as { tagName?: string; isContentEditable?: boolean } | null;
36
36
  if (el === null || typeof el !== 'object') return false;
37
37
  const tag = el.tagName?.toLowerCase();
@@ -0,0 +1,57 @@
1
+ import { useEffect, useRef } from 'react';
2
+ import { Platform } from 'react-native';
3
+ import type { ShortcutKeyEvent } from './useApprovalShortcuts';
4
+
5
+ /** Where keys are heard. A test brings its own. */
6
+ export type ShortcutTarget = Pick<EventTarget, 'addEventListener' | 'removeEventListener'>;
7
+
8
+ /**
9
+ * Every chat on screen that offers find, the newest last: a sheet over a screen has two, and only
10
+ * the one on top answers.
11
+ */
12
+ const owners: object[] = [];
13
+
14
+ /** ⌘F on a Mac, Ctrl+F elsewhere. Ctrl+F on a Mac moves the caret and stays the field's. */
15
+ export function isFindShortcut(event: ShortcutKeyEvent, mac: boolean): boolean {
16
+ if (event.defaultPrevented || event.isComposing || event.altKey || event.shiftKey) return false;
17
+ const modifier = mac ? event.metaKey && !event.ctrlKey : event.ctrlKey && !event.metaKey;
18
+ return modifier === true && event.key.toLowerCase() === 'f';
19
+ }
20
+
21
+ function isMac(): boolean {
22
+ const platform = (globalThis as { navigator?: { platform?: string } }).navigator?.platform;
23
+ return /mac|iphone|ipad/i.test(platform ?? '');
24
+ }
25
+
26
+ /**
27
+ * The web shortcut for find in the thread. The browser's own find cannot see the rows above the
28
+ * thread's mounted window, so while a chat offers find it takes the key; `undefined` leaves the
29
+ * key to the browser. Heard in the capture phase, because a text field stops its keys bubbling.
30
+ */
31
+ export function useFindShortcut(
32
+ open: (() => void) | undefined,
33
+ target: ShortcutTarget | undefined = Platform.OS === 'web'
34
+ ? (globalThis as { window?: Window }).window
35
+ : undefined,
36
+ ): void {
37
+ const latest = useRef(open);
38
+ latest.current = open;
39
+ const enabled = open !== undefined;
40
+
41
+ useEffect(() => {
42
+ if (!enabled || target === undefined) return;
43
+ const owner = {};
44
+ owners.push(owner);
45
+ const onKeyDown = (event: Event) => {
46
+ const key = event as unknown as ShortcutKeyEvent;
47
+ if (owners.at(-1) !== owner || !isFindShortcut(key, isMac())) return;
48
+ key.preventDefault?.();
49
+ latest.current?.();
50
+ };
51
+ target.addEventListener('keydown', onKeyDown, true);
52
+ return () => {
53
+ owners.splice(owners.indexOf(owner), 1);
54
+ target.removeEventListener('keydown', onKeyDown, true);
55
+ };
56
+ }, [enabled, target]);
57
+ }