From f0b54fe49d411174fb537f82384c2f432960a0bd Mon Sep 17 00:00:00 2001 From: Kas Elvirov Date: Wed, 8 Jan 2025 14:52:13 +0300 Subject: [PATCH] #86 - add on/off toggle button --- .env | 2 +- .env.development | 2 +- .env.production | 2 +- manifest.json | 2 +- package-lock.json | 4 +- package.json | 2 +- .../containers/LocIndicator/LocIndicator.tsx | 38 ++++++++++++++---- .../Popup/components/PopupPage/PopupPage.tsx | 39 ++++++++++++++++++- src/_shared/consts/defaults.ts | 9 +++++ 9 files changed, 84 insertions(+), 16 deletions(-) diff --git a/.env b/.env index 2f26026..a107ed3 100644 --- a/.env +++ b/.env @@ -1,4 +1,4 @@ -VITE_APP_APP_VERSION=10.0.11 +VITE_APP_APP_VERSION=10.0.12 VITE_APP_TOKEN_CREATION_LINK=https://github.com/settings/tokens/new?scopes=repo&description=Github%20GLOC VITE_APP_APPLICATION_REPO=https://github.com/kas-elvirov/gloc VITE_APP_DEVELOPER_WEBSITE=https://kas-elvirov.com diff --git a/.env.development b/.env.development index 2f26026..a107ed3 100644 --- a/.env.development +++ b/.env.development @@ -1,4 +1,4 @@ -VITE_APP_APP_VERSION=10.0.11 +VITE_APP_APP_VERSION=10.0.12 VITE_APP_TOKEN_CREATION_LINK=https://github.com/settings/tokens/new?scopes=repo&description=Github%20GLOC VITE_APP_APPLICATION_REPO=https://github.com/kas-elvirov/gloc VITE_APP_DEVELOPER_WEBSITE=https://kas-elvirov.com diff --git a/.env.production b/.env.production index 2f26026..a107ed3 100644 --- a/.env.production +++ b/.env.production @@ -1,4 +1,4 @@ -VITE_APP_APP_VERSION=10.0.11 +VITE_APP_APP_VERSION=10.0.12 VITE_APP_TOKEN_CREATION_LINK=https://github.com/settings/tokens/new?scopes=repo&description=Github%20GLOC VITE_APP_APPLICATION_REPO=https://github.com/kas-elvirov/gloc VITE_APP_DEVELOPER_WEBSITE=https://kas-elvirov.com diff --git a/manifest.json b/manifest.json index 10935e0..f1c9025 100644 --- a/manifest.json +++ b/manifest.json @@ -4,7 +4,7 @@ "short_name": "Gloc", "author": "Kas Elvirov", "description": "Github Gloc - counts locs on GitHub pages", - "version": "10.0.11", + "version": "10.0.12", "action": { "default_icon": { "16": "img/icon16.png", diff --git a/package-lock.json b/package-lock.json index b7bf512..ecaf611 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "gloc", - "version": "10.0.11", + "version": "10.0.12", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "gloc", - "version": "10.0.11", + "version": "10.0.12", "license": "GPL-2.0", "dependencies": { "@crxjs/vite-plugin": "^2.0.0-beta.26", diff --git a/package.json b/package.json index 5af78fc..100aeee 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gloc", - "version": "10.0.11", + "version": "10.0.12", "engines": { "node": "16.19.0", "npm": "0.39.3" diff --git a/src/_modules/Content/containers/LocIndicator/LocIndicator.tsx b/src/_modules/Content/containers/LocIndicator/LocIndicator.tsx index caba3eb..69e37a3 100644 --- a/src/_modules/Content/containers/LocIndicator/LocIndicator.tsx +++ b/src/_modules/Content/containers/LocIndicator/LocIndicator.tsx @@ -1,6 +1,7 @@ import { useGetRepoCodeFrequencyQuery } from 'src/_shared/api/github/endpoints'; +import { SYSTEM_DEFAULTS } from 'src/_shared/consts/defaults'; -import { FC } from 'react'; +import React, { FC, useEffect } from 'react'; import { FetchBaseQueryError } from '@reduxjs/toolkit/query'; @@ -22,11 +23,32 @@ export const LocIndicator: FC = ({ repository, token, }) => { - const response = useGetRepoCodeFrequencyQuery({ - repoName: repository, - author: author, - token, - }); + const [isAppEnabled, setAppStatus] = React.useState( + SYSTEM_DEFAULTS.STORAGE.APP_MODE.DEFAULT_VALUE, + ); + + useEffect(() => { + chrome?.storage?.sync?.get( + { + [SYSTEM_DEFAULTS.STORAGE.APP_MODE.KEY]: + SYSTEM_DEFAULTS.STORAGE.APP_MODE.DEFAULT_VALUE, + }, + result => { + setAppStatus(result[SYSTEM_DEFAULTS.STORAGE.APP_MODE.KEY]); + }, + ); + }, []); + + const response = useGetRepoCodeFrequencyQuery( + { + repoName: repository, + author: author, + token, + }, + { + skip: isAppEnabled === false, + }, + ); const { data, error, isFetching, refetch } = response; @@ -42,7 +64,7 @@ export const LocIndicator: FC = ({ * In order to prevent this error * - [Cannot refetch a query that has not been started yet](https://redux-toolkit.js.org/Errors?code=38) */ - if (!isFetching && needsToRetry) { + if (isAppEnabled === true && !isFetching && needsToRetry) { refetch(); } @@ -50,7 +72,7 @@ export const LocIndicator: FC = ({ diff --git a/src/_modules/Popup/components/PopupPage/PopupPage.tsx b/src/_modules/Popup/components/PopupPage/PopupPage.tsx index 5e4736e..5876d99 100644 --- a/src/_modules/Popup/components/PopupPage/PopupPage.tsx +++ b/src/_modules/Popup/components/PopupPage/PopupPage.tsx @@ -1,4 +1,6 @@ -import { FC } from 'react'; +import { SYSTEM_DEFAULTS } from 'src/_shared/consts/defaults'; + +import React, { FC, useEffect } from 'react'; import GitHubIcon from '@mui/icons-material/GitHub'; import SettingsIcon from '@mui/icons-material/Settings'; @@ -8,6 +10,7 @@ import { IconButton, Paper, Stack, + Switch, Typography, } from '@mui/material'; import { pink } from '@mui/material/colors'; @@ -24,6 +27,31 @@ import { useStyles } from './PopupPage.styles'; export const PopupPage: FC = () => { const classes = useStyles(); + const [isAppEnabled, setAppStatus] = React.useState( + SYSTEM_DEFAULTS.STORAGE.APP_MODE.DEFAULT_VALUE, + ); + + useEffect(() => { + chrome?.storage?.sync?.get( + { + [SYSTEM_DEFAULTS.STORAGE.APP_MODE.KEY]: + SYSTEM_DEFAULTS.STORAGE.APP_MODE.DEFAULT_VALUE, + }, + result => { + setAppStatus(result[SYSTEM_DEFAULTS.STORAGE.APP_MODE.KEY]); + }, + ); + }, []); + + const handleChangeAppMode = (event: React.ChangeEvent) => { + setAppStatus(event.target.checked); + + chrome.storage?.sync?.set?.( + { [SYSTEM_DEFAULTS.STORAGE.APP_MODE.KEY]: event.target.checked }, + () => {}, + ); + }; + const onSettingsClick = () => { chrome.tabs.create({ url: @@ -65,6 +93,15 @@ export const PopupPage: FC = () => { + + diff --git a/src/_shared/consts/defaults.ts b/src/_shared/consts/defaults.ts index 028f18d..176b00e 100644 --- a/src/_shared/consts/defaults.ts +++ b/src/_shared/consts/defaults.ts @@ -1,5 +1,14 @@ export const SYSTEM_DEFAULTS = { STORAGE: { + /** + * Is app working or not + * - true - yes + * - false - not yes + */ + APP_MODE: { + KEY: 'x-app-mode', + DEFAULT_VALUE: false, + }, GITHUB_TOKEN: { KEY: 'x-github-token', DEFAULT_VALUE: '',