> For the complete documentation index, see [llms.txt](https://docs.akinon.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.akinon.com/akinon-ui/ui-protocol/shell-application/useappshell.md).

# useAppShell

The primary hook for accessing the shell context. It provides access to registered apps, configurations, navigation, and iframe references.

```tsx
import { useAppShell } from '@akinon/app-shell';

const ShellComponent = () => {
  const { apps, configs, navigate } = useAppShell();

  return (
    <div>
      <h1>Registered Apps: {apps.length}</h1>
      <button onClick={() => navigate({ path: '/dashboard' })}>
        Go to Dashboard
      </button>
    </div>
  );
};
```

### <mark style="color:red;">Return Value</mark>

The `useAppShell` hook returns an `AppShellContextState` object:

<table><thead><tr><th width="150.45703125">Property</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td><code>apps</code></td><td><code>RegisteredApp[]</code></td><td>List of registered micro-frontend applications</td></tr><tr><td><code>configs</code></td><td><code>Map&#x3C;number | UUID, ApplicationConfig></code></td><td>Configuration for each registered app</td></tr><tr><td><code>navigate</code></td><td><code>(payload: NavigationPayload) => void</code></td><td>Navigation function</td></tr><tr><td><code>frames</code></td><td><code>MutableRefObject&#x3C;Map&#x3C;number | UUID, HTMLIFrameElement>></code></td><td>References to iframe elements</td></tr><tr><td><code>buses</code></td><td><code>MutableRefObject&#x3C;Map&#x3C;number | UUID, Framebus>></code></td><td>Framebus instances for communication</td></tr><tr><td><code>instances</code></td><td><code>Map&#x3C;number | UUID, AppInstanceInfo></code></td><td>App instances by ID</td></tr><tr><td><code>modals</code></td><td><code>Map&#x3C;UUID, number | UUID></code></td><td>UUID values for rich modals</td></tr><tr><td><code>modalContext</code></td><td><code>Map&#x3C;UUID, unknown></code></td><td>Context data for modals</td></tr></tbody></table>

### <mark style="color:red;">AppShellContextState Type</mark>

```typescript
interface AppShellContextState {
  apps: RegisteredApp[];
  configs: Map<number | UUID, PluginApplicationConfig | FullpageApplicationConfig>;
  navigate: (payload: ApplicationNavigationPayload) => void;
  frames?: React.MutableRefObject<Map<number | UUID, HTMLIFrameElement>>;
  buses?: React.MutableRefObject<Map<number | UUID, Framebus>>;
  instances: Map<number | UUID, AppInstanceInfo>;
  modals?: Map<UUID, number | UUID>;
  modalContext?: Map<UUID, unknown>;
}

interface AppInstanceInfo {
  id: number | UUID;
  basePath: string;
  config: FullpageApplicationConfig | PluginApplicationConfig;
  iframe: HTMLIFrameElement;
  bus: Framebus;
}
```

### <mark style="color:red;">Usage Examples</mark>

#### Accessing Registered Apps

```tsx
const AppList = () => {
  const { apps } = useAppShell();

  return (
    <ul>
      {apps.map(app => (
        <li key={app.pk}>
          {app.name} - {app.config.visible_type}
        </li>
      ))}
    </ul>
  );
};
```

#### Programmatic Navigation

```tsx
const NavigationButtons = () => {
  const { navigate } = useAppShell();

  return (
    <div>
      <button onClick={() => navigate({ path: '/orders' })}>Orders</button>
      <button onClick={() => navigate({ path: '/products' })}>Products</button>
      <button onClick={() => navigate({ path: '/settings', id: 1 })}>
        Settings (App 1)
      </button>
    </div>
  );
};
```

#### Accessing App Configurations

```tsx
const AppInfo = ({ appId }: { appId: number }) => {
  const { configs } = useAppShell();
  const config = configs.get(appId);

  if (!config) return <p>App not found</p>;

  return (
    <div>
      <p>URL: {config.web_url}</p>
      <p>Type: {config.visible_type}</p>
    </div>
  );
};
```

#### Working with Iframe References

```tsx
const IframeManager = () => {
  const { frames } = useAppShell();

  const reloadApp = (appId: number) => {
    const iframe = frames?.current.get(appId);
    if (iframe) {
      iframe.contentWindow?.location.reload();
    }
  };

  return (
    <button onClick={() => reloadApp(1)}>
      Reload App 1
    </button>
  );
};
```

#### Sending Messages via Framebus

```tsx
const MessageSender = () => {
  const { buses } = useAppShell();

  const sendMessage = (appId: number, event: string, data: any) => {
    const bus = buses?.current.get(appId);
    if (bus) {
      bus.emit(event, data);
    }
  };

  return (
    <button onClick={() => sendMessage(1, 'custom-event', { foo: 'bar' })}>
      Send Custom Message
    </button>
  );
};
```

### <mark style="color:red;">Best Practices</mark>

#### 1. Use at the Right Level

Call `useAppShell` in components that need shell context, not at the top level:

```tsx
// ✅ Good - Use where needed
const AppRenderer = ({ id }) => {
  const { frames } = useAppShell();
  // ...
};

// ❌ Avoid - Don't destructure everything at top level
const App = () => {
  const shellContext = useAppShell(); // Then pass to children
};
```

#### 2. Check for Optional Values

Some properties are optional and may be undefined:

```tsx
const Component = () => {
  const { frames, buses } = useAppShell();

  const sendMessage = (appId: number) => {
    const bus = buses?.current.get(appId);
    if (!bus) {
      console.warn('Framebus not available for app:', appId);
      return;
    }
    bus.emit('event', data);
  };
};
```

#### 3. Memoize Derived Values

When computing values from the context, memoize them:

```tsx
const ActiveApps = () => {
  const { apps } = useAppShell();

  const activeApps = useMemo(
    () => apps.filter(app => app.is_active && app.is_visible),
    [apps]
  );

  return <AppList apps={activeApps} />;
};
```

### <mark style="color:red;">Related</mark>

* [Components](https://docs.akinon.com/akinon-ui/ui-protocol/shell-application/components.md) - AppRenderer, PluginRenderer, ModalRenderer
* [API Reference](https://docs.akinon.com/akinon-ui/ui-protocol/shell-application/api-reference.md) - Complete type definitions


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.akinon.com/akinon-ui/ui-protocol/shell-application/useappshell.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
