From 653ae485a983ee00e593ebde7e5210ea02be4230 Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Tue, 17 Dec 2024 18:18:52 +0530 Subject: [PATCH] Reducer --- web/packages/new/photos/pages/duplicates.tsx | 61 ++++++++++++++++++-- 1 file changed, 55 insertions(+), 6 deletions(-) diff --git a/web/packages/new/photos/pages/duplicates.tsx b/web/packages/new/photos/pages/duplicates.tsx index b4e36be8cc..14c940a241 100644 --- a/web/packages/new/photos/pages/duplicates.tsx +++ b/web/packages/new/photos/pages/duplicates.tsx @@ -6,29 +6,48 @@ import MoreHorizIcon from "@mui/icons-material/MoreHoriz"; import SortIcon from "@mui/icons-material/Sort"; import { Box, IconButton, Stack, Typography } from "@mui/material"; import { useRouter } from "next/router"; -import React, { useEffect } from "react"; +import React, { useEffect, useReducer } from "react"; import type { DuplicateGroup } from "../services/dedup"; import { useAppContext } from "../types/context"; const Page: React.FC = () => { const { showNavBar } = useAppContext(); + const [state, dispatch] = useReducer(dedupReducer, initialDedupState); + useEffect(() => { showNavBar(false); + console.log(dispatch); }, []); + const contents = (() => { + switch (state.status) { + case undefined: + case "analyzing": + return ; + default: + return ; + } + })(); + return ( - + {contents} ); }; export default Page; -interface DuplicatesState { - status: "analyzing" | "deleting" | undefined; +interface DedupState { + status: + | undefined + | "analyzing" + | "analysisFailed" + | "showingResults" + | "deleting" + | "deletionFailed"; /** * Groups of duplicates. * @@ -43,7 +62,7 @@ interface DuplicatesState { /** * The attribute to use for sorting {@link duplicateGroups}. */ - sortType: "prunableCount" | "prunableSize"; + sortOrder: "prunableCount" | "prunableSize"; /** * The number of files that will be pruned if the user decides to dedup the * current selection. @@ -56,6 +75,36 @@ interface DuplicatesState { prunableSize: number; } +type DedupAction = + | { type: "analyze" } + | { type: "analysisFailed" } + | { type: "analyzed"; duplicateGroups: DuplicateGroup[] } + | { type: "changeSortOrder"; sortOrder: DedupState["sortOrder"] } + | { type: "select"; index: number } + | { type: "deselect"; index: number } + | { type: "deselectAll" } + | { type: "dedup" } + | { type: "dedupCompleted" } + | { type: "dedupFailed" }; + +const initialDedupState: DedupState = { + status: undefined, + duplicateGroups: [], + sortOrder: "prunableSize", + prunableCount: 0, + prunableSize: 0, +}; + +const dedupReducer: React.Reducer = ( + state, + action, +) => { + switch (action.type) { + default: + return state; + } +}; + const Navbar: React.FC = () => { const router = useRouter(); @@ -87,7 +136,7 @@ const Navbar: React.FC = () => { ); }; -const Contents: React.FC = () => { +const Loading: React.FC = () => { return (