about summary refs log tree commit diff
path: root/src/lib/api/debug-appview-proxy-header.ts
blob: 44363cde245786c52f47ca053c9aabddb25c4057 (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
/**
 * APP-700
 *
 * This is a temporary debug setting we're running on the Web build to
 * help the protocol team test some changes.
 *
 * It should be removed in ~2 weeks. It should only be used on the Web
 * version of the app.
 */

import {useState, useCallback, useEffect} from 'react'
import {BskyAgent} from '@atproto/api'
import * as Storage from 'lib/storage'

export function useDebugHeaderSetting(agent: BskyAgent): [boolean, () => void] {
  const [enabled, setEnabled] = useState<boolean>(false)

  useEffect(() => {
    async function check() {
      if (await isEnabled()) {
        setEnabled(true)
      }
    }
    check()
  }, [])

  const toggle = useCallback(() => {
    if (!enabled) {
      Storage.saveString('set-header-x-appview-proxy', 'yes')
      agent.api.xrpc.setHeader('x-appview-proxy', 'true')
      setEnabled(true)
    } else {
      Storage.remove('set-header-x-appview-proxy')
      agent.api.xrpc.unsetHeader('x-appview-proxy')
      setEnabled(false)
    }
  }, [setEnabled, enabled, agent])

  return [enabled, toggle]
}

export function setDebugHeader(agent: BskyAgent, enabled: boolean) {
  if (enabled) {
    Storage.saveString('set-header-x-appview-proxy', 'yes')
    agent.api.xrpc.setHeader('x-appview-proxy', 'true')
  } else {
    Storage.remove('set-header-x-appview-proxy')
    agent.api.xrpc.unsetHeader('x-appview-proxy')
  }
}

export async function applyDebugHeader(agent: BskyAgent) {
  if (await isEnabled()) {
    agent.api.xrpc.setHeader('x-appview-proxy', 'true')
  }
}

async function isEnabled() {
  return (await Storage.loadString('set-header-x-appview-proxy')) === 'yes'
}