diff --git a/app/AppContext.tsx b/app/AppContext.tsx index 8adf56c..712aef7 100644 --- a/app/AppContext.tsx +++ b/app/AppContext.tsx @@ -23,6 +23,7 @@ export interface AppContext { gameData: GameUpdate; noGame: boolean; tilts: Tilt[]; + selectCardIndex: number; flipCard: (cardIndex: number) => void; handleSettings: (gameData: GameUpdate) => void; redraw: (cardIndex: number) => void; @@ -55,6 +56,7 @@ export function AppProvider({ children }: { children: ReactNode }) { gameData, noGame, tilts, + selectCardIndex, flipCard, handleSettings, redraw, diff --git a/app/[gameID]/page.tsx b/app/[gameID]/page.tsx index a9221a6..1219b3d 100644 --- a/app/[gameID]/page.tsx +++ b/app/[gameID]/page.tsx @@ -1,29 +1,20 @@ 'use client'; -import { useEffect, useState } from 'react'; +import { useEffect } from 'react'; import { useParams } from 'next/navigation'; -import { useAppContext } from '@/app/AppContext'; -import { Eye } from 'lucide-react'; +import { useAppContext } from '@/app/AppContext'; import CardSelect from '@/components/CardSelect'; -import CopyButton from '@/components/CopyButton'; import Notes from '@/components/Notes'; import NotFound from '@/components/NotFound'; import Settings from '@/components/Settings'; +import { SpectatorLink } from '@/components/SpectatorLink'; import TarokkaGrid from '@/components/TarokkaGrid'; -import type { Deck } from '@/types'; - export default function GamePage() { - const { gameData, noGame, handleSettings, select, setGameID } = useAppContext(); + const { noGame, setGameID } = useAppContext(); const { gameID } = useParams(); - const [selectCard, setSelectCard] = useState(-1); - - const { dmID, cards, settings } = gameData; - const isDM = !!dmID; - const selectDeck: Deck | null = selectCard >= 0 ? cards[selectCard].deck : null; - useEffect(() => { if (gameID) { setGameID(Array.isArray(gameID) ? gameID[0] : gameID); @@ -32,28 +23,13 @@ export default function GamePage() { return noGame ? ( - ) : cards ? ( + ) : (
- {isDM && ( - - )} - - {isDM && } + + - flipped)} /> - setSelectCard(-1)} - selectAction={select} - /> + +
- ) : null; + ); } diff --git a/components/CardSelect.tsx b/components/CardSelect.tsx index 401aff9..fcddc6b 100644 --- a/components/CardSelect.tsx +++ b/components/CardSelect.tsx @@ -1,41 +1,36 @@ 'use client'; import { CircleX } from 'lucide-react'; +import { useAppContext } from '@/app/AppContext'; import TarokkaDeck from '@/lib/TarokkaDeck'; import getURL from '@/tools/getURL'; -import { Deck, Settings, TarokkaGameCard } from '@/types'; +import { Deck } from '@/types'; const tarokkaDeck = new TarokkaDeck(); type CardSelectProps = { - closeAction: () => void; - selectAction: (cardID: string) => void; - hand: TarokkaGameCard[]; - settings: Settings; - show: Deck | null; className?: string; }; -export default function CardSelect({ - closeAction, - selectAction, - hand, - settings, - show, - className = '', -}: CardSelectProps) { +export default function CardSelect({ className = '' }: CardSelectProps) { + const { gameData, select, selectCardIndex, setSelectCardIndex } = useAppContext(); + const { cards: hand, settings } = gameData; + const handIDs = hand.map(({ id }) => id); + const selectDeck: Deck | null = selectCardIndex >= 0 ? hand[selectCardIndex].deck : null; + + const close = () => setSelectCardIndex(-1); const handleClose = (event: React.MouseEvent) => { if (event.target === event.currentTarget) { - closeAction(); + close(); } }; - if (!show) return null; + if (!selectDeck) return null; - const cards = show === 'high' ? tarokkaDeck.getHigh() : tarokkaDeck.getLow(); + const cards = selectDeck === 'high' ? tarokkaDeck.getHigh() : tarokkaDeck.getLow(); return (
@@ -58,7 +53,7 @@ export default function CardSelect({
selectAction(card.id)} + onClick={() => select(card.id)} > flipped); const [open, setOpen] = useState(false); diff --git a/components/Settings.tsx b/components/Settings.tsx index ce5a83d..08d7a00 100644 --- a/components/Settings.tsx +++ b/components/Settings.tsx @@ -4,12 +4,13 @@ import { useState } from 'react'; import { Settings as Gear } from 'lucide-react'; import { Cinzel_Decorative } from 'next/font/google'; +import { useAppContext } from '@/app/AppContext'; import BuyMeACoffee from '@/components/BuyMeACoffee'; import CopyButton from '@/components/CopyButton'; import GitHubButton from '@/components/GitHubButton'; import Scrim from '@/components/Scrim'; import Switch from '@/components/Switch'; -import { CardStyle, GameUpdate } from '@/types'; +import { CardStyle } from '@/types'; const cinzel = Cinzel_Decorative({ variable: '--font-cinzel', @@ -17,18 +18,17 @@ const cinzel = Cinzel_Decorative({ weight: '400', }); -type SettingsProps = { - gameData: GameUpdate; - changeAction: (updatedSettings: GameUpdate) => void; -}; - const cardStyleOptions: CardStyle[] = ['standard', 'color', 'grayscale']; -export default function Settings({ gameData, changeAction }: SettingsProps) { +export default function Settings() { const [open, setOpen] = useState(false); + const { gameData, handleSettings } = useAppContext(); + + const { dmID } = gameData; + const isDM = !!dmID; const togglePermission = (key: string) => { - changeAction({ + handleSettings({ ...gameData, settings: { ...gameData.settings, @@ -38,7 +38,7 @@ export default function Settings({ gameData, changeAction }: SettingsProps) { }; const tuneRadio = (cardStyle: CardStyle) => { - changeAction({ + handleSettings({ ...gameData, settings: { ...gameData.settings, @@ -104,7 +104,7 @@ export default function Settings({ gameData, changeAction }: SettingsProps) { ); - return ( + return isDM ? (
setOpen((prev) => !prev)} @@ -129,5 +129,5 @@ export default function Settings({ gameData, changeAction }: SettingsProps) {
- ); + ) : null; } diff --git a/components/SpectatorLink.tsx b/components/SpectatorLink.tsx new file mode 100644 index 0000000..9b24b23 --- /dev/null +++ b/components/SpectatorLink.tsx @@ -0,0 +1,19 @@ +'use client'; + +import { Eye } from 'lucide-react'; +import { useAppContext } from '@/app/AppContext'; +import CopyButton from '@/components/CopyButton'; + +export function SpectatorLink() { + const { gameData } = useAppContext(); + + return ( + + ); +}