Skip to content

Commit

Permalink
chore: refactor npmrc, refactor local storage handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
uigywnkiub committed Dec 24, 2024
1 parent 3011a69 commit 08ef69c
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 14 deletions.
5 changes: 3 additions & 2 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# public-hoist-pattern[]=*@nextui-org/*
public-hoist-pattern[]=['*@nextui-org/*', '*eslint*', '*prettier*']
public-hoist-pattern[]=*@nextui-org/*
public-hoist-pattern[]=*eslint*
public-hoist-pattern[]=*prettier*
engine-strict=true
package-manager-strict-version=true
manage-package-manager-versions=true
Expand Down
15 changes: 12 additions & 3 deletions app/lib/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import getUserLocale from 'get-user-locale'
import { extendTailwindMerge } from 'tailwind-merge'
import defaultTheme from 'tailwindcss/defaultTheme'

import { LOCAL_STORAGE_KEY } from '@/config/constants/local-storage'
import {
CURRENCY_CODE,
DEFAULT_CATEGORY,
Expand Down Expand Up @@ -145,12 +146,20 @@ export const toNumber = (value: number | string | null) => {
return value == null ? NaN : Number(value)
}

export const getIsAmountHidden = (): boolean => {
return localStorage.getItem('isAmountHidden') === 'true'
const isLocalStorageAvailable = (): boolean => {
return typeof localStorage !== 'undefined'
}
export const getIsAmountHidden = (): boolean => {
if (!isLocalStorageAvailable()) return false

return localStorage.getItem(LOCAL_STORAGE_KEY.IS_AMOUNT_HIDDEN) === 'true'
}
export const setIsAmountHidden = () => {
localStorage.setItem('isAmountHidden', (!getIsAmountHidden()).toString())
if (!isLocalStorageAvailable()) return false
localStorage.setItem(
LOCAL_STORAGE_KEY.IS_AMOUNT_HIDDEN,
(!getIsAmountHidden()).toString(),
)
}

export const getFormattedCurrency = (
Expand Down
11 changes: 4 additions & 7 deletions app/lib/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,10 @@ const useAttemptTracker = (
attemptLimit: number = ATTEMPT_LIMIT,
attemptResetInterval: number = ATTEMPT_RESET_INTERVAL,
) => {
const [attemptsData, setAttemptsData] = useLocalStorage(
`${storageKey}-attempt`,
{
count: 0,
lastAttemptTime: Date.now(),
},
)
const [attemptsData, setAttemptsData] = useLocalStorage(storageKey, {
count: 0,
lastAttemptTime: Date.now(),
})

const resetAttempts = useCallback(() => {
setAttemptsData({ count: 0, lastAttemptTime: Date.now() })
Expand Down
3 changes: 2 additions & 1 deletion app/providers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { useRouter } from 'next/navigation'
import { NextUIProvider } from '@nextui-org/react'

import { SUCCESS } from '@/config/constants/colors'
import { LOCAL_STORAGE_KEY } from '@/config/constants/local-storage'
import { DEFAULT_THEME } from '@/config/constants/main'
import {
DARK_TOAST_OPTS,
Expand All @@ -32,7 +33,7 @@ export default function Providers({ children }: { children: React.ReactNode }) {
const [theme, setTheme] = useState<TTheme | undefined>(undefined)

useEffect(() => {
setTheme(localStorage.getItem('theme') as TTheme)
setTheme(localStorage.getItem(LOCAL_STORAGE_KEY.THEME) as TTheme)
}, [])

return (
Expand Down
5 changes: 4 additions & 1 deletion app/ui/monthly-report/monthly-report.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
startOfToday,
} from 'date-fns'

import { LOCAL_STORAGE_KEY } from '@/config/constants/local-storage'
import {
DEFAULT_CURRENCY_CODE,
DEFAULT_CURRENCY_SIGN,
Expand Down Expand Up @@ -47,7 +48,9 @@ function MonthlyReport({ transactions, currency }: TProps) {
const [tipsDataAI, setTipsDataAI] = useState<TExpenseAdvice[] | null>(null)
const [isLoadingTips, setIsLoadingTips] = useState(false)

const { canAttempt, registerAttempt } = useAttemptTracker('expense-ai-tips')
const { canAttempt, registerAttempt } = useAttemptTracker(
LOCAL_STORAGE_KEY.AI_EXPENSE_TIPS,
)
const { minTransaction, maxTransaction } = useMemo(
() => getMinMaxTransactionsByDate(transactions),
[transactions],
Expand Down
5 changes: 5 additions & 0 deletions config/constants/local-storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const enum LOCAL_STORAGE_KEY {
THEME = 'theme',
IS_AMOUNT_HIDDEN = 'isAmountHidden',
AI_EXPENSE_TIPS = 'aiExpenseTips',
}

0 comments on commit 08ef69c

Please sign in to comment.