Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

refactor: clean up share web collection flow

AmanRajSinghMourya949e6f9064b7
diff --git a/web/apps/share/src/components/file-share/CollectionShareView.tsx b/web/apps/share/src/components/file-share/CollectionShareView.tsx
index d75da43b63..dccd6fa82f 100644
--- a/web/apps/share/src/components/file-share/CollectionShareView.tsx
+++ b/web/apps/share/src/components/file-share/CollectionShareView.tsx
@@ -52,6 +52,38 @@ const translatedOrFallback = (key: string, fallback: string) => {
};
const contentMaxWidth = 560;
+const passwordCardSx = {
+ width: "100%",
+ maxWidth: 420,
+ px: { xs: 3, sm: 4 },
+ py: { xs: 3.5, sm: 4 },
+ borderRadius: "20px",
+ backgroundColor: "rgba(19, 21, 24, 0.96)",
+ border: "1px solid rgba(255, 255, 255, 0.08)",
+ boxShadow:
+ "0 24px 60px rgba(0, 0, 0, 0.32), inset 0 1px 0 rgba(255, 255, 255, 0.03)",
+ color: "#FFFFFF",
+ "& .MuiTypography-root": { color: "inherit" },
+ "& .MuiFormLabel-root": { color: "rgba(255, 255, 255, 0.64)" },
+ "& .MuiFormLabel-root.Mui-focused": { color: "accent.light" },
+ "& .MuiInputBase-root": {
+ backgroundColor: "rgba(255, 255, 255, 0.08)",
+ color: "#FFFFFF",
+ },
+ "& .MuiInputBase-input": { color: "#FFFFFF" },
+ "& .MuiInputAdornment-root": { color: "rgba(255, 255, 255, 0.52)" },
+ "& .MuiFormHelperText-root": { color: "rgba(255, 255, 255, 0.6)" },
+ "& .MuiButton-root": {
+ backgroundColor: "accent.main",
+ color: "accent.contrastText",
+ },
+ "& .MuiButton-root:hover": { backgroundColor: "accent.dark" },
+ "& .MuiButton-root.Mui-disabled": {
+ backgroundColor: "accent.main",
+ color: "accent.contrastText",
+ opacity: 0.7,
+ },
+} as const;
export const CollectionShareView: React.FC = () => {
const {
@@ -134,51 +166,7 @@ export const CollectionShareView: React.FC = () => {
p: 3,
}}
>
- <Paper
- elevation={0}
- sx={{
- width: "100%",
- maxWidth: 420,
- px: { xs: 3, sm: 4 },
- py: { xs: 3.5, sm: 4 },
- borderRadius: "20px",
- backgroundColor: "rgba(19, 21, 24, 0.96)",
- border: "1px solid rgba(255, 255, 255, 0.08)",
- boxShadow:
- "0 24px 60px rgba(0, 0, 0, 0.32), inset 0 1px 0 rgba(255, 255, 255, 0.03)",
- color: "#FFFFFF",
- "& .MuiTypography-root": { color: "inherit" },
- "& .MuiFormLabel-root": {
- color: "rgba(255, 255, 255, 0.64)",
- },
- "& .MuiFormLabel-root.Mui-focused": {
- color: "accent.light",
- },
- "& .MuiInputBase-root": {
- backgroundColor: "rgba(255, 255, 255, 0.08)",
- color: "#FFFFFF",
- },
- "& .MuiInputBase-input": { color: "#FFFFFF" },
- "& .MuiInputAdornment-root": {
- color: "rgba(255, 255, 255, 0.52)",
- },
- "& .MuiFormHelperText-root": {
- color: "rgba(255, 255, 255, 0.6)",
- },
- "& .MuiButton-root": {
- backgroundColor: "accent.main",
- color: "accent.contrastText",
- },
- "& .MuiButton-root:hover": {
- backgroundColor: "accent.dark",
- },
- "& .MuiButton-root.Mui-disabled": {
- backgroundColor: "accent.main",
- color: "accent.contrastText",
- opacity: 0.7,
- },
- }}
- >
+ <Paper elevation={0} sx={passwordCardSx}>
<Typography
variant="h4"
sx={{ fontWeight: 700, color: "#FFFFFF", mb: 1.5 }}
diff --git a/web/apps/share/src/components/file-share/FileShareView.tsx b/web/apps/share/src/components/file-share/FileShareView.tsx
index 90baff3b9d..3f0dd527c8 100644
--- a/web/apps/share/src/components/file-share/FileShareView.tsx
+++ b/web/apps/share/src/components/file-share/FileShareView.tsx
@@ -22,6 +22,11 @@ export const FileShareView: React.FC = () => {
handleCopyContent,
setNotificationAttributes,
} = useFileShare();
+ const iconInfo = fileInfo
+ ? getLockerFileIcon(fileInfo.fileName, {
+ lockerType: fileInfo.lockerType,
+ })
+ : null;
return (
<>
@@ -144,18 +149,8 @@ export const FileShareView: React.FC = () => {
)}
{/* File Info Display */}
- {fileInfo && !loading && (
+ {fileInfo && iconInfo && !loading && (
<>
- {(() => {
- const iconInfo = getLockerFileIcon(
- fileInfo.fileName,
- {
- lockerType: fileInfo.lockerType,
- },
- );
-
- return (
- <>
{/* File Info - Centered */}
<Box
sx={{
@@ -227,9 +222,6 @@ export const FileShareView: React.FC = () => {
/>
)}
</Box>
- </>
- );
- })()}
{/* Download Button - Only show if not a LockerInfoType */}
{!fileInfo.lockerType && (
diff --git a/web/apps/share/src/hooks/useCollectionShare.ts b/web/apps/share/src/hooks/useCollectionShare.ts
index 8687947a91..83f19188f2 100644
--- a/web/apps/share/src/hooks/useCollectionShare.ts
+++ b/web/apps/share/src/hooks/useCollectionShare.ts
@@ -53,38 +53,46 @@ interface CollectionShareLoadError {
isExpired: boolean;
}
-const accessTokenJWTStorageKey = (accessToken: string) =>
- `share-collection-access-token-jwt:${accessToken}`;
-
-const savedAccessTokenJWT = (accessToken: string): string | null => {
+const readLocalStorageItem = (key: string): string | null => {
try {
- return window.localStorage.getItem(
- accessTokenJWTStorageKey(accessToken),
- );
+ return window.localStorage.getItem(key);
} catch {
return null;
}
};
-const saveAccessTokenJWT = (accessToken: string, accessTokenJWT: string) => {
+const writeLocalStorageItem = (key: string, value: string) => {
try {
- window.localStorage.setItem(
- accessTokenJWTStorageKey(accessToken),
- accessTokenJWT,
- );
+ window.localStorage.setItem(key, value);
} catch {
// Ignore storage failures and continue with in-memory state.
}
};
-const removeAccessTokenJWT = (accessToken: string) => {
+const removeLocalStorageItem = (key: string) => {
try {
- window.localStorage.removeItem(accessTokenJWTStorageKey(accessToken));
+ window.localStorage.removeItem(key);
} catch {
// Ignore storage failures and continue with in-memory state.
}
};
+const accessTokenJWTStorageKey = (accessToken: string) =>
+ `share-collection-access-token-jwt:${accessToken}`;
+
+const savedAccessTokenJWT = (accessToken: string): string | null =>
+ readLocalStorageItem(accessTokenJWTStorageKey(accessToken));
+
+const saveAccessTokenJWT = (accessToken: string, accessTokenJWT: string) => {
+ writeLocalStorageItem(
+ accessTokenJWTStorageKey(accessToken),
+ accessTokenJWT,
+ );
+};
+
+const removeAccessTokenJWT = (accessToken: string) =>
+ removeLocalStorageItem(accessTokenJWTStorageKey(accessToken));
+
const collectionShareLoadError = async (
err: unknown,
): Promise<CollectionShareLoadError> => {
@@ -158,6 +166,16 @@ export const useCollectionShare = (): UseCollectionShareResult => {
const [notificationAttributes, setNotificationAttributes] =
useState<NotificationAttributes>();
const requiresPassword = !!passwordProtectedPublicURL && !accessTokenJWT;
+ const clearLoadedState = useCallback(() => {
+ setCollectionInfo(null);
+ setSelectedItem(null);
+ setPasswordProtectedPublicURL(null);
+ }, []);
+ const setInvalidLinkState = useCallback(() => {
+ clearLoadedState();
+ setErrorTitle("Invalid collection link");
+ setError("This collection link is invalid.");
+ }, [clearLoadedState]);
const loadCollection = useCallback(
async (
@@ -178,10 +196,7 @@ export const useCollectionShare = (): UseCollectionShareResult => {
const token =
opts?.accessToken ?? extractCollectionTokenFromURL(url);
if (!token) {
- setCollectionInfo(null);
- setSelectedItem(null);
- setErrorTitle("Invalid collection link");
- setError("This collection link is invalid.");
+ setInvalidLinkState();
return;
}
@@ -189,10 +204,7 @@ export const useCollectionShare = (): UseCollectionShareResult => {
opts?.collectionKey ??
(await extractCollectionKeyFromShareURL(url));
if (!resolvedCollectionKey) {
- setCollectionInfo(null);
- setSelectedItem(null);
- setErrorTitle("Invalid collection link");
- setError("This collection link is invalid.");
+ setInvalidLinkState();
return;
}
@@ -241,6 +253,7 @@ export const useCollectionShare = (): UseCollectionShareResult => {
metadata.publicURL ?? null,
);
setCollectionInfo(null);
+ setSelectedItem(null);
return;
}
throw err;
@@ -249,9 +262,7 @@ export const useCollectionShare = (): UseCollectionShareResult => {
setPasswordProtectedPublicURL(null);
setCollectionInfo(info);
} catch (err) {
- setCollectionInfo(null);
- setSelectedItem(null);
- setPasswordProtectedPublicURL(null);
+ clearLoadedState();
const loadError = await collectionShareLoadError(err);
setErrorTitle(loadError.title);
setError(loadError.message);
@@ -260,7 +271,7 @@ export const useCollectionShare = (): UseCollectionShareResult => {
setLoading(false);
}
},
- [],
+ [clearLoadedState, setInvalidLinkState],
);
useEffect(() => {
@@ -275,7 +286,10 @@ export const useCollectionShare = (): UseCollectionShareResult => {
}
const revalidate = () => {
- if (document.visibilityState === "hidden" || isRevalidatingRef.current) {
+ if (
+ document.visibilityState === "hidden" ||
+ isRevalidatingRef.current
+ ) {
return;
}
@@ -298,7 +312,10 @@ export const useCollectionShare = (): UseCollectionShareResult => {
return () => {
window.removeEventListener("focus", revalidate);
window.removeEventListener("pageshow", revalidate);
- document.removeEventListener("visibilitychange", onVisibilityChange);
+ document.removeEventListener(
+ "visibilitychange",
+ onVisibilityChange,
+ );
};
}, [router.isReady, loadCollection]);