From 45a9a7ad4bf79d3dcb12435bb128b5c4dc82307e Mon Sep 17 00:00:00 2001 From: Narin Sundarabhaya Date: Mon, 30 Oct 2023 16:40:47 +0700 Subject: [PATCH] refactor(Previous List): add more info to each item --- src/db/actions.ts | 51 ++++---- src/renderer/src/actions/ipc.ts | 4 +- src/renderer/src/actions/types.ts | 2 +- .../src/components/PreviousResults.tsx | 112 ++++++++++++------ src/renderer/src/hooks/useScansList.ts | 4 +- 5 files changed, 108 insertions(+), 65 deletions(-) diff --git a/src/db/actions.ts b/src/db/actions.ts index b24dfbc..a7ca7f2 100644 --- a/src/db/actions.ts +++ b/src/db/actions.ts @@ -82,32 +82,35 @@ export const updateScanById = async ( } export const getScansList = (): Promise< - DatabaseOperationResult[]> + DatabaseOperationResult[]> > => { - return performDatabaseOperation[]>(() => - prisma.scan.findMany({ - where: { - status: 'completed' - }, - select: { - id: true, - createdAt: true, - status: true, - deletedFiles: { - select: { - id: true, - count: true, - status: true, - errors: true, - success: true, - scanId: true + return performDatabaseOperation[]>( + () => + prisma.scan.findMany({ + where: { + status: 'completed' + }, + select: { + id: true, + createdAt: true, + status: true, + configuration: true, + deletedFiles: { + select: { + id: true, + count: true, + status: true, + errors: true, + success: true, + scanId: true + } } - } - }, - orderBy: { - createdAt: 'desc' - } - }) + }, + orderBy: { + createdAt: 'desc' + }, + take: 10 + }) ) } diff --git a/src/renderer/src/actions/ipc.ts b/src/renderer/src/actions/ipc.ts index c80a1c9..5f804f9 100644 --- a/src/renderer/src/actions/ipc.ts +++ b/src/renderer/src/actions/ipc.ts @@ -107,9 +107,9 @@ export const insertScan = ( } export const getScansList = (): Promise< - DatabaseOperationResult[]> + DatabaseOperationResult[]> > => { - return invokeIPC[]>(GET_SCANS_LIST) + return invokeIPC[]>(GET_SCANS_LIST) } export const deleteFiles = ( diff --git a/src/renderer/src/actions/types.ts b/src/renderer/src/actions/types.ts index 0eb611b..8cf461f 100644 --- a/src/renderer/src/actions/types.ts +++ b/src/renderer/src/actions/types.ts @@ -22,7 +22,7 @@ export type MainState = { directorySrcs: FilesDirectory[] error: string | null scans: Record - allScans: Pick[] + allScans: Pick[] scanConfiguration: { type: 'audio' | 'image' includeCrates: boolean diff --git a/src/renderer/src/components/PreviousResults.tsx b/src/renderer/src/components/PreviousResults.tsx index b3d64f0..dc50628 100644 --- a/src/renderer/src/components/PreviousResults.tsx +++ b/src/renderer/src/components/PreviousResults.tsx @@ -1,7 +1,9 @@ +import { Scan } from '@prisma/client' import useMain from '@renderer/context/hooks/useMain' import useScansList from '@renderer/hooks/useScansList' import { UPDATE_ACTIVE_TAB } from '@src/constants' -import { format } from 'date-fns' +import { ScanConfigurationSchema } from '@src/types' +import { formatDistanceToNow } from 'date-fns' import { useMemo } from 'react' const classNames = { @@ -10,10 +12,80 @@ const classNames = { ul: 'dropdown-content z-[1] menu p-2 shadow bg-base-100 rounded-box w-52 max-h-80 flex-nowrap overflow-y-auto' } +const SCAN_TYPE = { + duplicate: 'Duplicate', + not_crated: 'Not in Crate' +} + +const TYPE = { + audio: 'Audio', + image: 'Image' +} + +const ResultItem = ({ + scan +}: { + scan: Pick +}): JSX.Element => { + const { dispatch } = useMain() + + const configuration = useMemo(() => { + const configurationRes = ScanConfigurationSchema.safeParse(JSON.parse(scan.configuration)) + + return configurationRes.success ? configurationRes.data : null + }, [scan.configuration]) + + return ( +
  • { + dispatch({ + type: 'ADD_NEW_SCAN', + payload: { + id: scan.id, + scan: { + ...scan, + status: 'ready', + createdAt: new Date(), + updatedAt: new Date(), + results: { files: {} }, + configuration: configuration ?? { + directoryPaths: [], + type: 'audio', + matchType: 'name', + includeCrates: false, + scanType: 'duplicate' + }, + deletedFiles: [] + } + } + }) + + dispatch({ + type: UPDATE_ACTIVE_TAB, + payload: { + activeTab: scan.id + } + }) + }} + > + + + {SCAN_TYPE[configuration?.scanType as keyof typeof SCAN_TYPE]}{' '} + {TYPE[configuration?.type as keyof typeof TYPE]} + + + ({formatDistanceToNow(scan.createdAt, { addSuffix: true })}) + + +
  • + ) +} + export default function PreviousResults(): JSX.Element { // fetch previous results useScansList() - const { state, dispatch } = useMain() + const { state } = useMain() const list = useMemo(() => state.allScans, [state.allScans]) return ( @@ -26,41 +98,7 @@ export default function PreviousResults(): JSX.Element {
      {list.map((scan) => ( -
    • { - dispatch({ - type: 'ADD_NEW_SCAN', - payload: { - id: scan.id, - scan: { - ...scan, - status: 'ready', - createdAt: new Date(), - updatedAt: new Date(), - results: { files: {} }, - configuration: { - directoryPaths: [], - type: 'audio', - matchType: 'name', - includeCrates: false, - scanType: 'duplicate' - }, - deletedFiles: [] - } - } - }) - - dispatch({ - type: UPDATE_ACTIVE_TAB, - payload: { - activeTab: scan.id - } - }) - }} - > - {format(new Date(scan.createdAt), 'MMM dd, yy h:mma')} -
    • + ))}
    diff --git a/src/renderer/src/hooks/useScansList.ts b/src/renderer/src/hooks/useScansList.ts index 54b2c26..6210968 100644 --- a/src/renderer/src/hooks/useScansList.ts +++ b/src/renderer/src/hooks/useScansList.ts @@ -8,7 +8,9 @@ import { useEffect } from 'react' export default function useScansList(): void { const { dispatch } = useMain() - const { data } = useQuery[]>>({ + const { data } = useQuery< + DatabaseOperationResult[]> + >({ queryKey: ['scansList'], queryFn: getScansList })