Skip to content

armgit / ente

Public Imported from GitHub
0

💚 End-to-end encrypted cloud for everything.

Leave collection

Vishnu Mohandas033420cb9edc
diff --git a/web/apps/locker/src/components/ItemList.tsx b/web/apps/locker/src/components/ItemList.tsx
index aa44fb599c..f88ddf520e 100644
--- a/web/apps/locker/src/components/ItemList.tsx
+++ b/web/apps/locker/src/components/ItemList.tsx
@@ -9,6 +9,7 @@ import EditOutlinedIcon from "@mui/icons-material/EditOutlined";
import FileDownloadOutlinedIcon from "@mui/icons-material/FileDownloadOutlined";
import FilterListRoundedIcon from "@mui/icons-material/FilterListRounded";
import FolderOutlinedIcon from "@mui/icons-material/FolderOutlined";
+import LogoutOutlinedIcon from "@mui/icons-material/LogoutOutlined";
import ShareOutlinedIcon from "@mui/icons-material/ShareOutlined";
import StarIcon from "@mui/icons-material/Star";
import {
@@ -43,6 +44,7 @@ import {
import type { LockerCollection, LockerItem } from "types";
import {
canEditCollection,
+ canLeaveCollection,
canOpenCollectionSharing,
canShareLockerFileLink,
getItemTitle,
@@ -84,6 +86,7 @@ interface ItemListProps {
onDeleteCollection?: (collectionID: number) => void;
onCreateCollection?: (name: string) => Promise<number>;
onShareCollection?: (collection: LockerCollection) => void;
+ onLeaveCollection?: (collection: LockerCollection) => void;
searchTerm: string;
onNavigateBack?: () => void;
}
@@ -108,6 +111,7 @@ export const ItemList: React.FC<ItemListProps> = ({
onDeleteCollection,
onCreateCollection,
onShareCollection,
+ onLeaveCollection,
searchTerm,
onNavigateBack,
}) => {
@@ -451,6 +455,9 @@ export const ItemList: React.FC<ItemListProps> = ({
const canEditSelectedCollection =
selectedCollection !== null &&
canEditCollection(selectedCollection, currentUserID);
+ const canLeaveSelectedCollection =
+ selectedCollection !== null &&
+ canLeaveCollection(selectedCollection, currentUserID);
const canShareSelectedCollection =
selectedCollection !== null &&
canOpenCollectionSharing(selectedCollection);
@@ -899,6 +906,7 @@ export const ItemList: React.FC<ItemListProps> = ({
collections={displayCollections}
onSelectCollection={onSelectCollection}
onShareCollection={onShareCollection}
+ onLeaveCollection={onLeaveCollection}
onRequestRenameCollection={(
collection,
) => {
@@ -936,6 +944,7 @@ export const ItemList: React.FC<ItemListProps> = ({
collections={filteredCollections}
onSelectCollection={onSelectCollection}
onShareCollection={onShareCollection}
+ onLeaveCollection={onLeaveCollection}
onRequestRenameCollection={(
collection,
) => {
@@ -1042,7 +1051,8 @@ export const ItemList: React.FC<ItemListProps> = ({
</IconButton>
</Tooltip>
)}
- {canEditSelectedCollection && (
+ {(canEditSelectedCollection ||
+ canLeaveSelectedCollection) && (
<CollectionHeaderMenu
onShare={
canShareSelectedCollection &&
@@ -1070,6 +1080,15 @@ export const ItemList: React.FC<ItemListProps> = ({
)
: undefined
}
+ onLeave={
+ onLeaveCollection &&
+ canLeaveSelectedCollection
+ ? () =>
+ onLeaveCollection(
+ selectedCollection,
+ )
+ : undefined
+ }
/>
)}
</Stack>
@@ -1573,12 +1592,14 @@ const CollectionGrid: React.FC<{
collections: LockerCollection[];
onSelectCollection: (collectionID: number) => void;
onShareCollection?: (collection: LockerCollection) => void;
+ onLeaveCollection?: (collection: LockerCollection) => void;
onRequestRenameCollection?: (collection: LockerCollection) => void;
onDeleteCollection?: (collectionID: number) => void;
}> = ({
collections,
onSelectCollection,
onShareCollection,
+ onLeaveCollection,
onRequestRenameCollection,
onDeleteCollection,
}) => {
@@ -1605,6 +1626,12 @@ const CollectionGrid: React.FC<{
? () => onShareCollection(collection)
: undefined
}
+ onLeave={
+ onLeaveCollection &&
+ canLeaveCollection(collection, currentUserID)
+ ? () => onLeaveCollection(collection)
+ : undefined
+ }
onRename={
onRequestRenameCollection &&
canEditCollection(collection, currentUserID)
@@ -1861,9 +1888,10 @@ const CollectionCard: React.FC<{
collection: LockerCollection;
onClick: () => void;
onShare?: () => void;
+ onLeave?: () => void;
onRename?: () => void;
onDelete?: () => void;
-}> = ({ collection, onClick, onShare, onRename, onDelete }) => {
+}> = ({ collection, onClick, onShare, onLeave, onRename, onDelete }) => {
return (
<ButtonBase
component="div"
@@ -1975,13 +2003,14 @@ const CollectionCard: React.FC<{
</Typography>
</Box>
</Stack>
- {(onShare || onRename || onDelete) && (
+ {(onShare || onLeave || onRename || onDelete) && (
<Box
sx={{ flexShrink: 0, ml: 0.25 }}
onClick={(event) => event.stopPropagation()}
>
<CollectionContextMenu
onShare={onShare}
+ onLeave={onLeave}
onRename={onRename}
onDelete={onDelete}
/>
@@ -2016,9 +2045,10 @@ const SharedCollectionBadge: React.FC = () => {
const CollectionContextMenu: React.FC<{
onShare?: () => void;
+ onLeave?: () => void;
onRename?: () => void;
onDelete?: () => void;
-}> = ({ onShare, onRename, onDelete }) => (
+}> = ({ onShare, onLeave, onRename, onDelete }) => (
<OverflowMenu
ariaID="collection-context-menu"
triggerButtonSxProps={{
@@ -2036,6 +2066,15 @@ const CollectionContextMenu: React.FC<{
{t("share")}
</OverflowMenuOption>
)}
+ {onLeave && (
+ <OverflowMenuOption
+ startIcon={<LogoutOutlinedIcon />}
+ color="critical"
+ onClick={onLeave}
+ >
+ {t("leaveCollection")}
+ </OverflowMenuOption>
+ )}
{onRename && (
<OverflowMenuOption
startIcon={<EditOutlinedIcon />}
@@ -2058,9 +2097,10 @@ const CollectionContextMenu: React.FC<{
const CollectionHeaderMenu: React.FC<{
onShare?: () => void;
+ onLeave?: () => void;
onRename?: () => void;
onDelete?: () => void;
-}> = ({ onShare, onRename, onDelete }) => (
+}> = ({ onShare, onLeave, onRename, onDelete }) => (
<OverflowMenu
ariaID="collection-header-menu"
triggerButtonSxProps={{ color: "text.muted" }}
@@ -2073,6 +2113,15 @@ const CollectionHeaderMenu: React.FC<{
{t("share")}
</OverflowMenuOption>
)}
+ {onLeave && (
+ <OverflowMenuOption
+ startIcon={<LogoutOutlinedIcon />}
+ color="critical"
+ onClick={onLeave}
+ >
+ {t("leaveCollection")}
+ </OverflowMenuOption>
+ )}
{onRename && (
<OverflowMenuOption
startIcon={<EditOutlinedIcon />}
diff --git a/web/apps/locker/src/components/LockerCollectionShareDrawer.tsx b/web/apps/locker/src/components/LockerCollectionShareDrawer.tsx
index 177f56aee9..0d966e8275 100644
--- a/web/apps/locker/src/components/LockerCollectionShareDrawer.tsx
+++ b/web/apps/locker/src/components/LockerCollectionShareDrawer.tsx
@@ -1,4 +1,5 @@
import CloseIcon from "@mui/icons-material/Close";
+import LogoutOutlinedIcon from "@mui/icons-material/LogoutOutlined";
import MoreVertIcon from "@mui/icons-material/MoreVert";
import {
Avatar,
@@ -25,6 +26,7 @@ import log from "ente-base/log";
import { t } from "i18next";
import React, { useCallback, useEffect, useMemo, useState } from "react";
import {
+ canLeaveCollection,
canManageCollectionSharing,
type LockerCollection,
type LockerCollectionParticipant,
@@ -36,6 +38,7 @@ interface LockerCollectionShareDrawerProps {
onClose: () => void;
onShareCollection: (collectionID: number, email: string) => Promise<void>;
onUnshareCollection: (collectionID: number, email: string) => Promise<void>;
+ onLeaveCollection: (collection: LockerCollection) => void;
onRefreshSharees?: (
collectionID: number,
) => Promise<LockerCollectionParticipant[]>;
@@ -51,6 +54,7 @@ export const LockerCollectionShareDrawer: React.FC<
onClose,
onShareCollection,
onUnshareCollection,
+ onLeaveCollection,
onRefreshSharees,
}) => {
const { showMiniDialog } = useBaseContext();
@@ -70,6 +74,9 @@ export const LockerCollectionShareDrawer: React.FC<
const canManageParticipants = !!(
collection && canManageCollectionSharing(collection, currentUser.id)
);
+ const canLeaveSharedCollection = !!(
+ collection && canLeaveCollection(collection, currentUser.id)
+ );
useEffect(() => {
if (!open || !collection) {
@@ -390,6 +397,18 @@ export const LockerCollectionShareDrawer: React.FC<
{t("addEmail")}
</Button>
)}
+
+ {canLeaveSharedCollection && (
+ <Button
+ color="critical"
+ variant="outlined"
+ startIcon={<LogoutOutlinedIcon />}
+ onClick={() => onLeaveCollection(collection)}
+ sx={{ minHeight: 48, borderRadius: "14px" }}
+ >
+ {t("leaveCollection")}
+ </Button>
+ )}
</Stack>
</Stack>
</SidebarDrawer>
diff --git a/web/apps/locker/src/components/lockerPage/useLockerActions.tsx b/web/apps/locker/src/components/lockerPage/useLockerActions.tsx
index 7b20ff55c0..bce1c50192 100644
--- a/web/apps/locker/src/components/lockerPage/useLockerActions.tsx
+++ b/web/apps/locker/src/components/lockerPage/useLockerActions.tsx
@@ -12,9 +12,11 @@ import {
deleteCollection as deleteCollectionAPI,
deleteCollectionKeepingFiles,
emptyTrash as emptyTrashAPI,
+ leaveCollection as leaveCollectionAPI,
permanentlyDeleteFromTrash,
renameCollection as renameCollectionAPI,
restoreFromTrash,
+ setItemImportant,
shareCollection as shareCollectionAPI,
trashFiles,
unshareCollection as unshareCollectionAPI,
@@ -771,6 +773,100 @@ export const useLockerActions = ({
[refreshData],
);
+ const handleLeaveCollection = useCallback(
+ (collection: LockerCollection) => {
+ showMiniDialog({
+ title: t("leaveCollection"),
+ message: t("filesAddedByYouWillBeRemovedFromTheCollection"),
+ continue: {
+ text: t("leaveCollection"),
+ color: "critical",
+ action: async () => {
+ await leaveCollectionAPI(collection.id);
+ if (shareCollectionID === collection.id) {
+ setShareCollectionID(null);
+ }
+ if (selectedCollectionID === collection.id) {
+ navigateHome();
+ }
+ removeCollectionFromState(collection.id);
+ await refreshData();
+ setToast(t("leaveCollectionSuccessfully"));
+ },
+ },
+ });
+ },
+ [
+ navigateHome,
+ refreshData,
+ removeCollectionFromState,
+ selectedCollectionID,
+ shareCollectionID,
+ showMiniDialog,
+ ],
+ );
+
+ const handleSetItemsImportant = useCallback(
+ async (items: LockerItem[], shouldBeImportant: boolean) => {
+ if (!masterKey) {
+ throw new Error("No master key");
+ }
+ if (items.length === 0) {
+ return;
+ }
+
+ let changedCount = 0;
+ let pendingError: unknown;
+ try {
+ for (const item of items) {
+ if (
+ await setItemImportant(
+ item.id,
+ shouldBeImportant,
+ masterKey,
+ )
+ ) {
+ changedCount += 1;
+ }
+ }
+ } catch (error) {
+ pendingError = error;
+ }
+
+ if (changedCount > 0) {
+ await refreshData();
+ }
+ if (pendingError) {
+ throw pendingError instanceof Error
+ ? pendingError
+ : new Error(t("failedToUpdateImportantStatus"));
+ }
+
+ if (changedCount === 0) {
+ if (shouldBeImportant) {
+ setToast(t("allItemsAlreadyMarkedAsImportant"));
+ }
+ return;
+ }
+
+ if (shouldBeImportant) {
+ setToast(
+ changedCount === 1
+ ? t("fileMarkedAsImportant")
+ : t("itemsMarkedAsImportant", { count: changedCount }),
+ );
+ return;
+ }
+
+ setToast(
+ changedCount === 1
+ ? t("fileRemovedFromImportant")
+ : t("itemsRemovedFromImportant", { count: changedCount }),
+ );
+ },
+ [masterKey, refreshData],
+ );
+
return {
createDialogOpen,
deleteCollectionDialog,
@@ -791,8 +887,10 @@ export const useLockerActions = ({
handleEmptyTrash,
handleOpenShareCollection,
handlePermanentlyDelete,
+ handleLeaveCollection,
handleRenameCollection,
handleRestoreItem,
+ handleSetItemsImportant,
handleShareCollection,
handleUnshareCollection,
handleUpdateItem,
diff --git a/web/apps/locker/src/locales/en-US/translation.json b/web/apps/locker/src/locales/en-US/translation.json
index 885384868b..d37c89afea 100644
--- a/web/apps/locker/src/locales/en-US/translation.json
+++ b/web/apps/locker/src/locales/en-US/translation.json
@@ -176,6 +176,9 @@
"failedToShareCollection": "Failed to share collection",
"collectionSharedSuccessfully": "Collection shared successfully",
"viewerRemovedSuccessfully": "Viewer removed successfully",
+ "leaveCollection": "Leave",
+ "filesAddedByYouWillBeRemovedFromTheCollection": "Items added by you will be removed from this collection.",
+ "leaveCollectionSuccessfully": "Left collection successfully",
"unknownEmail": "Unknown email",
"shareLink": "Share link",
"creatingShareLink": "Creating link...",
@@ -196,6 +199,18 @@
"shareNotSupportedForSharedFiles": "Share is not supported for shared files",
"actionNotSupportedForSharedFiles": "Action is not supported for {{count, number}} shared items",
"actionNotSupportedForSharedFiles_one": "Action is not supported for 1 shared item",
+ "important": "Important",
+ "unimportant": "Unimportant",
+ "fileMarkedAsImportant": "Item marked as important",
+ "fileRemovedFromImportant": "Item removed from important",
+ "itemsMarkedAsImportant": "{{count, number}} items marked as important",
+ "itemsMarkedAsImportant_one": "1 item marked as important",
+ "itemsRemovedFromImportant": "{{count, number}} items removed from important",
+ "itemsRemovedFromImportant_one": "1 item removed from important",
+ "allItemsAlreadyMarkedAsImportant": "All selected items are already marked as important",
+ "importantNotSupportedForSharedFiles": "Important is not supported for {{count, number}} shared items",
+ "importantNotSupportedForSharedFiles_one": "Important is not supported for 1 shared item",
+ "failedToUpdateImportantStatus": "Failed to update important status",
"deleteMultipleFilesDialogBody": "{{count, number}} items will be deleted.",
"deleteMultipleFilesDialogBody_one": "1 item will be deleted.",
"yesDeleteFiles": "Yes, delete items",
diff --git a/web/apps/locker/src/locales/fr-FR/translation.json b/web/apps/locker/src/locales/fr-FR/translation.json
index a587c0d86f..4eaa211708 100644
--- a/web/apps/locker/src/locales/fr-FR/translation.json
+++ b/web/apps/locker/src/locales/fr-FR/translation.json
@@ -169,6 +169,9 @@
"failedToShareCollection": "Echec du partage de la collection",
"collectionSharedSuccessfully": "Collection partagee avec succes",
"viewerRemovedSuccessfully": "Lecteur retire avec succes",
+ "leaveCollection": "Quitter",
+ "filesAddedByYouWillBeRemovedFromTheCollection": "Les elements que tu as ajoutes seront retires de cette collection.",
+ "leaveCollectionSuccessfully": "Collection quittee avec succes",
"unknownEmail": "E-mail inconnu",
"shareLink": "Partager le lien",
"creatingShareLink": "Creation du lien...",
@@ -189,6 +192,18 @@
"shareNotSupportedForSharedFiles": "Le partage n'est pas pris en charge pour les fichiers partages",
"actionNotSupportedForSharedFiles": "Action non prise en charge pour {{count, number}} elements partages",
"actionNotSupportedForSharedFiles_one": "Action non prise en charge pour 1 element partage",
+ "important": "Important",
+ "unimportant": "Non important",
+ "fileMarkedAsImportant": "Element marque comme important",
+ "fileRemovedFromImportant": "Element retire des importants",
+ "itemsMarkedAsImportant": "{{count, number}} elements marques comme importants",
+ "itemsMarkedAsImportant_one": "1 element marque comme important",
+ "itemsRemovedFromImportant": "{{count, number}} elements retires des importants",
+ "itemsRemovedFromImportant_one": "1 element retire des importants",
+ "allItemsAlreadyMarkedAsImportant": "Tous les elements selectionnes sont deja marques comme importants",
+ "importantNotSupportedForSharedFiles": "Important n'est pas pris en charge pour {{count, number}} elements partages",
+ "importantNotSupportedForSharedFiles_one": "Important n'est pas pris en charge pour 1 element partage",
+ "failedToUpdateImportantStatus": "Echec de la mise a jour du statut important",
"deleteMultipleFilesDialogBody": "{{count, number}} elements seront supprimes.",
"deleteMultipleFilesDialogBody_one": "1 element sera supprime.",
"yesDeleteFiles": "Oui, supprimer les elements",
diff --git a/web/apps/locker/src/locales/ja-JP/translation.json b/web/apps/locker/src/locales/ja-JP/translation.json
index 5dc3681d2f..3560e3649d 100644
--- a/web/apps/locker/src/locales/ja-JP/translation.json
+++ b/web/apps/locker/src/locales/ja-JP/translation.json
@@ -171,6 +171,9 @@
"failedToShareCollection": "コレクションの共有に失敗しました",
"collectionSharedSuccessfully": "コレクションを共有しました",
"viewerRemovedSuccessfully": "閲覧者を削除しました",
+ "leaveCollection": "退出",
+ "filesAddedByYouWillBeRemovedFromTheCollection": "あなたが追加したアイテムはこのコレクションから削除されます。",
+ "leaveCollectionSuccessfully": "コレクションを退出しました",
"unknownEmail": "不明なメール",
"shareLink": "リンクの共有",
"creatingShareLink": "リンクを作成中...",
@@ -191,6 +194,18 @@
"shareNotSupportedForSharedFiles": "共有ファイルでは共有はサポートされていません",
"actionNotSupportedForSharedFiles": "{{count, number}}件の共有アイテムではこの操作はサポートされていません",
"actionNotSupportedForSharedFiles_one": "1件の共有アイテムではこの操作はサポートされていません",
+ "important": "重要",
+ "unimportant": "重要ではない",
+ "fileMarkedAsImportant": "アイテムを重要に設定しました",
+ "fileRemovedFromImportant": "アイテムを重要から外しました",
+ "itemsMarkedAsImportant": "{{count, number}}件のアイテムを重要に設定しました",
+ "itemsMarkedAsImportant_one": "1件のアイテムを重要に設定しました",
+ "itemsRemovedFromImportant": "{{count, number}}件のアイテムを重要から外しました",
+ "itemsRemovedFromImportant_one": "1件のアイテムを重要から外しました",
+ "allItemsAlreadyMarkedAsImportant": "選択したアイテムはすべてすでに重要に設定されています",
+ "importantNotSupportedForSharedFiles": "{{count, number}}件の共有アイテムでは重要の設定はサポートされていません",
+ "importantNotSupportedForSharedFiles_one": "1件の共有アイテムでは重要の設定はサポートされていません",
+ "failedToUpdateImportantStatus": "重要ステータスの更新に失敗しました",
"deleteMultipleFilesDialogBody": "{{count, number}}件のアイテムを削除します。",
"deleteMultipleFilesDialogBody_one": "1件のアイテムを削除します。",
"yesDeleteFiles": "アイテムを削除",
diff --git a/web/apps/locker/src/pages/locker.tsx b/web/apps/locker/src/pages/locker.tsx
index 019946a371..4bd25fa396 100644
--- a/web/apps/locker/src/pages/locker.tsx
+++ b/web/apps/locker/src/pages/locker.tsx
@@ -73,6 +73,7 @@ export const LockerPage: React.FC = () => {
handleDrop,
handleEditItem,
handleEmptyTrash,
+ handleLeaveCollection,
handleOpenShareCollection,
handlePermanentlyDelete,
handleRenameCollection,
@@ -206,6 +207,7 @@ export const LockerPage: React.FC = () => {
onDeleteCollection={handleDeleteCollection}
onCreateCollection={handleCreateCollection}
onShareCollection={handleOpenShareCollection}
+ onLeaveCollection={handleLeaveCollection}
searchTerm={searchTerm}
onNavigateBack={handleNavigateBack}
/>
@@ -230,6 +232,7 @@ export const LockerPage: React.FC = () => {
onClose={() => setShareCollectionID(null)}
onShareCollection={handleShareCollection}
onUnshareCollection={handleUnshareCollection}
+ onLeaveCollection={handleLeaveCollection}
onRefreshSharees={fetchCollectionSharees}
/>
<DeleteCollectionDialog
diff --git a/web/apps/locker/src/services/remote-collections.ts b/web/apps/locker/src/services/remote-collections.ts
index 8baa404239..409d567bbf 100644
--- a/web/apps/locker/src/services/remote-collections.ts
+++ b/web/apps/locker/src/services/remote-collections.ts
@@ -22,6 +22,30 @@ interface EnsureUncategorizedDeps<TCollectionRecord> {
refetchCollections: (masterKey: string) => Promise<void>;
}
+const ensureCollectionWithTypeWithDeps = async <
+ TCollectionRecord extends CollectionRecordLike,
+>(
+ name: string,
+ type: string,
+ masterKey: string,
+ deps: EnsureUncategorizedDeps<TCollectionRecord>,
+): Promise<TCollectionRecord> => {
+ let collection = deps.findCollectionByType(type);
+ if (collection) {
+ return collection;
+ }
+
+ await createCollectionWithDeps(name, masterKey, type);
+ await deps.refetchCollections(masterKey);
+
+ collection = deps.findCollectionByType(type);
+ if (!collection) {
+ throw new Error(`Failed to create ${name} collection`);
+ }
+
+ return collection;
+};
+
export const createCollectionWithDeps = async (
name: string,
masterKey: string,
@@ -56,22 +80,21 @@ export const ensureUncategorizedCollectionWithDeps = async <
>(
masterKey: string,
deps: EnsureUncategorizedDeps<TCollectionRecord>,
-): Promise<TCollectionRecord> => {
- let uncategorizedCollection = deps.findCollectionByType("uncategorized");
- if (uncategorizedCollection) {
- return uncategorizedCollection;
- }
-
- await createCollectionWithDeps("Uncategorized", masterKey, "uncategorized");
- await deps.refetchCollections(masterKey);
-
- uncategorizedCollection = deps.findCollectionByType("uncategorized");
- if (!uncategorizedCollection) {
- throw new Error("Failed to create Uncategorized collection");
- }
+): Promise<TCollectionRecord> =>
+ ensureCollectionWithTypeWithDeps(
+ "Uncategorized",
+ "uncategorized",
+ masterKey,
+ deps,
+ );
- return uncategorizedCollection;
-};
+export const ensureFavoritesCollectionWithDeps = async <
+ TCollectionRecord extends CollectionRecordLike,
+>(
+ masterKey: string,
+ deps: EnsureUncategorizedDeps<TCollectionRecord>,
+): Promise<TCollectionRecord> =>
+ ensureCollectionWithTypeWithDeps("Important", "favorites", masterKey, deps);
export const renameCollectionWithDeps = async <TCollectionRecord>(
collectionID: number,
diff --git a/web/apps/locker/src/services/remote.ts b/web/apps/locker/src/services/remote.ts
index ac6f35f522..4c17b08ea8 100644
--- a/web/apps/locker/src/services/remote.ts
+++ b/web/apps/locker/src/services/remote.ts
@@ -58,6 +58,7 @@ import {
import {
createCollectionWithDeps,
deleteCollectionWithDeps,
+ ensureFavoritesCollectionWithDeps,
ensureUncategorizedCollectionWithDeps,
renameCollectionWithDeps,
} from "./remote-collections";
@@ -922,6 +923,15 @@ const ensureUncategorizedCollection = async (masterKey: string) => {
});
};
+const ensureFavoritesCollection = async (masterKey: string) => {
+ return ensureFavoritesCollectionWithDeps(masterKey, {
+ findCollectionByType,
+ refetchCollections: async (resolvedMasterKey) => {
+ await fetchLockerData(resolvedMasterKey);
+ },
+ });
+};
+
/**
* Rename a collection.
*
@@ -1006,6 +1016,61 @@ export const unshareCollection = async (
});
};
+/**
+ * Leave a shared collection.
+ */
+export const leaveCollection = async (collectionID: number): Promise<void> => {
+ const res = await fetch(
+ await apiURL(`/collections/leave/${collectionID}`),
+ { method: "POST", headers: await authenticatedRequestHeaders() },
+ );
+ ensureOk(res);
+};
+
+/**
+ * Mark or unmark a Locker item as Important.
+ */
+export const setItemImportant = async (
+ fileID: number,
+ shouldBeImportant: boolean,
+ masterKey: string,
+): Promise<boolean> => {
+ const currentCollectionIDs = getCollectionIDsForFile(fileID);
+ if (currentCollectionIDs.length === 0) {
+ throw new Error(`File ${fileID} not found in cache`);
+ }
+
+ const favoritesCollection = findCollectionByType("favorites");
+ if (!shouldBeImportant && !favoritesCollection) {
+ return false;
+ }
+
+ const favoritesCollectionID =
+ favoritesCollection?.id ??
+ (await ensureFavoritesCollection(masterKey)).id;
+ const nextCollectionIDs = shouldBeImportant
+ ? Array.from(new Set([...currentCollectionIDs, favoritesCollectionID]))
+ : currentCollectionIDs.filter(
+ (collectionID) => collectionID !== favoritesCollectionID,
+ );
+
+ const hasChanged =
+ nextCollectionIDs.length !== currentCollectionIDs.length ||
+ nextCollectionIDs.some(
+ (collectionID) => !currentCollectionIDs.includes(collectionID),
+ );
+ if (!hasChanged) {
+ return false;
+ }
+
+ await updateItemCollectionsWithDeps(fileID, nextCollectionIDs, {
+ currentUserID: ensureLocalUser().id,
+ masterKey,
+ deps: createCollectionMutationDeps(),
+ });
+ return true;
+};
+
// ---------------------------------------------------------------------------
// File upload
// ---------------------------------------------------------------------------
diff --git a/web/apps/locker/src/types.ts b/web/apps/locker/src/types.ts
index bc244f8d2d..0371395db7 100644
--- a/web/apps/locker/src/types.ts
+++ b/web/apps/locker/src/types.ts
@@ -159,6 +159,13 @@ export const canOpenCollectionSharing = (collection: LockerCollection) =>
!isImportantCollection(collection) &&
!isUncategorizedCollection(collection);
+export const canLeaveCollection = (
+ collection: LockerCollection,
+ currentUserID: number,
+) =>
+ canOpenCollectionSharing(collection) &&
+ !isCollectionOwner(collection, currentUserID);
+
export const canShareLockerFileLink = (
item: LockerItem,
currentUserID: number,
@@ -183,11 +190,7 @@ export const sortLockerCollections = (collections: LockerCollection[]) =>
});
export const visibleLockerCollections = (collections: LockerCollection[]) =>
- sortLockerCollections(
- collections.filter(
- (collection) => !isUncategorizedCollection(collection),
- ),
- );
+ sortLockerCollections(collections);
/**
* Get the display title for a locker item.