aboutsummaryrefslogtreecommitdiff
path: root/src/lib/cards/Settings.svelte
blob: 2230c7f0576c1ec45031e6d4f5fc0906a46fab9f (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
<script lang="ts">
  import { browser } from '$app/environment';
  import { standardRelays } from '$lib/consts';
  import { ndk } from '$lib/ndk';
  import { tabBehaviour, userPublickey } from '$lib/state';
  import { NDKNip07Signer } from '@nostr-dev-kit/ndk';
  import { onMount } from 'svelte';

  let username = '...';
  let relays: string[] = [];
  let newTabBehaviour = $tabBehaviour;
  let newRelay = '';

  function removeRelay(index: number) {
    relays.splice(index, 1);
    relays = [...relays];
  }

  async function login() {
    if (browser) {
      if (!$ndk.signer) {
        const signer = new NDKNip07Signer();
        $ndk.signer = signer;
        ndk.set($ndk);
      }
      if ($ndk.signer && $userPublickey == '') {
        const newUserPublicKey = (await $ndk.signer.user()).hexpubkey();
        localStorage.setItem('wikinostr_loggedInPublicKey', newUserPublicKey);
        $userPublickey = newUserPublicKey;
        userPublickey.set($userPublickey);
      }
    }
  }

  function logout() {
    localStorage.removeItem('wikinostr_loggedInPublicKey');
    userPublickey.set('');
  }

  function addRelay() {
    if (newRelay) {
      relays.push(newRelay);
      newRelay = '';
      relays = [...relays];
    }
  }

  function saveData() {
    addRelay();
    localStorage.setItem('wikinostr_tabBehaviour', newTabBehaviour);
    localStorage.setItem('wikinostr_relays', JSON.stringify(relays));
    setTimeout(() => {
      window.location.href = '';
    }, 1);
  }

  if (browser) {
    relays = JSON.parse(localStorage.getItem('wikinostr_relays') || JSON.stringify(standardRelays));
  }

  onMount(async () => {
    // get user
    const user = await $ndk.getUser({ hexpubkey: $userPublickey });
    const profile = await user.fetchProfile();
    if (profile) {
      username = JSON.parse(Array.from(profile)[0].content).name;
    }
  });
</script>

<article class="font-sans mx-auto p-2 lg:max-w-4xl">
  <div class="prose">
    <h1 class="mt-0">Settings</h1>
  </div>

  <!-- Login Options -->
  <div class="my-6">
    <p class="text-sm">Account</p>
    {#if $userPublickey == ''}
      <p>You are not logged in!</p>
      <button
        on:click={login}
        type="button"
        class="inline-flex items-center px-2.5 py-1.5 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
        >Login with NIP07
      </button>
    {:else}
      <p>You are logged in as <a href={`nostr://${$userPublickey}`}>{username}</a></p>
      <button
        on:click={logout}
        type="button"
        class="inline-flex items-center px-2.5 py-1.5 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-red-600 hover:bg-red-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-red-500"
        >Logout
      </button>
    {/if}
  </div>

  <!-- Relay Selection -->
  <div class="mb-6">
    <p class="text-sm">Relays</p>
    {#each relays as relay, index}
      <div class="border rounded-full pl-2 my-1">
        <button
          class="text-red-500 py-0.5 px-1.5 rounded-full text-xl font-bold"
          on:click={() => removeRelay(index)}
        >
          -
        </button>
        {relay}
      </div>
    {/each}
    <div class="flex">
      <input
        bind:value={newRelay}
        type="text"
        class="inline mr-0 rounded-md rounded-r-none shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm border-gray-300"
        placeholder="wss://relay.example.com"
      />
      <button
        on:click={addRelay}
        type="button"
        class="inline-flex ml-0 rounded-md rounded-l-none items-center px-2.5 py-1.5 border border-transparent text-sm font-medium shadow-sm text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
        >Add</button
      >
    </div>
  </div>

  <!-- More options -->
  <div class="mb-6">
    <p class="text-sm">Tab Behaviour</p>
    <select
      class="mt-1 block w-full pl-3 pr-10 py-2 text-base border-gray-300 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm rounded-md"
      bind:value={newTabBehaviour}
    >
      <option value="replace">Replace Self Everywhere</option>
      <option value="normal">Normal</option>
      <option value="child">Create Child Everywhere</option>
    </select>
  </div>

  <!-- Save button -->
  <button
    on:click={saveData}
    type="button"
    class="inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
  >
    Save & Reload
  </button>
</article>