diff --git a/packages/api/src/router/snapshot.ts b/packages/api/src/router/snapshot.ts index ed7dc80f..54389c38 100644 --- a/packages/api/src/router/snapshot.ts +++ b/packages/api/src/router/snapshot.ts @@ -5,7 +5,11 @@ export const snapshotRouter = createTRPCRouter({ listByAddress: publicProcedure .input(z.object({ address: z.string() })) .query(({ ctx, input }) => { - return ctx.prisma.snapshot.findMany({ where: {} }) + return ctx.prisma.snapshot.findMany({ + where: { + address: input.address, + }, + }) }), upsert: publicProcedure diff --git a/packages/app/src/EditorApp.tsx b/packages/app/src/EditorApp.tsx index 95161308..9ccd2095 100644 --- a/packages/app/src/EditorApp.tsx +++ b/packages/app/src/EditorApp.tsx @@ -11,6 +11,7 @@ import { ClientOnly } from './components/ClientOnly' import { EditorLayout } from './EditorLayout/EditorLayout' import { LoginSuccessModal } from './EditorLayout/LoginSuccessModal' import { HotkeyBinding } from './HotkeyBinding' +import { SyncDetectorModal } from './SyncDetectorModal' import { UserQuery } from './UserQuery' import { WorkerStarter } from './WorkerStarter' @@ -55,6 +56,8 @@ export const EditorApp: FC = ({ children }) => { return ( + {address && } + diff --git a/packages/app/src/SyncDetectorModal.tsx b/packages/app/src/SyncDetectorModal.tsx new file mode 100644 index 00000000..7e6c51ae --- /dev/null +++ b/packages/app/src/SyncDetectorModal.tsx @@ -0,0 +1,94 @@ +import { useEffect, useState } from 'react' +import { Box } from '@fower/react' +import { Trans } from '@lingui/macro' +import { atom, useAtom } from 'jotai' +import { useAccount } from 'wagmi' +import { + Button, + Modal, + ModalClose, + ModalCloseButton, + ModalContent, + ModalHeader, + ModalOverlay, + useModalContext, +} from 'uikit' +import { ModalNames } from '@penx/constants' +import { Snapshot } from '@penx/db' +import { useUser } from '@penx/hooks' +import { db } from '@penx/local-db' +import { store } from '@penx/store' +import { trpc } from '@penx/trpc-client' + +const snapshotAtom = atom([]) + +const useSnapshots = () => { + const { address = '' } = useAccount() + const [snapshots, setSnapshots] = useAtom(snapshotAtom) + + async function run() { + const snapshots = await trpc.snapshot.listByAddress.query({ address }) + const spaces = await db.listSpaces() + const set = new Set(spaces.map((space) => space.id)) + const data = snapshots.filter((snapshot) => !set.has(snapshot.spaceId)) + + console.log('data', data) + setSnapshots(data) + } + + useEffect(() => { + run() + }, []) + + return { snapshots } +} + +function SyncItem({ spaceId }: { spaceId: string }) { + async function sync() { + const space = await db.getSpace(spaceId) + if (space) return + await store.createSpace({ + name: spaceId, + id: spaceId, + }) + } + return ( + + {spaceId} + + + ) +} + +const Content = () => { + const { snapshots } = useSnapshots() + return ( + + + Detect {snapshots.length} to sync + + + {snapshots.map((snapshot) => ( + + ))} + + + ) +} + +export const SyncDetectorModal = () => { + const { snapshots } = useSnapshots() + if (!snapshots.length) return null + return ( + + + + + + + + + ) +} diff --git a/packages/constants/src/constants.ts b/packages/constants/src/constants.ts index 218b852d..e49e97cd 100644 --- a/packages/constants/src/constants.ts +++ b/packages/constants/src/constants.ts @@ -54,6 +54,7 @@ export enum ModalNames { IMPORT_SPACE, DELETE_SPACE, LOGIN_SUCCESS, + SYNC_DETECTOR, } export const ELEMENT_P = 'p' diff --git a/packages/service/src/SpaceService.ts b/packages/service/src/SpaceService.ts index b5f8a03e..0b368e4f 100644 --- a/packages/service/src/SpaceService.ts +++ b/packages/service/src/SpaceService.ts @@ -12,7 +12,7 @@ export class SpaceService { nodes: INode[] = [] createSpace = async (name: string) => { - const space = await store.createSpace(name) + const space = await store.createSpace({ name }) return space } diff --git a/packages/store/src/store.ts b/packages/store/src/store.ts index 9da2437a..508b90c3 100644 --- a/packages/store/src/store.ts +++ b/packages/store/src/store.ts @@ -164,11 +164,15 @@ export const store = Object.assign(createStore(), { this.reloadNode(node) }, - async createSpace(name: string) { - const space = await db.createSpace({ name }) + async createSpace(input: Partial) { + const space = await db.createSpace(input) const spaces = await db.listSpaces() const nodeId = space.children[0] + const nodes = await db.listNormalNodes(space.id) const node = await db.getNode(nodeId) + + this.routeTo('NODE') + this.setNodes(nodes) this.setSpaces(spaces) this.reloadNode(node) return space