'use client' import { useEffect, useMemo, useState } from 'react' import { useForm } from 'react-hook-form' import { addCreation, getAreas } from '@/lib/api' import { extractErrorMessage } from '@/lib/extractErrorMessage' import { getUrl } from '@/lib/utils' import { zodResolver } from '@hookform/resolvers/zod' import { useQuery } from '@tanstack/react-query' import { get, set } from 'idb-keyval' import { toast } from 'sonner' import { z } from 'zod' import { defaultEditorContent } from '@penx/constants' import { useAreas } from '@penx/hooks/useAreas' import { localDB } from '@penx/local-db' import { ICreationNode } from '@penx/model-type' import { spaceAtom, store } from '@penx/store' import { CreationStatus, GateType, StructType } from '@penx/types' import { Avatar, AvatarFallback, AvatarImage } from '@penx/uikit/avatar' import { Button } from '@penx/uikit/button' import { Checkbox } from '@penx/uikit/checkbox' import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from '@penx/uikit/form' import { Input } from '@penx/uikit/input' import { LoadingDots } from '@penx/uikit/loading-dots' import { Select, SelectContent, SelectGroup, SelectItem, SelectLabel, SelectTrigger, SelectValue, } from '@penx/uikit/select' import { Textarea } from '@penx/uikit/textarea' import { uniqueId } from '@penx/unique-id' import { Tags } from './Tags' const AREA_ID = 'CURRENT_AREA_ID' const FormSchema = z.object({ url: z.string().min(1), title: z.string().min(1), description: z.string().optional(), avatar: z.string().optional(), areaId: z.string(), tags: z.array(z.any()), isPublic: z.boolean().optional(), }) export function AddBookmarkForm() { const [isLoading, setLoading] = useState(false) const { areas } = useAreas() console.log('==========areas:', areas) const form = useForm>({ resolver: zodResolver(FormSchema), defaultValues: { url: '', title: '', description: '', avatar: '', tags: [], isPublic: false, }, }) async function onSubmit(data: z.infer) { const site = store.space.get() const structs = store.structs.get() const struct = structs.find( (struct) => struct.props.type === StructType.BOOKMARK, )! try { setLoading(true) // TODO: const creation: ICreationNode = {} as any await localDB.addCreation(creation) // await addCreation({ // title: data.title, // description: data.description, // image: data.avatar, // props: { // url: data.url, // }, // type: StructType.BOOKMARK, // tagIds: data.tags.map((tag) => tag.id), // areaId: data.areaId, // }) await set(AREA_ID, data.areaId) window.close() } catch (error) { console.log('======error:', error) const msg = extractErrorMessage(error) toast.error(msg) } setLoading(false) } async function initFormValues() { const [tab] = await chrome.tabs.query({ active: true, currentWindow: true, }) if (tab) { console.log('tab=======:', tab) form.setValue('url', tab.url) form.setValue('title', tab.title) form.setValue('avatar', tab.favIconUrl) } } useEffect(() => { initFormValues() }, [form]) async function initAreaId() { if (areas.length > 0) { let areaId = await get(AREA_ID) // if (!areas.some((f) => f.id === areaId)) { // const field = areas.find((field) => field.isGenesis) || areas[0] // areaId = field.id // } setTimeout(() => { form.setValue('areaId', areaId) }, 0) } } useEffect(() => { initAreaId() }, [form, areas]) return (
( Link )} /> ( Title )} /> {/* ( Description