-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from RedDuck-Software/feat/admin
Feat/admin
- Loading branch information
Showing
13 changed files
with
234 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
module.exports = { | ||
singleQuote: true, | ||
trailingComma: "all", | ||
trailingComma: 'all', | ||
printWidth: 120, | ||
semi: true, | ||
plugins: ["prettier-plugin-tailwindcss"], | ||
plugins: ['prettier-plugin-tailwindcss'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Link } from 'react-router'; | ||
|
||
import { Button } from '@/components/ui/button'; | ||
import { routes } from '@/lib/router'; | ||
|
||
export const AdminBtn = () => { | ||
return ( | ||
<Link to={routes.ADMIN}> | ||
<Button | ||
variant={'outline'} | ||
className="min-h-[60px] w-full bg-[url('/images/textures/sand.png')] bg-repeat text-[22px] transition-all ease-in-out hover:opacity-80" | ||
> | ||
Admin | ||
</Button> | ||
</Link> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { useWallet } from '@solana/wallet-adapter-react'; | ||
import { useMutation, useQueryClient } from '@tanstack/react-query'; | ||
import bs58 from 'bs58'; | ||
|
||
import { post } from './utils'; | ||
|
||
import { authClient } from '@/lib/fetcher'; | ||
|
||
const useControlService = () => { | ||
const { publicKey, signMessage } = useWallet(); | ||
|
||
const client = useQueryClient(); | ||
|
||
return useMutation({ | ||
async mutationFn({ status }: { status: boolean }) { | ||
if (!publicKey || !signMessage) { | ||
return; | ||
} | ||
|
||
const client = authClient(); | ||
|
||
const uuid = crypto.randomUUID(); | ||
|
||
const message = `I am the admin of Tarot ${uuid}`; | ||
|
||
const encodedMessage = new TextEncoder().encode(message); | ||
const signature = bs58.encode(await signMessage(encodedMessage)); | ||
|
||
client.setHeader('x-admin-signature', signature); | ||
client.setHeader('x-admin-uuid', uuid); | ||
|
||
return await post<{ | ||
isShutDown: boolean; | ||
id: number; | ||
}>(client, status ? `shutdown/enable` : 'shutdown/shutdown'); | ||
}, | ||
async onSuccess() { | ||
await client.invalidateQueries({ | ||
queryKey: ['status'], | ||
}); | ||
}, | ||
onError(error) { | ||
console.trace(error); | ||
}, | ||
}); | ||
}; | ||
|
||
export default useControlService; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { useQuery } from '@tanstack/react-query'; | ||
|
||
import { get } from './utils'; | ||
|
||
import { authClient } from '@/lib/fetcher'; | ||
|
||
const useStatus = () => { | ||
return useQuery({ | ||
queryKey: ['status'], | ||
queryFn: async () => { | ||
const client = authClient(); | ||
|
||
return await get<{ | ||
isShutDown: boolean; | ||
id: number; | ||
} | null>(client, 'shutdown/status'); | ||
}, | ||
refetchInterval: 10000, | ||
}); | ||
}; | ||
|
||
export default useStatus; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* eslint-disable @typescript-eslint/no-floating-promises */ | ||
import { useWallet } from '@solana/wallet-adapter-react'; | ||
import { useEffect } from 'react'; | ||
import { useNavigate } from 'react-router'; | ||
|
||
import { Header } from '@/components/common/Header'; | ||
import { Button } from '@/components/ui/button'; | ||
import { OwnerAddress } from '@/constants/addresses'; | ||
import useControlService from '@/hooks/api/use-control-service'; | ||
import useStatus from '@/hooks/api/use-status'; | ||
import { network } from '@/lib/solana'; | ||
|
||
export default function AdminPage() { | ||
const { publicKey } = useWallet(); | ||
const navigate = useNavigate(); | ||
|
||
const { data: status } = useStatus(); | ||
|
||
const { mutateAsync } = useControlService(); | ||
|
||
useEffect(() => { | ||
if (!publicKey?.equals(OwnerAddress[network])) { | ||
console.log(publicKey?.toBase58(), OwnerAddress[network].toBase58()); | ||
navigate('/'); | ||
} | ||
}, [navigate, publicKey]); | ||
|
||
const handleControl = async (status: boolean) => { | ||
await mutateAsync({ | ||
status: status, | ||
}); | ||
}; | ||
|
||
return ( | ||
<> | ||
<Header /> | ||
|
||
<div className="mt-8 space-y-4 px-4"> | ||
<p>Status: {status?.isShutDown === true ? 'Disabled' : 'Enabled'}</p> | ||
|
||
<div className="flex h-[50vh] w-1/2 flex-row justify-between gap-4"> | ||
<Button | ||
onClick={() => handleControl(true)} | ||
variant={'outline'} | ||
className="min-h-[60px] w-full bg-[url('/images/textures/green.png')] bg-repeat text-[22px] transition-all ease-in-out hover:opacity-80" | ||
> | ||
Enable | ||
</Button> | ||
<Button | ||
onClick={() => handleControl(false)} | ||
variant={'outline'} | ||
className="min-h-[60px] w-full bg-[url('/images/textures/green.png')] bg-repeat text-[22px] transition-all ease-in-out hover:opacity-80" | ||
> | ||
Disable | ||
</Button> | ||
</div> | ||
</div> | ||
</> | ||
); | ||
} |