@asteby/metacore-app-providers

中文文档
153.1.0发布于 2026/10/22Apache-2.0TypeScriptES Module零依赖近 30 天下载 3895

Providers genéricos reutilizables para apps metacore (direction, font, layout, search, platform-config)

原始使用文档
文档目录10 节
  1. Stability
  2. Install
  3. Exports at a glance
  4. Basic providers
  5. PlatformConfigProvider — tenant branding
  6. Contract
  7. MetacoreAppShell — the full kit
  8. Addon install bridge
  9. Persistence summary
  10. License

@asteby/metacore-app-providers

Generic, reusable React providers for metacore host applications. One package wires the cross-cutting concerns every metacore app shares — direction, font, layout, search, platform branding — plus the optional "full kit" <MetacoreAppShell> that bundles API + PWA + toaster in a single tag.

Stability

Stable as of v1.0. The exports below follow semver. Persistence keys (cookie names, localStorage keys) and provider context shapes will not change inside the major. Optional peer integrations (@asteby/metacore-pwa, @asteby/metacore-runtime-react, @asteby/metacore-ui, sonner) move on their own cadence — the shell tolerates compatible versions via the peer ranges declared in package.json.

Install

pnpm add @asteby/metacore-app-providers @radix-ui/react-direction
# transports / kits used by MetacoreAppShell + PlatformConfigProvider
pnpm add @tanstack/react-query sonner
# optional, only if you mount MetacoreAppShell:
pnpm add @asteby/metacore-pwa @asteby/metacore-runtime-react @asteby/metacore-ui

Exports at a glance

ExportPurpose
DirectionProvider, useDirectionLTR/RTL with cookie persistence (dir).
FontProvider, useFontFont class on <html>, cookie-persisted (font). Requires fonts prop.
LayoutProvider, useLayoutSidebar variant + collapsible mode. Cookies: layout_variant, layout_collapsible.
SearchProvider, useSearchCmd/Ctrl+K hotkey + open/setOpen for command palettes.
PlatformConfigProvider, usePlatformConfigTenant branding (name, logo, primary/accent color, support URLs) — transport-agnostic.
useAgentSuggestions, resolveAgentSuggestions, DEFAULT_AGENT_SUGGESTIONSCopilot starter prompts from PlatformBranding.agent_suggestions (blanks ignored, neutral fallback).
applyBranding, applyCachedBrandingImperative helpers for pre-React paint and SW updates.
FALLBACK_BRANDINGEmpty defaults consumers can spread over.
usePublishMascotFace, useMascotFace, mascotFaceStoreOne shared mascot face across chat surfaces; the most active MascotState wins (error > speaking > processing > thinking > listening > happy > idle).
MetacoreAppShellFull kit: ApiProvider + QueryClient + PWA + Toaster + addon-install bridge.
getCookie, setCookie, removeCookieTiny cookie helpers shared by the providers.

Types: Direction, Collapsible, Variant, FontProviderProps, SearchProviderProps, PlatformBranding, BrandingFetcher, PlatformConfigProviderProps, MetacoreAppShellProps, MetacoreInstallRequest.

Basic providers

import {
  DirectionProvider,
  FontProvider,
  LayoutProvider,
  SearchProvider,
} from '@asteby/metacore-app-providers'
import { fonts } from '@asteby/metacore-starter-config/fonts'

<DirectionProvider>
  <FontProvider fonts={fonts}>
    <LayoutProvider>
      <SearchProvider>
        <App />
      </SearchProvider>
    </LayoutProvider>
  </FontProvider>
</DirectionProvider>
  • FontProvider requires fonts (the first entry is the default).
  • SearchProvider accepts hotkey (default 'k', with Cmd/Ctrl). The consumer renders the command menu using useSearch().open / setOpen().
  • All four persist state in cookies so SSR/edge runtimes can restore the initial frame.

PlatformConfigProvider — tenant branding

Centralised branding for any metacore app. Apps fetch their tenant's branding (name, logo, primary color, support URLs) from a backend endpoint they own; this provider caches it, applies CSS variables (--primary, --background, --sidebar-*, ...) to <html>, persists the payload in localStorage so subsequent loads paint the right brand before React mounts, and re-applies on dark/light toggles.

The provider is transport-agnostic: callers pass an async fetcher. No HTTP client is hard-wired — any of axios / fetch / ofetch / hand-rolled works.

import {
  PlatformConfigProvider,
  applyCachedBranding,
  type PlatformBranding,
} from '@asteby/metacore-app-providers'
import { api } from './lib/api'

// Paint the cached brand BEFORE React mounts.
applyCachedBranding()

const defaults: PlatformBranding = {
  platform_name: 'Acme Hub',
  platform_logo: '/logo.svg',
  primary_color: '#84cc16',
  accent_color: '#22d3ee',
  favicon_url: '/favicon.ico',
  support_email: '[email protected]',
  support_url: 'https://acme.test/support',
}

<PlatformConfigProvider
  fetcher={async () => (await api.get('/platform/branding')).data}
  defaults={defaults}
>
  <App />
</PlatformConfigProvider>

Inside the tree:

const { platform_name, primary_color, refetch } = usePlatformConfig()

refetch() invalidates the query if the host knows the branding just changed (e.g. after a settings save).

Contract

  • fetcher resolves to a Partial<PlatformBranding>. The provider merges it over defaults, so missing fields fall back instead of blanking the UI.
  • Hex colors (#rrggbb) are converted to oklch() and pushed onto the <html> element as CSS variables. The dark/light variant is derived by reading document.documentElement.classList; the provider observes class changes and re-applies on toggle.
  • Persistence key is platform-branding in localStorage. Override via storageKey. applyCachedBranding(storageKey?) is exported for hosts that need a non-default key in their pre-React boot script.
  • First frame: if window.__BRAND__ exists (the server inlines it via /api/brand/boot.js, same field names as PlatformBranding), applyCachedBranding paints it and rewrites the storage key with it; the stored value is used only when __BRAND__ is absent (offline). Every real fetcher response REPLACES the stored fields (no merge with older values). Colors must be #RRGGBB; anything else is dropped before painting or storing (sanitizeBranding, persistBranding, readWindowBranding are exported).
  • staleTime defaults to 5 minutes (TanStack Query). The provider keeps one query per storageKey.

MetacoreAppShell — the full kit

<MetacoreAppShell> wires every provider an app needs to run on the metacore platform: API context, QueryClient, PWA install + update prompts, offline indicator, sonner toaster, addon-install bridge for embedded Hub iframes, and stale-while-revalidate metadata-cache invalidation when a service-worker update is applied.

import { MetacoreAppShell } from '@asteby/metacore-app-providers/shell'
import { QueryClient } from '@tanstack/react-query'
import { api } from './lib/api'

const queryClient = new QueryClient()

<MetacoreAppShell api={api} queryClient={queryClient}>
  <RouterProvider router={router} />
</MetacoreAppShell>

Optional flags (all default to enabled):

PropEffect
hideToasterSkip the bundled <Toaster />.
hidePWAInstallSkip <PWAInstallPrompt />.
hidePWAUpdateSkip <PWAUpdatePrompt />.
hideOfflineIndicatorSkip <OfflineIndicator />.
disableMetadataInvalidateDon't drop the runtime metadata cache on SW update.
onAddonInstallCustom handler for the metacore:install postMessage from an embedded Hub iframe. Set to null to disable the listener.
toasterPosition'top-right' by default; passed through to sonner.

The shell pulls peers (@asteby/metacore-pwa, @asteby/metacore-runtime-react, @asteby/metacore-ui, sonner) when mounted. Apps that only want a subset wire the individual providers and skip the shell.

Addon install bridge

When mounted inside (or alongside) the metacore Hub, the shell listens for window.postMessage({ type: 'metacore:install', addonKey, ... }) events and either:

  1. invokes onAddonInstall(req, source) if you supplied one, OR
  2. POSTs req to /marketplace/install on the bundled API client.

On success it shows a sonner toast, drops the runtime metadata cache, dispatches a metacore:metadata-changed CustomEvent on window (so sidebars and command menus refetch without a reload), and replies to the iframe with metacore:installed. On failure it replies with metacore:install-failed and surfaces the error.

Persistence summary

Cookie / keyOwner
dirDirectionProvider
fontFontProvider
layout_variantLayoutProvider
layout_collapsibleLayoutProvider
platform-brandingPlatformConfigProvider (localStorage)

License

Apache-2.0

版本与发布记录

@asteby/metacore-app-providers 共发布 173 个版本,首次发布于 2026/04/21;过去 12 个月发布了 173 个版本。

版本发布日期距今
153.1.0latest2026/10/2217 天前
153.0.02026/10/1821 天前
152.0.02026/10/1821 天前
151.0.02026/10/1524 天前
150.0.02026/10/1424 天前
149.0.02026/10/1425 天前

关于 @asteby/metacore-app-providers 的常见问题

@asteby/metacore-app-providers 是什么?

Providers genéricos reutilizables para apps metacore (direction, font, layout, search, platform-config)

如何安装 @asteby/metacore-app-providers?

在项目目录运行 npm install @asteby/metacore-app-providers,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>=18)、sonner(>=1.7)、react-dom(>=18)、@tanstack/react-query(>=5)、@radix-ui/react-direction(^1.1.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@asteby/metacore-app-providers 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@asteby/metacore-app-providers 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@asteby/metacore-app-providers 可以商用吗?

@asteby/metacore-app-providers 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@asteby/metacore-app-providers 的下载量有多少?

根据 npm 官方下载统计,@asteby/metacore-app-providers 在 2026/10/07 – 2026/11/05 这 30 天内被下载 3,895 次,日均约 130 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@asteby/metacore-app-providers 还在维护吗?

最新版本 153.1.0 发布于 2026/10/22(17 天前);至今共发布 173 个版本,其中过去 12 个月发布了 173 个;首次发布于 2026/04/21。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。