aboutsummaryrefslogtreecommitdiff
path: root/extension/src/entries/store/features.ts
blob: ad83e160ba1ae517abe9dbb98d7b40659d6e4cce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright (C) 2024 Vaughn Nugent
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

import 'pinia'
import { } from 'lodash'
import { PiniaPluginContext } from 'pinia'
import type { IMfaFlowContinuiation } from '@vnuge/vnlib.browser'

import {
    useAuthApi,
    useHistoryApi,
    useIdentityApi,
    useNostrApi,
    useLocalPki,
    usePkiApi,
    useSettingsApi,
    useForegoundFeatures,
    useEventTagFilterApi,
    useInjectAllowList,
    onWatchableChange,
    useMfaConfigApi,
    usePermissionApi
} from "../../features"

import { ChannelContext } from '../../messaging'

export type BgPlugins = ReturnType<typeof usePlugins>
export type BgPluginState<T> = { plugins: BgPlugins } & T

declare module 'pinia' {
    export interface PiniaCustomProperties {
        plugins: BgPlugins
        mfaStatus: Partial<IMfaFlowContinuiation> | null
    }
}

const usePlugins = (context: ChannelContext) => {
    //Create plugin wrapping function
    const { use } = useForegoundFeatures(context)

    return {
        settings: use(useSettingsApi),
        user: use(useAuthApi),
        identity: use(useIdentityApi),
        nostr: use(useNostrApi),
        history: use(useHistoryApi),
        localPki: use(useLocalPki),
        pki: use(usePkiApi),
        tagFilter: use(useEventTagFilterApi),
        allowedOrigins: use(useInjectAllowList),
        mfaConfig: use(useMfaConfigApi),
        permission: use(usePermissionApi)
    }
}

export const useBackgroundPiniaPlugin = (context: ChannelContext) => {
    //Create port for context
    const plugins = usePlugins(context)
    const { user } = plugins;

    //Plugin store
    return ({ store }: PiniaPluginContext) => {

        //watch for status changes
        onWatchableChange(user, async () => {
            //Get status update and set the values
            const { loggedIn, userName, mfaStatus } = await user.getStatus();
            store.loggedIn = loggedIn;
            store.userName = userName;
            store.mfaStatus = mfaStatus

        }, { immediate: true })

        //Wait for settings changes
        onWatchableChange(plugins.settings, async () => {
            //Update settings and dark mode on change
            store.settings = await plugins.settings.getSiteConfig();
            store.darkMode = await plugins.settings.getDarkMode();
        }, { immediate: true })

        return{
            plugins,
        }
    }
}