mirror of
https://github.com/tulir/gomuks.git
synced 2025-04-20 18:43:41 -05:00
commited correct file
This commit is contained in:
parent
a30d7a0913
commit
9736919d1a
1 changed files with 108 additions and 52 deletions
|
@ -1,61 +1,117 @@
|
|||
import { RoomStateStore } from "@/api/statestore"
|
||||
import { useState } from "react"
|
||||
import { useState, use } from "react"
|
||||
import { EventType } from "@/api/types"
|
||||
import JSONView from "../util/JSONView"
|
||||
import ClientContext from "../ClientContext"
|
||||
import useEvent from "@/util/useEvent"
|
||||
|
||||
interface StateViewerProps {
|
||||
room: RoomStateStore
|
||||
}
|
||||
|
||||
// 1. you go in the state viewer, it shows buttons with each state event type
|
||||
// 2. you click a button, it shows the state keys for that type
|
||||
// 3. you click a state key, it shows a JSONView of the event
|
||||
|
||||
const StateViewer = ({ room }: StateViewerProps) => {
|
||||
// state
|
||||
const [page, setPage] = useState("state-type")
|
||||
const [stateType, setStateType] = useState("")
|
||||
const [stateKey, setStateKey] = useState("")
|
||||
// button actions
|
||||
// 1
|
||||
const stateTypeClick = (evt: React.MouseEvent<HTMLButtonElement>) => {
|
||||
const type = evt.currentTarget.getAttribute("data-state-type")
|
||||
if (!type) {
|
||||
return
|
||||
}
|
||||
console.log(room.state.get(type))
|
||||
// progress to 2
|
||||
setStateType(type)
|
||||
setPage("state-key")
|
||||
}
|
||||
// 2
|
||||
const stateKeyClick = (evt: React.MouseEvent<HTMLButtonElement>) => {
|
||||
const key = evt.currentTarget.getAttribute("data-state-key")
|
||||
if (!key) {
|
||||
return
|
||||
}
|
||||
// progress to 3
|
||||
setStateKey(key)
|
||||
setPage("state-event")
|
||||
interface StatePageProps {
|
||||
room: RoomStateStore,
|
||||
onClick?: (evt: React.MouseEvent<HTMLButtonElement>) => void,
|
||||
eventType?: EventType,
|
||||
stateKey?: string
|
||||
}
|
||||
|
||||
// render 1, 2 or 3
|
||||
switch (page) {
|
||||
case "state-type":
|
||||
interface StateState {
|
||||
page: "all" | "type" | "event",
|
||||
eventType?: EventType,
|
||||
stateKey?: string
|
||||
}
|
||||
|
||||
const StateAll = ({ room, onClick }: StatePageProps) => {
|
||||
const types: string[] = []
|
||||
for (const [type] of room.state) {
|
||||
types.push(type)
|
||||
}
|
||||
return types.map(type => <button data-state-type={type} onClick={stateTypeClick}>{type}</button>)
|
||||
case "state-key":
|
||||
return types.map(type => <button data-event-type={type} onClick={onClick}>{type}</button>)
|
||||
}
|
||||
|
||||
const StateType = ({ room, onClick, eventType }: StatePageProps) => {
|
||||
if (eventType == null) {
|
||||
return
|
||||
}
|
||||
const keysMap = room.state.get(eventType)
|
||||
const keys: string[] = []
|
||||
for (const [key] of room.state.get(stateType) ?? []) { // ?? [] makes the rest pointless, TODO short circuit lol
|
||||
for (const [key] of keysMap ?? []) {
|
||||
keys.push(key)
|
||||
}
|
||||
return keys.map(type => <button data-state-key={type} onClick={stateKeyClick}>{type}</button>)
|
||||
case "state-event":
|
||||
const content = room.getStateEvent(stateType, stateKey)
|
||||
return keys.map(key => <button data-state-key={key} onClick={onClick}>{key.length == 0 ? "<empty>" : key}</button>)
|
||||
}
|
||||
|
||||
const StateEvent = ({ room, eventType, stateKey }: StatePageProps) => {
|
||||
if (eventType == undefined || stateKey == undefined) {
|
||||
return
|
||||
}
|
||||
const content = room.getStateEvent(eventType, stateKey)
|
||||
return <JSONView data={content}/>
|
||||
}
|
||||
|
||||
const StateViewer = ({ room }: StateViewerProps) => {
|
||||
const [state, setState] = useState({page: "all"} as StateState)
|
||||
const client = use(ClientContext)
|
||||
if (!room.stateLoaded) {
|
||||
client?.loadRoomState(room.roomID, { omitMembers: false, refetch: true })
|
||||
}
|
||||
const onClickAll = useEvent((evt: React.MouseEvent<HTMLButtonElement>) => {
|
||||
const type = evt.currentTarget.getAttribute("data-event-type")
|
||||
if (type == null) {
|
||||
return
|
||||
}
|
||||
setState({
|
||||
page: "type",
|
||||
eventType: type
|
||||
})
|
||||
})
|
||||
|
||||
const onClickType = useEvent((evt: React.MouseEvent<HTMLButtonElement>) => {
|
||||
const key = evt.currentTarget.getAttribute("data-state-key")
|
||||
if (key == null) {
|
||||
return
|
||||
}
|
||||
setState({
|
||||
page: "event",
|
||||
eventType: state.eventType,
|
||||
stateKey: key
|
||||
})
|
||||
})
|
||||
|
||||
const onClickBack = useEvent(() => {
|
||||
switch (state.page) {
|
||||
case "type":
|
||||
setState({
|
||||
page: "all"
|
||||
})
|
||||
return
|
||||
case "event":
|
||||
setState({
|
||||
page: "type",
|
||||
eventType: state.eventType
|
||||
})
|
||||
return
|
||||
}
|
||||
})
|
||||
|
||||
let content = <></>
|
||||
switch (state.page) {
|
||||
case "all":
|
||||
content = <StateAll room={room} onClick={onClickAll}/>
|
||||
break
|
||||
case "type":
|
||||
content = <StateType room={room} onClick={onClickType} eventType={state.eventType}/>
|
||||
break
|
||||
case "event":
|
||||
content = <StateEvent room={room} eventType={state.eventType} stateKey={state.stateKey}/>
|
||||
break
|
||||
}
|
||||
return <>
|
||||
<h3>Explore room state</h3>
|
||||
{content}
|
||||
{state.page != "all" && <button onClick={onClickBack}>Back</button>}
|
||||
</>
|
||||
}
|
||||
|
||||
export default StateViewer
|
Loading…
Add table
Reference in a new issue