aboutsummaryrefslogtreecommitdiffstats
path: root/packages/main/src/stores/Service.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/main/src/stores/Service.ts')
-rw-r--r--packages/main/src/stores/Service.ts109
1 files changed, 79 insertions, 30 deletions
diff --git a/packages/main/src/stores/Service.ts b/packages/main/src/stores/Service.ts
index 9bc6a43..78c57cb 100644
--- a/packages/main/src/stores/Service.ts
+++ b/packages/main/src/stores/Service.ts
@@ -18,46 +18,95 @@
18 * SPDX-License-Identifier: AGPL-3.0-only 18 * SPDX-License-Identifier: AGPL-3.0-only
19 */ 19 */
20 20
21import type { ServiceSnapshotIn } from '@sophie/shared'; 21import type { UnreadCount } from '@sophie/service-shared';
22import {
23 service as originalService,
24 ServiceSettingsSnapshotIn,
25} from '@sophie/shared';
26import { Instance, getSnapshot, ReferenceIdentifier } from 'mobx-state-tree';
22 27
28import SettingsWithId from '../utils/SettingsWithId';
23import generateId from '../utils/generateId'; 29import generateId from '../utils/generateId';
30import overrideProps from '../utils/overrideProps';
24 31
25import type { ProfileSnapshotIn } from './Profile'; 32import { ProfileSettingsSnapshotWithId } from './Profile';
33import { serviceSettings } from './ServiceSettings';
26 34
27export interface PartialServiceSnapshotIn 35export interface ServiceConfig
28 extends Omit<ServiceSnapshotIn, 'id' | 'profile'> { 36 extends Omit<ServiceSettingsSnapshotIn, 'profile'> {
29 id?: string | undefined; 37 id?: string | undefined;
30 profile?: string | undefined; 38
39 profile?: ReferenceIdentifier | undefined;
31} 40}
32 41
42export const service = overrideProps(originalService, {
43 settings: serviceSettings,
44})
45 .views((self) => ({
46 get config(): ServiceConfig {
47 const { id, settings } = self;
48 return { ...getSnapshot(settings), id };
49 },
50 }))
51 .actions((self) => ({
52 setLocation({
53 url,
54 canGoBack,
55 canGoForward,
56 }: {
57 url: string;
58 canGoBack: boolean;
59 canGoForward: boolean;
60 }): void {
61 self.currentUrl = url;
62 self.canGoBack = canGoBack;
63 self.canGoForward = canGoForward;
64 },
65 setTitle(title: string): void {
66 self.title = title;
67 },
68 startedLoading(): void {
69 self.state = 'loading';
70 },
71 finishedLoading(): void {
72 if (self.state === 'loading') {
73 // Do not overwrite crashed state if the service haven't been reloaded yet.
74 self.state = 'loaded';
75 }
76 },
77 crashed(): void {
78 self.state = 'crashed';
79 },
80 setUnreadCount({ direct, indirect }: UnreadCount): void {
81 if (direct !== undefined) {
82 self.directMessageCount = direct;
83 }
84 if (indirect !== undefined) {
85 self.indirectMessageCount = indirect;
86 }
87 },
88 }));
89
90export interface Service extends Instance<typeof service> {}
91
92export type ServiceSettingsSnapshotWithId =
93 SettingsWithId<ServiceSettingsSnapshotIn>;
94
33export function addMissingServiceIdsAndProfiles( 95export function addMissingServiceIdsAndProfiles(
34 partialServices: PartialServiceSnapshotIn[] | undefined, 96 serviceConfigs: ServiceConfig[] | undefined,
35 profiles: ProfileSnapshotIn[], 97 profiles: ProfileSettingsSnapshotWithId[],
36): ServiceSnapshotIn[] { 98): ServiceSettingsSnapshotWithId[] {
37 return (partialServices ?? []).map((service) => { 99 return (serviceConfigs ?? []).map((serviceConfig) => {
38 const { name } = service; 100 const { id, ...settings } = serviceConfig;
39 let { id, profile } = service; 101 const { name } = settings;
40 if (typeof id === 'undefined') { 102 let { profile: profileId } = settings;
41 id = generateId(name); 103 if (profileId === undefined) {
42 } 104 profileId = generateId(name);
43 if (typeof profile === 'undefined') { 105 profiles.push({ id: profileId, settings: { name } });
44 profile = generateId(name);
45 profiles.push({
46 id: profile,
47 name: service.name,
48 });
49 } 106 }
50 return { 107 return {
51 ...service, 108 id: id === undefined ? generateId(name) : id,
52 id, 109 settings: { ...settings, profile: profileId },
53 profile,
54 }; 110 };
55 }); 111 });
56} 112}
57
58export type {
59 Service,
60 ServiceSnapshotOut,
61 ServiceSnapshotIn,
62} from '@sophie/shared';
63export { service } from '@sophie/shared';