about summary refs log tree commit diff
path: root/src/state
diff options
context:
space:
mode:
Diffstat (limited to 'src/state')
-rw-r--r--src/state/queries/resolve-short-link.ts24
-rw-r--r--src/state/shell/logged-out.tsx20
2 files changed, 44 insertions, 0 deletions
diff --git a/src/state/queries/resolve-short-link.ts b/src/state/queries/resolve-short-link.ts
new file mode 100644
index 000000000..a10bc12c1
--- /dev/null
+++ b/src/state/queries/resolve-short-link.ts
@@ -0,0 +1,24 @@
+import {useQuery} from '@tanstack/react-query'
+
+import {resolveShortLink} from 'lib/link-meta/resolve-short-link'
+import {parseStarterPackUri} from 'lib/strings/starter-pack'
+import {STALE} from 'state/queries/index'
+
+const ROOT_URI = 'https://go.bsky.app/'
+
+const RQKEY_ROOT = 'resolved-short-link'
+export const RQKEY = (code: string) => [RQKEY_ROOT, code]
+
+export function useResolvedStarterPackShortLink({code}: {code: string}) {
+  return useQuery({
+    queryKey: RQKEY(code),
+    queryFn: async () => {
+      const uri = `${ROOT_URI}${code}`
+      const res = await resolveShortLink(uri)
+      return parseStarterPackUri(res)
+    },
+    retry: 1,
+    enabled: Boolean(code),
+    staleTime: STALE.HOURS.ONE,
+  })
+}
diff --git a/src/state/shell/logged-out.tsx b/src/state/shell/logged-out.tsx
index dc78d03d5..2c577fdd2 100644
--- a/src/state/shell/logged-out.tsx
+++ b/src/state/shell/logged-out.tsx
@@ -50,6 +50,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
   const activeStarterPack = useActiveStarterPack()
   const {hasSession} = useSession()
   const shouldShowStarterPack = Boolean(activeStarterPack?.uri) && !hasSession
+
   const [state, setState] = React.useState<State>({
     showLoggedOut: shouldShowStarterPack,
     requestedAccountSwitchTo: shouldShowStarterPack
@@ -59,6 +60,25 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
       : undefined,
   })
 
+  const [prevActiveStarterPack, setPrevActiveStarterPack] =
+    React.useState(activeStarterPack)
+  if (activeStarterPack?.uri !== prevActiveStarterPack?.uri) {
+    setPrevActiveStarterPack(activeStarterPack)
+    if (activeStarterPack) {
+      setState(s => ({
+        ...s,
+        showLoggedOut: true,
+        requestedAccountSwitchTo: 'starterpack',
+      }))
+    } else {
+      setState(s => ({
+        ...s,
+        showLoggedOut: false,
+        requestedAccountSwitchTo: undefined,
+      }))
+    }
+  }
+
   const controls = React.useMemo<Controls>(
     () => ({
       setShowLoggedOut(show) {