summaryrefslogtreecommitdiff
path: root/frontend/src/components/DeckyErrorBoundary.tsx
blob: 6fe234b7c8def2bcbf543c6d637100d018682d91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import { sleep } from '@decky/ui';
import { FunctionComponent, useEffect, useReducer, useState } from 'react';

import { uninstallPlugin } from '../plugin';
import { VerInfo, doRestart, doShutdown } from '../updater';
import { ValveReactErrorInfo, getLikelyErrorSourceFromValveReactError } from '../utils/errors';

interface DeckyErrorBoundaryProps {
  error: ValveReactErrorInfo;
  errorKey: string;
  identifier: string;
  reset: () => void;
}

declare global {
  interface Window {
    SystemNetworkStore?: any;
  }
}

export const startSSH = DeckyBackend.callable('utilities/start_ssh');
export const starrCEFForwarding = DeckyBackend.callable('utilities/allow_remote_debugging');

function ipToString(ip: number) {
  return [(ip >>> 24) & 255, (ip >>> 16) & 255, (ip >>> 8) & 255, (ip >>> 0) & 255].join('.');
}

// Intentionally not localized since we can't really trust React here
const DeckyErrorBoundary: FunctionComponent<DeckyErrorBoundaryProps> = ({ error, identifier, reset }) => {
  const [actionLog, addLogLine] = useReducer((log: string, line: string) => (log += '\n' + line), '');
  const [actionsEnabled, setActionsEnabled] = useState<boolean>(true);
  const [debugAllowed, setDebugAllowed] = useState<boolean>(true);
  // Intentionally doesn't use DeckyState.
  const [versionInfo, setVersionInfo] = useState<VerInfo>();
  const [errorSource, wasCausedByPlugin] = getLikelyErrorSourceFromValveReactError(error);
  useEffect(() => {
    DeckyPluginLoader.updateVersion().then(setVersionInfo);
  }, []);
  return (
    <>
      <style>
        {`
          *:has(> .deckyErrorBoundary) {
            overflow: scroll !important;
          }
        `}
      </style>
      <div
        style={{
          overflow: 'auto',
          marginLeft: '15px',
          color: 'white',
          fontSize: '16px',
          userSelect: 'auto',
          backgroundColor: 'black',
          marginTop: '48px', // Incase this is a page
        }}
        className="deckyErrorBoundary"
      >
        <h1
          style={{
            fontSize: '20px',
            display: 'inline-block',
            userSelect: 'auto',
          }}
        >
          ⚠️ An error occured rendering this content.
        </h1>
        <pre style={{}}>
          <code>
            {identifier && `Error Reference: ${identifier}`}
            {versionInfo?.current && `\nDecky Version: ${versionInfo.current}`}
          </code>
        </pre>
        <p>This error likely occured in {errorSource}.</p>
        {actionLog?.length > 0 && (
          <pre>
            <code>
              Running actions...
              {actionLog}
            </code>
          </pre>
        )}
        {actionsEnabled && (
          <>
            <h3>Actions: </h3>
            <p>Use the touch screen.</p>
            <div style={{ display: 'block', marginBottom: '5px' }}>
              <button style={{ marginRight: '5px', padding: '5px' }} onClick={reset}>
                Retry
              </button>
              <button
                style={{ marginRight: '5px', padding: '5px' }}
                onClick={() => {
                  addLogLine('Restarting Steam...');
                  SteamClient.User.StartRestart();
                }}
              >
                Restart Steam
              </button>
            </div>
            <div style={{ display: 'block', marginBottom: '5px' }}>
              <button
                style={{ marginRight: '5px', padding: '5px' }}
                onClick={async () => {
                  setActionsEnabled(false);
                  addLogLine('Restarting Decky...');
                  doRestart();
                  await sleep(2000);
                  addLogLine('Reloading UI...');
                }}
              >
                Restart Decky
              </button>
              <button
                style={{ marginRight: '5px', padding: '5px' }}
                onClick={async () => {
                  setActionsEnabled(false);
                  addLogLine('Stopping Decky...');
                  doShutdown();
                  await sleep(5000);
                  addLogLine('Restarting Steam...');
                  SteamClient.User.StartRestart();
                }}
              >
                Disable Decky until next boot
              </button>
            </div>
            {debugAllowed && (
              <div style={{ display: 'block', marginBottom: '5px' }}>
                <button
                  style={{ marginRight: '5px', padding: '5px' }}
                  onClick={async () => {
                    setDebugAllowed(false);
                    addLogLine('Enabling CEF debugger forwarding...');
                    await starrCEFForwarding();
                    addLogLine('Enabling SSH...');
                    await startSSH();
                    addLogLine('Ready for debugging!');
                    if (window?.SystemNetworkStore?.wirelessNetworkDevice?.ip4?.addresses?.[0]?.ip) {
                      const ip = ipToString(window.SystemNetworkStore.wirelessNetworkDevice.ip4.addresses[0].ip);
                      addLogLine(`CEF Debugger: http://${ip}:8081`);
                      addLogLine(`SSH: deck@${ip}`);
                    }
                  }}
                >
                  Allow remote debugging and SSH until next boot
                </button>
              </div>
            )}
            {wasCausedByPlugin && (
              <div style={{ display: 'block', marginBottom: '5px' }}>
                {'\n'}
                <button
                  style={{ marginRight: '5px', padding: '5px' }}
                  onClick={async () => {
                    setActionsEnabled(false);
                    addLogLine(`Uninstalling ${errorSource}...`);
                    await uninstallPlugin(errorSource);
                    await DeckyPluginLoader.frozenPluginsService.invalidate();
                    await DeckyPluginLoader.hiddenPluginsService.invalidate();
                    await sleep(1000);
                    addLogLine('Restarting Decky...');
                    doRestart();
                    await sleep(2000);
                    addLogLine('Restarting Steam...');
                    await sleep(500);
                    SteamClient.User.StartRestart();
                  }}
                >
                  Uninstall {errorSource} and restart Decky
                </button>
              </div>
            )}
          </>
        )}

        <pre
          style={{
            marginTop: '15px',
            opacity: 0.7,
            userSelect: 'auto',
          }}
        >
          <code>
            {error.error.stack}
            {'\n\n'}
            Component Stack:
            {error.info.componentStack}
          </code>
        </pre>
      </div>
    </>
  );
};

export default DeckyErrorBoundary;