# SmartStack Contexts — Cheatsheet

> **Reference for `application` skill** — quick lookup of the 10 system contexts exposed by `<SmartStackProvider>`.

## When This Reference Applies

- You generate a component that needs to read auth/tenant/theme state
- You audit a page that re-implements existing context state
- The user asks "how do I read the current tenant", "how do I check a permission", "where is the SignalR connection"

---

## The 10 contexts (state map)

| # | Context | Hook | State exposed | Example use case |
|---|---|---|---|---|
| 1 | AuthContext | `useAuth()` | `user`, `roles`, `permissions`, `isAuthenticated`, `login()`, `logout()`, `refresh()`, `hasPermission(path)` | Read current user, check permission gate |
| 2 | TenantContext | `useTenant()` | `currentTenant`, `userTenants`, `isGlobalView`, `switchTenant(id)` | Display tenant switcher, scope a query |
| 3 | NavigationContext | `useNavigation()` | `menu` (MenuDto), `reload()`, `currentApp`, `currentModule`, `currentSection`, `isLoading`, `getApplicationsByZone()` (back-compat alias) | Render sidebar, breadcrumb, route resolution |
| 4 | ThemeContext | `useTheme()` | `mode`, `selectedTheme`, `selectedPreset`, `setMode()`, `setTheme(id)`, `setPreset(id)` | Toggle dark mode, switch theme |
| 5 | LicenseContext | `useLicense()` | `license`, `isFeatureEnabled(code)`, `hasModule(code)` | Gate a feature behind license |
| 6 | SignalRContext | `useSignalR()` | `isConnected`, `on(event, handler)`, `off(event, handler)`, `emit(event, payload)` | Real-time notifications |
| 7 | FavoritesContext | `useFavorites()` | `favorites`, `toggleFavorite(item)`, `isFavorite(id)` | Bookmark a page/entity |
| 8 | SidebarContext | `useSidebar()` | `isCollapsed`, `toggle()`, `setCollapsed(value)` | Sidebar collapse button |
| 9 | FeatureConfigContext | `useFeatureConfig()` | `features` (record from `/api/config/public`) | Feature flag, A/B test |
| 10 | DocPanelContext | `useDocPanel()` | `isOpen`, `toggle()`, `openDoc(slug)` | Contextual help panel |

---

## Common patterns

### Permission check — render guard

```tsx
const { hasPermission } = useAuth();
if (!hasPermission('administration.users.create')) return <Forbidden />;
```

### Tenant-scoped query

```tsx
const { currentTenant } = useTenant();
const { data } = useQuery(
  ['orders', currentTenant?.id],
  () => orderApi.list({ tenantId: currentTenant?.id })
);
```

### Real-time updates

```tsx
const { on, off } = useSignalR();
useEffect(() => {
  const handler = (notif) => toast(notif.message);
  on('NotificationReceived', handler);
  return () => off('NotificationReceived', handler);
}, []);
```

### Feature flag

```tsx
const { isFeatureEnabled } = useLicense();
if (isFeatureEnabled('ai-assistant')) return <AiAssistant />;
```

---

## DO / DON'T

| ✅ DO | ❌ DON'T |
|---|---|
| Consume contexts via the provided hooks | `useContext(SmartStackContext)` directly |
| Memoize derived values from context | Re-compute on every render |
| Subscribe/unsubscribe SignalR handlers in `useEffect` cleanup | Forget to `off()` (memory leak) |
| Treat `permissions` as opaque strings | Parse permissions client-side |
| Read tenant from `currentTenant`, not URL | Trust the URL slug as a source of truth |

---

## Reference source files (read-only)

- `D:\01 - projets\SmartStack.app\features\IA-Workflow\web\smartstack-web\src\contexts\` (all 10 files)
- `D:\01 - projets\SmartStack.app\features\IA-Workflow\web\smartstack-web\src\provider\SmartStackProvider.tsx` (wrapping order)

See [smartstack-provider.md](smartstack-provider.md) for the wrapping order.
