aboutsummaryrefslogtreecommitdiffstats
path: root/packages/renderer/src/components/WindowTitle.tsx
diff options
context:
space:
mode:
Diffstat (limited to 'packages/renderer/src/components/WindowTitle.tsx')
-rw-r--r--packages/renderer/src/components/WindowTitle.tsx63
1 files changed, 63 insertions, 0 deletions
diff --git a/packages/renderer/src/components/WindowTitle.tsx b/packages/renderer/src/components/WindowTitle.tsx
new file mode 100644
index 0000000..fca8b4f
--- /dev/null
+++ b/packages/renderer/src/components/WindowTitle.tsx
@@ -0,0 +1,63 @@
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 { observer } from 'mobx-react-lite';
22import { useEffect } from 'react';
23import { useTranslation } from 'react-i18next';
24
25import type Service from '../stores/Service.js';
26
27function WindowTitle({
28 selectedService,
29 devMode,
30}: {
31 selectedService: Service | undefined;
32 devMode: boolean;
33}): null {
34 const { ready, t } = useTranslation(undefined, {
35 useSuspense: false,
36 });
37
38 const {
39 settings: { name: serviceName },
40 title: serviceTitle,
41 } = selectedService ?? { settings: { name: undefined }, title: undefined };
42
43 useEffect(() => {
44 if (!ready) {
45 // Only set title once the translations have been loaded.
46 return;
47 }
48 let title: string;
49 if (serviceName === undefined) {
50 title = t('title.noServiceName');
51 } else if (serviceTitle === undefined) {
52 title = t('title.withServiceName', { serviceName });
53 } else {
54 title = t('title.withServiceNameAndTitle', { serviceName, serviceTitle });
55 }
56 document.title = devMode ? t('title.devMode', { title }) : title;
57 }, [devMode, ready, serviceName, serviceTitle, t]);
58
59 // eslint-disable-next-line unicorn/no-null -- React requires `null` to skip rendering.
60 return null;
61}
62
63export default observer(WindowTitle);