aboutsummaryrefslogtreecommitdiff
path: root/extension/src/entries/contentScript/primary/components/PromptPopup.vue
blob: d019b5daadf3c4cfe06eb10b2d45453c3d54cf51 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<template>
    <div v-show="isOpen" id="nvault-ext-prompt" :class="{'dark': darkMode }">
        
        <div class="fixed top-0 bottom-0 left-0 right-0 text-white" style="z-index:9147483647 !important" >
            
            <div class="fixed inset-0 left-0 w-full h-full bg-black/50" @click.self="close" />

            <div class="relative w-full max-w-[28rem] mx-auto mt-36 mb-auto" ref="prompt">
                <div class="w-full p-5 text-gray-800 bg-white border rounded-lg shadow-lg dark:bg-dark-900 dark:border-dark-500 dark:text-gray-200">
                    
                    <div v-if="loggedIn" class="">
                        <div class="flex flex-row justify-between">
                            <div class="">
                                <div class="text-lg font-bold">
                                    Allow access
                                </div>
                                <div class="text-sm">
                                    <span class="">
                                        Identity:
                                    </span>
                                    <span :class="[keyName ? '' : 'text-red-500']">
                                        {{ keyName ?? 'Select Identity' }}
                                    </span>
                                </div>
                            </div>
                            <div class="">
                                <Popover class="relative">
                                    <PopoverButton class="">
                                        <fa-icon icon="circle-info" class="w-4 h-4" />
                                    </PopoverButton>
                                    <PopoverPanel class="absolute right-0 z-10">
                                        <div class="min-w-[22rem] p-2 border rounded dark:bg-dark-800 bg-gray-50 dark:border-dark-500 shadow-md text-sm">
                                            <p class="pl-1">
                                                Event Data:
                                            </p>
                                            <div class="p-2 mt-1 text-left border rounded dark:border-dark-500 border-gray-300 overflow-auto max-h-[22rem] max-w-lg">
                                                <pre>{{ evData }}</pre>
                                            </div>
                                        </div>
                                    </PopoverPanel>
                                </Popover>
                            </div>
                        </div>
                        
                        <div class="py-3 text-sm text-center">
                            <span class="font-bold">{{ site }}</span>
                                would like to access to 
                            <span class="font-bold">{{ event?.msg }}</span>
                        </div>

                        <div class="flex gap-2 mt-4"> 
                            <div class="ml-auto">
                                <button class="rounded btn sm" @click="close">Close</button>
                            </div>
                            <div>
                                <button :disabled="selectedKey?.Id == undefined" class="rounded btn sm" @click="allow">Allow</button>
                            </div>
                        </div>
                    </div>
                    
                    <div v-else class="">
                        
                        <div class="">
                            <div class="text-lg font-bold">
                               Log in
                            </div>
                        </div>

                        <div class="py-3 text-sm text-center">
                             You must log in before you can allow access. 
                        </div>

                        <div class="flex justify-end gap-2 mt-4">
                            <div>
                                <button class="rounded btn xs" @click="close">Close</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

    </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { usePrompt, type UserPermissionRequest } from '../../util'
import { computed } from 'vue';
import { Popover, PopoverButton, PopoverPanel } from '@headlessui/vue'
import { clone, first } from 'lodash';
import { useStore } from '../../../store';
import { storeToRefs } from 'pinia';

const store = useStore()
const { loggedIn, selectedKey, darkMode } = storeToRefs(store)
const keyName = computed(() => selectedKey.value?.UserName)

const prompt = ref(null)

interface PopupEvent extends UserPermissionRequest {
    allow: () => void
    close: () => void
}

const evStack = ref<PopupEvent[]>([])
const isOpen = computed(() => evStack.value.length > 0)
const event = computed<PopupEvent | undefined>(() => first(evStack.value));

const site = computed(() => new URL(event.value?.origin || "https://example.com").host)
const evData = computed(() => JSON.stringify(event.value || {}, null, 2))


const close = () => {
    //Pop the first event off
    const res = evStack.value.shift()
    res?.close()
}
const allow = () => {
    //Pop the first event off
    const res = evStack.value.shift()
    res?.allow()
}

//Listen for events
usePrompt((ev: UserPermissionRequest):Promise<boolean> => {

    ev = clone(ev)

    console.log('[usePrompt] =>', ev)

    switch(ev.type){
        case 'getPublicKey':
            ev.msg = "your public key"
            break;
        case 'signEvent':
            ev.msg = "sign an event"
            break;
        case 'getRelays':
            ev.msg = "get your preferred relays"
            break;
        case 'nip04.encrypt':
            ev.msg = "encrypt data"
            break;
        case 'nip04.decrypt':
            ev.msg = "decrypt data"
            break;
        default:
            ev.msg = "unknown event"
            break;
    }

    return new Promise((resolve) => {
        evStack.value.push({
            ...ev,
            allow: () => resolve(true),
            close: () => resolve(false),
        })
    })
})

</script>