import { Button, Menu, Text } from "@mantine/core"; import { IconPlus } from "@tabler/icons-react"; import { useNavigate } from "react-router-dom"; import { useTranslation } from "react-i18next"; import { useGetSpacesQuery } from "@/features/space/queries/space-query.ts"; import { useCreatePageMutation } from "@/features/page/queries/page-query.ts"; import { buildPageUrl } from "@/features/page/page.utils.ts"; import { ISpace } from "@/features/space/types/space.types.ts"; import { CustomAvatar } from "@/components/ui/custom-avatar.tsx"; import { AvatarIconType } from "@/features/attachments/types/attachment.types.ts"; import { canCreatePage } from "./can-create-page.ts"; // Prominent home-screen action to create a new note (page). Because the home // screen has no active space, the target space is resolved from the user's // writable spaces: created directly when there is one, picked from a dropdown // when there are several. export default function NewNoteButton() { const { t } = useTranslation(); const navigate = useNavigate(); const createPageMutation = useCreatePageMutation(); const { data } = useGetSpacesQuery({ limit: 100 }); const writableSpaces = (data?.items ?? []).filter(canCreatePage); const createNote = async (space: ISpace) => { try { // `spaceId` is accepted by the create-page endpoint but is not part of // the shared `IPageInput` type; cast to satisfy the mutation signature. const createdPage = await createPageMutation.mutateAsync({ spaceId: space.id, } as any); navigate(buildPageUrl(space.slug, createdPage.slugId, createdPage.title)); } catch { // useCreatePageMutation already surfaces a red notification on error. } }; // No writable space → nothing to create in; render nothing. if (writableSpaces.length === 0) return null; const isPending = createPageMutation.isPending; // Exactly one writable space → create directly, no picker needed. if (writableSpaces.length === 1) { return ( } loading={isPending} onClick={() => createNote(writableSpaces[0])} > {t("New note")} ); } // Multiple writable spaces → pick the target space from a dropdown. return (
); }