Skip to content

Commit

Permalink
fix: adapt remix version and try to reduce call to session api
Browse files Browse the repository at this point in the history
  • Loading branch information
pionxzh committed Sep 4, 2024
1 parent 1cd1977 commit 950c8fc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import urlcat from 'urlcat'
import { apiUrl, baseUrl } from './constants'
import { getChatIdFromUrl, getConversationFromSharePage, isSharePage } from './page'
import { getChatIdFromUrl, getConversationFromSharePage, getPageAccessToken, isSharePage } from './page'
import { blobToDataURL } from './utils/dom'
import { memorize } from './utils/memorize'

Expand Down Expand Up @@ -450,6 +450,9 @@ async function _fetchSession(): Promise<ApiSession> {
const fetchSession = memorize(_fetchSession)

async function getAccessToken(): Promise<string> {
const pageAccessToken = getPageAccessToken()
if (pageAccessToken) return pageAccessToken

const session = await fetchSession()
return session.accessToken
}
Expand Down
34 changes: 33 additions & 1 deletion src/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,47 @@ declare global {
}
}
}
__remixContext: {
basename: string
future: {}
state: {
loaderData: {
root: {
clientBootstrap: {
accountStatus: null
session: {
accessToken: string
authProvider: string
expires: string
user: {
email: string
group: unknown[]
id: string
image: string
intercom_hash: string
mfa: boolean
name: string
picture: string
}
}
}
}
}
}
}
}
}

export function getHistoryDisabled(): boolean {
return localStorage.getItem(KEY_OAI_HISTORY_DISABLED) === '"true"'
}

export function getPageAccessToken(): string | null {
return unsafeWindow?.__remixContext?.state?.loaderData?.root?.clientBootstrap?.session?.accessToken ?? null
}

function getUserProfile() {
const user = unsafeWindow?.__NEXT_DATA__?.props?.pageProps?.user
const user = unsafeWindow?.__NEXT_DATA__?.props?.pageProps?.user ?? unsafeWindow?.__remixContext?.state?.loaderData?.root?.clientBootstrap?.session?.user
if (!user) throw new Error('No user found.')
return user
}
Expand Down

0 comments on commit 950c8fc

Please sign in to comment.