Skip to content

Commit c3000cc

Browse files
authored
Feature/click to switch mode (allthatjazzleo#6)
* feat(route): Delete useless route * feat(context): Add a context for state management * feat(context): Implement the context for state management
1 parent 2c70411 commit c3000cc

9 files changed

+62
-74
lines changed

β€Žfrontend/src/App.tsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
import { RouterProvider } from '@tanstack/react-router';
88
import { useMemo } from 'react';
99

10+
import { LoveAppProvider } from '@/context/LoveAppContext.tsx';
1011
import { Toaster } from '@/shadcn/components/ui/toaster';
1112
import { useToast } from '@/shadcn/hooks/use-toast';
1213

@@ -54,7 +55,9 @@ function App() {
5455
}
5556
walletConnectOptions={WALLET_CONNECT_OPTIONS}
5657
>
57-
<RouterProvider router={router} />
58+
<LoveAppProvider>
59+
<RouterProvider router={router} />
60+
</LoveAppProvider>
5861
</MantraProvider>
5962
</QueryClientProvider>
6063
<Toaster />
+13-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
import { HandHeart } from 'lucide-react';
22

3+
import { useLoveApp } from '@/context/LoveAppContext.tsx';
4+
35
export const AppIcon = () => {
6+
const { isLoveApp, setIsLoveApp } = useLoveApp();
7+
48
return (
59
<div className="flex flex-row items-center gap-2">
6-
<HandHeart />
7-
<div className="font-semibold">Proposal Manager</div>
10+
<button
11+
className="flex flex-row items-center gap-2"
12+
onClick={() => setIsLoveApp(!isLoveApp)}
13+
>
14+
<HandHeart />
15+
</button>
16+
<div className="font-semibold">
17+
{isLoveApp ? 'Love Manager πŸ’–' : 'Proposal Manager'}
18+
</div>
819
</div>
920
);
1021
};

β€Žfrontend/src/components/CreateProposalCard.tsx

+5-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import { useMemo, useState } from 'react';
77

88
import { ProposalManagerQueryClient } from '@/__generated__/ProposalManager.client';
99
import { Coin } from '@/__generated__/ProposalManager.types';
10+
import { useLoveApp } from '@/context/LoveAppContext.tsx';
1011
import { useAllNativeTokenBalances } from '@/hooks/useAllNativeTokenBalances';
1112
import { useCreateProposalMutation } from '@/hooks/useCreateProposalMutation';
12-
import { isLoveApp } from '@/lib/utils.ts';
1313
import { Button } from '@/shadcn/components/ui/button';
1414
import {
1515
Card,
@@ -188,6 +188,8 @@ export const CreateProposalCard: React.FC<Props> = ({ onSuccess }) => {
188188
return formatTokenBalance(successfulProposalFee.amount);
189189
}, [successfulProposalFee]);
190190

191+
const { isLoveApp } = useLoveApp();
192+
191193
return (
192194
<Card className="w-[900px]">
193195
<CardHeader>
@@ -212,7 +214,7 @@ export const CreateProposalCard: React.FC<Props> = ({ onSuccess }) => {
212214
className="font-mono"
213215
type="string"
214216
id="title"
215-
placeholder={isLoveApp() ? 'Marry me' : "Let's collaborate!"}
217+
placeholder={isLoveApp ? 'Marry me' : "Let's collaborate!"}
216218
value={title}
217219
onChange={(e) => setTitle(e.target.value)}
218220
/>
@@ -223,7 +225,7 @@ export const CreateProposalCard: React.FC<Props> = ({ onSuccess }) => {
223225
className="font-mono"
224226
type="string"
225227
id="speech"
226-
placeholder={isLoveApp() ? 'I love you' : 'Deal! Delay no more!'}
228+
placeholder={isLoveApp ? 'I love you' : 'Deal! Delay no more!'}
227229
value={speech}
228230
onChange={(e) => setSpeech(e.target.value)}
229231
/>

β€Žfrontend/src/components/Main.tsx

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { useCosmWasmClients, useMantra } from '@mantrachain/connect';
22
import { useQuery } from '@tanstack/react-query';
33

44
import { ProposalManagerQueryClient } from '@/__generated__/ProposalManager.client';
5-
import { isLoveApp } from '@/lib/utils.ts';
5+
import { useLoveApp } from '@/context/LoveAppContext.tsx';
66
import {
77
Tabs,
88
TabsContent,
@@ -75,14 +75,15 @@ const Connected = () => {
7575

7676
export const Main = () => {
7777
const { address } = useMantra();
78+
const { isLoveApp } = useLoveApp();
7879

7980
return (
8081
<div className="container">
8182
<h1 className="text-4xl font-semibold text-center mt-10">
82-
{isLoveApp() ? 'Love on the Chain ❀️' : 'Make a deal on chain'}
83+
{isLoveApp ? 'Love on the Chain ❀️' : 'Make a deal on chain'}
8384
</h1>
8485
<p className="text-sm text-center mt-3">
85-
{isLoveApp() ? 'Express your love' : 'Manage your proposals'} on Mantra
86+
{isLoveApp ? 'Express your love' : 'Manage your proposals'} on MANTRA
8687
Chain!{' '}
8788
<span className="text-pink-400">Create or respond to proposals</span>{' '}
8889
with a gift of tokens.
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { createContext, useContext, useState, ReactNode } from 'react';
2+
3+
interface LoveAppContextType {
4+
isLoveApp: boolean;
5+
setIsLoveApp: (isLoveApp: boolean) => void;
6+
}
7+
8+
const LoveAppContext = createContext<LoveAppContextType | undefined>(undefined);
9+
10+
export const LoveAppProvider = ({ children }: { children: ReactNode }) => {
11+
const [isLoveApp, setIsLoveApp] = useState(false);
12+
13+
return (
14+
<LoveAppContext.Provider value={{ isLoveApp, setIsLoveApp }}>
15+
{children}
16+
</LoveAppContext.Provider>
17+
);
18+
};
19+
20+
export const useLoveApp = () => {
21+
const context = useContext(LoveAppContext);
22+
if (!context) {
23+
throw new Error('useLoveApp must be used within a LoveAppProvider');
24+
}
25+
return context;
26+
};

β€Žfrontend/src/lib/utils.ts

-5
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,3 @@ import { twMerge } from 'tailwind-merge';
44
export function cn(...inputs: ClassValue[]) {
55
return twMerge(clsx(inputs));
66
}
7-
8-
export function isLoveApp() {
9-
// πŸ’–-> %F0%9F%92%96
10-
return window.location.pathname.includes('/love/%F0%9F%92%96');
11-
}

β€Žfrontend/src/routeTree.gen.ts

+9-48
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ import { Route as rootRoute } from './routes/__root'
1717
// Create Virtual Routes
1818

1919
const ProposalManagerLazyImport = createFileRoute('/proposal-manager')()
20-
const ProposalManagerLoveLazyImport = createFileRoute(
21-
'/proposal-manager/love/πŸ’–',
22-
)()
2320

2421
// Create/Update Routes
2522

@@ -31,14 +28,6 @@ const ProposalManagerLazyRoute = ProposalManagerLazyImport.update({
3128
import('./routes/proposal-manager.lazy').then((d) => d.Route),
3229
)
3330

34-
const ProposalManagerLoveLazyRoute = ProposalManagerLoveLazyImport.update({
35-
id: '/love/πŸ’–',
36-
path: '/love/πŸ’–',
37-
getParentRoute: () => ProposalManagerLazyRoute,
38-
} as any).lazy(() =>
39-
import('./routes/proposal-manager.love.πŸ’–.lazy').then((d) => d.Route),
40-
)
41-
4231
// Populate the FileRoutesByPath interface
4332

4433
declare module '@tanstack/react-router' {
@@ -50,60 +39,39 @@ declare module '@tanstack/react-router' {
5039
preLoaderRoute: typeof ProposalManagerLazyImport
5140
parentRoute: typeof rootRoute
5241
}
53-
'/proposal-manager/love/πŸ’–': {
54-
id: '/proposal-manager/love/πŸ’–'
55-
path: '/love/πŸ’–'
56-
fullPath: '/proposal-manager/love/πŸ’–'
57-
preLoaderRoute: typeof ProposalManagerLoveLazyImport
58-
parentRoute: typeof ProposalManagerLazyImport
59-
}
6042
}
6143
}
6244

6345
// Create and export the route tree
6446

65-
interface ProposalManagerLazyRouteChildren {
66-
ProposalManagerLoveLazyRoute: typeof ProposalManagerLoveLazyRoute
67-
}
68-
69-
const ProposalManagerLazyRouteChildren: ProposalManagerLazyRouteChildren = {
70-
ProposalManagerLoveLazyRoute: ProposalManagerLoveLazyRoute,
71-
}
72-
73-
const ProposalManagerLazyRouteWithChildren =
74-
ProposalManagerLazyRoute._addFileChildren(ProposalManagerLazyRouteChildren)
75-
7647
export interface FileRoutesByFullPath {
77-
'/proposal-manager': typeof ProposalManagerLazyRouteWithChildren
78-
'/proposal-manager/love/πŸ’–': typeof ProposalManagerLoveLazyRoute
48+
'/proposal-manager': typeof ProposalManagerLazyRoute
7949
}
8050

8151
export interface FileRoutesByTo {
82-
'/proposal-manager': typeof ProposalManagerLazyRouteWithChildren
83-
'/proposal-manager/love/πŸ’–': typeof ProposalManagerLoveLazyRoute
52+
'/proposal-manager': typeof ProposalManagerLazyRoute
8453
}
8554

8655
export interface FileRoutesById {
8756
__root__: typeof rootRoute
88-
'/proposal-manager': typeof ProposalManagerLazyRouteWithChildren
89-
'/proposal-manager/love/πŸ’–': typeof ProposalManagerLoveLazyRoute
57+
'/proposal-manager': typeof ProposalManagerLazyRoute
9058
}
9159

9260
export interface FileRouteTypes {
9361
fileRoutesByFullPath: FileRoutesByFullPath
94-
fullPaths: '/proposal-manager' | '/proposal-manager/love/πŸ’–'
62+
fullPaths: '/proposal-manager'
9563
fileRoutesByTo: FileRoutesByTo
96-
to: '/proposal-manager' | '/proposal-manager/love/πŸ’–'
97-
id: '__root__' | '/proposal-manager' | '/proposal-manager/love/πŸ’–'
64+
to: '/proposal-manager'
65+
id: '__root__' | '/proposal-manager'
9866
fileRoutesById: FileRoutesById
9967
}
10068

10169
export interface RootRouteChildren {
102-
ProposalManagerLazyRoute: typeof ProposalManagerLazyRouteWithChildren
70+
ProposalManagerLazyRoute: typeof ProposalManagerLazyRoute
10371
}
10472

10573
const rootRouteChildren: RootRouteChildren = {
106-
ProposalManagerLazyRoute: ProposalManagerLazyRouteWithChildren,
74+
ProposalManagerLazyRoute: ProposalManagerLazyRoute,
10775
}
10876

10977
export const routeTree = rootRoute
@@ -120,14 +88,7 @@ export const routeTree = rootRoute
12088
]
12189
},
12290
"/proposal-manager": {
123-
"filePath": "proposal-manager.lazy.tsx",
124-
"children": [
125-
"/proposal-manager/love/πŸ’–"
126-
]
127-
},
128-
"/proposal-manager/love/πŸ’–": {
129-
"filePath": "proposal-manager.love.πŸ’–.lazy.tsx",
130-
"parent": "/proposal-manager"
91+
"filePath": "proposal-manager.lazy.tsx"
13192
}
13293
}
13394
}

β€Žfrontend/src/routes/proposal-manager.love.πŸ’–.lazy.tsx

-11
This file was deleted.

β€Žfrontend/src/tanstack.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { routeTree } from './routeTree.gen';
88
const queryClient = new QueryClient();
99

1010
const memoryHistory = createMemoryHistory({
11-
initialEntries: ['/proposal-manager/love/πŸ’–'],
11+
initialEntries: ['/proposal-manager/'],
1212
});
1313

1414
const router = createRouter({

0 commit comments

Comments
Β (0)