added isShared property to collection and isSharedCollectionActive state

This commit is contained in:
abhinav-grd 2021-09-20 19:16:08 +05:30
parent 1e45557e34
commit 5fcde0b6e6
3 changed files with 49 additions and 4 deletions

View file

@ -65,6 +65,8 @@ import { PAGES } from 'types';
import { import {
copyOrMoveFromCollection, copyOrMoveFromCollection,
COLLECTION_OPS_TYPE, COLLECTION_OPS_TYPE,
addIsSharedProperty,
isSharedCollection,
} from 'utils/collection'; } from 'utils/collection';
import { logError } from 'utils/sentry'; import { logError } from 'utils/sentry';
@ -172,6 +174,9 @@ export default function Gallery() {
useState<Map<number, number>>(); useState<Map<number, number>>();
const [activeCollection, setActiveCollection] = useState(0); const [activeCollection, setActiveCollection] = useState(0);
const [isSharedCollectionActive, setIsSharedCollectionActive] =
useState(false);
useEffect(() => { useEffect(() => {
const key = getKey(SESSION_KEYS.ENCRYPTION_KEY); const key = getKey(SESSION_KEYS.ENCRYPTION_KEY);
if (!key) { if (!key) {
@ -220,6 +225,13 @@ export default function Gallery() {
}`; }`;
router.push(href, undefined, { shallow: true }); router.push(href, undefined, { shallow: true });
}, [activeCollection]); }, [activeCollection]);
useEffect(
() =>
setIsSharedCollectionActive(
isSharedCollection(collections, activeCollection)
),
[activeCollection]
);
const syncWithRemote = async (force = false, silent = false) => { const syncWithRemote = async (force = false, silent = false) => {
if (syncInProgress.current && !force) { if (syncInProgress.current && !force) {
@ -269,10 +281,19 @@ export default function Gallery() {
} }
}; };
const setDerivativeState = async (collections, files) => { const setDerivativeState = async (
const nonEmptyCollections = getNonEmptyCollections(collections, files); collections: Collection[],
const collectionsAndTheirLatestFile = files: File[]
await getCollectionsAndTheirLatestFile(nonEmptyCollections, files); ) => {
const collectionsWithIsSharedInfo = addIsSharedProperty(collections);
const nonEmptyCollections = getNonEmptyCollections(
collectionsWithIsSharedInfo,
files
);
const collectionsAndTheirLatestFile = getCollectionsAndTheirLatestFile(
nonEmptyCollections,
files
);
const collectionWiseFiles = sortFilesIntoCollections(files); const collectionWiseFiles = sortFilesIntoCollections(files);
const collectionFilesCount = new Map<number, number>(); const collectionFilesCount = new Map<number, number>();
for (const [id, files] of collectionWiseFiles) { for (const [id, files] of collectionWiseFiles) {
@ -542,6 +563,7 @@ export default function Gallery() {
deleted={deleted} deleted={deleted}
setDialogMessage={setDialogMessage} setDialogMessage={setDialogMessage}
activeCollection={activeCollection} activeCollection={activeCollection}
isSharedCollection={isSharedCollectionActive}
/> />
{selected.count > 0 && {selected.count > 0 &&
selected.collectionID === activeCollection && ( selected.collectionID === activeCollection && (

View file

@ -39,6 +39,7 @@ export interface Collection {
encryptedKey: string; encryptedKey: string;
keyDecryptionNonce: string; keyDecryptionNonce: string;
isDeleted: boolean; isDeleted: boolean;
isSharedCollection?: boolean;
} }
interface EncryptedFileKey { interface EncryptedFileKey {

View file

@ -9,6 +9,8 @@ import { getSelectedFiles } from 'utils/file';
import { File } from 'services/fileService'; import { File } from 'services/fileService';
import { CustomError } from 'utils/common/errorUtil'; import { CustomError } from 'utils/common/errorUtil';
import { SelectedState } from 'pages/gallery'; import { SelectedState } from 'pages/gallery';
import { User } from 'services/userService';
import { getData, LS_KEYS } from 'utils/storage/localStorage';
export enum COLLECTION_OPS_TYPE { export enum COLLECTION_OPS_TYPE {
ADD, ADD,
@ -58,3 +60,23 @@ export async function copyOrMoveFromCollection(
export function getSelectedCollection(collectionID: number, collections) { export function getSelectedCollection(collectionID: number, collections) {
return collections.find((collection) => collection.id === collectionID); return collections.find((collection) => collection.id === collectionID);
} }
export function addIsSharedProperty(collections: Collection[]) {
const user: User = getData(LS_KEYS.USER);
for (const collection of collections) {
if (user.id === collection.owner.id) {
collection.isSharedCollection = false;
} else {
collection.isSharedCollection = true;
}
}
return collections;
}
export function isSharedCollection(
collections: Collection[],
collectionID: number
) {
return !!collections.find((collection) => collection.id === collectionID)
?.isSharedCollection;
}