This commit is contained in:
Ren Amamiya
2023-08-06 15:11:58 +07:00
parent 71338b3b07
commit 02ff9e3b68
34 changed files with 321 additions and 320 deletions

37
src/stores/blocks.tsx Normal file
View File

@@ -0,0 +1,37 @@
import { create } from 'zustand';
import { createJSONStorage, persist } from 'zustand/middleware';
import { createBlock, getBlocks, removeBlock } from '@libs/storage';
import { Block } from '@utils/types';
interface BlockState {
blocks: null | Array<Block>;
fetchBlocks: () => void;
setBlock: ({ kind, title, content }: Block) => void;
removeBlock: (id: string) => void;
}
export const useBlocks = create<BlockState>()(
persist(
(set) => ({
blocks: null,
fetchBlocks: async () => {
const blocks = await getBlocks();
set({ blocks: blocks });
},
setBlock: async ({ kind, title, content }: Block) => {
const block: Block = await createBlock(kind, title, content);
set((state) => ({ blocks: [...state.blocks, block] }));
},
removeBlock: async (id: string) => {
await removeBlock(id);
set((state) => ({ blocks: state.blocks.filter((block) => block.id !== id) }));
},
}),
{
name: 'blocks',
storage: createJSONStorage(() => localStorage),
}
)
);