mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-21 18:57:03 +02:00
feat: migrated duel store
This commit is contained in:
1 parent
eaaea114a0
commit
332c1787f4
32 files changed
+223
-191
No files matched your search
@@ -1,26 +0,0 @@
|
||||
import { mutateDuel } from '../../stores/duel';
|
||||
import { provisionDefinitions } from './provisionDefinitions';
|
||||
import { tankDefinitions } from './tankDefinitions';
|
||||
import { tankToDuelMember } from './tankToDuelMember';
|
||||
|
||||
export async function assignDuelMember(
|
||||
type: 'antagonist' | 'protagonist' | 'both',
|
||||
id: number,
|
||||
) {
|
||||
const awaitedTankDefinitions = await tankDefinitions;
|
||||
const awaitedProvisionDefinitions = await provisionDefinitions;
|
||||
const tank = awaitedTankDefinitions[id];
|
||||
const member = tankToDuelMember(tank, awaitedProvisionDefinitions);
|
||||
|
||||
mutateDuel((draft) => {
|
||||
draft.assigned = true;
|
||||
if (type === 'antagonist') {
|
||||
draft.antagonist = member;
|
||||
} else if (type === 'protagonist') {
|
||||
draft.protagonist = member;
|
||||
} else {
|
||||
draft.protagonist = member;
|
||||
draft.antagonist = { ...member };
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -1,5 +1,13 @@
|
||||
import { Draft, produce } from 'immer';
|
||||
import { createContext, ReactNode, useContext, useRef } from 'react';
|
||||
import { usePathname } from 'next/navigation';
|
||||
import {
|
||||
createContext,
|
||||
ReactNode,
|
||||
useContext,
|
||||
useMemo,
|
||||
use as useReact,
|
||||
useRef,
|
||||
} from 'react';
|
||||
import { StoreApi, useStore as useStoreZustand } from 'zustand';
|
||||
|
||||
interface StoreProviderProps {
|
||||
@@ -13,16 +21,24 @@ type ExtractState<S> = S extends {
|
||||
? T
|
||||
: never;
|
||||
|
||||
interface InitialContext {
|
||||
pathName: string;
|
||||
}
|
||||
|
||||
export function createNextSafeStore<Store extends StoreApi<unknown>>(
|
||||
initial: () => Store,
|
||||
initial: (context: InitialContext) => Store | Promise<Store>,
|
||||
) {
|
||||
type Type = Store extends StoreApi<infer T> ? T : never;
|
||||
|
||||
const Context = createContext<Store | undefined>(undefined);
|
||||
|
||||
function Provider({ children }: StoreProviderProps) {
|
||||
const pathName = usePathname();
|
||||
const storeRef = useRef<Store>();
|
||||
if (!storeRef.current) storeRef.current = initial() as Store;
|
||||
const context = { pathName };
|
||||
const storePromise = useMemo(async () => await initial(context), []);
|
||||
const awaitedStore = useReact(storePromise);
|
||||
if (!storeRef.current) storeRef.current = awaitedStore;
|
||||
|
||||
return (
|
||||
<Context.Provider value={storeRef.current}>{children}</Context.Provider>
|
||||
|
||||
Reference in new issue
Block a user