Skip to content

Commit

Permalink
#86 - add on/off toggle button
Browse files Browse the repository at this point in the history
  • Loading branch information
kas-elvirov committed Jan 8, 2025
1 parent 3a6ae18 commit f0b54fe
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion .env.development
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion .env.production
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gloc",
"version": "10.0.11",
"version": "10.0.12",
"engines": {
"node": "16.19.0",
"npm": "0.39.3"
Expand Down
38 changes: 30 additions & 8 deletions src/_modules/Content/containers/LocIndicator/LocIndicator.tsx
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -22,11 +23,32 @@ export const LocIndicator: FC<LocIndicatorProps> = ({
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;

Expand All @@ -42,15 +64,15 @@ export const LocIndicator: FC<LocIndicatorProps> = ({
* 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();
}

return (
<Chip
disabled={isFetching}
color={isFetching ? undefined : isError ? 'error' : 'success'}
label={loc}
label={isAppEnabled ? loc : 'App is disabled'}
avatar={
isFetching ? (
<CircularProgress size='16px' />
Expand Down
39 changes: 38 additions & 1 deletion src/_modules/Popup/components/PopupPage/PopupPage.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -8,6 +10,7 @@ import {
IconButton,
Paper,
Stack,
Switch,
Typography,
} from '@mui/material';
import { pink } from '@mui/material/colors';
Expand All @@ -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<HTMLInputElement>) => {
setAppStatus(event.target.checked);

chrome.storage?.sync?.set?.(
{ [SYSTEM_DEFAULTS.STORAGE.APP_MODE.KEY]: event.target.checked },
() => {},
);
};

const onSettingsClick = () => {
chrome.tabs.create({
url:
Expand Down Expand Up @@ -65,6 +93,15 @@ export const PopupPage: FC = () => {
</IconButton>
</Stack>

<Switch
checked={isAppEnabled}
inputProps={{ 'aria-label': 'App mode' }}
defaultChecked
color='secondary'
onChange={handleChangeAppMode}
title={`App is ${isAppEnabled ? 'enabled' : 'disabled'}`}
/>

<Chip
avatar={
<Avatar>
Expand Down
9 changes: 9 additions & 0 deletions src/_shared/consts/defaults.ts
Original file line number Diff line number Diff line change
@@ -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: '',
Expand Down

0 comments on commit f0b54fe

Please sign in to comment.