diff options
Diffstat (limited to 'packages/renderer/src/components/sidebar/ServiceSwitcher.tsx')
-rw-r--r-- | packages/renderer/src/components/sidebar/ServiceSwitcher.tsx | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/packages/renderer/src/components/sidebar/ServiceSwitcher.tsx b/packages/renderer/src/components/sidebar/ServiceSwitcher.tsx new file mode 100644 index 0000000..4261205 --- /dev/null +++ b/packages/renderer/src/components/sidebar/ServiceSwitcher.tsx | |||
@@ -0,0 +1,123 @@ | |||
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 | |||
21 | import Tab from '@mui/material/Tab'; | ||
22 | import Tabs from '@mui/material/Tabs'; | ||
23 | import { alpha, styled } from '@mui/material/styles'; | ||
24 | import type { TFunction } from 'i18next'; | ||
25 | import { observer } from 'mobx-react-lite'; | ||
26 | import React from 'react'; | ||
27 | import { useTranslation } from 'react-i18next'; | ||
28 | |||
29 | import type RendererStore from '../../stores/RendererStore.js'; | ||
30 | import type Service from '../../stores/Service.js'; | ||
31 | import { getServicePanelID } from '../ServicePanel.js'; | ||
32 | |||
33 | import ServiceIcon from './ServiceIcon.js'; | ||
34 | |||
35 | const ServiceSwitcherRoot = styled(Tabs, { | ||
36 | name: 'ServiceSwitcher', | ||
37 | slot: 'Root', | ||
38 | })({ | ||
39 | // Move the indicator to the outer side of the window. | ||
40 | '.MuiTabs-indicator': { | ||
41 | left: 0, | ||
42 | right: 'auto', | ||
43 | width: 4, | ||
44 | }, | ||
45 | }); | ||
46 | |||
47 | const ServiceSwitcherTab = styled(Tab, { | ||
48 | name: 'ServiceSwitcher', | ||
49 | slot: 'Tab', | ||
50 | })(({ theme }) => ({ | ||
51 | minWidth: 0, | ||
52 | paddingInline: theme.spacing(2), | ||
53 | transition: theme.transitions.create('background-color', { | ||
54 | duration: theme.transitions.duration.shortest, | ||
55 | }), | ||
56 | '&.Mui-selected': { | ||
57 | backgroundColor: | ||
58 | theme.palette.mode === 'dark' | ||
59 | ? alpha(theme.palette.text.primary, 0.1) | ||
60 | : alpha(theme.palette.primary.light, 0.24), | ||
61 | }, | ||
62 | })); | ||
63 | |||
64 | function getServiceTitle(service: Service, t: TFunction) { | ||
65 | const { | ||
66 | settings: { name }, | ||
67 | directMessageCount, | ||
68 | indirectMessageCount, | ||
69 | } = service; | ||
70 | let messagesText: string | undefined; | ||
71 | if (indirectMessageCount > 0) { | ||
72 | messagesText = | ||
73 | directMessageCount > 0 | ||
74 | ? t('service.title.directAndIndirectMessageCount', { | ||
75 | directMessageCount, | ||
76 | indirectMessageCount, | ||
77 | }) | ||
78 | : t('service.title.indirectMessageCount', { | ||
79 | count: indirectMessageCount, | ||
80 | }); | ||
81 | } else if (directMessageCount > 0) { | ||
82 | messagesText = t('service.title.directMessageCount', { | ||
83 | count: directMessageCount, | ||
84 | }); | ||
85 | } | ||
86 | if (messagesText === undefined) { | ||
87 | return t('service.title.nameWithNoMessages', { name }); | ||
88 | } | ||
89 | return t('service.title.nameWithMessages', { name, messages: messagesText }); | ||
90 | } | ||
91 | |||
92 | function ServiceSwitcher({ | ||
93 | store: { settings, services }, | ||
94 | }: { | ||
95 | store: RendererStore; | ||
96 | }): JSX.Element { | ||
97 | const { t } = useTranslation(); | ||
98 | |||
99 | const { selectedService } = settings; | ||
100 | |||
101 | return ( | ||
102 | <ServiceSwitcherRoot | ||
103 | variant="scrollable" | ||
104 | orientation="vertical" | ||
105 | value={selectedService === undefined ? false : selectedService.id} | ||
106 | onChange={(_event, newValue: string) => | ||
107 | settings.setSelectedServiceId(newValue) | ||
108 | } | ||
109 | > | ||
110 | {services.map((service) => ( | ||
111 | <ServiceSwitcherTab | ||
112 | key={service.id} | ||
113 | value={service.id} | ||
114 | icon={<ServiceIcon service={service} />} | ||
115 | aria-label={getServiceTitle(service, t)} | ||
116 | aria-controls={getServicePanelID(service)} | ||
117 | /> | ||
118 | ))} | ||
119 | </ServiceSwitcherRoot> | ||
120 | ); | ||
121 | } | ||
122 | |||
123 | export default observer(ServiceSwitcher); | ||