From 8269f3adadaa05cdaad3edc797598a24a04627c3 Mon Sep 17 00:00:00 2001 From: ManInDark <61268856+ManInDark@users.noreply.github.com> Date: Sat, 9 Aug 2025 18:57:04 +0200 Subject: [PATCH] fix: refactored code & removed unused parts --- app/actions/data.ts | 46 ++++------- app/layout.tsx | 10 +-- components/AddEditHabitModal.tsx | 2 +- hooks/useCoins.tsx | 53 ++++--------- hooks/useHabits.tsx | 58 ++++---------- hooks/useWishlist.tsx | 39 ++-------- lib/atoms.ts | 12 +-- lib/client-helpers.ts | 5 +- lib/types.ts | 3 +- lib/utils.test.ts | 51 +----------- lib/utils.ts | 128 ++++++++++++------------------- package.json | 3 - 12 files changed, 104 insertions(+), 306 deletions(-) diff --git a/app/actions/data.ts b/app/actions/data.ts index 0d8d68f..5948946 100644 --- a/app/actions/data.ts +++ b/app/actions/data.ts @@ -21,7 +21,7 @@ import { WishlistData, WishlistItemType } from '@/lib/types'; -import { d2t, generateCryptoHash, getNow, prepareDataForHashing, uuid } from '@/lib/utils'; +import { d2t, generateCryptoHash, getNow, prepareDataForHashing } from '@/lib/utils'; import { signInSchema } from '@/lib/zod'; import fs from 'fs/promises'; import _ from 'lodash'; @@ -33,21 +33,6 @@ type ResourceType = 'habit' | 'wishlist' | 'coins' type ActionType = 'write' | 'interact' -async function verifyPermission( - resource: ResourceType, - action: ActionType -): Promise { - // const user = await getCurrentUser() - - // if (!user) throw new PermissionError('User not authenticated') - // if (user.isAdmin) return // Admins bypass permission checks - - // if (!checkPermission(user.permissions, resource, action)) { - // throw new PermissionError(`User does not have ${action} permission for ${resource}`) - // } - return -} - function getDefaultData(type: DataType): T { return DATA_DEFAULTS[type]() as T; } @@ -126,11 +111,13 @@ async function saveData(type: DataType, data: T): Promise { */ async function calculateServerFreshnessToken(): Promise { try { - const settings = await loadSettings(); - const habits = await loadHabitsData(); - const coins = await loadCoinsData(); - const wishlist = await loadWishlistData(); - const users = await loadUsersData(); + const [settings, habits, coins, wishlist, users] = await Promise.all([ + loadSettings(), + loadHabitsData(), + loadCoinsData(), + loadWishlistData(), + loadUsersData() + ]); const dataString = prepareDataForHashing( settings, @@ -139,8 +126,7 @@ async function calculateServerFreshnessToken(): Promise { wishlist, users ); - const serverToken = await generateCryptoHash(dataString); - return serverToken; + return generateCryptoHash(dataString); } catch (error) { console.error("Error calculating server freshness token:", error); throw error; @@ -165,7 +151,6 @@ export async function loadWishlistItems(): Promise { } export async function saveWishlistItems(data: WishlistData): Promise { - await verifyPermission('wishlist', 'write') const user = await getCurrentUser() data.items = data.items.map(wishlist => ({ @@ -191,14 +176,11 @@ export async function loadHabitsData(): Promise { if (!user) return getDefaultHabitsData() const data = await loadData('habits') return { - ...data, habits: data.habits.filter(x => user.isAdmin || x.userIds?.includes(user.id)) } } export async function saveHabitsData(data: HabitsData): Promise { - await verifyPermission('habit', 'write') - const user = await getCurrentUser() // Create clone of input data const newData = _.cloneDeep(data) @@ -210,7 +192,7 @@ export async function saveHabitsData(data: HabitsData): Promise { })) if (!user?.isAdmin) { - const existingData = await loadData('habits') + const existingData = await loadHabitsData(); const existingHabits = existingData.habits.filter(x => user?.id && !x.userIds?.includes(user?.id)) newData.habits = [ ...existingHabits, @@ -273,11 +255,10 @@ export async function addCoins({ note?: string userId?: string }): Promise { - await verifyPermission('coins', type === 'MANUAL_ADJUSTMENT' ? 'write' : 'interact') const currentUser = await getCurrentUser() const data = await loadCoinsData() const newTransaction: CoinTransaction = { - id: uuid(), + id: crypto.randomUUID(), amount, type, description, @@ -328,11 +309,10 @@ export async function removeCoins({ note?: string userId?: string }): Promise { - await verifyPermission('coins', type === 'MANUAL_ADJUSTMENT' ? 'write' : 'interact') const currentUser = await getCurrentUser() const data = await loadCoinsData() const newTransaction: CoinTransaction = { - id: uuid(), + id: crypto.randomUUID(), amount: -amount, type, description, @@ -434,7 +414,7 @@ export async function createUser(formData: FormData): Promise { const newUser: User = { - id: uuid(), + id: crypto.randomUUID(), username, password: hashedPassword, permissions, diff --git a/app/layout.tsx b/app/layout.tsx index f7ac3e7..04438c0 100644 --- a/app/layout.tsx +++ b/app/layout.tsx @@ -12,20 +12,12 @@ import { Suspense } from 'react' import { loadCoinsData, loadHabitsData, loadServerSettings, loadSettings, loadUsersData, loadWishlistData } from './actions/data' import './globals.css' -// Inter (clean, modern, excellent readability) -// const inter = Inter({ -// subsets: ['latin'], -// weight: ['400', '500', '600', '700'] -// }) - // Clean and contemporary -const dmSans = DM_Sans({ +const activeFont = DM_Sans({ subsets: ['latin'], weight: ['400', '500', '600', '700'] }) -const activeFont = dmSans - export const metadata = { title: 'HabitTrove', description: 'Track your habits and get rewarded', diff --git a/components/AddEditHabitModal.tsx b/components/AddEditHabitModal.tsx index 1355c71..fb5c909 100644 --- a/components/AddEditHabitModal.tsx +++ b/components/AddEditHabitModal.tsx @@ -123,7 +123,7 @@ export default function AddEditHabitModal({ onClose, onSave, habit, isTask }: Ad }} /> - ohsimpson +