Skip to content

Commit 58eb7ca

Browse files
authored
feat: popup a notification when switch off (#104)
1 parent a6784af commit 58eb7ca

File tree

2 files changed

+138
-2
lines changed

2 files changed

+138
-2
lines changed

src/renderer/components/connection/DeviceSelect.tsx

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,92 @@ import debugDriver from '@/renderer/utils/debugDriver';
1111
import { setSimulatorViewMode } from '@/renderer/utils/ldtApi';
1212
import { IDevice } from '@lynx-js/devtool-plugin-core/renderer';
1313
import { DesktopOutlined, MobileOutlined } from '@ant-design/icons';
14-
import { Dropdown, Button } from 'antd';
14+
import { Dropdown, Button, notification } from 'antd';
1515
import { MenuProps } from 'antd';
1616
import { useEffect, useMemo } from 'react';
1717
import './DeviceSelect.scss';
1818
import IconPlatform from './IconPlatform/IconPlatform';
1919
import useldtConnection from '@/renderer/ldt/store/ldtConnection';
20-
import { getStopAtEntry, getFetchDebugInfo } from '@/renderer/utils/switchUtils';
20+
import { getStopAtEntry, getFetchDebugInfo, getSwitchStatus, openDevtool, openDomTree } from '@/renderer/utils/switchUtils';
21+
22+
let notificationKey = '';
23+
24+
interface SwitchNotificationContentProps {
25+
devtoolSwitch: boolean;
26+
domTreeSwitch: boolean;
27+
close: () => void;
28+
}
29+
30+
function SwitchNotificationContent({ devtoolSwitch, domTreeSwitch, close }: SwitchNotificationContentProps) {
31+
const handleEnableAll = async () => {
32+
const results = await Promise.all([
33+
!devtoolSwitch ? openDevtool(true) : Promise.resolve(true),
34+
!domTreeSwitch ? openDomTree(true) : Promise.resolve(true)
35+
]);
36+
if (results.every(Boolean)) {
37+
close();
38+
}
39+
};
40+
41+
return (
42+
<div>
43+
<p style={{ marginBottom: 8 }}>
44+
The following switches need to be enabled for DevTool to work properly:
45+
</p>
46+
<ul style={{ marginBottom: 12, paddingLeft: 20 }}>
47+
{!devtoolSwitch && <li>enable_devtool</li>}
48+
{!domTreeSwitch && <li>enable_dom_tree</li>}
49+
</ul>
50+
<Button type="primary" size="small" onClick={handleEnableAll}>
51+
Enable All
52+
</Button>
53+
</div>
54+
);
55+
}
2156

2257
export default function DeviceSelect() {
2358
const { deviceList, selectedDevice, setSelectedDevice } = useConnection();
2459
const { selectedDevice: currentDevice, setCurrentDevice } = useldtConnection();
2560

2661
useEffect(() => {
2762
setCurrentDevice(xdbDriver.getCurrentDevice());
63+
checkDevtoolSwitch();
2864
getStopAtEntry('DEFAULT');
2965
getStopAtEntry('MTS');
3066
getFetchDebugInfo('MTS');
3167
}, [selectedDevice]);
3268

69+
const checkDevtoolSwitch = async () => {
70+
const devtoolSwitch = await getSwitchStatus('enable_devtool');
71+
const domTreeSwitch = await getSwitchStatus('enable_dom_tree');
72+
const currentClientId = debugDriver.getSelectClientId();
73+
if ((!devtoolSwitch || !domTreeSwitch) && currentClientId === selectedDevice?.clientId) {
74+
if (notificationKey !== '') {
75+
notification.destroy(notificationKey);
76+
}
77+
notificationKey = `devtool-switch-${Date.now()}`;
78+
notification.warning({
79+
key: notificationKey,
80+
message: 'DevTool Switch Required',
81+
description: (
82+
<SwitchNotificationContent
83+
devtoolSwitch={devtoolSwitch}
84+
domTreeSwitch={domTreeSwitch}
85+
close={closeNotification}
86+
/>
87+
),
88+
duration: 0
89+
});
90+
}
91+
};
92+
93+
const closeNotification = () => {
94+
if (notificationKey !== '') {
95+
notification.destroy(notificationKey);
96+
notificationKey = '';
97+
}
98+
};
99+
33100
function combieDevices(groupKey: 'App' | 'deviceModel' = 'App'): IDevice[] {
34101
const sortList = deviceList
35102
.filter((item) => item.clientId && item.info?.deviceType !== 'simulator')

src/renderer/utils/switchUtils.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,75 @@ import debugDriver from './debugDriver';
66
import { getStore } from './flooks';
77
import useConnection from '../hooks/connection';
88

9+
export async function setGlobalSwitch(params: any) {
10+
try {
11+
const result = await debugDriver.sendCustomMessageAsync({ params, type: 'SetGlobalSwitch' });
12+
if (typeof result === 'object') {
13+
return result?.global_value === 'true' || result?.global_value === true;
14+
} else {
15+
return result === 'true' || result === true;
16+
}
17+
} catch (error) {
18+
console.error('setGlobalSwitch error', error);
19+
return false;
20+
}
21+
}
22+
23+
export async function getGlobalSwitch(params: any) {
24+
try {
25+
const result = await debugDriver.sendCustomMessageAsync({ params, type: 'GetGlobalSwitch' });
26+
if (typeof result === 'object') {
27+
return result?.global_value === 'true' || result?.global_value === true;
28+
} else {
29+
return result === 'true' || result === true;
30+
}
31+
} catch (error) {
32+
console.error('getGlobalSwitch error', error);
33+
return true;
34+
}
35+
}
36+
37+
export async function getSwitchStatus(key: string) {
38+
const params = {
39+
global_key: key
40+
};
41+
return await getGlobalSwitch(params);
42+
}
43+
44+
export async function openDevtool(open: boolean) {
45+
try {
46+
const params = {
47+
global_key: 'enable_devtool',
48+
global_value: open
49+
};
50+
const result = await setGlobalSwitch(params);
51+
if (result !== open) {
52+
return false;
53+
}
54+
} catch (error) {
55+
console.error('openDevtool error', error);
56+
return false;
57+
}
58+
return true;
59+
}
60+
61+
export async function openDomTree(open: boolean) {
62+
try {
63+
const params = {
64+
global_key: 'enable_dom_tree',
65+
global_value: open
66+
};
67+
const result = await setGlobalSwitch(params);
68+
if (result !== open) {
69+
return false;
70+
}
71+
} catch (error) {
72+
console.error('openDomTree error', error);
73+
return false;
74+
}
75+
return true;
76+
}
77+
978
function updateDeviceInfo(info: string, type: string, value: boolean) {
1079
const currentClientId = debugDriver.getSelectClientId();
1180
if (currentClientId === undefined) {

0 commit comments

Comments
 (0)