fix: adapt remix version and try to reduce call to session api

This commit is contained in:
Pionxzh
2024-09-05 02:28:15 +08:00
parent 1cd1977bad
commit 950c8fc7cf
2 changed files with 37 additions and 2 deletions

View File

@@ -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'
@@ -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
}

View File

@@ -39,6 +39,34 @@ 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
}
}
}
}
}
}
}
}
}
@@ -46,8 +74,12 @@ 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
}