aboutsummaryrefslogtreecommitdiff
path: root/extension/src/entries/store/allowedOrigins.ts
blob: 7fc5e15015ce87a3b93a63d5f74bb3be7731fe8c (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

import 'pinia'
import {  } from 'lodash'
import { PiniaPluginContext } from 'pinia'
import { computed, ref } from 'vue';
import { onWatchableChange } from '../../features/types';
import { type AllowedOriginStatus } from '../../features/nip07allow-api';

declare module 'pinia' {
    export interface PiniaCustomProperties {
        isTabAllowed: boolean;
        currentOrigin: string | undefined;
        allowedOrigins: Array<string>;
        isOriginProtectionOn: boolean;
        allowOrigin(origin?:string): Promise<void>;
        dissallowOrigin(origin?:string): Promise<void>;
        disableOriginProtection(): Promise<void>;
        setOriginProtection(value: boolean): Promise<void>;
    }
}

export const originPlugin = ({ store }: PiniaPluginContext) => {
   
    const { plugins } = store
    const status = ref<AllowedOriginStatus>()

    onWatchableChange(plugins.allowedOrigins, async () => {
        //Update the status
        status.value = await plugins.allowedOrigins.getStatus()
    }, { immediate: true })

    return {
        allowedOrigins: computed(() => status.value?.allowedOrigins || []),
        isTabAllowed: computed(() => status.value?.isAllowed == true),
        currentOrigin: computed(() => status.value?.currentOrigin),
        isOriginProtectionOn: computed(() => status.value?.enabled == true),
        //Push to the allow list, will trigger a change if needed
        allowOrigin: plugins.allowedOrigins.addOrigin,
        //Remove from allow list, will trigger a change if needed
        dissallowOrigin: plugins.allowedOrigins.removeOrigin,
        setOriginProtection: plugins.allowedOrigins.enable
    }
}