From c4ac4fa30425e271cf8f64a9f4d82c162be4c679 Mon Sep 17 00:00:00 2001 From: Abhinav Date: Mon, 1 Aug 2022 10:23:32 +0530 Subject: [PATCH] add ML sidebar options --- src/components/Sidebar/UtilitySection.tsx | 89 ++++++++++++++++++++++- src/utils/strings/englishConstants.tsx | 4 +- 2 files changed, 90 insertions(+), 3 deletions(-) diff --git a/src/components/Sidebar/UtilitySection.tsx b/src/components/Sidebar/UtilitySection.tsx index 55618f3d5..5449f21fd 100644 --- a/src/components/Sidebar/UtilitySection.tsx +++ b/src/components/Sidebar/UtilitySection.tsx @@ -7,10 +7,17 @@ import TwoFactorModal from 'components/TwoFactor/Modal'; import { PAGES } from 'constants/pages'; import { useRouter } from 'next/router'; import { AppContext } from 'pages/_app'; +import { canEnableMlSearch } from 'utils/machineLearning/compatibility'; +import mlIDbStorage from 'utils/storage/mlIDbStorage'; export default function UtilitySection({ closeSidebar }) { const router = useRouter(); - const { setDialogMessage, startLoading } = useContext(AppContext); + const { + setDialogMessage, + startLoading, + mlSearchEnabled, + updateMlSearchEnabled, + } = useContext(AppContext); const [recoverModalView, setRecoveryModalView] = useState(false); const [twoFactorModalView, setTwoFactorModalView] = useState(false); @@ -43,6 +50,46 @@ export default function UtilitySection({ closeSidebar }) { close: { variant: 'danger' }, }); + const redirectToMLDebug = () => { + router.push(PAGES.ML_DEBUG); + }; + + const enableMlSearch = async () => { + await updateMlSearchEnabled(true); + }; + const disableMlSearch = async () => { + await updateMlSearchEnabled(false); + }; + + const clearMLDB = async () => { + await mlIDbStorage.clearMLDB(); + }; + + const toggleMLSearch = () => { + if (!mlSearchEnabled) { + if (!canEnableMlSearch()) { + setDialogMessage({ + title: constants.ENABLE_ML_SEARCH, + content: constants.ML_SEARCH_NOT_COMPATIBLE, + close: { text: constants.OK }, + }); + return; + } + setDialogMessage({ + title: constants.ENABLE_ML_SEARCH, + content: constants.ENABLE_ML_SEARCH_MESSAGE, + staticBackdrop: true, + proceed: { + text: constants.ENABLE, + action: enableMlSearch, + variant: 'accent', + }, + close: { text: constants.CANCEL }, + }); + } else { + disableMlSearch(); + } + }; return ( <> @@ -60,6 +107,46 @@ export default function UtilitySection({ closeSidebar }) { {constants.DEDUPLICATE_FILES} + + {mlSearchEnabled + ? constants.DISABLE_ML_SEARCH + : constants.ENABLE_ML_SEARCH} + + + { + if (!mlSearchEnabled) { + if (!canEnableMlSearch()) { + setDialogMessage({ + title: constants.ENABLE_ML_SEARCH, + content: constants.ML_SEARCH_NOT_COMPATIBLE, + close: { text: constants.OK }, + }); + return; + } + setDialogMessage({ + title: 'clear mb db', + content: 'clear mb db', + staticBackdrop: true, + proceed: { + text: 'clear', + action: clearMLDB, + variant: 'accent', + }, + close: { text: constants.CANCEL }, + }); + } else { + disableMlSearch(); + } + }}> + {'Clear ML db'} + + + {mlSearchEnabled && ( + + {constants.ML_DEBUG} + + )} {/* {constants.COMPRESS_THUMBNAILS} diff --git a/src/utils/strings/englishConstants.tsx b/src/utils/strings/englishConstants.tsx index 0da193fc3..7bf6644d3 100644 --- a/src/utils/strings/englishConstants.tsx +++ b/src/utils/strings/englishConstants.tsx @@ -189,8 +189,8 @@ const englishConstants = { registered email address ), - ENABLE_ML_SEARCH: 'enable ML Search beta', - DISABLE_ML_SEARCH: 'disable ML Search beta', + ENABLE_ML_SEARCH: 'Enable ML Search beta', + DISABLE_ML_SEARCH: 'Disable ML Search beta', ENABLE_ML_SEARCH_MESSAGE: 'This will enable on-device machine learning and people search. ' + 'It will keep analyzing your uploaded photos locally and will store ' +