@frigear-nu/dashboard 0.0.0-stage → 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/README.md +4 -2
  2. package/dist/module.d.mts +81 -0
  3. package/dist/module.json +12 -0
  4. package/dist/module.mjs +128 -0
  5. package/dist/runtime/access.d.ts +1 -0
  6. package/dist/runtime/access.js +12 -0
  7. package/dist/runtime/app/components/ActionCard.d.vue.ts +19 -0
  8. package/dist/runtime/app/components/ActionCard.vue +54 -0
  9. package/dist/runtime/app/components/ActionCard.vue.d.ts +19 -0
  10. package/dist/runtime/app/components/ChartCard.d.vue.ts +28 -0
  11. package/dist/runtime/app/components/ChartCard.vue +53 -0
  12. package/dist/runtime/app/components/ChartCard.vue.d.ts +28 -0
  13. package/dist/runtime/app/components/Dashboard.d.vue.ts +27 -0
  14. package/dist/runtime/app/components/Dashboard.vue +156 -0
  15. package/dist/runtime/app/components/Dashboard.vue.d.ts +27 -0
  16. package/dist/runtime/app/components/DashboardGrid.d.vue.ts +15 -0
  17. package/dist/runtime/app/components/DashboardGrid.vue +9 -0
  18. package/dist/runtime/app/components/DashboardGrid.vue.d.ts +15 -0
  19. package/dist/runtime/app/components/DashboardItem.d.vue.ts +26 -0
  20. package/dist/runtime/app/components/DashboardItem.vue +153 -0
  21. package/dist/runtime/app/components/DashboardItem.vue.d.ts +26 -0
  22. package/dist/runtime/app/components/EmptyCard.d.vue.ts +18 -0
  23. package/dist/runtime/app/components/EmptyCard.vue +25 -0
  24. package/dist/runtime/app/components/EmptyCard.vue.d.ts +18 -0
  25. package/dist/runtime/app/composables/useDashboard.d.ts +46 -0
  26. package/dist/runtime/app/composables/useDashboard.js +59 -0
  27. package/dist/runtime/app/composables/useDashboardItem.d.ts +34 -0
  28. package/dist/runtime/app/composables/useDashboardItem.js +28 -0
  29. package/dist/runtime/app/composables/useDashboardLayout.d.ts +24 -0
  30. package/dist/runtime/app/composables/useDashboardLayout.js +67 -0
  31. package/dist/runtime/app/composables/useDashboardPlatform.d.ts +10 -0
  32. package/dist/runtime/app/composables/useDashboardPlatform.js +17 -0
  33. package/dist/runtime/app/composables/useSavedDashboardLayouts.d.ts +20 -0
  34. package/dist/runtime/app/composables/useSavedDashboardLayouts.js +35 -0
  35. package/dist/runtime/app/pages/home.d.vue.ts +3 -0
  36. package/dist/runtime/app/pages/home.vue +16 -0
  37. package/dist/runtime/app/pages/home.vue.d.ts +3 -0
  38. package/dist/runtime/app/utils/components.d.ts +8 -0
  39. package/dist/runtime/app/utils/components.js +19 -0
  40. package/dist/runtime/app/utils/span.d.ts +1 -0
  41. package/dist/runtime/app/utils/span.js +10 -0
  42. package/dist/runtime/i18n/lang/da.json +49 -0
  43. package/dist/runtime/i18n/lang/en.json +49 -0
  44. package/dist/runtime/preferences.d.ts +37 -0
  45. package/dist/runtime/preferences.js +25 -0
  46. package/dist/runtime/server/layouts.d.ts +9 -0
  47. package/dist/runtime/server/layouts.js +25 -0
  48. package/dist/runtime/server/routes/me/layouts.get.d.ts +2 -0
  49. package/dist/runtime/server/routes/me/layouts.get.js +6 -0
  50. package/dist/runtime/server/routes/me/layouts.patch.d.ts +2 -0
  51. package/dist/runtime/server/routes/me/layouts.patch.js +10 -0
  52. package/dist/runtime/shared/home-tab.d.ts +31 -0
  53. package/dist/runtime/shared/home-tab.js +21 -0
  54. package/dist/runtime/shared/layout.d.ts +62 -0
  55. package/dist/runtime/shared/layout.js +110 -0
  56. package/dist/runtime/shared/registry.d.ts +11 -0
  57. package/dist/runtime/shared/registry.js +15 -0
  58. package/dist/runtime/shared/stored-layouts.d.ts +12 -0
  59. package/dist/runtime/shared/stored-layouts.js +16 -0
  60. package/dist/runtime/utils/api.d.ts +11 -0
  61. package/dist/runtime/utils/api.js +11 -0
  62. package/dist/runtime/utils/dashboard.d.ts +22 -0
  63. package/dist/runtime/utils/dashboard.js +32 -0
  64. package/dist/runtime/utils/index.d.ts +4 -0
  65. package/dist/runtime/utils/index.js +18 -0
  66. package/dist/types.d.mts +3 -0
  67. package/package.json +110 -3
@@ -0,0 +1,15 @@
1
+ declare const __VLS_template: () => {
2
+ __VLS_Slots: {
3
+ default?: (props: {}) => any;
4
+ };
5
+ };
6
+ type __VLS_Slots = ReturnType<typeof __VLS_template>['__VLS_Slots'];
7
+ declare const __VLS_base: import("vue").DefineComponent<{}, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<{}> & Readonly<{}>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
8
+ declare const __VLS_export: __VLS_WithSlots<typeof __VLS_base, __VLS_Slots>;
9
+ declare const _default: typeof __VLS_export;
10
+ export default _default;
11
+ type __VLS_WithSlots<T, S> = T & {
12
+ new (): {
13
+ $slots: S;
14
+ };
15
+ };
@@ -0,0 +1,9 @@
1
+ <script setup>
2
+
3
+ </script>
4
+
5
+ <template>
6
+ <div class="grid grid-cols-1 items-start gap-4 md:grid-cols-2 xl:grid-cols-4">
7
+ <slot />
8
+ </div>
9
+ </template>
@@ -0,0 +1,15 @@
1
+ declare const __VLS_template: () => {
2
+ __VLS_Slots: {
3
+ default?: (props: {}) => any;
4
+ };
5
+ };
6
+ type __VLS_Slots = ReturnType<typeof __VLS_template>['__VLS_Slots'];
7
+ declare const __VLS_base: import("vue").DefineComponent<{}, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<{}> & Readonly<{}>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
8
+ declare const __VLS_export: __VLS_WithSlots<typeof __VLS_base, __VLS_Slots>;
9
+ declare const _default: typeof __VLS_export;
10
+ export default _default;
11
+ type __VLS_WithSlots<T, S> = T & {
12
+ new (): {
13
+ $slots: S;
14
+ };
15
+ };
@@ -0,0 +1,26 @@
1
+ import type { DashboardEntry } from '../composables/useDashboard.js';
2
+ /**
3
+ * The frame of one dashboard item: its title with a collapse toggle, the item's component, a skeleton while the item
4
+ * reports `loading` and an error with a retry. Provides `useDashboardItem()` to the component. In `editing` mode it
5
+ * shows the buttons to move and hide the item (the dashboard handles the events).
6
+ */
7
+ type __VLS_Props = {
8
+ item: DashboardEntry;
9
+ context?: Record<string, unknown>;
10
+ editing?: boolean;
11
+ /** Disables the edit buttons, while a layout change is being saved. */
12
+ disabled?: boolean;
13
+ first?: boolean;
14
+ last?: boolean;
15
+ };
16
+ declare const __VLS_export: import("vue").DefineComponent<__VLS_Props, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {
17
+ hide: () => any;
18
+ moveUp: () => any;
19
+ moveDown: () => any;
20
+ }, string, import("vue").PublicProps, Readonly<__VLS_Props> & Readonly<{
21
+ onHide?: (() => any) | undefined;
22
+ onMoveUp?: (() => any) | undefined;
23
+ onMoveDown?: (() => any) | undefined;
24
+ }>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
25
+ declare const _default: typeof __VLS_export;
26
+ export default _default;
@@ -0,0 +1,153 @@
1
+ <script setup>
2
+ import { computed, onErrorCaptured, provide, ref } from "vue";
3
+ import { errorMessage, surfaceClass, useModuleT } from "#imports";
4
+ import { dashboardItemKey } from "../composables/useDashboardItem";
5
+ import { useDashboardLayout } from "../composables/useDashboardLayout";
6
+ import { setItemSettings, toggleCollapsed } from "../../shared/layout";
7
+ import { getDashboardComponent } from "../utils/components";
8
+ import { dashboardSpanClass } from "../utils/span";
9
+ const props = defineProps({
10
+ item: { type: Object, required: true },
11
+ context: { type: Object, required: false },
12
+ editing: { type: Boolean, required: false },
13
+ disabled: { type: Boolean, required: false },
14
+ first: { type: Boolean, required: false },
15
+ last: { type: Boolean, required: false }
16
+ });
17
+ const emit = defineEmits(["moveUp", "moveDown", "hide"]);
18
+ const t = useModuleT("dashboard");
19
+ const { layout, saving, update } = useDashboardLayout(props.item.dashboard);
20
+ const component = computed(() => getDashboardComponent(props.item.dashboard, props.item.id));
21
+ const label = computed(() => props.item.title ?? props.item.id);
22
+ const loading = ref(false);
23
+ const error = ref(null);
24
+ const renderKey = ref(0);
25
+ let refreshHandler;
26
+ const collapsed = computed(() => layout.value.collapsed.includes(props.item.id));
27
+ const itemContext = {
28
+ dashboard: props.item.dashboard,
29
+ id: props.item.id,
30
+ loading,
31
+ error,
32
+ collapsed,
33
+ settings: computed(() => layout.value.settings?.[props.item.id] ?? {}),
34
+ setSettings: (value) => update((current) => setItemSettings(current, props.item.id, value)),
35
+ async refresh() {
36
+ error.value = null;
37
+ if (refreshHandler) await refreshHandler();
38
+ else renderKey.value++;
39
+ },
40
+ onRefresh(handler) {
41
+ refreshHandler = handler;
42
+ }
43
+ };
44
+ provide(dashboardItemKey, itemContext);
45
+ onErrorCaptured((cause) => {
46
+ error.value = cause;
47
+ return false;
48
+ });
49
+ const toggle = () => update((current) => toggleCollapsed(current, props.item.id));
50
+ </script>
51
+
52
+ <template>
53
+ <div
54
+ :class="['flex min-w-0 flex-col gap-2', dashboardSpanClass(item.span)]"
55
+ :data-dashboard-item="item.id"
56
+ >
57
+ <div
58
+ v-if="item.title || editing"
59
+ class="flex min-h-7 items-center justify-between gap-2"
60
+ >
61
+ <button
62
+ v-if="item.title"
63
+ type="button"
64
+ class="text-highlighted flex min-w-0 items-center gap-1 text-sm font-semibold"
65
+ :aria-expanded="!collapsed"
66
+ :disabled="saving"
67
+ :aria-label="collapsed ? t('item.expand', 'Expand {title}', { title: label }) : t('item.collapse', 'Collapse {title}', { title: label })"
68
+ @click="toggle"
69
+ >
70
+ <UIcon
71
+ :name="collapsed ? 'i-lucide-chevron-right' : 'i-lucide-chevron-down'"
72
+ class="text-muted size-4 shrink-0"
73
+ />
74
+ <span class="truncate">{{ item.title }}</span>
75
+ </button>
76
+ <span v-else />
77
+ <div
78
+ v-if="editing"
79
+ class="flex shrink-0 items-center gap-1"
80
+ >
81
+ <UButton
82
+ icon="i-lucide-arrow-up"
83
+ size="xs"
84
+ color="neutral"
85
+ variant="ghost"
86
+ :disabled="first || disabled"
87
+ :aria-label="t('item.moveUp', 'Move {title} up', { title: label })"
88
+ @click="emit('moveUp')"
89
+ />
90
+ <UButton
91
+ icon="i-lucide-arrow-down"
92
+ size="xs"
93
+ color="neutral"
94
+ variant="ghost"
95
+ :disabled="last || disabled"
96
+ :aria-label="t('item.moveDown', 'Move {title} down', { title: label })"
97
+ @click="emit('moveDown')"
98
+ />
99
+ <UButton
100
+ icon="i-lucide-eye-off"
101
+ size="xs"
102
+ color="neutral"
103
+ variant="ghost"
104
+ :disabled="disabled"
105
+ :aria-label="t('item.hide', 'Hide {title}', { title: label })"
106
+ @click="emit('hide')"
107
+ />
108
+ </div>
109
+ </div>
110
+
111
+ <template v-if="!collapsed">
112
+ <div
113
+ v-if="error"
114
+ :class="[surfaceClass, 'flex flex-col items-start gap-2 p-4']"
115
+ role="alert"
116
+ >
117
+ <p class="text-highlighted text-sm font-medium">
118
+ {{ t("item.error", "{title} could not be loaded.", { title: label }) }}
119
+ </p>
120
+ <p class="text-muted text-xs">
121
+ {{ errorMessage(error, t("item.errorHint", "Something went wrong. Try again in a moment.")) }}
122
+ </p>
123
+ <UButton
124
+ size="xs"
125
+ color="neutral"
126
+ variant="outline"
127
+ icon="i-lucide-rotate-cw"
128
+ :label="t('item.retry', 'Try again')"
129
+ @click="itemContext.refresh()"
130
+ />
131
+ </div>
132
+ <div
133
+ v-if="!error && loading"
134
+ :class="[surfaceClass, 'space-y-3 p-4']"
135
+ data-dashboard-skeleton
136
+ >
137
+ <USkeleton class="h-4 w-1/3" />
138
+ <USkeleton class="h-5 w-2/3" />
139
+ <USkeleton class="h-4 w-1/2" />
140
+ </div>
141
+ <div
142
+ v-show="!error && !loading"
143
+ class="h-full"
144
+ >
145
+ <component
146
+ :is="component"
147
+ :key="renderKey"
148
+ v-bind="{ ...item.props, ...context }"
149
+ />
150
+ </div>
151
+ </template>
152
+ </div>
153
+ </template>
@@ -0,0 +1,26 @@
1
+ import type { DashboardEntry } from '../composables/useDashboard.js';
2
+ /**
3
+ * The frame of one dashboard item: its title with a collapse toggle, the item's component, a skeleton while the item
4
+ * reports `loading` and an error with a retry. Provides `useDashboardItem()` to the component. In `editing` mode it
5
+ * shows the buttons to move and hide the item (the dashboard handles the events).
6
+ */
7
+ type __VLS_Props = {
8
+ item: DashboardEntry;
9
+ context?: Record<string, unknown>;
10
+ editing?: boolean;
11
+ /** Disables the edit buttons, while a layout change is being saved. */
12
+ disabled?: boolean;
13
+ first?: boolean;
14
+ last?: boolean;
15
+ };
16
+ declare const __VLS_export: import("vue").DefineComponent<__VLS_Props, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {
17
+ hide: () => any;
18
+ moveUp: () => any;
19
+ moveDown: () => any;
20
+ }, string, import("vue").PublicProps, Readonly<__VLS_Props> & Readonly<{
21
+ onHide?: (() => any) | undefined;
22
+ onMoveUp?: (() => any) | undefined;
23
+ onMoveDown?: (() => any) | undefined;
24
+ }>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
25
+ declare const _default: typeof __VLS_export;
26
+ export default _default;
@@ -0,0 +1,18 @@
1
+ /**
2
+ * An empty state in a card (`UEmpty`) with exactly one action: a link to `to` or the `action` event. Without
3
+ * `actionLabel` it has none, for a state the viewer cannot change.
4
+ */
5
+ type __VLS_Props = {
6
+ title: string;
7
+ description?: string;
8
+ icon?: string;
9
+ actionLabel?: string;
10
+ to?: string;
11
+ };
12
+ declare const __VLS_export: import("vue").DefineComponent<__VLS_Props, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {
13
+ action: () => any;
14
+ }, string, import("vue").PublicProps, Readonly<__VLS_Props> & Readonly<{
15
+ onAction?: (() => any) | undefined;
16
+ }>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
17
+ declare const _default: typeof __VLS_export;
18
+ export default _default;
@@ -0,0 +1,25 @@
1
+ <script setup>
2
+ import { computed } from "vue";
3
+ import { surfaceClass } from "#imports";
4
+ const props = defineProps({
5
+ title: { type: String, required: true },
6
+ description: { type: String, required: false },
7
+ icon: { type: String, required: false },
8
+ actionLabel: { type: String, required: false },
9
+ to: { type: String, required: false }
10
+ });
11
+ const emit = defineEmits(["action"]);
12
+ const actions = computed(() => props.actionLabel ? [{ label: props.actionLabel, to: props.to, color: "neutral", variant: "outline", onClick: () => emit("action") }] : void 0);
13
+ </script>
14
+
15
+ <template>
16
+ <div :class="[surfaceClass, 'h-full']">
17
+ <UEmpty
18
+ :icon="icon"
19
+ :title="title"
20
+ :description="description"
21
+ :actions="actions"
22
+ variant="naked"
23
+ />
24
+ </div>
25
+ </template>
@@ -0,0 +1,18 @@
1
+ /**
2
+ * An empty state in a card (`UEmpty`) with exactly one action: a link to `to` or the `action` event. Without
3
+ * `actionLabel` it has none, for a state the viewer cannot change.
4
+ */
5
+ type __VLS_Props = {
6
+ title: string;
7
+ description?: string;
8
+ icon?: string;
9
+ actionLabel?: string;
10
+ to?: string;
11
+ };
12
+ declare const __VLS_export: import("vue").DefineComponent<__VLS_Props, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {
13
+ action: () => any;
14
+ }, string, import("vue").PublicProps, Readonly<__VLS_Props> & Readonly<{
15
+ onAction?: (() => any) | undefined;
16
+ }>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
17
+ declare const _default: typeof __VLS_export;
18
+ export default _default;
@@ -0,0 +1,46 @@
1
+ import { type ComputedRef, type MaybeRefOrGetter, type Ref } from 'vue';
2
+ import { useWidgets } from '#imports';
3
+ import type { WidgetMeta } from '@frigear-nu/widget';
4
+ import type { DashboardLayout, DashboardPlatform, DashboardRegistryEntry } from '../../../types.js';
5
+ /** A resolved item: its widget data, its dashboard data and its title translated. */
6
+ export type DashboardEntry = WidgetMeta & Omit<DashboardRegistryEntry, 'id'> & {
7
+ /** The dashboard's name. */
8
+ dashboard: string;
9
+ /** Whether the user collapsed it. */
10
+ collapsed: boolean;
11
+ };
12
+ export interface UseDashboard {
13
+ /** The items to render: allowed for the viewer, on this platform, not hidden, in the user's order. */
14
+ items: ComputedRef<DashboardEntry[]>;
15
+ /** The items the user hid, in their order. */
16
+ hidden: ComputedRef<DashboardEntry[]>;
17
+ /** Ids of the collapsed items. */
18
+ collapsed: ComputedRef<string[]>;
19
+ /** The user's layout of this dashboard. */
20
+ layout: ComputedRef<DashboardLayout>;
21
+ /** Whether layout changes are saved for the user, not only kept for this session. */
22
+ persisted: ComputedRef<boolean>;
23
+ /** Whether a layout change is being saved: disable the edit buttons meanwhile. */
24
+ saving: Ref<boolean>;
25
+ platform: Ref<DashboardPlatform>;
26
+ /** Moves an item to `toIndex` among `items`. */
27
+ move: (id: string, toIndex: number) => Promise<void>;
28
+ hide: (id: string) => Promise<void>;
29
+ show: (id: string) => Promise<void>;
30
+ toggleCollapsed: (id: string) => Promise<void>;
31
+ /** Back to the registered default. */
32
+ reset: () => Promise<void>;
33
+ status: ReturnType<typeof useWidgets>['status'];
34
+ error: ReturnType<typeof useWidgets>['error'];
35
+ refresh: () => Promise<void>;
36
+ }
37
+ /**
38
+ * A dashboard's items, resolved for the current viewer: filtered by `permission` (the `widgets:filter` hook of
39
+ * `@frigear-nu/widget`), by platform, and laid out in the user's order without the hidden ones. Plus the edits of the
40
+ * layout. Await it on a page so the items render on the server.
41
+ *
42
+ * ```ts
43
+ * const { items, hidden, move, hide, show, reset } = await useDashboard('crew.home')
44
+ * ```
45
+ */
46
+ export declare function useDashboard(name: string, context?: MaybeRefOrGetter<Record<string, unknown> | undefined>): UseDashboard & Promise<UseDashboard>;
@@ -0,0 +1,59 @@
1
+ import { computed } from "vue";
2
+ import { useNuxtApp, useWidgets } from "#imports";
3
+ import { dashboards } from "#dashboard/registry";
4
+ import { applyLayout, filterByPlatform, hideItem, moveItem, resetLayout, showItem, toggleCollapsed } from "../../shared/layout.js";
5
+ import { dashboardArea } from "../../shared/registry.js";
6
+ import { getDashboardItemIds } from "../utils/components.js";
7
+ import { useDashboardLayout } from "./useDashboardLayout.js";
8
+ import { useDashboardPlatform } from "./useDashboardPlatform.js";
9
+ export function useDashboard(name, context) {
10
+ const nuxtApp = useNuxtApp();
11
+ const i18n = nuxtApp.$i18n;
12
+ const translate = (key, fallback) => {
13
+ if (!key || !i18n) return fallback;
14
+ const text = i18n.t(key);
15
+ return text && text !== key ? text : fallback;
16
+ };
17
+ const platform = useDashboardPlatform();
18
+ const registry = dashboards[name] ?? {};
19
+ const widgets = useWidgets(dashboardArea(name), context);
20
+ const { layout, persisted, ready, saving, update } = useDashboardLayout(name);
21
+ const available = computed(() => {
22
+ const collapsed = new Set(layout.value.collapsed);
23
+ const joined = (widgets.data.value ?? []).flatMap((widget) => {
24
+ const entry = registry[widget.id];
25
+ if (!entry) return [];
26
+ const { id: _id, ...extra } = entry;
27
+ return [{
28
+ ...widget,
29
+ ...extra,
30
+ title: translate(entry.titleKey, entry.title),
31
+ dashboard: name,
32
+ collapsed: collapsed.has(widget.id)
33
+ }];
34
+ });
35
+ return filterByPlatform(joined, platform.value);
36
+ });
37
+ const applied = computed(() => applyLayout(available.value, layout.value, getDashboardItemIds(name)));
38
+ const items = computed(() => applied.value.items);
39
+ const result = {
40
+ items,
41
+ hidden: computed(() => applied.value.hidden),
42
+ collapsed: computed(() => applied.value.collapsed),
43
+ layout,
44
+ persisted,
45
+ saving,
46
+ platform,
47
+ // Within the full order of every item, so items of the other platform (or another viewer) keep their place.
48
+ move: (id, toIndex) => update((current) => moveItem(current, getDashboardItemIds(name), items.value.map((item) => item.id), id, toIndex)),
49
+ hide: (id) => update((current) => hideItem(current, id)),
50
+ show: (id) => update((current) => showItem(current, id)),
51
+ toggleCollapsed: (id) => update((current) => toggleCollapsed(current, id)),
52
+ reset: () => update(() => resetLayout()),
53
+ status: widgets.status,
54
+ error: widgets.error,
55
+ refresh: () => widgets.refresh()
56
+ };
57
+ const promise = Promise.all([widgets, ready]).then(() => result);
58
+ return Object.assign(promise, result);
59
+ }
@@ -0,0 +1,34 @@
1
+ import { type ComputedRef, type InjectionKey, type Ref } from 'vue';
2
+ export interface DashboardItemContext {
3
+ /** The dashboard's name. */
4
+ dashboard: string;
5
+ /** The item's id. */
6
+ id: string;
7
+ /** Set by the item while it loads: the frame shows a skeleton instead of an empty card. */
8
+ loading: Ref<boolean>;
9
+ /** Set by the item when its data failed: the frame shows the error with a retry button. */
10
+ error: Ref<unknown>;
11
+ /** Whether the user collapsed the item. A collapsed item is not rendered. */
12
+ collapsed: ComputedRef<boolean>;
13
+ /** The user's settings of this item (stored in their layout). */
14
+ settings: ComputedRef<Record<string, unknown>>;
15
+ /** Replaces the user's settings of this item; an empty object clears them. */
16
+ setSettings: (value: Record<string, unknown>) => Promise<void>;
17
+ /** Reloads the item: clears `error` and calls the handler registered with `onRefresh`. */
18
+ refresh: () => Promise<void>;
19
+ /** Registers what `refresh()` (and the retry button) calls, such as the `refresh` of the item's `useFetch`. */
20
+ onRefresh: (handler: () => unknown) => void;
21
+ }
22
+ export declare const dashboardItemKey: InjectionKey<DashboardItemContext>;
23
+ /**
24
+ * The context of the dashboard item being rendered: report `loading` and `error`, register a `refresh`, and read or
25
+ * change the user's `settings` of the item.
26
+ *
27
+ * ```ts
28
+ * const { loading, error, onRefresh } = useDashboardItem()
29
+ * const { data, status, error: fetchError, refresh } = await useFetch('/api/shifts/me/next')
30
+ * watchEffect(() => { loading.value = status.value === 'pending'; error.value = fetchError.value })
31
+ * onRefresh(refresh)
32
+ * ```
33
+ */
34
+ export declare function useDashboardItem(): DashboardItemContext;
@@ -0,0 +1,28 @@
1
+ import { computed, inject, ref } from "vue";
2
+ export const dashboardItemKey = Symbol("dashboard-item");
3
+ function standalone() {
4
+ let handler;
5
+ const error = ref(null);
6
+ const settings = ref({});
7
+ return {
8
+ dashboard: "",
9
+ id: "",
10
+ loading: ref(false),
11
+ error,
12
+ collapsed: computed(() => false),
13
+ settings: computed(() => settings.value),
14
+ setSettings: async (value) => {
15
+ settings.value = { ...value };
16
+ },
17
+ refresh: async () => {
18
+ error.value = null;
19
+ await handler?.();
20
+ },
21
+ onRefresh: (fn) => {
22
+ handler = fn;
23
+ }
24
+ };
25
+ }
26
+ export function useDashboardItem() {
27
+ return inject(dashboardItemKey, null) ?? standalone();
28
+ }
@@ -0,0 +1,24 @@
1
+ import { type ComputedRef, type Ref } from 'vue';
2
+ import type { DashboardLayout } from '../../../types.js';
3
+ export interface DashboardLayoutState {
4
+ /** The user's layout of this dashboard: this session's changes, else the saved preference, else the default. */
5
+ layout: ComputedRef<DashboardLayout>;
6
+ /** Whether changes are saved for the user (Preferences, a cookie session, signed in), not only for this session. */
7
+ persisted: ComputedRef<boolean>;
8
+ /** Resolves once the saved layouts are loaded (at once when they are not saved). */
9
+ ready: Promise<void>;
10
+ /** Whether a save is in flight. */
11
+ saving: Ref<boolean>;
12
+ /**
13
+ * Applies a change (ids that are not items of the dashboard are dropped), then saves it for the user when it can. A
14
+ * failed save shows an error toast and rolls the change back.
15
+ */
16
+ update: (change: (layout: DashboardLayout) => DashboardLayout) => Promise<void>;
17
+ }
18
+ /**
19
+ * The per-user layout of one dashboard. With `@frigear-nu/preferences`, Auth in server mode and a signed-in user it is
20
+ * the `dashboardLayouts` preference, read and written through `/api/dashboard/me/layouts` (it is `managed`, so never in
21
+ * the session cookie); otherwise it lives in `useState` for the session (keyed by user) and the registered default
22
+ * applies again on reload.
23
+ */
24
+ export declare function useDashboardLayout(name: string): DashboardLayoutState;
@@ -0,0 +1,67 @@
1
+ import { computed, watch } from "vue";
2
+ import { useErrorToast, useModuleT, useNuxtApp, useState } from "#imports";
3
+ import { useSavedDashboardLayouts, useUserSession } from "#dashboard/optional";
4
+ import { normalizeLayout, pruneLayout } from "../../shared/layout.js";
5
+ import { getDashboardItemIds } from "../utils/components.js";
6
+ const writers = /* @__PURE__ */ new WeakMap();
7
+ export function useDashboardLayout(name) {
8
+ const nuxtApp = useNuxtApp();
9
+ const t = useModuleT("dashboard");
10
+ const showError = useErrorToast();
11
+ const state = useState("dashboard:layouts", () => ({ owner: "anon", layouts: {}, confirmed: {} }));
12
+ const saving = useState("dashboard:saving", () => false);
13
+ const store = useSavedDashboardLayouts?.();
14
+ const userSession = useUserSession?.();
15
+ const owner = computed(() => {
16
+ const id = userSession?.loggedIn.value ? userSession.user.value?.id : void 0;
17
+ return id === void 0 || id === null ? "anon" : `user:${id}`;
18
+ });
19
+ const session = computed(() => state.value.owner === owner.value ? state.value.layouts : {});
20
+ const persisted = computed(() => !!store && !!userSession?.loggedIn.value);
21
+ const saved = computed(() => persisted.value ? store.layouts(owner.value) : {});
22
+ const layout = computed(() => normalizeLayout(session.value[name] ?? saved.value[name]));
23
+ const load = () => persisted.value ? store.load(owner.value) : Promise.resolve();
24
+ const ready = load();
25
+ watch(owner, () => {
26
+ void load();
27
+ });
28
+ const own = () => state.value.owner === owner.value ? state.value : { owner: owner.value, layouts: {}, confirmed: {} };
29
+ async function save() {
30
+ let writer = writers.get(nuxtApp);
31
+ if (!writer) writers.set(nuxtApp, writer = { running: null, dirty: false });
32
+ writer.dirty = true;
33
+ if (writer.running) return writer.running;
34
+ const current = writer;
35
+ current.running = (async () => {
36
+ saving.value = true;
37
+ try {
38
+ while (current.dirty) {
39
+ current.dirty = false;
40
+ const sent = { ...own().layouts };
41
+ try {
42
+ await store.save(owner.value, sent);
43
+ state.value = { ...own(), confirmed: sent };
44
+ } catch (error) {
45
+ current.dirty = false;
46
+ const base = own();
47
+ state.value = { ...base, layouts: { ...base.confirmed } };
48
+ showError(error, t("save.error", "Your dashboard layout could not be saved."));
49
+ return;
50
+ }
51
+ }
52
+ } finally {
53
+ saving.value = false;
54
+ current.running = null;
55
+ }
56
+ })();
57
+ return current.running;
58
+ }
59
+ async function update(change) {
60
+ const next = pruneLayout(change(layout.value), getDashboardItemIds(name));
61
+ const base = own();
62
+ const layouts = { ...base.layouts, [name]: next };
63
+ state.value = { ...base, layouts, confirmed: persisted.value ? base.confirmed : layouts };
64
+ if (persisted.value) await save();
65
+ }
66
+ return { layout, persisted, ready, saving, update };
67
+ }
@@ -0,0 +1,10 @@
1
+ import type { Ref } from 'vue';
2
+ import type { DashboardPlatform } from '../../../types.js';
3
+ /**
4
+ * `mobile` inside the native shell of `@frigear-nu/mobile` (`useCapacitor().isNative`), `web` everywhere else,
5
+ * including the server and a browser without mobile installed.
6
+ *
7
+ * The server always renders `web`. When the page was server-rendered (mobile with `spa: false`), the client switches
8
+ * to `mobile` only once hydration is done, so the first client render matches the server's HTML.
9
+ */
10
+ export declare function useDashboardPlatform(): Ref<DashboardPlatform>;
@@ -0,0 +1,17 @@
1
+ import { useNuxtApp, useState } from "#imports";
2
+ import { useCapacitor } from "#dashboard/optional";
3
+ export function useDashboardPlatform() {
4
+ const nuxtApp = useNuxtApp();
5
+ const platform = useState("dashboard:platform", () => "web");
6
+ if (import.meta.client && useCapacitor) {
7
+ const detected = useCapacitor().isNative ? "mobile" : "web";
8
+ if (platform.value !== detected) {
9
+ const apply = () => {
10
+ platform.value = detected;
11
+ };
12
+ if (nuxtApp.isHydrating) nuxtApp.hooks.hookOnce("app:suspense:resolve", apply);
13
+ else apply();
14
+ }
15
+ }
16
+ return platform;
17
+ }
@@ -0,0 +1,20 @@
1
+ import { type ComputedRef } from 'vue';
2
+ import type { DashboardLayout } from '../../../types.js';
3
+ type Layouts = Record<string, DashboardLayout>;
4
+ export interface SavedDashboardLayouts {
5
+ /** The saved layouts of `owner`, by dashboard name (empty while they load or for another owner). */
6
+ layouts: (owner: string) => Layouts;
7
+ /** Loads the saved layouts of `owner` once (on the server too, with the request's cookie). A failure leaves none. */
8
+ load: (owner: string) => Promise<void>;
9
+ /** Saves the named layouts (`null` removes one) and keeps the answer as the saved layouts. Throws on failure. */
10
+ save: (owner: string, change: Record<string, DashboardLayout | null>) => Promise<void>;
11
+ /** The last loaded owner. */
12
+ owner: ComputedRef<string | null>;
13
+ }
14
+ /**
15
+ * The signed-in user's saved dashboard layouts, from `GET`/`PATCH /api/dashboard/me/layouts` (the `managed`
16
+ * `dashboardLayouts` preference, which is never in the session cookie). Used by `useDashboardLayout`; only available
17
+ * with `@frigear-nu/preferences` and Auth in server mode.
18
+ */
19
+ export declare function useSavedDashboardLayouts(): SavedDashboardLayouts;
20
+ export {};