Skip to content

Commit

Permalink
Add sanity checks when patching route
Browse files Browse the repository at this point in the history
  • Loading branch information
FrogTheFrog committed Jan 17, 2023
1 parent e1a07b4 commit 5a90523
Showing 1 changed file with 39 additions and 20 deletions.
59 changes: 39 additions & 20 deletions src/lib/patchLibraryApp.tsx
Original file line number Diff line number Diff line change
@@ -1,39 +1,58 @@
import { afterPatch, ServerAPI, wrapReactType } from 'decky-frontend-lib'
import {
afterPatch,
ServerAPI,
wrapReactType,
findInReactTree,
appDetailsClasses
} from 'decky-frontend-lib'
import { ReactElement } from 'react'
import ProtonMedal from '../components/protonMedal'
import { SettingsProvider } from '../context/settingsContext'

function patchLibraryApp(serverAPI: ServerAPI) {
return serverAPI.routerHook.addPatch(
'/library/app/:appid',
(props: { path: string; children: ReactElement }) => {
(props?: { path?: string; children?: ReactElement }) => {
if (!props?.children?.props?.renderFunc) {
return props
}

afterPatch(
props.children.props,
'renderFunc',
(_: Record<string, unknown>[], ret: ReactElement) => {
(_: Record<string, unknown>[], ret?: ReactElement) => {
if (!ret?.props?.children?.type?.type) {
return ret
}

wrapReactType(ret.props.children)
afterPatch(
ret.props.children.type,
'type',
(_2: Record<string, unknown>[], ret2: ReactElement) => {
const alreadySpliced = Boolean(
ret2.props?.children?.[1]?.props.children.props.children.find(
(child: ReactElement) =>
child?.props?.className === 'protondb-decky-indicator'
)
(_2: Record<string, unknown>[], ret2?: ReactElement) => {
const container = findInReactTree(
ret2,
(x: any) =>
Array.isArray(x?.props?.children) &&
x?.props?.className?.includes(
appDetailsClasses.InnerContainer
)
)
if (!alreadySpliced) {
ret2.props.children?.[1]?.props.children.props.children.splice(
1,
0,
<SettingsProvider>
<ProtonMedal
serverAPI={serverAPI}
className="protondb-decky-indicator"
/>
</SettingsProvider>
)
if (typeof container !== 'object') {
return ret2
}

container.props.children.splice(
1,
0,
<SettingsProvider>
<ProtonMedal
serverAPI={serverAPI}
className="protondb-decky-indicator"
/>
</SettingsProvider>
)

return ret2
}
)
Expand Down

0 comments on commit 5a90523

Please sign in to comment.