@shellui/core 0.3.1 → 0.4.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 (56) hide show
  1. package/package.json +2 -2
  2. package/src/app.tsx +11 -1
  3. package/src/components/ui/dialog.tsx +26 -6
  4. package/src/components/ui/sonner.tsx +9 -5
  5. package/src/features/admin/AdminView.tsx +1 -20
  6. package/src/features/auth/AuthProvider.tsx +68 -13
  7. package/src/features/auth/backends/shellui.ts +8 -1
  8. package/src/features/auth/components/AccessPendingView.tsx +112 -0
  9. package/src/features/auth/components/LoginView.tsx +17 -0
  10. package/src/features/auth/components/OAuthCallbackView.tsx +211 -48
  11. package/src/features/auth/hooks/useAuth.tsx +8 -1
  12. package/src/features/auth/utils/authRequestError.spec.ts +52 -0
  13. package/src/features/auth/utils/authRequestError.ts +45 -0
  14. package/src/features/auth/utils/index.ts +7 -0
  15. package/src/features/auth/utils/isTokenAutoRefreshDisabled.spec.ts +71 -0
  16. package/src/features/auth/utils/isTokenAutoRefreshDisabled.ts +18 -0
  17. package/src/features/config/types.ts +46 -0
  18. package/src/features/layouts/AppLayout.tsx +3 -3
  19. package/src/features/layouts/OverlayShell.tsx +10 -6
  20. package/src/features/settings/SettingsIcons.tsx +35 -0
  21. package/src/features/settings/SettingsProvider.tsx +69 -4
  22. package/src/features/settings/SettingsRoutes.tsx +14 -6
  23. package/src/features/settings/SettingsView.tsx +11 -5
  24. package/src/features/settings/components/Advanced.tsx +117 -44
  25. package/src/features/settings/components/Develop.tsx +38 -0
  26. package/src/features/settings/components/Storage.tsx +138 -0
  27. package/src/features/settings/components/develop/StoragePickerTestButtons.tsx +148 -0
  28. package/src/features/settings/components/develop/UploadToastTestButton.tsx +29 -0
  29. package/src/features/settings/utils/buildSettingsForPropagation.spec.ts +54 -0
  30. package/src/features/settings/utils/buildSettingsForPropagation.ts +34 -0
  31. package/src/features/settings/utils/toSettingsUser.spec.ts +4 -0
  32. package/src/features/settings/utils/toSettingsUser.ts +2 -0
  33. package/src/features/storage/StorageBridge.tsx +50 -0
  34. package/src/features/storage/StoragePickerContext.tsx +214 -0
  35. package/src/features/storage/api.spec.ts +303 -0
  36. package/src/features/storage/api.ts +414 -0
  37. package/src/features/storage/handleRequest.spec.ts +88 -0
  38. package/src/features/storage/handleRequest.ts +53 -0
  39. package/src/features/storage/quota.spec.ts +151 -0
  40. package/src/features/storage/quota.ts +104 -0
  41. package/src/features/storage/uploads/UploadToastCard.tsx +442 -0
  42. package/src/features/storage/uploads/UploadToaster.tsx +41 -0
  43. package/src/features/storage/uploads/handleTrackedUpload.spec.ts +170 -0
  44. package/src/features/storage/uploads/handleTrackedUpload.ts +97 -0
  45. package/src/features/storage/uploads/types.ts +24 -0
  46. package/src/features/storage/uploads/uploadQueue.spec.ts +118 -0
  47. package/src/features/storage/uploads/uploadQueue.ts +349 -0
  48. package/src/features/storage/uploads/useUploadQueue.ts +30 -0
  49. package/src/i18n/translations/en/common.json +33 -0
  50. package/src/i18n/translations/en/settings.json +47 -4
  51. package/src/i18n/translations/fr/common.json +33 -0
  52. package/src/i18n/translations/fr/settings.json +47 -4
  53. package/src/index.css +2 -1
  54. package/src/index.ts +2 -0
  55. package/src/lib/z-index.ts +9 -3
  56. package/src/service-worker/register.ts +4 -12
@@ -0,0 +1,138 @@
1
+ import { useCallback, useEffect, useState } from 'react';
2
+ import { useTranslation } from 'react-i18next';
3
+ import { Button } from '../../../components/ui/button';
4
+ import { useAuth } from '../../auth/hooks/useAuth';
5
+ import { useConfig } from '../../config/useConfig';
6
+ import {
7
+ fetchStorageQuota,
8
+ formatBytes,
9
+ getStorageBaseUrl,
10
+ getUsagePercent,
11
+ type StorageQuotaBucket,
12
+ type StorageQuotaSnapshot,
13
+ } from '../../storage/quota';
14
+
15
+ const QuotaMeter = ({
16
+ label,
17
+ description,
18
+ bucket,
19
+ }: {
20
+ label: string;
21
+ description?: string;
22
+ bucket: StorageQuotaBucket;
23
+ }) => {
24
+ const { t, i18n } = useTranslation('settings');
25
+ const locale = i18n.language || 'en';
26
+ const percent = getUsagePercent(bucket.usedBytes, bucket.maxBytes);
27
+ const used = formatBytes(bucket.usedBytes, locale);
28
+ const max = bucket.maxBytes !== null ? formatBytes(bucket.maxBytes, locale) : null;
29
+ const barTone = percent !== null && percent >= 100 ? 'bg-destructive' : 'bg-primary';
30
+
31
+ return (
32
+ <div className="space-y-2">
33
+ <div className="space-y-0.5">
34
+ <h3
35
+ className="text-sm font-medium leading-none"
36
+ style={{ fontFamily: 'var(--heading-font-family, inherit)' }}
37
+ >
38
+ {label}
39
+ </h3>
40
+ {description ? <p className="text-sm text-muted-foreground">{description}</p> : null}
41
+ </div>
42
+ <p className="text-sm tabular-nums">
43
+ {max ? t('storage.usedOfMax', { used, max }) : t('storage.usedOnly', { used })}
44
+ </p>
45
+ {percent !== null ? (
46
+ <div
47
+ className="h-2 w-full overflow-hidden rounded-full bg-muted"
48
+ role="progressbar"
49
+ aria-valuemin={0}
50
+ aria-valuemax={100}
51
+ aria-valuenow={Math.round(percent)}
52
+ aria-label={label}
53
+ >
54
+ <div
55
+ className={`h-full rounded-full transition-[width] ${barTone}`}
56
+ style={{ width: `${percent}%` }}
57
+ />
58
+ </div>
59
+ ) : null}
60
+ </div>
61
+ );
62
+ };
63
+
64
+ export const Storage = () => {
65
+ const { t } = useTranslation('settings');
66
+ const { config } = useConfig();
67
+ const { session, isAuthenticated } = useAuth();
68
+ const storageBaseUrl = getStorageBaseUrl(config);
69
+ const accessToken = session?.accessToken ?? null;
70
+
71
+ const [snapshot, setSnapshot] = useState<StorageQuotaSnapshot | null>(null);
72
+ const [loading, setLoading] = useState(false);
73
+ const [error, setError] = useState(false);
74
+
75
+ const load = useCallback(async () => {
76
+ if (!storageBaseUrl || !accessToken) {
77
+ setSnapshot(null);
78
+ setLoading(false);
79
+ setError(false);
80
+ return;
81
+ }
82
+ setLoading(true);
83
+ setError(false);
84
+ try {
85
+ setSnapshot(await fetchStorageQuota(storageBaseUrl, accessToken));
86
+ } catch {
87
+ setSnapshot(null);
88
+ setError(true);
89
+ } finally {
90
+ setLoading(false);
91
+ }
92
+ }, [accessToken, storageBaseUrl]);
93
+
94
+ useEffect(() => {
95
+ void load();
96
+ }, [load]);
97
+
98
+ if (!isAuthenticated || !accessToken) {
99
+ return <p className="text-sm text-muted-foreground">{t('storage.signInRequired')}</p>;
100
+ }
101
+
102
+ return (
103
+ <div className="space-y-6">
104
+ <p className="text-sm text-muted-foreground">{t('storage.description')}</p>
105
+
106
+ {loading && !snapshot ? (
107
+ <p className="text-sm text-muted-foreground">{t('storage.loading')}</p>
108
+ ) : null}
109
+
110
+ {error ? (
111
+ <div className="space-y-3">
112
+ <p className="text-sm text-destructive">{t('storage.error')}</p>
113
+ <Button
114
+ variant="outline"
115
+ size="sm"
116
+ onClick={() => void load()}
117
+ >
118
+ {t('storage.retry')}
119
+ </Button>
120
+ </div>
121
+ ) : null}
122
+
123
+ {snapshot ? (
124
+ <div className="space-y-6">
125
+ <QuotaMeter
126
+ label={t('storage.yourUsage')}
127
+ description={snapshot.user.maxBytes === null ? t('storage.noPersonalLimit') : undefined}
128
+ bucket={snapshot.user}
129
+ />
130
+ <QuotaMeter
131
+ label={t('storage.organization')}
132
+ bucket={snapshot.company}
133
+ />
134
+ </div>
135
+ ) : null}
136
+ </div>
137
+ );
138
+ };
@@ -0,0 +1,148 @@
1
+ import { useState } from 'react';
2
+ import { useTranslation } from 'react-i18next';
3
+ import { shellui, type StorageSelectedItem } from '@shellui/sdk';
4
+ import { Button } from '../../../../components/ui/button';
5
+
6
+ const FolderIcon = () => (
7
+ <svg
8
+ xmlns="http://www.w3.org/2000/svg"
9
+ width="16"
10
+ height="16"
11
+ viewBox="0 0 24 24"
12
+ fill="none"
13
+ stroke="currentColor"
14
+ strokeWidth="2"
15
+ strokeLinecap="round"
16
+ strokeLinejoin="round"
17
+ aria-hidden
18
+ >
19
+ <path d="M3 7a2 2 0 0 1 2-2h4l2 2h8a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2Z" />
20
+ </svg>
21
+ );
22
+
23
+ const FileIcon = () => (
24
+ <svg
25
+ xmlns="http://www.w3.org/2000/svg"
26
+ width="16"
27
+ height="16"
28
+ viewBox="0 0 24 24"
29
+ fill="none"
30
+ stroke="currentColor"
31
+ strokeWidth="2"
32
+ strokeLinecap="round"
33
+ strokeLinejoin="round"
34
+ aria-hidden
35
+ >
36
+ <path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8Z" />
37
+ <path d="M14 2v6h6" />
38
+ </svg>
39
+ );
40
+
41
+ const CloseIcon = () => (
42
+ <svg
43
+ xmlns="http://www.w3.org/2000/svg"
44
+ width="16"
45
+ height="16"
46
+ viewBox="0 0 24 24"
47
+ fill="none"
48
+ stroke="currentColor"
49
+ strokeWidth="2"
50
+ strokeLinecap="round"
51
+ strokeLinejoin="round"
52
+ aria-hidden
53
+ >
54
+ <path d="M18 6 6 18" />
55
+ <path d="m6 6 12 12" />
56
+ </svg>
57
+ );
58
+
59
+ export const StoragePickerTestButtons = () => {
60
+ const { t } = useTranslation('settings');
61
+ const [items, setItems] = useState<StorageSelectedItem[]>([]);
62
+
63
+ const runPicker = async (fn: () => Promise<{ items: StorageSelectedItem[] } | null>) => {
64
+ try {
65
+ const result = await fn();
66
+ if (result) setItems(result.items);
67
+ } catch (err) {
68
+ shellui.toast({
69
+ title: t('develop.testing.storagePicker.errorTitle'),
70
+ description: err instanceof Error ? err.message : t('develop.testing.storagePicker.error'),
71
+ type: 'error',
72
+ });
73
+ }
74
+ };
75
+
76
+ return (
77
+ <div>
78
+ <h4
79
+ className="text-sm font-medium mb-2"
80
+ style={{ fontFamily: 'var(--heading-font-family, inherit)' }}
81
+ >
82
+ {t('develop.testing.storagePicker.title')}
83
+ </h4>
84
+ <div className="flex flex-wrap gap-2">
85
+ <Button
86
+ variant="outline"
87
+ onClick={() => void runPicker(() => shellui.selectFolders())}
88
+ >
89
+ {t('develop.testing.storagePicker.buttons.folders')}
90
+ </Button>
91
+ <Button
92
+ variant="outline"
93
+ onClick={() => void runPicker(() => shellui.selectFolders({ multiple: true }))}
94
+ >
95
+ {t('develop.testing.storagePicker.buttons.foldersMultiple')}
96
+ </Button>
97
+ <Button
98
+ variant="outline"
99
+ onClick={() => void runPicker(() => shellui.selectFiles({ multiple: true }))}
100
+ >
101
+ {t('develop.testing.storagePicker.buttons.files')}
102
+ </Button>
103
+ <Button
104
+ variant="outline"
105
+ onClick={() =>
106
+ void runPicker(() => shellui.selectFiles({ multiple: true, folders: true }))
107
+ }
108
+ >
109
+ {t('develop.testing.storagePicker.buttons.filesAndFolders')}
110
+ </Button>
111
+ </div>
112
+ {items.length > 0 ? (
113
+ <ul className="mt-3 divide-y divide-border rounded-md border border-border">
114
+ {items.map((item) => (
115
+ <li
116
+ key={`${item.bucket}:${item.type}:${item.id}`}
117
+ className="flex items-center gap-2 px-3 py-2 text-sm"
118
+ >
119
+ <span className="shrink-0 text-muted-foreground">
120
+ {item.type === 'folder' ? <FolderIcon /> : <FileIcon />}
121
+ </span>
122
+ <span className="min-w-0 flex-1 truncate">{item.name}</span>
123
+ <button
124
+ type="button"
125
+ className="inline-flex h-7 w-7 shrink-0 items-center justify-center rounded-md text-muted-foreground hover:bg-muted hover:text-foreground"
126
+ onClick={() =>
127
+ setItems((current) =>
128
+ current.filter(
129
+ (entry) =>
130
+ !(
131
+ entry.id === item.id &&
132
+ entry.bucket === item.bucket &&
133
+ entry.type === item.type
134
+ ),
135
+ ),
136
+ )
137
+ }
138
+ aria-label={t('develop.testing.storagePicker.remove', { name: item.name })}
139
+ >
140
+ <CloseIcon />
141
+ </button>
142
+ </li>
143
+ ))}
144
+ </ul>
145
+ ) : null}
146
+ </div>
147
+ );
148
+ };
@@ -0,0 +1,29 @@
1
+ import { useTranslation } from 'react-i18next';
2
+ import { Button } from '../../../../components/ui/button';
3
+ import { requestUploadToastDemo } from '../../../storage/uploads/uploadQueue';
4
+
5
+ export const UploadToastTestButton = () => {
6
+ const { t } = useTranslation('settings');
7
+
8
+ return (
9
+ <div>
10
+ <h4
11
+ className="text-sm font-medium mb-2"
12
+ style={{ fontFamily: 'var(--heading-font-family, inherit)' }}
13
+ >
14
+ {t('develop.testing.uploadProgress.title')}
15
+ </h4>
16
+ <div className="flex flex-wrap gap-2">
17
+ <Button
18
+ onClick={() => requestUploadToastDemo()}
19
+ variant="outline"
20
+ >
21
+ {t('develop.testing.uploadProgress.buttons.simulate')}
22
+ </Button>
23
+ </div>
24
+ <p className="text-xs text-muted-foreground mt-2">
25
+ {t('develop.testing.uploadProgress.description')}
26
+ </p>
27
+ </div>
28
+ );
29
+ };
@@ -164,4 +164,58 @@ describe('buildSettingsForPropagation', () => {
164
164
  const result = buildSettingsForPropagation(baseSettings, config, 'en');
165
165
  expect(result.authBackendBaseUrl).toBeNull();
166
166
  });
167
+
168
+ it('injects localized administration navigation from config', () => {
169
+ const config = {
170
+ storage: { url: 'http://localhost:8001/', filesUrl: 'http://localhost:5175/' },
171
+ administration: {
172
+ title: { en: 'Applications', fr: 'Applications FR' },
173
+ navigation: [
174
+ {
175
+ label: { en: 'Billing', fr: 'Facturation' },
176
+ path: 'billing',
177
+ url: 'https://app.example.com/billing',
178
+ icon: '/icons/billing.svg',
179
+ },
180
+ {
181
+ label: { en: 'Django admin', fr: 'Admin Django' },
182
+ path: 'django-admin',
183
+ url: '/admin/',
184
+ requiresStaff: true,
185
+ openIn: 'external',
186
+ },
187
+ ],
188
+ },
189
+ } as ShellUIConfig;
190
+
191
+ const result = buildSettingsForPropagation(baseSettings, config, 'fr');
192
+ expect(result.administration).toEqual({
193
+ title: 'Applications FR',
194
+ navigation: [
195
+ {
196
+ path: 'billing',
197
+ url: 'https://app.example.com/billing',
198
+ label: 'Facturation',
199
+ icon: '/icons/billing.svg',
200
+ },
201
+ {
202
+ path: 'django-admin',
203
+ url: '/admin/',
204
+ label: 'Admin Django',
205
+ requiresStaff: true,
206
+ openIn: 'external',
207
+ },
208
+ ],
209
+ });
210
+ expect(result.storage).toEqual({
211
+ url: 'http://localhost:8001',
212
+ filesUrl: 'http://localhost:5175/',
213
+ });
214
+ });
215
+
216
+ it('sets administration and storage to null when not configured', () => {
217
+ const result = buildSettingsForPropagation(baseSettings, undefined, 'en');
218
+ expect(result.administration).toBeNull();
219
+ expect(result.storage).toBeNull();
220
+ });
167
221
  });
@@ -41,11 +41,45 @@ export const buildSettingsForPropagation = (
41
41
  path: item.path,
42
42
  url: item.url,
43
43
  label: resolveLabel(item.label, lang),
44
+ ...(item.icon ? { icon: item.icon } : {}),
44
45
  }),
45
46
  );
46
47
  result = { ...result, navigation: { items } };
47
48
  }
48
49
 
50
+ if (config?.administration) {
51
+ result = {
52
+ ...result,
53
+ administration: {
54
+ title: resolveLabel(config.administration.title, lang),
55
+ navigation: (config.administration.navigation ?? []).map((item) => ({
56
+ path: item.path,
57
+ url: item.url,
58
+ label: resolveLabel(item.label, lang),
59
+ ...(item.icon ? { icon: item.icon } : {}),
60
+ ...(item.requiresStaff ? { requiresStaff: true } : {}),
61
+ ...(item.openIn === 'external' ? { openIn: 'external' as const } : {}),
62
+ })),
63
+ },
64
+ };
65
+ } else {
66
+ result = { ...result, administration: null };
67
+ }
68
+
69
+ const storageUrl = config?.storage?.url?.trim().replace(/\/+$/, '') || null;
70
+ if (storageUrl) {
71
+ const filesUrl = config.storage?.filesUrl?.trim() || null;
72
+ result = {
73
+ ...result,
74
+ storage: {
75
+ url: storageUrl,
76
+ ...(filesUrl ? { filesUrl } : {}),
77
+ },
78
+ };
79
+ } else {
80
+ result = { ...result, storage: null };
81
+ }
82
+
49
83
  const authBackendBaseUrl =
50
84
  config?.backend?.type === 'shellui' && config.backend.url?.trim()
51
85
  ? config.backend.url.trim().replace(/\/+$/, '')
@@ -25,6 +25,8 @@ describe('toSettingsUser', () => {
25
25
  profilePicture: 'https://example.com/avatar.png',
26
26
  authProvider: 'github',
27
27
  groups: null,
28
+ isStaff: false,
29
+ isCompanyOwner: false,
28
30
  });
29
31
 
30
32
  const withGroups = toSettingsUser({
@@ -44,6 +46,8 @@ describe('toSettingsUser', () => {
44
46
  profilePicture: null,
45
47
  authProvider: 'github',
46
48
  groups: ['beta', 'alpha'],
49
+ isStaff: true,
50
+ isCompanyOwner: false,
47
51
  });
48
52
  });
49
53
  });
@@ -11,5 +11,7 @@ export const toSettingsUser = (user: AuthUser | null): Settings['user'] => {
11
11
  profilePicture: user.profilePicture,
12
12
  authProvider: user.authProvider,
13
13
  groups: user.groups.length ? [...user.groups] : null,
14
+ isStaff: user.isStaff,
15
+ isCompanyOwner: user.isCompanyOwner,
14
16
  };
15
17
  };
@@ -0,0 +1,50 @@
1
+ import { useEffect } from 'react';
2
+ import { shellui, type ShellUIMessage, type StorageRequestPayload } from '@shellui/sdk';
3
+ import { useAuth } from '../auth/hooks/useAuth';
4
+ import { useConfig } from '../config/useConfig';
5
+ import { handleStorageRequest } from './handleRequest';
6
+ import { getStorageBaseUrl } from './quota';
7
+ import { handleTrackedUpload, isUploadPayload } from './uploads/handleTrackedUpload';
8
+
9
+ /**
10
+ * Root-window bridge: iframe `SHELLUI_STORAGE_REQUEST` messages are handled here
11
+ * with `storage.url` and the signed-in user's access token.
12
+ */
13
+ export const StorageBridge = () => {
14
+ const { config } = useConfig();
15
+ const { session } = useAuth();
16
+ const storageUrl = getStorageBaseUrl(config);
17
+ const accessToken = session?.accessToken ?? null;
18
+
19
+ useEffect(() => {
20
+ if (typeof window === 'undefined' || window.parent !== window) {
21
+ return;
22
+ }
23
+
24
+ const listener = (message: ShellUIMessage) => {
25
+ const payload = message.payload as StorageRequestPayload | undefined;
26
+ if (!payload?.id || !payload.op) return;
27
+
28
+ const run = isUploadPayload(payload)
29
+ ? handleTrackedUpload({ storageUrl, accessToken, payload })
30
+ : handleStorageRequest({ storageUrl, accessToken, payload });
31
+
32
+ void run.then((response) => {
33
+ const reply = {
34
+ type: 'SHELLUI_STORAGE_RESPONSE' as const,
35
+ payload: response,
36
+ };
37
+ const from = message.from?.filter(Boolean) as string[] | undefined;
38
+ if (from?.length) {
39
+ shellui.sendMessage({ ...reply, to: from });
40
+ return;
41
+ }
42
+ window.postMessage(reply, '*');
43
+ });
44
+ };
45
+
46
+ return shellui.addMessageListener('SHELLUI_STORAGE_REQUEST', listener);
47
+ }, [storageUrl, accessToken]);
48
+
49
+ return null;
50
+ };