aboutsummaryrefslogtreecommitdiff
path: root/extension/src/entries/contentScript/primary/components/PromptPopup.vue
blob: 195c6db66f9c63701857b339015f3be4c93b00ac (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
<template>
    <div v-show="isOpen" id="nvault-ext-prompt">
        <div class="relative text-white" style="z-index:9147483647 !important" ref="prompt">
            <div class="fixed inset-0 left-0 flex justify-center w-full h-full p-4 bg-black/50">
                <div class="relative w-full max-w-md mx-auto mt-20 mb-auto">
                    <div class="w-full p-4 border rounded-lg shadow-lg bg-dark-700 border-dark-400">
                        <div v-if="loggedIn" class="">
                            <h3 class="">Allow access</h3>
                            <div class="pl-1 text-sm">
                                Identity:
                            </div>
                            <div class="p-2 mt-1 text-center border rounded border-dark-400 bg-dark-600">
                                <div :class="[keyName ? '' : 'text-red-500']">
                                    {{ keyName ?? 'Select Identity' }}
                                </div>
                            </div>
                            <div class="mt-5 text-center">
                                <span class="text-primary-500">{{ site }}</span>
                                 would like to access to 
                                <span class="text-yellow-500">{{ event?.msg }}</span>
                            </div>
                            <div class="flex gap-2 mt-4">
                                <div class="">
                                    <Popover class="relative">
                                        <PopoverButton class="rounded btn sm">View Raw</PopoverButton>
                                        <PopoverPanel class="absolute z-10">
                                            <div class="min-w-[22rem] p-2 border rounded bg-dark-700 border-dark-400 shadow-md text-sm">
                                                <p class="pl-1">
                                                    Event Data:
                                                </p>
                                                 <div class="p-2 mt-1 text-left border rounded border-dark-400 bg-dark-600 overflow-y-auto max-h-[22rem]">
<pre>
{{ evData }}
</pre>
                                                 </div>
                                            </div>
                                        </PopoverPanel>
                                    </Popover>
                                </div>
                                <div class="ml-auto">
                                    <button :disabled="selectedKey?.Id == undefined" class="rounded btn primary sm" @click="allow">Allow</button>
                                </div>
                                <div>
                                    <button class="rounded btn sm red" @click="close">Close</button>
                                </div>
                            </div>
                        </div>
                        <div v-else class="">
                            <h3 class="">Log in!</h3>
                            <div class="">
                               You must log in before you can allow access.
                            </div>
                            <div class="flex justify-end gap-2 mt-4">
                                <div>
                                    <button class="rounded btn sm red" @click="close">Close</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { usePrompt } from '../../nostr-shim.js'
import { computed } from '@vue/reactivity';
import {  } from '@vueuse/core';
import { Popover, PopoverButton, PopoverPanel } from '@headlessui/vue'
import { first } from 'lodash';
import { useStore } from '../../../store';
import { storeToRefs } from 'pinia';

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

const prompt = ref(null)

interface PopupEvent{
    type: string
    msg: string
    origin: string
    data: any
    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()
}

//Setup click outside
//onClickOutside(prompt, () => isOpen.value ? close() : null)

//Listen for events
usePrompt(async (ev: PopupEvent) => {

    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;
    }

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


</script>

<style lang="scss">


</style>