Skip to content

Commit

Permalink
refactor(store): use custom hooks instead of redux default hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
dubisdev committed Jan 6, 2024
1 parent 13479c8 commit 2211990
Show file tree
Hide file tree
Showing 16 changed files with 37 additions and 75 deletions.
7 changes: 2 additions & 5 deletions app/renderer/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@ import {
} from "contexts";
import { Layout, Preloader } from "components";
import { compactRoutes, routes } from "config";
import { useSelector } from "react-redux";
import { AppStateTypes } from "store";
import { useAppSelector } from "hooks/storeHooks";

export default function App() {
const settings = useSelector(
(state: AppStateTypes) => state.settings
);
const settings = useAppSelector((state) => state.settings);

useEffect(() => {
const contextEvent = (event: MouseEvent) => {
Expand Down
16 changes: 4 additions & 12 deletions app/renderer/src/components/Updater.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@ import Header from "./Header";
import styled from "styled-components/macro";
import ReactMarkdown from "react-markdown";
import { useAppDispatch, useAppSelector } from "hooks/storeHooks";
import { AppStateTypes, setIgnoreUpdate, SettingTypes } from "../store";
import {
setUpdateBody,
setUpdateVersion,
UpdateTypes,
} from "../store/update";
import { setIgnoreUpdate } from "../store";
import { setUpdateBody, setUpdateVersion } from "../store/update";
import {
StyledButtonNormal,
StyledButtonPrimary,
Expand Down Expand Up @@ -51,12 +47,8 @@ const IgnoreVersion = styled.div`
`;

const Updater: React.FC = () => {
const settings: SettingTypes = useAppSelector(
(state: AppStateTypes) => state.settings
);
const update: UpdateTypes = useAppSelector(
(state: AppStateTypes) => state.update
);
const settings = useAppSelector((state) => state.settings);
const update = useAppSelector((state) => state.update);

const dispatch = useAppDispatch();

Expand Down
2 changes: 1 addition & 1 deletion app/renderer/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SVGTypes } from "components";
import { TaskList, Config, Timer, Settings } from "routes";
import { ConfigSliderProps } from "routes";
import { AppStateTypes, SettingTypes } from "./store";
import { AppStateTypes } from "./store";
import { DefaultRootState } from "react-redux";

export const APP_NAME = "Pomatez";
Expand Down
6 changes: 2 additions & 4 deletions app/renderer/src/contexts/CounterContext.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useState, useEffect, useCallback } from "react";
import useStayAwake from "use-stay-awake";
import { setRound, setTimerType, SettingTypes, setPlay } from "store";
import { setRound, setTimerType, setPlay } from "store";
import { useNotification } from "hooks";
import { padNum, isEqualToOne } from "utils";

Expand Down Expand Up @@ -36,9 +36,7 @@ const CounterProvider: React.FC = ({ children }) => {
config: state.config,
}));

const settings: SettingTypes = useAppSelector(
(state) => state.settings
);
const settings = useAppSelector((state) => state.settings);

const { preventSleeping, allowSleeping } = useStayAwake();

Expand Down
10 changes: 4 additions & 6 deletions app/renderer/src/contexts/ThemeContext.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React, { useRef } from "react";
import { isPreferredDark } from "utils";
import { GlobalStyles } from "styles";
import { useSelector, useDispatch } from "react-redux";
import { AppStateTypes, setEnableDarkTheme, SettingTypes } from "store";
import { useAppDispatch, useAppSelector } from "hooks/storeHooks";
import { setEnableDarkTheme } from "store";

type ThemeProps = {
isDarkMode: boolean;
Expand All @@ -14,11 +14,9 @@ const ThemeContext = React.createContext<ThemeProps>({
});

const ThemeProvider: React.FC = ({ children }) => {
const settings: SettingTypes = useSelector(
(state: AppStateTypes) => state.settings
);
const settings = useAppSelector((state) => state.settings);

const dispatch = useDispatch();
const dispatch = useAppDispatch();

const useNativeTitlebar = useRef(settings.useNativeTitlebar);

Expand Down
9 changes: 3 additions & 6 deletions app/renderer/src/contexts/connectors/ElectronConnector.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { useCallback, useContext, useEffect } from "react";
import { ConnnectorContext } from "../ConnnectorContext";
import { useSelector } from "react-redux";
import { AppStateTypes, SettingTypes } from "../../store";
import { useAppSelector } from "hooks/storeHooks";
import { CounterContext } from "../CounterContext";
import {
SET_ALWAYS_ON_TOP,
Expand Down Expand Up @@ -29,11 +28,9 @@ export const ElectronInvokeConnector: InvokeConnector = {
export const ElectronConnectorProvider: React.FC = ({ children }) => {
const { electron } = window;

const timer = useSelector((state: AppStateTypes) => state.timer);
const timer = useAppSelector((state) => state.timer);

const settings: SettingTypes = useSelector(
(state: AppStateTypes) => state.settings
);
const settings = useAppSelector((state) => state.settings);

const { shouldFullscreen } = useContext(CounterContext);

Expand Down
11 changes: 4 additions & 7 deletions app/renderer/src/contexts/connectors/TauriConnector.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { useCallback, useContext, useEffect } from "react";
import { ConnnectorContext } from "../ConnnectorContext";
import { useDispatch, useSelector } from "react-redux";
import { AppStateTypes, SettingTypes } from "../../store";
import { useAppDispatch, useAppSelector } from "hooks/storeHooks";
import { CounterContext } from "../CounterContext";
import {
CHECK_FOR_UPDATES,
Expand Down Expand Up @@ -36,11 +35,9 @@ export const TauriInvokeConnector = {
};

export const TauriConnectorProvider: React.FC = ({ children }) => {
const dispatch = useDispatch();
const dispatch = useAppDispatch();

const settings: SettingTypes = useSelector(
(state: AppStateTypes) => state.settings
);
const settings = useAppSelector((state) => state.settings);

// Prevent webpage behavior (naitive apps shouldn't refresh with F5 or Ctrl+R)
useEffect(() => {
Expand Down Expand Up @@ -114,7 +111,7 @@ export const TauriConnectorProvider: React.FC = ({ children }) => {
});
}, [settings.openAtLogin]);

const timer = useSelector((state: AppStateTypes) => state.timer);
const timer = useAppSelector((state) => state.timer);

const { shouldFullscreen } = useContext(CounterContext);

Expand Down
5 changes: 2 additions & 3 deletions app/renderer/src/hooks/useTrayIconUpdates.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { CounterContext } from "contexts";
import { useEffect, useContext } from "react";
import { useSelector } from "react-redux";
import type { AppStateTypes } from "store";
import { useAppSelector } from "./storeHooks";
import { TraySVG } from "components";
import { encodeSvg } from "utils";

export const useTrayIconUpdates = (
onNewIcon: (dataUrl: string) => void
) => {
const timer = useSelector((state: AppStateTypes) => state.timer);
const timer = useAppSelector((state) => state.timer);

const { count, duration, timerType } = useContext(CounterContext);
const dashOffset = (duration - count) * (24 / duration);
Expand Down
4 changes: 2 additions & 2 deletions app/renderer/src/routes/Config/ConfigHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React, { useCallback, useState } from "react";
import { useDispatch } from "react-redux";
import { useAppDispatch } from "hooks/storeHooks";
import { restoreDefaultConfig } from "store";

import { StyledHeaderButton } from "styles";
import { Header } from "components";

const ConfigHeader: React.FC = () => {
const dispatch = useDispatch();
const dispatch = useAppDispatch();

const [success, setSuccess] = useState(false);

Expand Down
3 changes: 1 addition & 2 deletions app/renderer/src/routes/Config/SpecialBreaks.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { useCallback } from "react";
import { useAppSelector, useAppDispatch } from "hooks/storeHooks";
import {
AppStateTypes,
setFirstSpecialBreak,
setSecondSpecialBreak,
setThirdSpecialBreak,
Expand All @@ -15,7 +14,7 @@ import {
import SpecialField from "./SpecialField";

const SpecialBreaks: React.FC = () => {
const config = useAppSelector((state: AppStateTypes) => state.config);
const config = useAppSelector((state) => state.config);

const dispath = useAppDispatch();

Expand Down
6 changes: 1 addition & 5 deletions app/renderer/src/routes/Settings/FeatureSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import { useAppDispatch, useAppSelector } from "hooks/storeHooks";
import {
setAlwaysOnTop,
setEnableStrictMode,
AppStateTypes,
setEnableProgressAnimation,
SettingTypes,
setNotificationType,
setEnableFullscreenBreak,
setUseNativeTitlebar,
Expand All @@ -24,9 +22,7 @@ import { detectOS } from "utils";
import { NotificationTypes } from "store/settings/types";

const FeatureSection: React.FC = () => {
const settings: SettingTypes = useAppSelector(
(state: AppStateTypes) => state.settings
);
const settings = useAppSelector((state) => state.settings);

const dispatch = useAppDispatch();

Expand Down
4 changes: 2 additions & 2 deletions app/renderer/src/routes/Settings/SettingHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React, { useCallback, useState } from "react";
import { restoreDefaultSettings } from "store";
import { useDispatch } from "react-redux";
import { useAppDispatch } from "hooks/storeHooks";

import { Header } from "components";
import { StyledHeaderButton } from "styles";

const SettingHeader: React.FC = () => {
const dispatch = useDispatch();
const dispatch = useAppDispatch();

const [success, setSuccess] = useState(false);

Expand Down
8 changes: 2 additions & 6 deletions app/renderer/src/routes/Settings/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,13 @@ import HelpSection from "./HelpSection";
import ShortcutSection from "./ShortcutSection";
import StickySection from "./StickySection";
import SettingHeader from "./SettingHeader";
import { AppStateTypes, SettingTypes } from "../../store";
import { useSelector } from "react-redux";
import { UpdateTypes } from "../../store/update";
import { useAppSelector } from "hooks/storeHooks";
import { Updater } from "../../components";

export default function Settings() {
const alertState = getFromStorage("alert") || null;

const update: UpdateTypes = useSelector(
(state: AppStateTypes) => state.update
);
const update = useAppSelector((state) => state.update);

const [alert, setAlert] = useState(alertState);

Expand Down
7 changes: 2 additions & 5 deletions app/renderer/src/routes/Timer/Control/CompactModeButton.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import { SVG } from "components";
import { CounterContext } from "contexts";
import { useRippleEffect } from "hooks";
import { useAppSelector } from "hooks/storeHooks";
import React, { useCallback, useRef } from "react";
import { useSelector } from "react-redux";
import { AppStateTypes, SettingTypes } from "store";
import { StyledCompactButton } from "styles";

type Props = { flipped?: boolean } & React.HTMLProps<HTMLButtonElement>;

const CompactModeButton: React.FC<Props> = ({ onClick, flipped }) => {
const { compactMode }: SettingTypes = useSelector(
(state: AppStateTypes) => state.settings
);
const { compactMode } = useAppSelector((state) => state.settings);

const buttonRef = useRef<HTMLButtonElement>(null);

Expand Down
7 changes: 2 additions & 5 deletions app/renderer/src/routes/Timer/Counter/Counter.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { CounterContext } from "contexts";
import { useTime } from "hooks";
import React, { useContext } from "react";
import { useSelector } from "react-redux";
import { AppStateTypes, SettingTypes } from "store";
import { useAppSelector } from "hooks/storeHooks";
import {
StyledCounterContainer,
StyledCounterProgress,
Expand All @@ -13,9 +12,7 @@ import CounterTimer from "./CounterTimer";
import CounterType from "./CounterType";

const Counter: React.FC = () => {
const settings: SettingTypes = useSelector(
(state: AppStateTypes) => state.settings
);
const settings = useAppSelector((state) => state.settings);

const { count, duration, timerType, shouldFullscreen } =
useContext(CounterContext);
Expand Down
7 changes: 3 additions & 4 deletions app/renderer/src/routes/Timer/index.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { CounterContext } from "contexts";
import React, { useCallback, useContext } from "react";
import { useSelector } from "react-redux";
import { AppStateTypes } from "store";
import { useAppSelector } from "hooks/storeHooks";
import { StyledTimer } from "styles";
import Control from "./Control";
import Counter from "./Counter";
import PriorityCard from "./PriorityCard";

export default function Timer() {
const compactMode = useSelector(
(state: AppStateTypes) => state.settings.compactMode
const compactMode = useAppSelector(
(state) => state.settings.compactMode
);
const { resetTimerAction } = useContext(CounterContext);

Expand Down

0 comments on commit 2211990

Please sign in to comment.