Skip to content

Commit f3eef14

Browse files
authored
Merge pull request supabase-community#34 from supabase-community/feat/focus-input
2 parents f85d90c + b01847c commit f3eef14

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

apps/postgres-new/components/app-provider.tsx

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ import { User } from '@supabase/supabase-js'
88
import {
99
createContext,
1010
PropsWithChildren,
11+
RefObject,
1112
useCallback,
1213
useContext,
1314
useEffect,
14-
useMemo,
15+
useRef,
1516
useState,
1617
} from 'react'
1718
import { DbManager } from '~/lib/db'
@@ -27,8 +28,20 @@ export default function AppProvider({ children }: AppProps) {
2728
const [isLoadingUser, setIsLoadingUser] = useState(true)
2829
const [user, setUser] = useState<User>()
2930

31+
const focusRef = useRef<FocusHandle>(null)
32+
3033
const supabase = createClient()
3134

35+
useEffect(() => {
36+
const {
37+
data: { subscription },
38+
} = supabase.auth.onAuthStateChange((e) => {
39+
focusRef.current?.focus()
40+
})
41+
42+
return () => subscription.unsubscribe()
43+
}, [supabase])
44+
3245
const loadUser = useCallback(async () => {
3346
setIsLoadingUser(true)
3447
try {
@@ -97,6 +110,7 @@ export default function AppProvider({ children }: AppProps) {
97110
isLoadingUser,
98111
signIn,
99112
signOut,
113+
focusRef,
100114
isPreview,
101115
dbManager,
102116
pgliteVersion,
@@ -108,11 +122,16 @@ export default function AppProvider({ children }: AppProps) {
108122
)
109123
}
110124

125+
export type FocusHandle = {
126+
focus(): void
127+
}
128+
111129
export type AppContextValues = {
112130
user?: User
113131
isLoadingUser: boolean
114132
signIn: () => Promise<User | undefined>
115133
signOut: () => Promise<void>
134+
focusRef: RefObject<FocusHandle>
116135
isPreview: boolean
117136
dbManager?: DbManager
118137
pgliteVersion?: string

apps/postgres-new/components/chat.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
FormEventHandler,
1010
useCallback,
1111
useEffect,
12+
useImperativeHandle,
1213
useMemo,
1314
useRef,
1415
useState,
@@ -47,7 +48,7 @@ export function getInitialMessages(tables: TablesData): Message[] {
4748
}
4849

4950
export default function Chat() {
50-
const { user, isLoadingUser } = useApp()
51+
const { user, isLoadingUser, focusRef } = useApp()
5152
const [inputFocusState, setInputFocusState] = useState(false)
5253

5354
const {
@@ -175,6 +176,15 @@ export default function Chat() {
175176
const isSubmitEnabled =
176177
!isLoadingMessages && !isLoadingSchema && Boolean(input.trim()) && user !== undefined
177178

179+
// Create imperative handle that can be used to focus the input anywhere in the app
180+
useImperativeHandle(focusRef, () => ({
181+
focus() {
182+
if (inputRef.current) {
183+
inputRef.current.focus()
184+
}
185+
},
186+
}))
187+
178188
return (
179189
<div ref={dropZoneRef} className="h-full flex flex-col items-stretch relative">
180190
{isDraggingOver && (

apps/postgres-new/components/sidebar.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import {
4040
} from './ui/dropdown-menu'
4141

4242
export default function Sidebar() {
43-
const { user, signOut } = useApp()
43+
const { user, signOut, focusRef } = useApp()
4444
let { id: currentDatabaseId } = useParams<{ id: string }>()
4545
const router = useRouter()
4646
const { data: databases, isLoading: isLoadingDatabases } = useDatabasesQuery()
@@ -84,6 +84,7 @@ export default function Sidebar() {
8484
<Button
8585
onClick={() => {
8686
router.push('/')
87+
focusRef.current?.focus()
8788
}}
8889
className="gap-2"
8990
>
@@ -176,6 +177,7 @@ export default function Sidebar() {
176177
size={'icon'}
177178
onClick={() => {
178179
router.push('/')
180+
focusRef.current?.focus()
179181
}}
180182
>
181183
<PackagePlus size={14} />

0 commit comments

Comments
 (0)