From 31d0e204bdb53a8a687c39da05320c4390324d8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?nicole=20miko=C5=82ajczyk?= Date: Wed, 6 Aug 2025 22:00:48 +0200 Subject: [PATCH] pl-fe: provide preloaded instance to pl-api client MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: nicole mikołajczyk --- packages/pl-fe/src/actions/preload.ts | 2 ++ packages/pl-fe/src/reducers/auth.ts | 14 ++++++++++---- packages/pl-fe/src/reducers/instance.ts | 2 +- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/packages/pl-fe/src/actions/preload.ts b/packages/pl-fe/src/actions/preload.ts index 71ad2e187..7b4a970b7 100644 --- a/packages/pl-fe/src/actions/preload.ts +++ b/packages/pl-fe/src/actions/preload.ts @@ -67,6 +67,8 @@ interface PreloadAction { export { PLEROMA_PRELOAD_IMPORT, MASTODON_PRELOAD_IMPORT, + pleromaDecoder, + decodeFromMarkup, preload, preloadMastodon, type PreloadAction, diff --git a/packages/pl-fe/src/reducers/auth.ts b/packages/pl-fe/src/reducers/auth.ts index f1083a725..abb748485 100644 --- a/packages/pl-fe/src/reducers/auth.ts +++ b/packages/pl-fe/src/reducers/auth.ts @@ -1,9 +1,9 @@ import trim from 'lodash/trim'; import { create, Draft } from 'mutative'; -import { applicationSchema, PlApiClient, tokenSchema, type CredentialAccount, type CredentialApplication, type Token } from 'pl-api'; +import { applicationSchema, instanceSchema, PlApiClient, tokenSchema, type CredentialAccount, type CredentialApplication, type Token } from 'pl-api'; import * as v from 'valibot'; -import { MASTODON_PRELOAD_IMPORT, type PreloadAction } from 'pl-fe/actions/preload'; +import { decodeFromMarkup, MASTODON_PRELOAD_IMPORT, pleromaDecoder, type PreloadAction } from 'pl-fe/actions/preload'; import * as BuildConfig from 'pl-fe/build-config'; import { coerceObject } from 'pl-fe/schemas/utils'; import KVStore from 'pl-fe/storage/kv-store'; @@ -24,6 +24,10 @@ import { ME_FETCH_SKIP, type MeAction } from '../actions/me'; import type { PlfeResponse } from 'pl-fe/api'; import type { Account as AccountEntity } from 'pl-fe/normalizers/account'; +const preloadedInstance = decodeFromMarkup('initial-results', pleromaDecoder)['/api/v1/instance']; +const parsedInstance = v.safeParse(instanceSchema, preloadedInstance); +const instance = parsedInstance.success ? parsedInstance.output : undefined; + type Action = AuthAction | MeAction | PreloadAction; const backendUrl = (isURL(BuildConfig.BACKEND_URL) ? BuildConfig.BACKEND_URL : ''); @@ -89,7 +93,9 @@ const getLocalState = (): State | undefined => { tokens: Object.fromEntries(Object.entries(state.tokens).map(([key, value]) => [key, v.parse(tokenWithAppSchema, value)])), users: Object.fromEntries(Object.entries(state.users).map(([key, value]) => [key, v.parse(authUserSchema, value)])), me: state.me, - client: new PlApiClient(parseBaseURL(state.me) || backendUrl, state.users[state.me]?.access_token), + client: new PlApiClient(parseBaseURL(state.me) || backendUrl, state.users[state.me]?.access_token, { + instance, + }), }); }; @@ -193,7 +199,7 @@ const initialState: State = initialize({ tokens: {}, users: {}, me: null, - client: new PlApiClient(backendUrl), + client: new PlApiClient(backendUrl, undefined, { instance }), ...localState, }); diff --git a/packages/pl-fe/src/reducers/instance.ts b/packages/pl-fe/src/reducers/instance.ts index 02f205160..d0261a228 100644 --- a/packages/pl-fe/src/reducers/instance.ts +++ b/packages/pl-fe/src/reducers/instance.ts @@ -10,7 +10,7 @@ import ConfigDB from 'pl-fe/utils/config-db'; const initialState: State = { fetched: false, ...v.parse(instanceSchema, {}) }; -type State = Instance & { fetched : boolean }; +type State = Instance & { fetched: boolean }; const preloadImport = (state: State, action: PreloadAction, path: string): State => { const instance = action.data[path];