import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { Plus } from 'lucide-react';
import { useState } from 'react';
import { Button } from '#/components/atoms/button.jsx';
import { Card } from '#/components/atoms/card.jsx';
import { Link } from '#/components/atoms/link.jsx';
import { Notice } from '#/components/atoms/notice.jsx';
import { Skeleton } from '#/components/atoms/skeleton.jsx';
import { JsonView } from '#/components/molecules/json-view.jsx';
import { RepoScope } from '#/components/molecules/repo-scope.jsx';
import { ago, rkeyTime } from '#/lib/format.js';
import { href } from '#/lib/navigation.jsx';
import { useSession } from '#/lib/session.jsx';
import { createRecord, listRecords } from '#/lib/spaces.js';
/** The starting document for a new record, so a writer edits rather than types. */
function template(collection) {
return JSON.stringify(
{ $type: collection, createdAt: new Date().toISOString() },
null,
2,
);
}
function NewRecord({ space, collection, onCreated }) {
const { session } = useSession();
const [open, setOpen] = useState(false);
const [text, setText] = useState(() => template(collection));
const [rkey, setRkey] = useState('');
const create = useMutation({
mutationFn: () =>
createRecord(session, {
space,
collection,
rkey: rkey || undefined,
record: JSON.parse(text),
}),
onSuccess: () => {
setOpen(false);
setText(template(collection));
setRkey('');
onCreated();
},
});
if (!open) {
return (
);
}
return (