diff options
Diffstat (limited to 'packages/main/src/stores/Profile.ts')
-rw-r--r-- | packages/main/src/stores/Profile.ts | 45 |
1 files changed, 25 insertions, 20 deletions
diff --git a/packages/main/src/stores/Profile.ts b/packages/main/src/stores/Profile.ts index 4705862..eaf23c4 100644 --- a/packages/main/src/stores/Profile.ts +++ b/packages/main/src/stores/Profile.ts | |||
@@ -18,34 +18,39 @@ | |||
18 | * SPDX-License-Identifier: AGPL-3.0-only | 18 | * SPDX-License-Identifier: AGPL-3.0-only |
19 | */ | 19 | */ |
20 | 20 | ||
21 | import type { ProfileSnapshotIn } from '@sophie/shared'; | 21 | import { |
22 | profile as originalProfile, | ||
23 | ProfileSettingsSnapshotIn, | ||
24 | } from '@sophie/shared'; | ||
25 | import { getSnapshot, Instance } from 'mobx-state-tree'; | ||
22 | 26 | ||
27 | import SettingsWithId from '../utils/SettingsWithId'; | ||
23 | import generateId from '../utils/generateId'; | 28 | import generateId from '../utils/generateId'; |
24 | 29 | ||
25 | export interface PartialProfileSnapshotIn | 30 | export interface ProfileConfig extends ProfileSettingsSnapshotIn { |
26 | extends Omit<ProfileSnapshotIn, 'id'> { | ||
27 | id?: string | undefined; | 31 | id?: string | undefined; |
28 | } | 32 | } |
29 | 33 | ||
34 | export const profile = originalProfile.views((self) => ({ | ||
35 | get config(): ProfileConfig { | ||
36 | const { id, settings } = self; | ||
37 | return { ...getSnapshot(settings), id }; | ||
38 | }, | ||
39 | })); | ||
40 | |||
41 | export interface Profile extends Instance<typeof profile> {} | ||
42 | |||
43 | export type ProfileSettingsSnapshotWithId = | ||
44 | SettingsWithId<ProfileSettingsSnapshotIn>; | ||
45 | |||
30 | export function addMissingProfileIds( | 46 | export function addMissingProfileIds( |
31 | partialProfiles: PartialProfileSnapshotIn[] | undefined, | 47 | profileConfigs: ProfileConfig[] | undefined, |
32 | ): ProfileSnapshotIn[] { | 48 | ): ProfileSettingsSnapshotWithId[] { |
33 | return (partialProfiles ?? []).map((profile) => { | 49 | return (profileConfigs ?? []).map((profileConfig) => { |
34 | const { name } = profile; | 50 | const { id, ...settings } = profileConfig; |
35 | let { id } = profile; | ||
36 | if (typeof id === 'undefined') { | ||
37 | id = generateId(name); | ||
38 | } | ||
39 | return { | 51 | return { |
40 | ...profile, | 52 | id: typeof id === 'undefined' ? generateId(settings.name) : id, |
41 | id, | 53 | settings, |
42 | }; | 54 | }; |
43 | }); | 55 | }); |
44 | } | 56 | } |
45 | |||
46 | export type { | ||
47 | Profile, | ||
48 | ProfileSnapshotOut, | ||
49 | ProfileSnapshotIn, | ||
50 | } from '@sophie/shared'; | ||
51 | export { profile } from '@sophie/shared'; | ||