@pelicanplatform/components 1.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.
- package/LICENSE +201 -0
- package/README.md +0 -0
- package/dist/AuthenticatedClient/AuthenticatedClient.d.ts +6 -0
- package/dist/AuthenticatedClient/AuthenticatedClient.d.ts.map +1 -0
- package/dist/AuthenticatedClient/AuthenticatedClient.js +98 -0
- package/dist/AuthenticatedClient/AuthenticatedClient.js.map +1 -0
- package/dist/AuthenticatedClient/index.d.ts +3 -0
- package/dist/AuthenticatedClient/index.d.ts.map +1 -0
- package/dist/AuthenticatedClient/index.js +3 -0
- package/dist/AuthenticatedClient/index.js.map +1 -0
- package/dist/Client/Client.d.ts +12 -0
- package/dist/Client/Client.d.ts.map +1 -0
- package/dist/Client/Client.js +13 -0
- package/dist/Client/Client.js.map +1 -0
- package/dist/Client/index.d.ts +3 -0
- package/dist/Client/index.d.ts.map +1 -0
- package/dist/Client/index.js +3 -0
- package/dist/Client/index.js.map +1 -0
- package/dist/ClientMenu/ClientMenu.d.ts +7 -0
- package/dist/ClientMenu/ClientMenu.d.ts.map +1 -0
- package/dist/ClientMenu/ClientMenu.js +9 -0
- package/dist/ClientMenu/ClientMenu.js.map +1 -0
- package/dist/ClientMenu/index.d.ts +1 -0
- package/dist/ClientMenu/index.d.ts.map +1 -0
- package/dist/ClientMenu/index.js +2 -0
- package/dist/ClientMenu/index.js.map +1 -0
- package/dist/ClientMetadata/ClientMetadata.d.ts +14 -0
- package/dist/ClientMetadata/ClientMetadata.d.ts.map +1 -0
- package/dist/ClientMetadata/ClientMetadata.js +12 -0
- package/dist/ClientMetadata/ClientMetadata.js.map +1 -0
- package/dist/ClientMetadata/index.d.ts +3 -0
- package/dist/ClientMetadata/index.d.ts.map +1 -0
- package/dist/ClientMetadata/index.js +3 -0
- package/dist/ClientMetadata/index.js.map +1 -0
- package/dist/CollectionShortcuts/CollectionShortcuts.d.ts +13 -0
- package/dist/CollectionShortcuts/CollectionShortcuts.d.ts.map +1 -0
- package/dist/CollectionShortcuts/CollectionShortcuts.js +22 -0
- package/dist/CollectionShortcuts/CollectionShortcuts.js.map +1 -0
- package/dist/CollectionShortcuts/index.d.ts +3 -0
- package/dist/CollectionShortcuts/index.d.ts.map +1 -0
- package/dist/CollectionShortcuts/index.js +3 -0
- package/dist/CollectionShortcuts/index.js.map +1 -0
- package/dist/CollectionView/CollectionView.d.ts +13 -0
- package/dist/CollectionView/CollectionView.d.ts.map +1 -0
- package/dist/CollectionView/CollectionView.js +12 -0
- package/dist/CollectionView/CollectionView.js.map +1 -0
- package/dist/CollectionView/index.d.ts +2 -0
- package/dist/CollectionView/index.d.ts.map +1 -0
- package/dist/CollectionView/index.js +2 -0
- package/dist/CollectionView/index.js.map +1 -0
- package/dist/LabeledIconButton/LabeledIconButton.d.ts +10 -0
- package/dist/LabeledIconButton/LabeledIconButton.d.ts.map +1 -0
- package/dist/LabeledIconButton/LabeledIconButton.js +11 -0
- package/dist/LabeledIconButton/LabeledIconButton.js.map +1 -0
- package/dist/LabeledIconButton/index.d.ts +2 -0
- package/dist/LabeledIconButton/index.d.ts.map +1 -0
- package/dist/LabeledIconButton/index.js +2 -0
- package/dist/LabeledIconButton/index.js.map +1 -0
- package/dist/ObjectInput/ObjectInput.d.ts +18 -0
- package/dist/ObjectInput/ObjectInput.d.ts.map +1 -0
- package/dist/ObjectInput/ObjectInput.js +46 -0
- package/dist/ObjectInput/ObjectInput.js.map +1 -0
- package/dist/ObjectInput/StartAdornment.d.ts +9 -0
- package/dist/ObjectInput/StartAdornment.d.ts.map +1 -0
- package/dist/ObjectInput/StartAdornment.js +13 -0
- package/dist/ObjectInput/StartAdornment.js.map +1 -0
- package/dist/ObjectInput/index.d.ts +3 -0
- package/dist/ObjectInput/index.d.ts.map +1 -0
- package/dist/ObjectInput/index.js +3 -0
- package/dist/ObjectInput/index.js.map +1 -0
- package/dist/ObjectUpload/ObjectUpload.d.ts +27 -0
- package/dist/ObjectUpload/ObjectUpload.d.ts.map +1 -0
- package/dist/ObjectUpload/ObjectUpload.js +177 -0
- package/dist/ObjectUpload/ObjectUpload.js.map +1 -0
- package/dist/ObjectUpload/index.d.ts +3 -0
- package/dist/ObjectUpload/index.d.ts.map +1 -0
- package/dist/ObjectUpload/index.js +3 -0
- package/dist/ObjectUpload/index.js.map +1 -0
- package/dist/ObjectView/ObjectView.d.ts +18 -0
- package/dist/ObjectView/ObjectView.d.ts.map +1 -0
- package/dist/ObjectView/ObjectView.js +115 -0
- package/dist/ObjectView/ObjectView.js.map +1 -0
- package/dist/ObjectView/index.d.ts +3 -0
- package/dist/ObjectView/index.d.ts.map +1 -0
- package/dist/ObjectView/index.js +3 -0
- package/dist/ObjectView/index.js.map +1 -0
- package/dist/PelicanClientProvider/PelicanClientContext.d.ts +26 -0
- package/dist/PelicanClientProvider/PelicanClientContext.d.ts.map +1 -0
- package/dist/PelicanClientProvider/PelicanClientContext.js +4 -0
- package/dist/PelicanClientProvider/PelicanClientContext.js.map +1 -0
- package/dist/PelicanClientProvider/PelicanClientProvider.d.ts +15 -0
- package/dist/PelicanClientProvider/PelicanClientProvider.d.ts.map +1 -0
- package/dist/PelicanClientProvider/PelicanClientProvider.js +377 -0
- package/dist/PelicanClientProvider/PelicanClientProvider.js.map +1 -0
- package/dist/PelicanClientProvider/index.d.ts +6 -0
- package/dist/PelicanClientProvider/index.d.ts.map +1 -0
- package/dist/PelicanClientProvider/index.js +4 -0
- package/dist/PelicanClientProvider/index.js.map +1 -0
- package/dist/PelicanClientProvider/usePelicanClient.d.ts +21 -0
- package/dist/PelicanClientProvider/usePelicanClient.d.ts.map +1 -0
- package/dist/PelicanClientProvider/usePelicanClient.js +29 -0
- package/dist/PelicanClientProvider/usePelicanClient.js.map +1 -0
- package/dist/PermissionIcon/PermissionIcon.d.ts +7 -0
- package/dist/PermissionIcon/PermissionIcon.d.ts.map +1 -0
- package/dist/PermissionIcon/PermissionIcon.js +16 -0
- package/dist/PermissionIcon/PermissionIcon.js.map +1 -0
- package/dist/PermissionIcon/index.d.ts +2 -0
- package/dist/PermissionIcon/index.d.ts.map +1 -0
- package/dist/PermissionIcon/index.js +2 -0
- package/dist/PermissionIcon/index.js.map +1 -0
- package/dist/PublicClient/PublicClient.d.ts +6 -0
- package/dist/PublicClient/PublicClient.d.ts.map +1 -0
- package/dist/PublicClient/PublicClient.js +33 -0
- package/dist/PublicClient/PublicClient.js.map +1 -0
- package/dist/PublicClient/index.d.ts +3 -0
- package/dist/PublicClient/index.d.ts.map +1 -0
- package/dist/PublicClient/index.js +3 -0
- package/dist/PublicClient/index.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/usePelicanClient/index.d.ts +3 -0
- package/dist/usePelicanClient/index.d.ts.map +1 -0
- package/dist/usePelicanClient/index.js +3 -0
- package/dist/usePelicanClient/index.js.map +1 -0
- package/dist/usePelicanClient/useAuthExchange.d.ts +41 -0
- package/dist/usePelicanClient/useAuthExchange.d.ts.map +1 -0
- package/dist/usePelicanClient/useAuthExchange.js +89 -0
- package/dist/usePelicanClient/useAuthExchange.js.map +1 -0
- package/dist/usePelicanClient/useCodeVerifier.d.ts +12 -0
- package/dist/usePelicanClient/useCodeVerifier.d.ts.map +1 -0
- package/dist/usePelicanClient/useCodeVerifier.js +37 -0
- package/dist/usePelicanClient/useCodeVerifier.js.map +1 -0
- package/dist/usePelicanClient/useEventCallback.d.ts +18 -0
- package/dist/usePelicanClient/useEventCallback.d.ts.map +1 -0
- package/dist/usePelicanClient/useEventCallback.js +12 -0
- package/dist/usePelicanClient/useEventCallback.js.map +1 -0
- package/dist/usePelicanClient/useEventListener.d.ts +7 -0
- package/dist/usePelicanClient/useEventListener.d.ts.map +1 -0
- package/dist/usePelicanClient/useEventListener.js +57 -0
- package/dist/usePelicanClient/useEventListener.js.map +1 -0
- package/dist/usePelicanClient/useIsomorphicLayoutEffect.d.ts +16 -0
- package/dist/usePelicanClient/useIsomorphicLayoutEffect.d.ts.map +1 -0
- package/dist/usePelicanClient/useIsomorphicLayoutEffect.js +16 -0
- package/dist/usePelicanClient/useIsomorphicLayoutEffect.js.map +1 -0
- package/dist/usePelicanClient/usePelicanClient.d.ts +27 -0
- package/dist/usePelicanClient/usePelicanClient.d.ts.map +1 -0
- package/dist/usePelicanClient/usePelicanClient.js +245 -0
- package/dist/usePelicanClient/usePelicanClient.js.map +1 -0
- package/dist/usePelicanClient/useSessionStorage.d.ts +39 -0
- package/dist/usePelicanClient/useSessionStorage.d.ts.map +1 -0
- package/dist/usePelicanClient/useSessionStorage.js +121 -0
- package/dist/usePelicanClient/useSessionStorage.js.map +1 -0
- package/dist/usePelicanClient/util.d.ts +1 -0
- package/dist/usePelicanClient/util.d.ts.map +1 -0
- package/dist/usePelicanClient/util.js +2 -0
- package/dist/usePelicanClient/util.js.map +1 -0
- package/package.json +53 -0
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { UnauthenticatedError, downloadResponse, fetchFederation, fetchNamespace, get, list, parseObjectUrl, put, startAuthorizationCodeFlow, getTokenCollections } from "@pelicanplatform/web-client";
|
|
3
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
4
|
+
import { useSessionStorage } from "./useSessionStorage";
|
|
5
|
+
import { useCodeVerifier } from "./useCodeVerifier";
|
|
6
|
+
import { useAuthExchange } from "./useAuthExchange";
|
|
7
|
+
/**
|
|
8
|
+
* A React hook to manage Pelican client state and actions.
|
|
9
|
+
*
|
|
10
|
+
* The main pieces of state are `objectUrl`, `federationHostname`, and `objectPrefix`.
|
|
11
|
+
* These each represent:
|
|
12
|
+
* - `objectUrl`: The full Pelican URL, as inputed by the user
|
|
13
|
+
* - `federationHostname`: The hostname of the federation being accessed
|
|
14
|
+
* - `objectPrefix`: The prefix (namespace) of the object being accessed
|
|
15
|
+
*/
|
|
16
|
+
function usePelicanClient({ objectUrl, enableAuth = true }) {
|
|
17
|
+
var _a, _b;
|
|
18
|
+
const [loading, setLoading] = useState(false);
|
|
19
|
+
const [error, setError] = useState(null);
|
|
20
|
+
const [federations, setFederations] = useSessionStorage("pelican-wc-federations", {});
|
|
21
|
+
const [prefixToNamespace, setPrefixToNamespace] = useSessionStorage("pelican-wc-p2n", {});
|
|
22
|
+
const [loginRequired, setLoginRequired] = useState(!enableAuth);
|
|
23
|
+
// Store in-flight metadata fetch promises to prevent duplicate concurrent requests
|
|
24
|
+
const metadataPromises = useRef(new Map());
|
|
25
|
+
// Handle OAuth authorization code exchange
|
|
26
|
+
const [_, ensureCodeVerifier] = useCodeVerifier();
|
|
27
|
+
const { exchangeComplete: authExchangeComplete } = useAuthExchange({
|
|
28
|
+
enabled: enableAuth,
|
|
29
|
+
codeVerifier: ensureCodeVerifier(),
|
|
30
|
+
getNamespace: (federationHostname, namespacePrefix) => {
|
|
31
|
+
var _a;
|
|
32
|
+
return (_a = federations[federationHostname]) === null || _a === void 0 ? void 0 : _a.namespaces[namespacePrefix];
|
|
33
|
+
},
|
|
34
|
+
onTokenReceived: (result) => {
|
|
35
|
+
// Update federation state with the newly acquired token
|
|
36
|
+
setFederations((f) => {
|
|
37
|
+
var _a, _b, _c;
|
|
38
|
+
return ({
|
|
39
|
+
...f,
|
|
40
|
+
[result.federationHostname]: {
|
|
41
|
+
...f[result.federationHostname],
|
|
42
|
+
namespaces: {
|
|
43
|
+
...(_a = f[result.federationHostname]) === null || _a === void 0 ? void 0 : _a.namespaces,
|
|
44
|
+
[result.namespacePrefix]: {
|
|
45
|
+
...(_c = (_b = f[result.federationHostname]) === null || _b === void 0 ? void 0 : _b.namespaces) === null || _c === void 0 ? void 0 : _c[result.namespacePrefix],
|
|
46
|
+
token: result.token,
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
const { federationHostname, objectPath } = useMemo(() => {
|
|
55
|
+
try {
|
|
56
|
+
return parseObjectUrl(objectUrl);
|
|
57
|
+
}
|
|
58
|
+
catch (_a) {
|
|
59
|
+
return { federationHostname: null, objectPath: null };
|
|
60
|
+
}
|
|
61
|
+
}, [objectUrl]);
|
|
62
|
+
const federation = federationHostname && federations ? federations[federationHostname] : null;
|
|
63
|
+
const namespace = objectPath ? ((_a = federation === null || federation === void 0 ? void 0 : federation.namespaces) === null || _a === void 0 ? void 0 : _a[(_b = prefixToNamespace === null || prefixToNamespace === void 0 ? void 0 : prefixToNamespace[objectPath]) === null || _b === void 0 ? void 0 : _b.namespace]) || null : null;
|
|
64
|
+
const collections = useMemo(() => {
|
|
65
|
+
if (!(namespace === null || namespace === void 0 ? void 0 : namespace.token))
|
|
66
|
+
return [];
|
|
67
|
+
return getTokenCollections(namespace.token);
|
|
68
|
+
}, [namespace]);
|
|
69
|
+
/**
|
|
70
|
+
* Helper function to ensure federation and namespace metadata is available.
|
|
71
|
+
* Fetches on-demand if not in cache. Deduplicates concurrent requests for the same URL.
|
|
72
|
+
*
|
|
73
|
+
* @param targetObjectUrl - The pelican:// URL to fetch metadata for
|
|
74
|
+
* @returns Promise resolving to { federation, namespace }
|
|
75
|
+
*/
|
|
76
|
+
const ensureMetadata = useCallback(async (targetObjectUrl) => {
|
|
77
|
+
var _a;
|
|
78
|
+
const { federationHostname, objectPath } = parseObjectUrl(targetObjectUrl);
|
|
79
|
+
if (!federationHostname || !objectPath) {
|
|
80
|
+
throw new Error(`Invalid object URL: ${targetObjectUrl}`);
|
|
81
|
+
}
|
|
82
|
+
// Create a cache key for this specific URL
|
|
83
|
+
const cacheKey = `${federationHostname}:${objectPath}`;
|
|
84
|
+
// Check if there's already an in-flight request for this URL
|
|
85
|
+
const existingPromise = metadataPromises.current.get(cacheKey);
|
|
86
|
+
if (existingPromise) {
|
|
87
|
+
console.log(`Reusing in-flight request for ${cacheKey}`);
|
|
88
|
+
return existingPromise;
|
|
89
|
+
}
|
|
90
|
+
// Check if we already have both federation and namespace in cache
|
|
91
|
+
const _federation = federations[federationHostname];
|
|
92
|
+
const namespaceKey = (_a = prefixToNamespace[objectPath]) === null || _a === void 0 ? void 0 : _a.namespace;
|
|
93
|
+
const _namespace = namespaceKey && _federation ? _federation.namespaces[namespaceKey] : null;
|
|
94
|
+
if (_federation && _namespace) {
|
|
95
|
+
// Fast path: already in cache
|
|
96
|
+
return { federation: _federation, namespace: _namespace };
|
|
97
|
+
}
|
|
98
|
+
// Create a new promise for this fetch operation
|
|
99
|
+
const fetchPromise = (async () => {
|
|
100
|
+
var _a;
|
|
101
|
+
try {
|
|
102
|
+
// Fetch federation if missing
|
|
103
|
+
let federation = federations[federationHostname];
|
|
104
|
+
if (!federation) {
|
|
105
|
+
federation = await fetchFederation(federationHostname);
|
|
106
|
+
setFederations((prev) => ({
|
|
107
|
+
...prev,
|
|
108
|
+
[federationHostname]: federation
|
|
109
|
+
}));
|
|
110
|
+
}
|
|
111
|
+
// Fetch namespace if missing
|
|
112
|
+
const namespaceKey = (_a = prefixToNamespace[objectPath]) === null || _a === void 0 ? void 0 : _a.namespace;
|
|
113
|
+
let namespace = namespaceKey ? federation.namespaces[namespaceKey] : null;
|
|
114
|
+
if (!namespace) {
|
|
115
|
+
namespace = await fetchNamespace(objectPath, federation);
|
|
116
|
+
// Update prefix-to-namespace mapping
|
|
117
|
+
setPrefixToNamespace((prev) => ({
|
|
118
|
+
...prev,
|
|
119
|
+
[objectPath]: {
|
|
120
|
+
federation: federationHostname,
|
|
121
|
+
namespace: namespace.prefix
|
|
122
|
+
}
|
|
123
|
+
}));
|
|
124
|
+
// Update federation with new namespace
|
|
125
|
+
federation.namespaces[namespace.prefix] = namespace;
|
|
126
|
+
setFederations((prev) => ({
|
|
127
|
+
...prev,
|
|
128
|
+
[federationHostname]: federation
|
|
129
|
+
}));
|
|
130
|
+
}
|
|
131
|
+
return { federation, namespace };
|
|
132
|
+
}
|
|
133
|
+
finally {
|
|
134
|
+
// Clean up the in-flight promise from the cache
|
|
135
|
+
metadataPromises.current.delete(cacheKey);
|
|
136
|
+
}
|
|
137
|
+
})();
|
|
138
|
+
// Store the promise so concurrent calls can reuse it
|
|
139
|
+
metadataPromises.current.set(cacheKey, fetchPromise);
|
|
140
|
+
return fetchPromise;
|
|
141
|
+
}, [federations, prefixToNamespace, setFederations, setPrefixToNamespace]);
|
|
142
|
+
// Pull Federation and Namespace Metadata as needed for the current objectUrl
|
|
143
|
+
useEffect(() => {
|
|
144
|
+
(async () => {
|
|
145
|
+
setLoading(true);
|
|
146
|
+
if (!objectUrl || !federationHostname || !objectPath) {
|
|
147
|
+
setLoading(false);
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
try {
|
|
151
|
+
await ensureMetadata(objectUrl);
|
|
152
|
+
}
|
|
153
|
+
catch (e) {
|
|
154
|
+
setError(`Failed to fetch metadata for ${objectUrl}: ${e}`);
|
|
155
|
+
}
|
|
156
|
+
setLoading(false);
|
|
157
|
+
})();
|
|
158
|
+
}, [objectUrl, federationHostname, objectPath, ensureMetadata]);
|
|
159
|
+
const getObjectList = async () => {
|
|
160
|
+
const { objectPath } = parseObjectUrl(objectUrl);
|
|
161
|
+
const { federation, namespace } = await ensureMetadata(objectUrl);
|
|
162
|
+
try {
|
|
163
|
+
let objects = await list(objectUrl, federation, namespace);
|
|
164
|
+
// add parent directory entry
|
|
165
|
+
const pathParts = objectPath.split("/").filter((p) => p.length > 0);
|
|
166
|
+
if (pathParts.length > 0) {
|
|
167
|
+
const parentParts = pathParts.slice(0, -1);
|
|
168
|
+
const parentPath = parentParts.length > 0 ? "/" + parentParts.join("/") : "";
|
|
169
|
+
objects.push({
|
|
170
|
+
href: parentPath || "/",
|
|
171
|
+
getcontentlength: 0,
|
|
172
|
+
getlastmodified: "",
|
|
173
|
+
resourcetype: "collection",
|
|
174
|
+
iscollection: true,
|
|
175
|
+
executable: "",
|
|
176
|
+
status: "",
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
const objectPathWithoutSlash = objectPath.replace(/\/+$/, ""); // remove trailing slashes for comparison
|
|
180
|
+
// remove current directory entry
|
|
181
|
+
objects = objects.filter((obj) => obj.href !== objectPathWithoutSlash && obj.href !== "");
|
|
182
|
+
// reverse so directories show first (and the parent entry shows at top)
|
|
183
|
+
objects.reverse();
|
|
184
|
+
return objects;
|
|
185
|
+
}
|
|
186
|
+
catch (e) {
|
|
187
|
+
if (e instanceof UnauthenticatedError) {
|
|
188
|
+
setLoginRequired(true);
|
|
189
|
+
return [];
|
|
190
|
+
}
|
|
191
|
+
setError(`Failed to fetch metadata for ${objectUrl}: ${e}`);
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
const handleDownload = async (downloadObjectUrl) => {
|
|
195
|
+
try {
|
|
196
|
+
// Ensure metadata is loaded for this specific URL (deduplicated)
|
|
197
|
+
const { federation, namespace } = await ensureMetadata(downloadObjectUrl);
|
|
198
|
+
const response = await get(downloadObjectUrl, federation, namespace);
|
|
199
|
+
downloadResponse(response);
|
|
200
|
+
}
|
|
201
|
+
catch (e) {
|
|
202
|
+
setError(`Download failed: ${e}`);
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
const handleLogin = async () => {
|
|
206
|
+
try {
|
|
207
|
+
// Ensure metadata is loaded for this specific URL (deduplicated)
|
|
208
|
+
const { federation, namespace } = await ensureMetadata(objectUrl);
|
|
209
|
+
if (!federation || !namespace)
|
|
210
|
+
return; // Cannot login without federation and namespace
|
|
211
|
+
if (!enableAuth)
|
|
212
|
+
return;
|
|
213
|
+
await startAuthorizationCodeFlow(ensureCodeVerifier(), namespace, federation, { objectUrl: objectUrl });
|
|
214
|
+
}
|
|
215
|
+
catch (e) {
|
|
216
|
+
setError(`Login failed: ${e}`);
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
const handleUpload = async (file, uploadObjectUrl) => {
|
|
220
|
+
try {
|
|
221
|
+
const targetUrl = uploadObjectUrl || objectUrl;
|
|
222
|
+
// Ensure metadata is loaded for the target URL (deduplicated)
|
|
223
|
+
const { federation, namespace } = await ensureMetadata(targetUrl);
|
|
224
|
+
// Construct upload URL by appending filename to current directory
|
|
225
|
+
const finalUploadUrl = targetUrl.endsWith("/")
|
|
226
|
+
? `${targetUrl}${file.name}`
|
|
227
|
+
: `${targetUrl}/${file.name}`;
|
|
228
|
+
await put(finalUploadUrl, file, federation, namespace);
|
|
229
|
+
}
|
|
230
|
+
catch (e) {
|
|
231
|
+
setError(`Upload failed: ${e}`);
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
return {
|
|
235
|
+
loading,
|
|
236
|
+
collections,
|
|
237
|
+
loginRequired,
|
|
238
|
+
getObjectList,
|
|
239
|
+
handleLogin,
|
|
240
|
+
handleDownload,
|
|
241
|
+
handleUpload
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
export default usePelicanClient;
|
|
245
|
+
//# sourceMappingURL=usePelicanClient.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"usePelicanClient.js","sourceRoot":"","sources":["../../src/usePelicanClient/usePelicanClient.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,EAIL,oBAAoB,EACpB,gBAAgB,EAChB,eAAe,EACf,cAAc,EACd,GAAG,EACH,IAAI,EACJ,cAAc,EACd,GAAG,EACH,0BAA0B,EAE1B,mBAAmB,EAGpB,MAAM,6BAA6B,CAAC;AACrC,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAC1E,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AASpD;;;;;;;;GAQG;AACH,SAAS,gBAAgB,CAAC,EAAE,SAAS,EAAE,UAAU,GAAG,IAAI,EAA2B;;IAEjF,M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AAC;QACzD,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACX,QAAQ,CAAC,kBAAkB,CAAC,EAAE,CAAC,CAAC;QAClC,CAAC;IACH,CAAC,CAAA;IAED,OAAO;QACL,OAAO;QACP,WAAW;QACX,aAAa;QACb,aAAa;QACb,WAAW;QACX,cAAc;QACd,YAAY;KACb,CAAC;AACJ,CAAC;AAED,eAAe,gBAAgB,CAAC"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { Dispatch, SetStateAction } from "react";
|
|
2
|
+
declare global {
|
|
3
|
+
interface WindowEventMap {
|
|
4
|
+
"session-storage": CustomEvent;
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Represents the options for customizing the behavior of serialization and deserialization.
|
|
9
|
+
* @template T - The type of the state to be stored in session storage.
|
|
10
|
+
*/
|
|
11
|
+
type UseSessionStorageOptions<T> = {
|
|
12
|
+
/** A function to serialize the value before storing it. */
|
|
13
|
+
serializer?: (value: T) => string;
|
|
14
|
+
/** A function to deserialize the stored value. */
|
|
15
|
+
deserializer?: (value: string) => T;
|
|
16
|
+
/**
|
|
17
|
+
* If `true` (default), the hook will initialize reading the session storage. In SSR, you should set it to `false`, returning the initial value initially.
|
|
18
|
+
* @default true
|
|
19
|
+
*/
|
|
20
|
+
initializeWithValue?: boolean;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Custom hook that uses the [`sessionStorage API`](https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage) to persist state across page reloads.
|
|
24
|
+
* @template T - The type of the state to be stored in session storage.
|
|
25
|
+
* @param {string} key - The key under which the value will be stored in session storage.
|
|
26
|
+
* @param {T | (() => T)} initialValue - The initial value of the state or a function that returns the initial value.
|
|
27
|
+
* @param {?UseSessionStorageOptions<T>} [options] - Options for customizing the behavior of serialization and deserialization (optional).
|
|
28
|
+
* @returns {[T, Dispatch<SetStateAction<T>>, () => void]} A tuple containing the stored value, a function to set the value and a function to remove the key from storage.
|
|
29
|
+
* @public
|
|
30
|
+
* @see [Documentation](https://usehooks-ts.com/react-hook/use-session-storage)
|
|
31
|
+
* @example
|
|
32
|
+
* ```tsx
|
|
33
|
+
* const [count, setCount, removeCount] = useSessionStorage('count', 0);
|
|
34
|
+
* // Access the `count` value, the `setCount` function to update it and `removeCount` function to remove the key from storage.
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export declare function useSessionStorage<T>(key: string, initialValue: T | (() => T), options?: UseSessionStorageOptions<T>): [T, Dispatch<SetStateAction<T>>, () => void];
|
|
38
|
+
export {};
|
|
39
|
+
//# sourceMappingURL=useSessionStorage.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useSessionStorage.d.ts","sourceRoot":"","sources":["../../src/usePelicanClient/useSessionStorage.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,QAAQ,EAAE,cAAc,EAAE,MAAM,OAAO,CAAC;AAKtD,OAAO,CAAC,MAAM,CAAC;IAEX,UAAU,cAAc;QACpB,iBAAiB,EAAE,WAAW,CAAC;KAClC;CACJ;AAED;;;GAGG;AACH,KAAK,wBAAwB,CAAC,CAAC,IAAI;IAC/B,2DAA2D;IAC3D,UAAU,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,MAAM,CAAC;IAClC,kDAAkD;IAClD,YAAY,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,CAAC,CAAC;IACpC;;;OAGG;IACH,mBAAmB,CAAC,EAAE,OAAO,CAAC;CACjC,CAAC;AAIF;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,EAC/B,GAAG,EAAE,MAAM,EACX,YAAY,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,EAC3B,OAAO,GAAE,wBAAwB,CAAC,CAAC,CAAM,GAC1C,CAAC,CAAC,EAAE,QAAQ,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,EAAE,MAAM,IAAI,CAAC,CAoI9C"}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { useCallback, useEffect, useState } from "react";
|
|
2
|
+
import { useEventCallback } from "./useEventCallback";
|
|
3
|
+
import { useEventListener } from "./useEventListener";
|
|
4
|
+
const IS_SERVER = typeof window === "undefined";
|
|
5
|
+
/**
|
|
6
|
+
* Custom hook that uses the [`sessionStorage API`](https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage) to persist state across page reloads.
|
|
7
|
+
* @template T - The type of the state to be stored in session storage.
|
|
8
|
+
* @param {string} key - The key under which the value will be stored in session storage.
|
|
9
|
+
* @param {T | (() => T)} initialValue - The initial value of the state or a function that returns the initial value.
|
|
10
|
+
* @param {?UseSessionStorageOptions<T>} [options] - Options for customizing the behavior of serialization and deserialization (optional).
|
|
11
|
+
* @returns {[T, Dispatch<SetStateAction<T>>, () => void]} A tuple containing the stored value, a function to set the value and a function to remove the key from storage.
|
|
12
|
+
* @public
|
|
13
|
+
* @see [Documentation](https://usehooks-ts.com/react-hook/use-session-storage)
|
|
14
|
+
* @example
|
|
15
|
+
* ```tsx
|
|
16
|
+
* const [count, setCount, removeCount] = useSessionStorage('count', 0);
|
|
17
|
+
* // Access the `count` value, the `setCount` function to update it and `removeCount` function to remove the key from storage.
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export function useSessionStorage(key, initialValue, options = {}) {
|
|
21
|
+
const { initializeWithValue = true } = options;
|
|
22
|
+
const serializer = useCallback((value) => {
|
|
23
|
+
if (options.serializer) {
|
|
24
|
+
return options.serializer(value);
|
|
25
|
+
}
|
|
26
|
+
return JSON.stringify(value);
|
|
27
|
+
}, [options]);
|
|
28
|
+
const deserializer = useCallback((value) => {
|
|
29
|
+
if (options.deserializer) {
|
|
30
|
+
return options.deserializer(value);
|
|
31
|
+
}
|
|
32
|
+
// Support 'undefined' as a value
|
|
33
|
+
if (value === "undefined") {
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
const defaultValue = initialValue instanceof Function ? initialValue() : initialValue;
|
|
37
|
+
let parsed;
|
|
38
|
+
try {
|
|
39
|
+
parsed = JSON.parse(value);
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
console.error("Error parsing JSON:", error);
|
|
43
|
+
return defaultValue; // Return initialValue if parsing fails
|
|
44
|
+
}
|
|
45
|
+
return parsed;
|
|
46
|
+
}, [options, initialValue]);
|
|
47
|
+
// Get from session storage then
|
|
48
|
+
// parse stored json or return initialValue
|
|
49
|
+
const readValue = useCallback(() => {
|
|
50
|
+
const initialValueToUse = initialValue instanceof Function ? initialValue() : initialValue;
|
|
51
|
+
// Prevent build error "window is undefined" but keep working
|
|
52
|
+
if (IS_SERVER) {
|
|
53
|
+
return initialValueToUse;
|
|
54
|
+
}
|
|
55
|
+
try {
|
|
56
|
+
const raw = window.sessionStorage.getItem(key);
|
|
57
|
+
return raw ? deserializer(raw) : initialValueToUse;
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
console.warn(`Error reading sessionStorage key “${key}”:`, error);
|
|
61
|
+
return initialValueToUse;
|
|
62
|
+
}
|
|
63
|
+
}, [initialValue, key, deserializer]);
|
|
64
|
+
const [storedValue, setStoredValue] = useState(() => {
|
|
65
|
+
if (initializeWithValue) {
|
|
66
|
+
return readValue();
|
|
67
|
+
}
|
|
68
|
+
return initialValue instanceof Function ? initialValue() : initialValue;
|
|
69
|
+
});
|
|
70
|
+
// Return a wrapped version of useState's setter function that ...
|
|
71
|
+
// ... persists the new value to sessionStorage.
|
|
72
|
+
const setValue = useEventCallback((value) => {
|
|
73
|
+
// Prevent build error "window is undefined" but keeps working
|
|
74
|
+
if (IS_SERVER) {
|
|
75
|
+
console.warn(`Tried setting sessionStorage key “${key}” even though environment is not a client`);
|
|
76
|
+
}
|
|
77
|
+
try {
|
|
78
|
+
// Allow value to be a function so we have the same API as useState
|
|
79
|
+
const newValue = value instanceof Function ? value(readValue()) : value;
|
|
80
|
+
// Save to session storage
|
|
81
|
+
window.sessionStorage.setItem(key, serializer(newValue));
|
|
82
|
+
// Save state
|
|
83
|
+
setStoredValue(newValue);
|
|
84
|
+
// We dispatch a custom event so every similar useSessionStorage hook is notified
|
|
85
|
+
window.dispatchEvent(new StorageEvent("session-storage", { key }));
|
|
86
|
+
}
|
|
87
|
+
catch (error) {
|
|
88
|
+
console.warn(`Error setting sessionStorage key “${key}”:`, error);
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
const removeValue = useEventCallback(() => {
|
|
92
|
+
// Prevent build error "window is undefined" but keeps working
|
|
93
|
+
if (IS_SERVER) {
|
|
94
|
+
console.warn(`Tried removing sessionStorage key “${key}” even though environment is not a client`);
|
|
95
|
+
}
|
|
96
|
+
const defaultValue = initialValue instanceof Function ? initialValue() : initialValue;
|
|
97
|
+
// Remove the key from session storage
|
|
98
|
+
window.sessionStorage.removeItem(key);
|
|
99
|
+
// Save state with default value
|
|
100
|
+
setStoredValue(defaultValue);
|
|
101
|
+
// We dispatch a custom event so every similar useSessionStorage hook is notified
|
|
102
|
+
window.dispatchEvent(new StorageEvent("session-storage", { key }));
|
|
103
|
+
});
|
|
104
|
+
useEffect(() => {
|
|
105
|
+
setStoredValue(readValue());
|
|
106
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
107
|
+
}, [key]);
|
|
108
|
+
const handleStorageChange = useCallback((event) => {
|
|
109
|
+
if (event.key && event.key !== key) {
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
setStoredValue(readValue());
|
|
113
|
+
}, [key, readValue]);
|
|
114
|
+
// this only works for other documents, not the current one
|
|
115
|
+
useEventListener("storage", handleStorageChange);
|
|
116
|
+
// this is a custom event, triggered in writeValueToSessionStorage
|
|
117
|
+
// See: useSessionStorage()
|
|
118
|
+
useEventListener("session-storage", handleStorageChange);
|
|
119
|
+
return [storedValue, setValue, removeValue];
|
|
120
|
+
}
|
|
121
|
+
//# sourceMappingURL=useSessionStorage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useSessionStorage.js","sourceRoot":"","sources":["../../src/usePelicanClient/useSessionStorage.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAIzD,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAyBtD,MAAM,SAAS,GAAG,OAAO,MAAM,KAAK,WAAW,CAAC;AAEhD;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,iBAAiB,CAC7B,GAAW,EACX,YAA2B,EAC3B,UAAuC,EAAE;IAEzC,MAAM,EAAE,mBAAmB,GAAG,IAAI,EAAE,GAAG,OAAO,CAAC;IAE/C,MAAM,UAAU,GAAG,WAAW,CAC1B,CAAC,KAAK,EAAE,EAAE;QACN,IAAI,OAAO,CAAC,UAAU,EAAE,CAAC;YACrB,OAAO,OAAO,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC;QACrC,CAAC;QAED,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;IACjC,CAAC,EACD,CAAC,OAAO,CAAC,CACZ,CAAC;IAEF,MAAM,YAAY,GAAG,WAAW,CAC5B,CAAC,KAAK,EAAE,EAAE;QACN,IAAI,OAAO,CAAC,YAAY,EAAE,CAAC;YACvB,OAAO,OAAO,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;QACvC,CAAC;QACD,iCAAiC;QACjC,IAAI,KAAK,KAAK,WAAW,EAAE,CAAC;YACxB,OAAO,SAAyB,CAAC;QACrC,CAAC;QAED,MAAM,YAAY,GAAG,YAAY,YAAY,QAAQ,CAAC,CAAC,CAAC,YAAY,EAAE,CAAC,CAAC,CAAC,YAAY,CAAC;QAEtF,IAAI,MAAe,CAAC;QACpB,IAAI,CAAC;YACD,MAAM,GAAG,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CAAC,IAAI,YAAY,CAAC,iBAAiB,EAAE,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC;IACvE,CAAC,CAAC,CAAC;IAEH,SAAS,CAAC,GAAG,EAAE;QACX,cAAc,CAAC,SAAS,EAAE,CAAC,CAAC;QAC5B,uDAAuD;IAC3D,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;IAEV,MAAM,mBAAmB,GAAG,WAAW,CACnC,CAAC,KAAiC,EAAE,EAAE;QAClC,IAAK,KAAsB,CAAC,GAAG,IAAK,KAAsB,CAAC,GAAG,KAAK,GAAG,EAAE,CAAC;YACrE,OAAO;QACX,CAAC;QACD,cAAc,CAAC,SAAS,EAAE,CAAC,CAAC;IAChC,CAAC,EACD,CAAC,GAAG,EAAE,SAAS,CAAC,CACnB,CAAC;IAEF,2DAA2D;IAC3D,gBAAgB,CAAC,SAAS,EAAE,mBAAmB,CAAC,CAAC;IAEjD,kEAAkE;IAClE,2BAA2B;IAC3B,gBAAgB,CAAC,iBAAiB,EAAE,mBAAmB,CAAC,CAAC;IAEzD,OAAO,CAAC,WAAW,EAAE,QAAQ,EAAE,WAAW,CAAC,CAAC;AAChD,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=util.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../../src/usePelicanClient/util.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"util.js","sourceRoot":"","sources":["../../src/usePelicanClient/util.ts"],"names":[],"mappings":""}
|
package/package.json
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pelicanplatform/components",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"homepage": "https://pelicanplatform.org",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/PelicanPlatform/web-client.git"
|
|
8
|
+
},
|
|
9
|
+
"description": "Pelican Client Components",
|
|
10
|
+
"main": "dist/index.js",
|
|
11
|
+
"types": "dist/index.d.ts",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"import": {
|
|
15
|
+
"development": "./src/index.ts",
|
|
16
|
+
"default": "./dist/index.js"
|
|
17
|
+
},
|
|
18
|
+
"require": "./dist/index.cjs"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist/*",
|
|
23
|
+
"README.md",
|
|
24
|
+
"package.json"
|
|
25
|
+
],
|
|
26
|
+
"author": "",
|
|
27
|
+
"license": "Apache-2.0",
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@emotion/react": "^11.14.0",
|
|
30
|
+
"@emotion/styled": "^11.14.1",
|
|
31
|
+
"@mui/icons-material": "^7.3.3",
|
|
32
|
+
"@mui/material": "^7.3.3",
|
|
33
|
+
"lodash-es": "^4.17.21",
|
|
34
|
+
"next": "^14.2.31",
|
|
35
|
+
"react": "^18",
|
|
36
|
+
"react-dom": "^18",
|
|
37
|
+
"usehooks-ts": "^3.1.1",
|
|
38
|
+
"@pelicanplatform/web-client": "1.0.17"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@types/node": "^20",
|
|
42
|
+
"@types/react": "^18",
|
|
43
|
+
"@types/react-dom": "^18",
|
|
44
|
+
"eslint": "^8",
|
|
45
|
+
"eslint-config-next": "14.0.2",
|
|
46
|
+
"typescript": "^5"
|
|
47
|
+
},
|
|
48
|
+
"scripts": {
|
|
49
|
+
"test": "jest --coverage --json --outputFile=.github/tests/test-summary.json",
|
|
50
|
+
"build": "tsc --project ./tsconfig.json",
|
|
51
|
+
"prepublish": "npm run build"
|
|
52
|
+
}
|
|
53
|
+
}
|