aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Kristóf Marussy <kristof@marussy.com>2022-03-15 00:15:31 +0100
committerLibravatar Kristóf Marussy <kristof@marussy.com>2022-05-16 00:54:56 +0200
commitd425435c290a7e63c4e30a469da51f43be4db5cc (patch)
tree73b1cfed3ad81bc987312a9093b654ca4bc37c1f
parentfeat(renderer): Show service error on service icon (diff)
downloadsophie-d425435c290a7e63c4e30a469da51f43be4db5cc.tar.gz
sophie-d425435c290a7e63c4e30a469da51f43be4db5cc.tar.zst
sophie-d425435c290a7e63c4e30a469da51f43be4db5cc.zip
feat: Open in external browser
Signed-off-by: Kristóf Marussy <kristof@marussy.com>
-rw-r--r--packages/main/src/index.ts5
-rw-r--r--packages/main/src/infrastructure/electron/impl/electronShell.ts36
-rw-r--r--packages/main/src/stores/MainEnv.ts (renamed from packages/renderer/src/env/RendererEnv.ts)12
-rw-r--r--packages/main/src/stores/MainStore.ts4
-rw-r--r--packages/main/src/stores/Service.ts29
-rw-r--r--packages/renderer/src/components/locationBar/ExtraButtons.tsx52
-rw-r--r--packages/renderer/src/components/locationBar/LocationBar.tsx2
-rw-r--r--packages/renderer/src/components/locationBar/NavigationButtons.tsx2
-rw-r--r--packages/renderer/src/stores/GlobalSettings.ts3
-rw-r--r--packages/renderer/src/stores/RendererEnv.ts (renamed from packages/renderer/src/env/getEnv.ts)11
-rw-r--r--packages/renderer/src/stores/RendererStore.ts3
-rw-r--r--packages/renderer/src/stores/Service.ts8
-rw-r--r--packages/shared/src/schemas/ServiceAction.ts3
13 files changed, 140 insertions, 30 deletions
diff --git a/packages/main/src/index.ts b/packages/main/src/index.ts
index 40b15f0..869c555 100644
--- a/packages/main/src/index.ts
+++ b/packages/main/src/index.ts
@@ -26,8 +26,9 @@ import { ensureDirSync } from 'fs-extra';
26import osName from 'os-name'; 26import osName from 'os-name';
27 27
28import { enableStacktraceSourceMaps } from './infrastructure/electron/impl/devTools'; 28import { enableStacktraceSourceMaps } from './infrastructure/electron/impl/devTools';
29import electronShell from './infrastructure/electron/impl/electronShell';
29import initReactions from './initReactions'; 30import initReactions from './initReactions';
30import { createMainStore } from './stores/MainStore'; 31import MainStore from './stores/MainStore';
31import { getLogger } from './utils/log'; 32import { getLogger } from './utils/log';
32 33
33const isDevelopment = import.meta.env.MODE === 'development'; 34const isDevelopment = import.meta.env.MODE === 'development';
@@ -77,7 +78,7 @@ app.setAboutPanelOptions({
77 version: '', 78 version: '',
78}); 79});
79 80
80const store = createMainStore(); 81const store = MainStore.create({}, electronShell);
81 82
82app.on('second-instance', () => { 83app.on('second-instance', () => {
83 store.mainWindow?.bringToForeground(); 84 store.mainWindow?.bringToForeground();
diff --git a/packages/main/src/infrastructure/electron/impl/electronShell.ts b/packages/main/src/infrastructure/electron/impl/electronShell.ts
new file mode 100644
index 0000000..0e8c0c1
--- /dev/null
+++ b/packages/main/src/infrastructure/electron/impl/electronShell.ts
@@ -0,0 +1,36 @@
1/*
2 * Copyright (C) 2022 Kristóf Marussy <kristof@marussy.com>
3 *
4 * This file is part of Sophie.
5 *
6 * Sophie is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as
8 * published by the Free Software Foundation, version 3.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: AGPL-3.0-only
19 */
20
21import { shell } from 'electron';
22import { getLogger } from 'loglevel';
23
24import type MainEnv from '../../../stores/MainEnv';
25
26const log = getLogger('ElectronShell');
27
28const electronShell: MainEnv = {
29 openURLInExternalBrowser(url: string): void {
30 shell.openExternal(url).catch((error) => {
31 log.error('Failed to open', url, 'in external program', error);
32 });
33 },
34};
35
36export default electronShell;
diff --git a/packages/renderer/src/env/RendererEnv.ts b/packages/main/src/stores/MainEnv.ts
index ba4c43e..94c34f3 100644
--- a/packages/renderer/src/env/RendererEnv.ts
+++ b/packages/main/src/stores/MainEnv.ts
@@ -1,5 +1,5 @@
1/* 1/*
2 * Copyright (C) 2021-2022 Kristóf Marussy <kristof@marussy.com> 2 * Copyright (C) 2022 Kristóf Marussy <kristof@marussy.com>
3 * 3 *
4 * This file is part of Sophie. 4 * This file is part of Sophie.
5 * 5 *
@@ -18,8 +18,12 @@
18 * SPDX-License-Identifier: AGPL-3.0-only 18 * SPDX-License-Identifier: AGPL-3.0-only
19 */ 19 */
20 20
21import type { Action } from '@sophie/shared'; 21import { getEnv as getAnyEnv, IAnyStateTreeNode } from 'mobx-state-tree';
22 22
23export default interface RendererEnv { 23export function getEnv(model: IAnyStateTreeNode): MainEnv {
24 dispatchMainAction(action: Action): void; 24 return getAnyEnv<MainEnv>(model);
25}
26
27export default interface MainEnv {
28 openURLInExternalBrowser(url: string): void;
25} 29}
diff --git a/packages/main/src/stores/MainStore.ts b/packages/main/src/stores/MainStore.ts
index dedc740..ed0f391 100644
--- a/packages/main/src/stores/MainStore.ts
+++ b/packages/main/src/stores/MainStore.ts
@@ -124,7 +124,3 @@ const MainStore = types
124interface MainStore extends Instance<typeof MainStore> {} 124interface MainStore extends Instance<typeof MainStore> {}
125 125
126export default MainStore; 126export default MainStore;
127
128export function createMainStore(): MainStore {
129 return MainStore.create();
130}
diff --git a/packages/main/src/stores/Service.ts b/packages/main/src/stores/Service.ts
index 9b2bf1e..26e3517 100644
--- a/packages/main/src/stores/Service.ts
+++ b/packages/main/src/stores/Service.ts
@@ -30,6 +30,7 @@ import { Instance, getSnapshot, cast } from 'mobx-state-tree';
30import type { ServiceView } from '../infrastructure/electron/types'; 30import type { ServiceView } from '../infrastructure/electron/types';
31import { getLogger } from '../utils/log'; 31import { getLogger } from '../utils/log';
32 32
33import { getEnv } from './MainEnv';
33import ServiceSettings from './ServiceSettings'; 34import ServiceSettings from './ServiceSettings';
34import type ServiceConfig from './config/ServiceConfig'; 35import type ServiceConfig from './config/ServiceConfig';
35 36
@@ -88,6 +89,22 @@ const Service = defineServiceModel(ServiceSettings)
88 self.indirectMessageCount = indirect; 89 self.indirectMessageCount = indirect;
89 } 90 }
90 }, 91 },
92 goBack(): void {
93 self.serviceView?.goBack();
94 },
95 goForward(): void {
96 self.serviceView?.goForward();
97 },
98 stop(): void {
99 self.serviceView?.stop();
100 },
101 openCurrentURLInExternalBrowser(): void {
102 if (self.currentUrl === undefined) {
103 log.error('Cannot open empty URL in external browser');
104 return;
105 }
106 getEnv(self).openURLInExternalBrowser(self.currentUrl);
107 },
91 })) 108 }))
92 .actions((self) => { 109 .actions((self) => {
93 function setState(state: ServiceStateSnapshotIn): void { 110 function setState(state: ServiceStateSnapshotIn): void {
@@ -134,12 +151,6 @@ const Service = defineServiceModel(ServiceSettings)
134 }); 151 });
135 } 152 }
136 }, 153 },
137 goBack(): void {
138 self.serviceView?.goBack();
139 },
140 goForward(): void {
141 self.serviceView?.goForward();
142 },
143 reload(ignoreCache = false): void { 154 reload(ignoreCache = false): void {
144 if (self.serviceView === undefined) { 155 if (self.serviceView === undefined) {
145 setState({ type: 'initializing' }); 156 setState({ type: 'initializing' });
@@ -147,9 +158,6 @@ const Service = defineServiceModel(ServiceSettings)
147 self.serviceView?.reload(ignoreCache); 158 self.serviceView?.reload(ignoreCache);
148 } 159 }
149 }, 160 },
150 stop(): void {
151 self.serviceView?.stop();
152 },
153 go(url: string): void { 161 go(url: string): void {
154 if (self.serviceView === undefined) { 162 if (self.serviceView === undefined) {
155 self.currentUrl = url; 163 self.currentUrl = url;
@@ -207,6 +215,9 @@ const Service = defineServiceModel(ServiceSettings)
207 case 'temporarily-trust-current-certificate': 215 case 'temporarily-trust-current-certificate':
208 self.temporarilyTrustCurrentCertificate(action.fingerprint); 216 self.temporarilyTrustCurrentCertificate(action.fingerprint);
209 break; 217 break;
218 case 'open-current-url-in-external-browser':
219 self.openCurrentURLInExternalBrowser();
220 break;
210 default: 221 default:
211 log.error('Unknown action to dispatch', action); 222 log.error('Unknown action to dispatch', action);
212 break; 223 break;
diff --git a/packages/renderer/src/components/locationBar/ExtraButtons.tsx b/packages/renderer/src/components/locationBar/ExtraButtons.tsx
new file mode 100644
index 0000000..4eaee29
--- /dev/null
+++ b/packages/renderer/src/components/locationBar/ExtraButtons.tsx
@@ -0,0 +1,52 @@
1/*
2 * Copyright (C) 2022 Kristóf Marussy <kristof@marussy.com>
3 *
4 * This file is part of Sophie.
5 *
6 * Sophie is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as
8 * published by the Free Software Foundation, version 3.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: AGPL-3.0-only
19 */
20
21import IconOpenInBrowser from '@mui/icons-material/OpenInBrowser';
22import Box from '@mui/material/Box';
23import IconButton from '@mui/material/IconButton';
24import { observer } from 'mobx-react-lite';
25import React from 'react';
26
27import type Service from '../../stores/Service';
28
29function ExtraButtons({
30 service,
31}: {
32 service: Service | undefined;
33}): JSX.Element {
34 return (
35 <Box
36 sx={{
37 display: 'flex',
38 flexDirection: 'row',
39 }}
40 >
41 <IconButton
42 aria-label="Open in browser"
43 disabled={service?.currentUrl === undefined}
44 onClick={() => service?.openCurrentURLInExternalBrowser()}
45 >
46 <IconOpenInBrowser />
47 </IconButton>
48 </Box>
49 );
50}
51
52export default observer(ExtraButtons);
diff --git a/packages/renderer/src/components/locationBar/LocationBar.tsx b/packages/renderer/src/components/locationBar/LocationBar.tsx
index 0debaab..fc9c147 100644
--- a/packages/renderer/src/components/locationBar/LocationBar.tsx
+++ b/packages/renderer/src/components/locationBar/LocationBar.tsx
@@ -24,6 +24,7 @@ import React from 'react';
24 24
25import { useStore } from '../StoreProvider'; 25import { useStore } from '../StoreProvider';
26 26
27import ExtraButtons from './ExtraButtons';
27import LocationTextField from './LocationTextField'; 28import LocationTextField from './LocationTextField';
28import NavigationButtons from './NavigationButtons'; 29import NavigationButtons from './NavigationButtons';
29 30
@@ -49,6 +50,7 @@ function LocationBar(): JSX.Element {
49 <LocationBarRoot id={LOCATION_BAR_ID} hidden={!showLocationBar}> 50 <LocationBarRoot id={LOCATION_BAR_ID} hidden={!showLocationBar}>
50 <NavigationButtons service={selectedService} /> 51 <NavigationButtons service={selectedService} />
51 <LocationTextField service={selectedService} /> 52 <LocationTextField service={selectedService} />
53 <ExtraButtons service={selectedService} />
52 </LocationBarRoot> 54 </LocationBarRoot>
53 ); 55 );
54} 56}
diff --git a/packages/renderer/src/components/locationBar/NavigationButtons.tsx b/packages/renderer/src/components/locationBar/NavigationButtons.tsx
index 5c5c959..9995a21 100644
--- a/packages/renderer/src/components/locationBar/NavigationButtons.tsx
+++ b/packages/renderer/src/components/locationBar/NavigationButtons.tsx
@@ -29,7 +29,7 @@ import IconButton from '@mui/material/IconButton';
29import { observer } from 'mobx-react-lite'; 29import { observer } from 'mobx-react-lite';
30import React from 'react'; 30import React from 'react';
31 31
32import Service from '../../stores/Service'; 32import type Service from '../../stores/Service';
33 33
34function NavigationButtons({ 34function NavigationButtons({
35 service, 35 service,
diff --git a/packages/renderer/src/stores/GlobalSettings.ts b/packages/renderer/src/stores/GlobalSettings.ts
index 79815ba..52971d4 100644
--- a/packages/renderer/src/stores/GlobalSettings.ts
+++ b/packages/renderer/src/stores/GlobalSettings.ts
@@ -21,8 +21,7 @@
21import { defineGlobalSettingsModel, ThemeSource } from '@sophie/shared'; 21import { defineGlobalSettingsModel, ThemeSource } from '@sophie/shared';
22import { Instance } from 'mobx-state-tree'; 22import { Instance } from 'mobx-state-tree';
23 23
24import getEnv from '../env/getEnv'; 24import { getEnv } from './RendererEnv';
25
26import Service from './Service'; 25import Service from './Service';
27 26
28const GlobalSettings = defineGlobalSettingsModel(Service).actions((self) => ({ 27const GlobalSettings = defineGlobalSettingsModel(Service).actions((self) => ({
diff --git a/packages/renderer/src/env/getEnv.ts b/packages/renderer/src/stores/RendererEnv.ts
index 4f7357a..496ede6 100644
--- a/packages/renderer/src/env/getEnv.ts
+++ b/packages/renderer/src/stores/RendererEnv.ts
@@ -1,5 +1,5 @@
1/* 1/*
2 * Copyright (C) 2022 Kristóf Marussy <kristof@marussy.com> 2 * Copyright (C) 2021-2022 Kristóf Marussy <kristof@marussy.com>
3 * 3 *
4 * This file is part of Sophie. 4 * This file is part of Sophie.
5 * 5 *
@@ -18,10 +18,9 @@
18 * SPDX-License-Identifier: AGPL-3.0-only 18 * SPDX-License-Identifier: AGPL-3.0-only
19 */ 19 */
20 20
21import type { Action } from '@sophie/shared';
21import { getEnv as getAnyEnv, IAnyStateTreeNode } from 'mobx-state-tree'; 22import { getEnv as getAnyEnv, IAnyStateTreeNode } from 'mobx-state-tree';
22 23
23import type RendererEnv from './RendererEnv';
24
25/** 24/**
26 * Gets a well-typed environment from `model`. 25 * Gets a well-typed environment from `model`.
27 * 26 *
@@ -29,6 +28,10 @@ import type RendererEnv from './RendererEnv';
29 * 28 *
30 * @param model The state tree node. 29 * @param model The state tree node.
31 */ 30 */
32export default function getEnv(model: IAnyStateTreeNode): RendererEnv { 31export function getEnv(model: IAnyStateTreeNode): RendererEnv {
33 return getAnyEnv<RendererEnv>(model); 32 return getAnyEnv<RendererEnv>(model);
34} 33}
34
35export default interface RendererEnv {
36 dispatchMainAction(action: Action): void;
37}
diff --git a/packages/renderer/src/stores/RendererStore.ts b/packages/renderer/src/stores/RendererStore.ts
index 8f424f6..f48d484 100644
--- a/packages/renderer/src/stores/RendererStore.ts
+++ b/packages/renderer/src/stores/RendererStore.ts
@@ -21,11 +21,10 @@
21import { BrowserViewBounds, SophieRenderer } from '@sophie/shared'; 21import { BrowserViewBounds, SophieRenderer } from '@sophie/shared';
22import { applySnapshot, applyPatch, Instance, types } from 'mobx-state-tree'; 22import { applySnapshot, applyPatch, Instance, types } from 'mobx-state-tree';
23 23
24import RendererEnv from '../env/RendererEnv';
25import getEnv from '../env/getEnv';
26import { getLogger } from '../utils/log'; 24import { getLogger } from '../utils/log';
27 25
28import GlobalSettings from './GlobalSettings'; 26import GlobalSettings from './GlobalSettings';
27import RendererEnv, { getEnv } from './RendererEnv';
29import Service from './Service'; 28import Service from './Service';
30import SharedStore from './SharedStore'; 29import SharedStore from './SharedStore';
31 30
diff --git a/packages/renderer/src/stores/Service.ts b/packages/renderer/src/stores/Service.ts
index 695cff4..c50e5bd 100644
--- a/packages/renderer/src/stores/Service.ts
+++ b/packages/renderer/src/stores/Service.ts
@@ -21,8 +21,7 @@
21import { defineServiceModel, ServiceAction } from '@sophie/shared'; 21import { defineServiceModel, ServiceAction } from '@sophie/shared';
22import { Instance } from 'mobx-state-tree'; 22import { Instance } from 'mobx-state-tree';
23 23
24import getEnv from '../env/getEnv'; 24import { getEnv } from './RendererEnv';
25
26import ServiceSettings from './ServiceSettings'; 25import ServiceSettings from './ServiceSettings';
27 26
28const Service = defineServiceModel(ServiceSettings).actions((self) => { 27const Service = defineServiceModel(ServiceSettings).actions((self) => {
@@ -76,6 +75,11 @@ const Service = defineServiceModel(ServiceSettings).actions((self) => {
76 fingerprint: self.state.certificate.fingerprint, 75 fingerprint: self.state.certificate.fingerprint,
77 }); 76 });
78 }, 77 },
78 openCurrentURLInExternalBrowser(): void {
79 dispatch({
80 action: 'open-current-url-in-external-browser',
81 });
82 },
79 }; 83 };
80}); 84});
81 85
diff --git a/packages/shared/src/schemas/ServiceAction.ts b/packages/shared/src/schemas/ServiceAction.ts
index 8961bfe..da0b96b 100644
--- a/packages/shared/src/schemas/ServiceAction.ts
+++ b/packages/shared/src/schemas/ServiceAction.ts
@@ -46,6 +46,9 @@ export const ServiceAction = /* @__PURE__ */ (() =>
46 action: z.literal('temporarily-trust-current-certificate'), 46 action: z.literal('temporarily-trust-current-certificate'),
47 fingerprint: z.string(), 47 fingerprint: z.string(),
48 }), 48 }),
49 z.object({
50 action: z.literal('open-current-url-in-external-browser'),
51 }),
49 ]))(); 52 ]))();
50 53
51/* 54/*