The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { |
| 2 | createContext, |
| 3 | useContext, |
| 4 | useLayoutEffect, |
| 5 | useState, |
| 6 | useSyncExternalStore, |
| 7 | type ReactNode, |
| 8 | } from "react"; |
| 9 | import { View } from "react-native"; |
| 10 | |
| 11 | import { ScreenContent } from "../../components/ui/ScreenContent"; |
| 12 | import { ThreadDetailScreen, type ThreadDetailScreenProps } from "./ThreadDetailScreen"; |
| 13 | |
| 14 | type ThreadScreenContent = |
| 15 | | { readonly props: ThreadDetailScreenProps } |
| 16 | | { readonly fallback: ReactNode; readonly header?: ReactNode }; |
| 17 | |
| 18 | function createThreadScreenStore() { |
| 19 | let current: ThreadScreenContent | null = null; |
| 20 | const listeners = new Set<() => void>(); |
| 21 | return { |
| 22 | getSnapshot: () => current, |
| 23 | subscribe: (listener: () => void) => { |
| 24 | listeners.add(listener); |
| 25 | return () => { |
| 26 | listeners.delete(listener); |
| 27 | }; |
| 28 | }, |
| 29 | publish: (content: ThreadScreenContent) => { |
| 30 | current = content; |
| 31 | for (const listener of listeners) listener(); |
| 32 | }, |
| 33 | }; |
| 34 | } |
| 35 | const ThreadScreenContext = createContext<ReturnType<typeof createThreadScreenStore> | null>(null); |
| 36 | |
| 37 | function ThreadScreenOutlet() { |
| 38 | const store = useContext(ThreadScreenContext)!; |
| 39 | const content = useSyncExternalStore(store.subscribe, store.getSnapshot); |
| 40 | if (content === null) return null; |
| 41 | const body = "props" in content ? <ThreadDetailScreen {...content.props} /> : content.fallback; |
| 42 | return ( |
| 43 | <> |
| 44 | {"props" in content ? content.props.header : content.header} |
| 45 | <ScreenContent surface="canvas">{body}</ScreenContent> |
| 46 | </> |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | // Controllers can change from draft to live without replacing the native screen, |
| 51 | // the keyboard host, or the editor. Publish before paint to avoid an intermediate frame. |
| 52 | export function ThreadScreenHost({ children }: { readonly children: ReactNode }) { |
| 53 | const [store] = useState(createThreadScreenStore); |
| 54 | return ( |
| 55 | <ThreadScreenContext.Provider value={store}> |
| 56 | {children} |
| 57 | <View className="flex-1 bg-screen android:bg-header"> |
| 58 | <ThreadScreenOutlet /> |
| 59 | </View> |
| 60 | </ThreadScreenContext.Provider> |
| 61 | ); |
| 62 | } |
| 63 | |
| 64 | export function ThreadScreenBinding(props: ThreadDetailScreenProps) { |
| 65 | const store = useContext(ThreadScreenContext)!; |
| 66 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- publishes the route's thread screen props to the external store the outlet reads |
| 67 | useLayoutEffect(() => { |
| 68 | store.publish({ props }); |
| 69 | }); |
| 70 | return null; |
| 71 | } |
| 72 | |
| 73 | export function ThreadScreenFallback({ |
| 74 | children, |
| 75 | header, |
| 76 | }: { |
| 77 | readonly children: ReactNode; |
| 78 | readonly header?: ReactNode; |
| 79 | }) { |
| 80 | const store = useContext(ThreadScreenContext)!; |
| 81 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- publishes the fallback content and header to the external store the outlet reads |
| 82 | useLayoutEffect(() => { |
| 83 | store.publish({ fallback: children, header }); |
| 84 | }); |
| 85 | return null; |
| 86 | } |