aboutsummaryrefslogtreecommitdiff
path: root/extension/src/entries/options/components/SiteSettings.vue
blob: b31bb9c9b75a62ba901942d07e01f5b161518fc8 (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<template>
    <div class="flex flex-col w-full mt-4 sm:px-2">
      
        <form @submit.prevent="">
           <div class="w-full max-w-md mx-auto">
                <h3 class="text-center">
                    Extension settings
                </h3>
                <div class="my-6">
                    <fieldset :disabled="waiting">
                        <div class="">
                            <div class="w-full">
                                <div class="flex flex-row w-full">
                                    <Switch
                                        v-model="originProtection"
                                        :class="originProtection ? 'bg-black dark:bg-gray-50' : 'bg-gray-200 dark:bg-dark-600'"
                                        class="relative inline-flex items-center h-5 rounded-full w-11"
                                    >
                                        <span class="sr-only">Origin protection</span>
                                        <span
                                            :class="originProtection ? 'translate-x-6' : 'translate-x-1'"
                                            class="inline-block w-4 h-4 transition transform bg-white rounded-full dark:bg-dark-900"
                                        />
                                    </Switch>
                                    <div class="my-auto ml-2 text-sm dark:text-gray-200">
                                       Tracking protection
                                    </div>
                                </div>
                            </div>
                        </div>
                        
                        <div class="mt-3">
                            <div class="flex flex-row w-fit">
                                <Switch
                                    v-model="v$.heartbeat.$model"
                                    :class="v$.heartbeat.$model ? 'bg-black dark:bg-white' : 'bg-gray-200 dark:bg-dark-600'"
                                    class="relative inline-flex items-center h-5 mx-auto rounded-full w-11"
                                >
                                    <span class="sr-only">Stay logged in</span>
                                    <span
                                        :class="v$.heartbeat.$model ? 'translate-x-6' : 'translate-x-1'"
                                        class="inline-block w-4 h-4 transition transform rounded-full bg-gray-50 dark:bg-dark-900"
                                    />
                                </Switch>
                                <div class="my-auto ml-2 text-sm dark:text-gray-200">
                                   Stay logged-in
                                </div>
                            </div>
                        </div>
                    </fieldset>
                </div>
                <h3 class="text-center">
                    Server settings
                </h3>
                <p class="text-xs dark:text-gray-400">
                    You must be careful when editing these settings as you may loose connection to your vault
                    server if you input the wrong values.
                </p>
                <div class="flex justify-end mt-2">
                    <div class="button-group">
                        <button class="rounded btn sm" @click="toggleEdit()">
                            <fa-icon v-if="editMode" icon="lock-open"/>
                            <fa-icon v-else icon="lock"/>
                        </button>
                        <a :href="data.apiUrl" target="_blank">
                            <button type="button" class="rounded btn sm">
                                <fa-icon icon="external-link-alt"/>
                            </button>
                        </a>
                    </div>
                </div>
                <fieldset>
                    <div class="pl-1 mt-2">
                        
                    </div>
                    <div class="mt-2">
                        <label class="pl-1">BaseUrl</label>
                        <input 
                            class="w-full input" 
                            :class="{'error': v$.apiUrl.$invalid }"
                            v-model="v$.apiUrl.$model"
                            :readonly="!editMode"
                        />
                        <p class="pl-1 mt-1 text-xs text-gray-600 dark:text-gray-400">
                            * The http path to the vault server (must start with http:// or https://)
                        </p>
                    </div>
                    <div class="mt-2">
                        <label class="pl-1">Account endpoint</label>
                        <input 
                            class="w-full input" 
                            v-model="v$.accountBasePath.$model" 
                            :class="{ 'error': v$.accountBasePath.$invalid }" 
                            :readonly="!editMode"
                        />
                        <p class="pl-1 mt-1 text-xs text-gray-600 dark:text-gray-400">
                            * This is the path to the account server endpoint (must start with /)
                        </p>
                    </div>
                    <div class="mt-2">
                        <label class="pl-1">Nostr endpoint</label>
                        <input 
                            class="w-full input" 
                            v-model="v$.nostrEndpoint.$model" 
                            :class="{ 'error': v$.nostrEndpoint.$invalid }"
                            :readonly="!editMode"
                        />
                        <p class="pl-1 mt-1 text-xs text-gray-600 dark:text-gray-400">
                            * This is the path to the Nostr plugin endpoint path (must start with /)
                        </p>
                    </div>
                </fieldset>
                <div class="flex justify-end mt-2">
                    <button :disabled="!modified || waiting" class="rounded btn sm" @click="onSave">Save</button>
                </div>
            </div>
        </form>
    </div>
</template>

<script setup lang="ts">
import { apiCall, useDataBuffer, useVuelidateWrapper, useWait } from '@vnuge/vnlib.browser';
import { computed, watch } from 'vue';
import { useToggle, watchDebounced } from '@vueuse/core';
import { maxLength, helpers, required } from '@vuelidate/validators'
import { Switch } from '@headlessui/vue'
import { useStore } from '../../store';
import { storeToRefs } from 'pinia';
import useVuelidate from '@vuelidate/core'

const { waiting } = useWait();
const store = useStore()
const { settings, isOriginProtectionOn } = storeToRefs(store)

const { apply, data, buffer, modified, update } = useDataBuffer(settings.value, async sb =>{
    const newConfig = await store.saveSiteConfig(sb.buffer)
    apply(newConfig)
    return newConfig;
})

//Watch for store settings changes and apply them
watch(settings, v => apply(v))

const originProtection = computed({
    get: () => isOriginProtectionOn.value,
    set: v => store.setOriginProtection(v)
})

const url = (val : string) => /^https?:\/\/[a-zA-Z0-9\.\:\/-]+$/.test(val);
const path = (val : string) => /^\/[a-zA-Z0-9-_]+$/.test(val);

const vRules = {
    apiUrl: {
        required:helpers.withMessage('Base url is required', required),
        maxLength: helpers.withMessage('Base url must be less than 100 characters', maxLength(100)),
        url: helpers.withMessage('You must input a valid url', url)
    },
    accountBasePath: {
        required:helpers.withMessage('Account path is required', required),
        maxLength: maxLength(50),
        alphaNum: helpers.withMessage('Account path is not a valid endpoint path that begins with /', path)
    },
    nostrEndpoint:{
        required: helpers.withMessage('Nostr path is required', required),
        maxLength: maxLength(50),
        alphaNum: helpers.withMessage('Nostr path is not a valid endpoint path that begins with /', path)
    },
    heartbeat: {},
}

//Configure validator and validate function
const v$ = useVuelidate(vRules, buffer)
const { validate } = useVuelidateWrapper(v$ as any);

const [ editMode, toggleEdit ] = useToggle(false);

const autoInject = computed(() => buffer.autoInject)
const heartbeat = computed(() => buffer.heartbeat)

const onSave = async () => {

    //Validate
    const result = await validate();
    if(!result){
        return;
    }

    //Test connection to the server
    if(await testConnection() !== true){
        return;
    }

    await update();

    //disable dit
    toggleEdit();
}

const testConnection = async () =>{
    return await apiCall(async ({axios, toaster}) =>{
        try{
            await axios.get(`${buffer.apiUrl}`);
            toaster.general.success({
                title: 'Success',
                text: 'Succcesfully connected to the vault server'
            });
            return true;
        }
        catch(e){
            toaster.form.error({
                title: 'Warning',
                text: `Failed to connect to the vault server. Status code: ${(e as any).response?.status}`
            });
        }
    })
}

//Watch for changes to autoinject value and publish changes when it does
watchDebounced(autoInject, update, { debounce: 500, immediate: false })
watchDebounced(heartbeat, update, { debounce: 500, immediate: false })

</script>

<style lang="scss">

</style>