useDashboardView
Composable that backs CnDashboardPage. Manages the merged widget catalog (app-defined + Nextcloud Dashboard API), the layout (load/save/default), role-based visibility filtering, and edit-mode state.
Signature
import { useDashboardView } from '@conduction/nextcloud-vue'
const {
widgets, layout, loading, saving, isEditing,
activeWidgetIds, availableWidgets, ncWidgets,
onLayoutChange, addWidget, removeWidget, setWidgets, init,
} = useDashboardView({
widgets: APP_WIDGETS,
defaultLayout: DEFAULT_LAYOUT,
loadLayout: () => fetch('/api/dashboard-layout').then(r => r.json()),
saveLayout: (layout) => fetch('/api/dashboard-layout', { method: 'PUT', body: JSON.stringify(layout) }),
includeNcWidgets: true,
columns: 12,
})
Options
| Key | Type | Default | Description |
|---|---|---|---|
widgets | Array | [] | Static widget definitions. Each entry: { id, title, type, iconUrl?, iconClass?, visibility?, … }. |
defaultLayout | Array | [] | Fallback layout used when loadLayout returns null or an empty array. Each entry: { id, widgetId, gridX, gridY, gridWidth, gridHeight, showTitle? }. |
loadLayout | () => Promise<Array|null> | null | Async loader for a persisted layout. |
saveLayout | (layout) => Promise<void> | null | Async persister; called automatically from onLayoutChange. |
includeNcWidgets | boolean | false | When true, init() also fetches widgets from ocs/apps/dashboard/api/v1/widgets and merges them into the catalog. |
columns | number | 12 | Grid column count (for reference; the actual grid renderer reads this). |
Visibility filtering
Each widget may declare a visibility block:
{ id: 'admin-panel', type: 'custom', title: 'Admin',
visibility: { users: ['admin'], groups: ['Admins'] } }
init() (and setWidgets()) run both widget arrays through filterWidgetsByVisibility. Widgets without a visibility block (or with empty arrays) are visible to everyone. Layout items that reference a filtered-out widget are removed from layout.value.
Return value
Reactive state:
widgets—computed: concatenation of visible app widgets + visible NC widgets.layout—Ref<Array>: current grid placements.loading— true whileinit()runs.saving— true whilesaveLayoutis in flight.isEditing— edit-mode flag (toggled by the consumer, e.g. fromCnDashboardPage's edit button).
Derived:
activeWidgetIds— IDs currently placed on the dashboard.availableWidgets—widgetsminus any whose ID is inactiveWidgetIds(i.e. the "add widget" picker list).ncWidgets— Raw NC Dashboard API widget list (pre-visibility), normalized to the same shape as app widgets (iconClass,iconUrl,widgetUrl,itemApiVersions,itemIconsRound,reloadInterval,buttons,type: 'nc-widget').
Methods:
onLayoutChange(newLayout)— Setslayoutand callssaveLayoutif provided. Use as the@layout-changehandler onCnDashboardPage.addWidget(widgetId, position?)— Appends a new layout item.id= highest existing numericid + 1;gridY= bottom of the current layout;gridWidth/gridHeightdefault to 6/3 unless overridden inposition.removeWidget(itemId)— Filters out the layout item with the matching numericid.setWidgets(newWidgets)— ReplacesappWidgetsand re-runs visibility filtering (use when widget defs are data-driven).init()— Re-runs the full initialization. Called automatically on mount.
Lifecycle
onMounted calls init(), which:
- Kicks off
loadNcWidgets()ifincludeNcWidgets. - Kicks off
loadLayout()if provided, else usesdefaultLayout. - Awaits all tasks, then applies visibility filtering.
- On any thrown error, falls back to
defaultLayoutand logs to console.
Related
- CnDashboardPage — Primary consumer.
- filterWidgetsByVisibility — The visibility helper used here.