From acfb0560bc39727390b33fc8a2f0be30610b9c53 Mon Sep 17 00:00:00 2001 From: Tyler Brown Cifu Shuster Date: Thu, 11 Mar 2021 20:11:48 -0800 Subject: [PATCH] interface: added debug menu --- .../apps/settings/components/lib/CalmPref.tsx | 2 +- .../apps/settings/components/lib/Debug.tsx | 101 ++++++++++++++++++ .../src/views/apps/settings/settings.tsx | 22 +++- 3 files changed, 122 insertions(+), 3 deletions(-) create mode 100644 pkg/interface/src/views/apps/settings/components/lib/Debug.tsx diff --git a/pkg/interface/src/views/apps/settings/components/lib/CalmPref.tsx b/pkg/interface/src/views/apps/settings/components/lib/CalmPref.tsx index c368ccc9e2..16530b5734 100644 --- a/pkg/interface/src/views/apps/settings/components/lib/CalmPref.tsx +++ b/pkg/interface/src/views/apps/settings/components/lib/CalmPref.tsx @@ -80,7 +80,7 @@ export function CalmPrefs(props: {
- + CalmEngine diff --git a/pkg/interface/src/views/apps/settings/components/lib/Debug.tsx b/pkg/interface/src/views/apps/settings/components/lib/Debug.tsx new file mode 100644 index 0000000000..c667ee2bcd --- /dev/null +++ b/pkg/interface/src/views/apps/settings/components/lib/Debug.tsx @@ -0,0 +1,101 @@ +import { BaseInput, Box, Col, Text } from "@tlon/indigo-react"; +import _ from "lodash"; +import React, { useCallback, useState } from "react"; +import { UseStore } from "zustand"; +import { BaseState } from "~/logic/state/base"; +import useContactState from "~/logic/state/contact"; +import useGraphState from "~/logic/state/graph"; +import useGroupState from "~/logic/state/group"; +import useHarkState from "~/logic/state/hark"; +import useInviteState from "~/logic/state/invite"; +import useLaunchState from "~/logic/state/launch"; +import useMetadataState from "~/logic/state/metadata"; +import useSettingsState from "~/logic/state/settings"; +import useStorageState from "~/logic/state/storage"; +import { BackButton } from "./BackButton"; + +interface StoreDebuggerProps { + name: string; + useStore: UseStore>; +} + +const objectToString = (obj: any): string => JSON.stringify(obj, null, ' '); + +const StoreDebugger = (props: StoreDebuggerProps) => { + const name = props.name; + const state = props.useStore(); + const [filter, setFilter] = useState(''); + const [text, setText] = useState(objectToString(state)); + const [visible, setVisible] = useState(false); + + const tryFilter = useCallback((filterToTry) => { + let output: any = false; + try { + output = _.get(state, filterToTry, undefined); + } catch (e) { } + if (output) { + console.log(output); + setText(objectToString(output)); + setFilter(filterToTry); + } + }, [state, filter, text]); + + + return ( + + setVisible(!visible)}>{name} + {visible && + { + if (event.target.value) { + tryFilter(event.target.value); + } else { + setFilter(''); + setText(objectToString(state)); + } + }} /> + {text} + } + + ); +}; + +const DebugPane = () => { + return ( + <> + + + + + Debug Menu + + + Debug Landscape state. Click any state to see its contents and drill down. + + + + + + + + + + + + + + ) +}; + +export default DebugPane; \ No newline at end of file diff --git a/pkg/interface/src/views/apps/settings/settings.tsx b/pkg/interface/src/views/apps/settings/settings.tsx index b2ae9f8550..a4a293820f 100644 --- a/pkg/interface/src/views/apps/settings/settings.tsx +++ b/pkg/interface/src/views/apps/settings/settings.tsx @@ -1,4 +1,4 @@ -import React, { ReactNode } from 'react'; +import React, { ReactNode, useEffect } from 'react'; import { useLocation } from 'react-router-dom'; import Helmet from 'react-helmet'; @@ -13,6 +13,8 @@ import { LeapSettings } from './components/lib/LeapSettings'; import { useHashLink } from '~/logic/lib/useHashLink'; import { SidebarItem as BaseSidebarItem } from '~/views/landscape/components/SidebarItem'; import { PropFunc } from '~/types'; +import DebugPane from './components/lib/Debug'; +import useHarkState from '~/logic/state/hark'; export const Skeleton = (props: { children: ReactNode }) => ( @@ -70,11 +72,26 @@ function SettingsItem(props: { children: ReactNode }) { export default function SettingsScreen(props: any) { const location = useLocation(); const hash = location.hash.slice(1); + const notificationsCount = useHarkState(state => state.notificationsCount); + + useEffect(() => { + const debugShower = (event) => { + if (hash) return; + if (event.key === '~') { + window.location.hash = 'debug'; + } + }; + document.addEventListener('keyup', debugShower); + + return () => { + document.removeEventListener('keyup', debugShower); + } + }, [hash]); return ( <> - Landscape - Settings + { notificationsCount ? `(${String(notificationsCount) }) `: '' }Landscape - Settings } {hash === 'calm' && } {hash === 'security' && } + {hash === 'debug' && }