about summary refs log tree commit diff stats
path: root/src/pages/messages_src/App.svelte
blob: 825265306742eede9b52f63da4e25f435a7ef6dd (plain) (blame)
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
<script>
  const browser = window.browser || window.chrome

  import utils from "../../assets/javascripts/utils.js"
  import { onDestroy } from "svelte"
  import servicesHelper from "../../assets/javascripts/services.js"
  import { onMount } from "svelte"

  import { options, config, page } from "./stores"
  import Button from "../components/Button.svelte"
  import AutoPickIcon from "../icons/AutoPickIcon.svelte"

  let _options
  const unsubscribeOptions = options.subscribe(val => {
    if (val) {
      _options = val
      browser.storage.local.set({ options: val })
    }
  })

  let _config
  const unsubscribeConfig = config.subscribe(val => (_config = val))

  onDestroy(() => {
    unsubscribeOptions()
    unsubscribeConfig()
  })

  onMount(async () => {
    let opts = await utils.getOptions()
    if (!opts) {
      await servicesHelper.initDefaults()
      opts = await utils.getOptions()
    }
    options.set(opts)
    config.set(await utils.getConfig())
  })

  let _page
  page.subscribe(val => (_page = val))

  let style
  $: if (_options) style = utils.style(_options, window)

  let autoPicking = false

  const params = new URLSearchParams(window.location.search)
  const oldUrl = new URL(params.get("url"))

  async function autoPick() {
    autoPicking = true
    const frontend = params.get("frontend")
    const redirects = await utils.getList(_options)
    const clearnet = redirects[frontend]["clearnet"]
    const instance = await utils.autoPickInstance(clearnet)
    _options[frontend].push(instance)
    options.set(_options)
    autoPicking = false
  }

  async function redirectUrl() {
    const newUrl = await servicesHelper.redirectAsync(oldUrl, "main_frame", null, null, false, true)
    browser.tabs.update({ url: newUrl })
  }
</script>

{#if _options && _config}
  <div class="main" dir="auto" {style}>
    {#if params.get("message") == "disabled"}
      <div>
        <h1>You disabled redirections for this service</h1>
        <Button
          on:click={async () => {
            const { service } = await servicesHelper.computeServiceFrontend(oldUrl)
            _options[service].enabled = true
            options.set(_options)
            await redirectUrl()
          }}
        >
          {browser.i18n.getMessage("enable") || "Enable"}
        </Button>
      </div>
    {:else if params.get("message") == "no_instance"}
      <div>
        <h1>You have no instance selected for this frontend</h1>
        <Button
          on:click={async () => {
            await autoPick()
            await redirectUrl()
          }}
          disabled={autoPicking}
        >
          <AutoPickIcon class="margin margin_{document.body.dir}" />
          {browser.i18n.getMessage("autoPickInstance") || "Auto Pick Instance"}
        </Button>
      </div>
    {/if}
  </div>
{:else}
  <p>Loading...</p>
{/if}

<style>
  :global(body) {
    width: 100vw;
    height: 100vh;
    margin: 0;
    padding: 0;
  }

  div.main {
    height: 100%;
    display: grid;
    grid-template-columns: 800px;
    margin: 0;
    padding-top: 50px;
    justify-content: center;
    font-family: "Inter", sans-serif;
    box-sizing: border-box;
    font-size: 16px;
    background-color: var(--bg-main);
    color: var(--text);
    overflow: scroll;
  }

  :global(.margin) {
    margin-right: 10px;
    margin-left: 0;
  }
  :global(.margin_rtl) {
    margin-right: 0;
    margin-left: 10px;
  }
</style>