1
0
Fork 0
forked from Mirrors/gomuks

web/roomlist: select first entry and clear query on enter

This commit is contained in:
Tulir Asokan 2024-11-08 12:02:41 +01:00
parent 5701bbf708
commit 67f9bc348b

View file

@ -19,6 +19,7 @@ import { useEventAsState } from "@/util/eventdispatcher.ts"
import reverseMap from "@/util/reversemap.ts" import reverseMap from "@/util/reversemap.ts"
import toSearchableString from "@/util/searchablestring.ts" import toSearchableString from "@/util/searchablestring.ts"
import ClientContext from "../ClientContext.ts" import ClientContext from "../ClientContext.ts"
import MainScreenContext from "../MainScreenContext.ts"
import { keyToString } from "../keybindings.ts" import { keyToString } from "../keybindings.ts"
import Entry from "./Entry.tsx" import Entry from "./Entry.tsx"
import CloseIcon from "@/icons/close.svg?react" import CloseIcon from "@/icons/close.svg?react"
@ -31,6 +32,7 @@ interface RoomListProps {
const RoomList = ({ activeRoomID }: RoomListProps) => { const RoomList = ({ activeRoomID }: RoomListProps) => {
const client = use(ClientContext)! const client = use(ClientContext)!
const mainScreen = use(MainScreenContext)
const roomList = useEventAsState(client.store.roomList) const roomList = useEventAsState(client.store.roomList)
const roomFilterRef = useRef<HTMLInputElement>(null) const roomFilterRef = useRef<HTMLInputElement>(null)
const [roomFilter, setRoomFilter] = useState("") const [roomFilter, setRoomFilter] = useState("")
@ -48,8 +50,17 @@ const RoomList = ({ activeRoomID }: RoomListProps) => {
roomFilterRef.current?.focus() roomFilterRef.current?.focus()
}, [client]) }, [client])
const onKeyDown = useCallback((evt: React.KeyboardEvent<HTMLInputElement>) => { const onKeyDown = useCallback((evt: React.KeyboardEvent<HTMLInputElement>) => {
if (keyToString(evt) === "Escape") { const key = keyToString(evt)
if (key === "Escape") {
clearQuery() clearQuery()
evt.stopPropagation()
evt.preventDefault()
} else if (key === "Enter") {
const roomList = client.store.getFilteredRoomList()
mainScreen.setActiveRoom(roomList[roomList.length-1]?.room_id)
clearQuery()
evt.stopPropagation()
evt.preventDefault()
} }
}, [clearQuery]) }, [clearQuery])