Skip to content

Commit

Permalink
Add global state using zustand (schema wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
CarboxyDev committed Jan 7, 2024
1 parent f465c62 commit 63f30b3
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 9 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"react-circular-progressbar": "^2.1.0",
"react-dom": "^18",
"tailwind-merge": "^2.2.0",
"uuid": "^9.0.1"
"uuid": "^9.0.1",
"zustand": "^4.4.7"
},
"devDependencies": {
"@types/node": "^20",
Expand Down
35 changes: 31 additions & 4 deletions pnpm-lock.yaml

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

11 changes: 7 additions & 4 deletions src/components/PrimaryValues.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
LooksIcon,
MoraleIcon,
} from '@/components/Icons';
import { useGetPrimaryValues } from '@/lib/store/player';
import { PrimaryValue } from '@/lib/types/general';
import { CircularProgressbarWithChildren } from 'react-circular-progressbar';
import 'react-circular-progressbar/dist/styles.css';
Expand Down Expand Up @@ -80,12 +81,14 @@ export const PrimaryValueItem = (props: PrimaryValueItemProps) => {
};

const PrimaryValues = () => {
const values = useGetPrimaryValues();

return (
<div className="flex flex-col items-center justify-center gap-y-12 rounded-xl border border-dark-700 bg-dark-900 py-8">
<PrimaryValueItem name={'health'} value={86} />
<PrimaryValueItem name={'morale'} value={73} />
<PrimaryValueItem name={'intellect'} value={81} />
<PrimaryValueItem name={'looks'} value={45} />
<PrimaryValueItem name={'health'} value={values?.health || 0} />
<PrimaryValueItem name={'morale'} value={values?.morale || 0} />
<PrimaryValueItem name={'intellect'} value={values?.intellect || 0} />
<PrimaryValueItem name={'looks'} value={values?.looks || 0} />
</div>
);
};
Expand Down
33 changes: 33 additions & 0 deletions src/lib/store/player.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { useStore } from '@/lib/store/store';
import { create } from 'zustand';

interface PlayerState {
name: string;
country: string;
age: number; // In months
money: number;
primaryValues: {
health: number;
morale: number;
intellect: number;
looks: number;
};
}

export const usePlayer = create<PlayerState>()((set) => ({
name: 'Player',
country: 'Undefined',
age: 12 * 18,
money: 0,
primaryValues: {
health: 100,
morale: 90,
intellect: 80,
looks: 70,
},
}));

export const useGetPrimaryValues = () => {
const _values = useStore(usePlayer, (state) => state.primaryValues);
return _values;
};
15 changes: 15 additions & 0 deletions src/lib/store/store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { useEffect, useState } from 'react';

export const useStore = <T, F>(
store: (callback: (state: T) => unknown) => unknown,
callback: (state: T) => F
) => {
const result = store(callback) as F;
const [data, setData] = useState<F>();

useEffect(() => {
setData(result);
}, [result]);

return data;
};

1 comment on commit 63f30b3

@vercel
Copy link

@vercel vercel bot commented on 63f30b3 Jan 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

lifely – ./

lifely-git-master-carboxydev.vercel.app
lifely.carboxy.tech
lifely-carboxydev.vercel.app

Please sign in to comment.