The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { measureRestingComposerControls } from "./restingComposerControlsMeasurement"; |
| 2 | import { observeResize } from "~/lib/observeResize"; |
| 3 | import { resolveRestingComposerControlsLayout } from "../composerFooterLayout"; |
| 4 | import { useLayoutEffect, useRef, useState } from "react"; |
| 5 | |
| 6 | export function useRestingComposerControlsLayout( |
| 7 | host: HTMLDivElement | null, |
| 8 | useControlsAsHost = false, |
| 9 | ) { |
| 10 | const [controls, setControls] = useState<HTMLDivElement | null>(null); |
| 11 | const controlsRef = useRef<HTMLDivElement>(null); |
| 12 | const attachControls = (element: HTMLDivElement | null) => { |
| 13 | controlsRef.current = element; |
| 14 | setControls(element); |
| 15 | }; |
| 16 | const [layout, setLayout] = useState<{ |
| 17 | hiddenCount: number; |
| 18 | iconOnlyCount?: number; |
| 19 | visible: boolean; |
| 20 | }>({ hiddenCount: 0, visible: true }); |
| 21 | |
| 22 | const measure = () => { |
| 23 | const currentHost = useControlsAsHost ? controls : host; |
| 24 | if (currentHost === null || !controls) return; |
| 25 | |
| 26 | const measurement = measureRestingComposerControls(controls); |
| 27 | if (!measurement) return; |
| 28 | const style = getComputedStyle(currentHost); |
| 29 | const hostWidth = |
| 30 | currentHost.clientWidth - |
| 31 | (Number.parseFloat(style.paddingInlineStart) || 0) - |
| 32 | (Number.parseFloat(style.paddingInlineEnd) || 0); |
| 33 | |
| 34 | setLayout((current) => { |
| 35 | const next = resolveRestingComposerControlsLayout({ |
| 36 | ...measurement, |
| 37 | hostWidth, |
| 38 | previous: current, |
| 39 | }); |
| 40 | return next.hiddenCount === current.hiddenCount && |
| 41 | next.iconOnlyCount === current.iconOnlyCount && |
| 42 | next.visible === current.visible |
| 43 | ? current |
| 44 | : next; |
| 45 | }); |
| 46 | }; |
| 47 | |
| 48 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- observes control resize, DOM mutations and font loading to measure which controls fit |
| 49 | useLayoutEffect(() => { |
| 50 | const currentHost = useControlsAsHost ? controls : host; |
| 51 | if (!currentHost || !controls) return; |
| 52 | let stopObserving = () => {}; |
| 53 | const observeControls = () => { |
| 54 | stopObserving(); |
| 55 | stopObserving = observeResize( |
| 56 | [ |
| 57 | currentHost, |
| 58 | controls, |
| 59 | ...controls.querySelectorAll<HTMLElement>( |
| 60 | "[data-resting-block], [data-composer-control-label], [data-chat-provider-model-picker-label]", |
| 61 | ), |
| 62 | ], |
| 63 | measure, |
| 64 | ); |
| 65 | measure(); |
| 66 | }; |
| 67 | observeControls(); |
| 68 | const mutations = new MutationObserver(observeControls); |
| 69 | mutations.observe(controls, { childList: true, subtree: true, characterData: true }); |
| 70 | document.fonts.addEventListener("loadingdone", measure); |
| 71 | return () => { |
| 72 | stopObserving(); |
| 73 | mutations.disconnect(); |
| 74 | document.fonts.removeEventListener("loadingdone", measure); |
| 75 | }; |
| 76 | }, [host, controls, useControlsAsHost, measure]); |
| 77 | |
| 78 | return { |
| 79 | controlsRef, |
| 80 | attachControls, |
| 81 | hiddenBlockCount: layout.hiddenCount, |
| 82 | iconOnlyBlockCount: layout.iconOnlyCount ?? 0, |
| 83 | controlsVisible: layout.visible, |
| 84 | }; |
| 85 | } |