|
| 1 | +import { useAtomValue } from "@effect/atom-react"; |
| 2 | +import { useLinkTo } from "@react-navigation/native"; |
| 3 | +import { |
| 4 | + pluginNotificationChanges, |
| 5 | + pluginNotificationDescription, |
| 6 | + type PluginNotificationMark, |
| 7 | + type ReceivedPluginNotification, |
| 8 | +} from "@t3tools/client-runtime/state/plugin-notifications"; |
| 9 | +import type { ContributionStatusTone, EnvironmentId } from "@t3tools/contracts"; |
| 10 | +import { useCallback, useEffect, useRef, useState } from "react"; |
| 11 | +import { Pressable, View } from "react-native"; |
| 12 | +import { useSafeAreaInsets } from "react-native-safe-area-context"; |
| 13 | + |
| 14 | +import { AppText as Text } from "../../components/AppText"; |
| 15 | +import { cn } from "../../lib/cn"; |
| 16 | +import { useEnvironments } from "../../state/environments"; |
| 17 | +import { pluginNotificationEnvironment } from "../../state/plugin-notifications"; |
| 18 | +import { |
| 19 | + type Banner, |
| 20 | + queueBannerChanges, |
| 21 | + removeEnvironmentBanners, |
| 22 | +} from "./plugin-notification-banners"; |
| 23 | + |
| 24 | +const SHOWN_MS = 5_000; |
| 25 | + |
| 26 | +const TONE_DOT_CLASS = { |
| 27 | + neutral: "bg-foreground-muted", |
| 28 | + info: "bg-adaptive-sky-600-400", |
| 29 | + success: "bg-adaptive-emerald-600-400", |
| 30 | + warning: "bg-adaptive-amber-700-400", |
| 31 | + error: "bg-adaptive-rose-600-400", |
| 32 | +} as const satisfies Record<ContributionStatusTone, string>; |
| 33 | + |
| 34 | +/** |
| 35 | + * Plugin notifications as an in-app banner under the status bar, one at a |
| 36 | + * time for a few seconds each. Only notifications that arrive while the app |
| 37 | + * is open are shown; tapping one about a thread opens it, and a banner goes |
| 38 | + * away once the server no longer retains its notification (its plugin |
| 39 | + * stopped, it expired or was evicted, or the server restarted) or its |
| 40 | + * environment is removed. |
| 41 | + */ |
| 42 | +export function PluginNotificationBannerHost() { |
| 43 | + const { environments } = useEnvironments(); |
| 44 | + const [queue, setQueue] = useState<ReadonlyArray<Banner>>([]); |
| 45 | + const onChanges = useCallback( |
| 46 | + ( |
| 47 | + environmentId: EnvironmentId, |
| 48 | + show: ReadonlyArray<ReceivedPluginNotification>, |
| 49 | + keep: ReadonlySet<string>, |
| 50 | + ) => setQueue((current) => queueBannerChanges(current, environmentId, show, keep)), |
| 51 | + [], |
| 52 | + ); |
| 53 | + const onRemoved = useCallback( |
| 54 | + (environmentId: EnvironmentId) => |
| 55 | + setQueue((current) => removeEnvironmentBanners(current, environmentId)), |
| 56 | + [], |
| 57 | + ); |
| 58 | + const dismiss = useCallback( |
| 59 | + (banner: Banner) => setQueue((current) => current.filter((each) => each !== banner)), |
| 60 | + [], |
| 61 | + ); |
| 62 | + const head = queue[0]; |
| 63 | + |
| 64 | + useEffect(() => { |
| 65 | + if (head === undefined) return; |
| 66 | + const timer = setTimeout(() => dismiss(head), SHOWN_MS); |
| 67 | + return () => clearTimeout(timer); |
| 68 | + }, [dismiss, head]); |
| 69 | + |
| 70 | + return ( |
| 71 | + <> |
| 72 | + {environments.map((environment) => ( |
| 73 | + <EnvironmentPluginNotifications |
| 74 | + key={environment.environmentId} |
| 75 | + environmentId={environment.environmentId} |
| 76 | + onChanges={onChanges} |
| 77 | + onRemoved={onRemoved} |
| 78 | + /> |
| 79 | + ))} |
| 80 | + {head === undefined ? null : <PluginNotificationBanner banner={head} onDone={dismiss} />} |
| 81 | + </> |
| 82 | + ); |
| 83 | +} |
| 84 | + |
| 85 | +function EnvironmentPluginNotifications(props: { |
| 86 | + readonly environmentId: EnvironmentId; |
| 87 | + readonly onChanges: ( |
| 88 | + environmentId: EnvironmentId, |
| 89 | + show: ReadonlyArray<ReceivedPluginNotification>, |
| 90 | + keep: ReadonlySet<string>, |
| 91 | + ) => void; |
| 92 | + readonly onRemoved: (environmentId: EnvironmentId) => void; |
| 93 | +}) { |
| 94 | + const { environmentId, onChanges, onRemoved } = props; |
| 95 | + const frame = useAtomValue(pluginNotificationEnvironment.frame(environmentId)); |
| 96 | + // The newest notification seen; banners themselves live in the host's queue. |
| 97 | + const mark = useRef<PluginNotificationMark | undefined>(undefined); |
| 98 | + |
| 99 | + // Unmounting means the environment was removed, so no later frame will drop its banners. |
| 100 | + useEffect(() => () => onRemoved(environmentId), [environmentId, onRemoved]); |
| 101 | + |
| 102 | + useEffect(() => { |
| 103 | + const changes = pluginNotificationChanges(mark.current, frame); |
| 104 | + mark.current = changes.mark; |
| 105 | + onChanges(environmentId, changes.show, changes.keep); |
| 106 | + }, [environmentId, frame, onChanges]); |
| 107 | + |
| 108 | + return null; |
| 109 | +} |
| 110 | + |
| 111 | +function PluginNotificationBanner(props: { |
| 112 | + readonly banner: Banner; |
| 113 | + readonly onDone: (banner: Banner) => void; |
| 114 | +}) { |
| 115 | + const { banner, onDone } = props; |
| 116 | + const insets = useSafeAreaInsets(); |
| 117 | + const linkTo = useLinkTo(); |
| 118 | + const { threadId, title, tone } = banner.entry.notification; |
| 119 | + const description = pluginNotificationDescription(banner.entry); |
| 120 | + |
| 121 | + return ( |
| 122 | + <View |
| 123 | + pointerEvents="box-none" |
| 124 | + className="absolute inset-x-0 items-center px-3" |
| 125 | + style={{ top: insets.top + 4 }} |
| 126 | + > |
| 127 | + <Pressable |
| 128 | + accessibilityRole={threadId === undefined ? "button" : "link"} |
| 129 | + accessibilityLabel={`${title}. ${description}`} |
| 130 | + accessibilityHint={ |
| 131 | + threadId === undefined ? "Dismisses the notification" : "Opens the thread" |
| 132 | + } |
| 133 | + className="w-full max-w-[480px] flex-row items-start gap-2.5 rounded-2xl border border-border-subtle bg-card-alt px-3.5 py-3 active:opacity-70" |
| 134 | + onPress={() => { |
| 135 | + onDone(banner); |
| 136 | + if (threadId !== undefined) |
| 137 | + linkTo( |
| 138 | + `/threads/${encodeURIComponent(banner.environmentId)}/${encodeURIComponent(threadId)}`, |
| 139 | + ); |
| 140 | + }} |
| 141 | + > |
| 142 | + <View |
| 143 | + className={cn("mt-1.5 h-2 w-2 shrink-0 rounded-full", TONE_DOT_CLASS[tone ?? "neutral"])} |
| 144 | + /> |
| 145 | + <View className="min-w-0 flex-1"> |
| 146 | + <Text numberOfLines={1} className="text-sm font-semibold text-foreground"> |
| 147 | + {title} |
| 148 | + </Text> |
| 149 | + <Text numberOfLines={2} className="text-xs text-foreground-secondary"> |
| 150 | + {description} |
| 151 | + </Text> |
| 152 | + </View> |
| 153 | + </Pressable> |
| 154 | + </View> |
| 155 | + ); |
| 156 | +} |
0 commit comments