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 {isWeb} from 'platform/detection' 14import * as Storage from 'lib/storage' 15 16export function useDebugHeaderSetting(agent: BskyAgent): [boolean, () => void] { 17 const [enabled, setEnabled] = useState<boolean>(false) 18 19 useEffect(() => { 20 async function check() { 21 if (await isEnabled()) { 22 setEnabled(true) 23 } 24 } 25 check() 26 }, []) 27 28 const toggle = useCallback(() => { 29 if (!enabled) { 30 Storage.saveString('set-header-x-appview-proxy', 'yes') 31 agent.api.xrpc.setHeader('x-appview-proxy', 'true') 32 setEnabled(true) 33 } else { 34 Storage.remove('set-header-x-appview-proxy') 35 agent.api.xrpc.unsetHeader('x-appview-proxy') 36 setEnabled(false) 37 } 38 }, [setEnabled, enabled, agent]) 39 40 return [enabled, toggle] 41} 42 43export function setDebugHeader(agent: BskyAgent, enabled: boolean) { 44 if (enabled) { 45 Storage.saveString('set-header-x-appview-proxy', 'yes') 46 agent.api.xrpc.setHeader('x-appview-proxy', 'true') 47 } else { 48 Storage.remove('set-header-x-appview-proxy') 49 agent.api.xrpc.unsetHeader('x-appview-proxy') 50 } 51} 52 53export async function applyDebugHeader(agent: BskyAgent) { 54 if (!isWeb) { 55 return 56 } 57 if (await isEnabled()) { 58 agent.api.xrpc.setHeader('x-appview-proxy', 'true') 59 } 60} 61 62async function isEnabled() { 63 return (await Storage.loadString('set-header-x-appview-proxy')) === 'yes' 64}