mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1/**
2 * APP-700
3 *
4 * This is a temporary debug setting we're running on the Web build to
5 * help the protocol team test some changes.
6 *
7 * It should be removed in ~2 weeks. It should only be used on the Web
8 * version of the app.
9 */
10
11import {useState, useCallback, useEffect} from 'react'
12import {BskyAgent} from '@atproto/api'
13import * as Storage from 'lib/storage'
14
15export function useDebugHeaderSetting(agent: BskyAgent): [boolean, () => void] {
16 const [enabled, setEnabled] = useState<boolean>(false)
17
18 useEffect(() => {
19 async function check() {
20 if (await isEnabled()) {
21 setEnabled(true)
22 }
23 }
24 check()
25 }, [])
26
27 const toggle = useCallback(() => {
28 if (!enabled) {
29 Storage.saveString('set-header-x-appview-proxy', 'yes')
30 agent.api.xrpc.setHeader('x-appview-proxy', 'true')
31 setEnabled(true)
32 } else {
33 Storage.remove('set-header-x-appview-proxy')
34 agent.api.xrpc.unsetHeader('x-appview-proxy')
35 setEnabled(false)
36 }
37 }, [setEnabled, enabled, agent])
38
39 return [enabled, toggle]
40}
41
42export function setDebugHeader(agent: BskyAgent, enabled: boolean) {
43 if (enabled) {
44 Storage.saveString('set-header-x-appview-proxy', 'yes')
45 agent.api.xrpc.setHeader('x-appview-proxy', 'true')
46 } else {
47 Storage.remove('set-header-x-appview-proxy')
48 agent.api.xrpc.unsetHeader('x-appview-proxy')
49 }
50}
51
52export async function applyDebugHeader(agent: BskyAgent) {
53 if (await isEnabled()) {
54 agent.api.xrpc.setHeader('x-appview-proxy', 'true')
55 }
56}
57
58async function isEnabled() {
59 return (await Storage.loadString('set-header-x-appview-proxy')) === 'yes'
60}