@sdkwork/core-pc-react
v0.1.1
Published
Unified SDKWork core runtime for Tauri React desktop apps
Readme
@sdkwork/core-pc-react
@sdkwork/core-pc-react is the unified desktop React runtime for SDKWork Tauri applications.
It centralizes:
- Vite env parsing
- owner-scoped
baseUrlandaccessTokenresolution - app sdk bootstrap
- IM backend sdk + realtime sdk bootstrap
- dual-token and api-key auth mode handling
- shell preference persistence for theme selection, theme color, locale preference, and resolved locale
- standard
useAppClientanduseImClienthooks - standard
usePcReactShellPreferencesandusePcReactResolvedShellPreferenceshooks - runtime session persistence
- legacy desktop storage migration
- injected desktop env compatibility
- app client compatibility aliases
Runtime model
One package owns five concerns:
Env
- resolves
VITE_*keys into one normalized desktop runtime contract - supports owner-scoped
root,tenant,organizationbaseUrlandaccessToken - supports compatibility keys from older desktop apps
- detects Tauri runtime through
__TAURI__,__TAURI_IPC__, or__TAURI_INTERNALS__
- resolves
Session
- persists
authToken,accessToken,refreshToken, and IM identity - keeps
authTokenandaccessTokenseparated - automatically reads legacy desktop storage keys during migration
- persists
Clients
- provides one shared app sdk client
- provides one shared IM backend client + composed IM runtime
- applies runtime session changes to both clients after login, refresh, and logout
Hooks
useAppClient()useImClient()usePcReactEnv()usePcReactRuntimeSession()usePcReactShellPreferences()usePcReactResolvedShellPreferences()
Preferences
- persists
themeSelection,themeColor,localePreference, and effectivelocale - keeps UI shell preferences separate from auth and IM session storage
- resolves concrete
colorModefromlight,dark, orsystem - allows hosts to pass one normalized preference snapshot into
@sdkwork/ui-pc-react
- persists
Standard usage
import {
SDKWORK_PC_REACT_DEFAULT_APP_CLIENT_COMPAT_ALIASES,
configurePcReactRuntime,
persistPcReactRuntimeSession,
persistPcReactShellPreferences,
syncImClientSession,
useAppClient,
useImClient,
usePcReactResolvedShellPreferences,
} from "@sdkwork/core-pc-react";
const getAppLanguage = () => navigator.language || "en-US";
configurePcReactRuntime({
envSource: import.meta.env,
envGlobalKeys: ["__SDKWORK_NOTES_ENV__"],
appClientCompatAliases: SDKWORK_PC_REACT_DEFAULT_APP_CLIENT_COMPAT_ALIASES,
preferences: {
defaults: {
localePreference: "zh-CN",
themeColor: "lobster",
themeSelection: "system"
}
},
headersResolver: ({ target }) => ({
"Accept-Language": getAppLanguage(),
"X-SDK-Client": target === "im" ? "desktop-im" : "desktop-app"
})
});
persistPcReactRuntimeSession({
authToken: "user-auth-token",
accessToken: "tenant-access-token",
refreshToken: "refresh-token"
});
persistPcReactShellPreferences({
localePreference: "zh-CN",
themeColor: "lobster",
themeSelection: "dark"
});
await syncImClientSession({
userId: "1001",
username: "neo",
displayName: "Neo",
authToken: "user-auth-token",
accessToken: "tenant-access-token"
});
const appClient = useAppClient();
const imClient = useImClient();
const shellPreferences = usePcReactResolvedShellPreferences();Env standard
Primary env keys:
VITE_APP_ENVVITE_OWNER_MODEVITE_API_BASE_URLVITE_ROOT_API_BASE_URLVITE_TENANT_API_BASE_URLVITE_ORGANIZATION_API_BASE_URLVITE_ACCESS_TOKENVITE_ROOT_ACCESS_TOKENVITE_TENANT_ACCESS_TOKENVITE_ORGANIZATION_ACCESS_TOKENVITE_IM_WS_URLVITE_API_KEYVITE_TENANT_IDVITE_ORGANIZATION_IDVITE_PLATFORMVITE_DISTRIBUTION_IDVITE_APP_IDVITE_RELEASE_CHANNELVITE_ENABLE_STARTUP_UPDATE_CHECK
Compatibility env keys that are still supported for migration:
VITE_APP_API_BASE_URLVITE_APP_BASE_URLVITE_APP_IM_WS_URLVITE_APP_ACCESS_TOKENVITE_APP_PLATFORMSDKWORK_API_BASE_URLSDKWORK_ACCESS_TOKENSDKWORK_API_KEYSDKWORK_TENANT_IDSDKWORK_ORGANIZATION_IDSDKWORK_PLATFORMSDKWORK_OWNER_MODE
Injected desktop env compatibility:
configurePcReactRuntime({ envGlobalKeys: ["__SDKWORK_NOTES_ENV__"] })- any named global object in
globalThiscan be merged into runtime env before explicitenvSource
Storage standard
Standard storage keys:
sdkwork.core.pc-react.auth-tokensdkwork.core.pc-react.access-tokensdkwork.core.pc-react.refresh-tokensdkwork.core.pc-react.im-sessionsdkwork.core.pc-react.preferences
Shell preference standard
Shell preference model:
themeSelection:light|dark|systemthemeColor:green-tech|lobster|rose|tech-blue|violet|zinclocalePreference:systemor any normalized desktop locale string such aszh-CNoren-USlocale: resolved locale after applyinglocalePreference
Resolved shell preference model:
colorMode:light|darkthemeSelection: preserved user preferencethemeColor: preserved user preferencelocalePreference: preserved user preferencelocale: effective locale after defaults and storage normalization
Preference APIs:
readPcReactShellPreferences()persistPcReactShellPreferences(patch)clearPcReactShellPreferences()resolvePcReactShellPreferences()usePcReactShellPreferences()usePcReactResolvedShellPreferences()
This separation is intentional: clearing runtime auth state should not clear theme or locale preferences, and preference changes should not force app/im client hooks to rerender.
Automatically supported legacy migration keys:
sdkwork_tokensdkwork_access_tokensdkwork_refresh_tokensdkwork-notes-auth-sessionsdkwork-drive-auth-sessionclaw-studio-auth-session
Legacy JSON session keys are read as:
{
"authToken": "user-auth-token",
"refreshToken": "refresh-token"
}App client compatibility aliases
The exported SDKWORK_PC_REACT_DEFAULT_APP_CLIENT_COMPAT_ALIASES adds plural compatibility getters for desktop apps that previously wrapped the generated app sdk:
{
"analytics": "analytic",
"assets": "asset",
"coupons": "coupon",
"notes": "note",
"orders": "order",
"payments": "payment",
"projects": "project",
"settings": "setting",
"workspaces": "workspace"
}This is intended for Magic Studio style compatibility migration while keeping the generated sdk as the actual source of truth.
Header standard
Dual-token mode:
Access-Token: <owner-scoped-access-token>Authorization: Bearer <auth-token>
API key mode:
- app sdk uses
Authorization: Bearer <apiKey> - IM backend sdk uses
X-API-Key: <apiKey>
Runtime header extension:
- use
headersResolverto inject shared desktop headers such asAccept-Language - resolver output is merged into both app sdk and IM backend sdk configs
- explicit per-client
headersoverrides still win
JSON preview
Resolved env preview:
{
"appEnv": "development",
"owner": {
"mode": "tenant",
"tenantId": "2001",
"organizationId": ""
},
"api": {
"baseUrl": "https://tenant-api.sdkwork.com",
"baseUrls": {
"default": "https://api.sdkwork.com",
"root": "https://root-api.sdkwork.com",
"tenant": "https://tenant-api.sdkwork.com",
"organization": "https://org-api.sdkwork.com"
},
"timeout": 30000
},
"auth": {
"mode": "dual-token",
"accessToken": "tenant-access-token",
"accessTokens": {
"default": "root-access-token",
"root": "root-access-token",
"tenant": "tenant-access-token",
"organization": "org-access-token"
},
"apiKey": ""
},
"realtime": {
"imWsUrl": "wss://tenant-api.sdkwork.com/ws"
},
"distribution": {
"id": "global"
},
"platform": {
"id": "desktop",
"isDesktop": true,
"isTauri": true
}
}Runtime session preview:
{
"authToken": "user-auth-token",
"accessToken": "tenant-access-token",
"refreshToken": "refresh-token",
"im": {
"userId": "1001",
"username": "neo",
"displayName": "Neo",
"authToken": "user-auth-token",
"accessToken": "tenant-access-token"
}
}Effective request header preview:
{
"appSdk": {
"Access-Token": "tenant-access-token",
"Authorization": "Bearer user-auth-token",
"Accept-Language": "zh-CN"
},
"imBackendSdk": {
"Access-Token": "tenant-access-token",
"Authorization": "Bearer user-auth-token",
"Accept-Language": "zh-CN"
}
}