import { useMemo, useState } from "react"; import Image from "next/image"; import { Button, Card, Center, Grid, Input, Stack, Text } from "@mantine/core"; import { IconPlus, IconSearch } from "@tabler/icons-react"; import { clientApi } from "@homarr/api/client"; import { createModal, useModalAction } from "@homarr/modals"; import { useI18n } from "@homarr/translation/client"; import { QuickAddAppModal } from "./quick-add-app/quick-add-app-modal"; interface AppSelectModalProps { onSelect?: (appId: string) => void; } export const AppSelectModal = createModal(({ actions, innerProps }) => { const [search, setSearch] = useState(""); const t = useI18n(); const { data: apps = [], isPending } = clientApi.app.selectable.useQuery(); const { openModal: openQuickAddAppModal } = useModalAction(QuickAddAppModal); const filteredApps = useMemo( () => apps .filter((app) => app.name.toLowerCase().includes(search.toLowerCase())) .sort((a, b) => a.name.localeCompare(b.name)), [apps, search], ); const handleSelect = (appId: string) => { if (innerProps.onSelect) { innerProps.onSelect(appId); } actions.closeModal(); }; const handleAddNewApp = () => { openQuickAddAppModal({ onClose(createdAppId) { if (innerProps.onSelect) { innerProps.onSelect(createdAppId); } actions.closeModal(); }, }); }; return ( setSearch(event.currentTarget.value)} leftSection={} placeholder={`${t("app.action.select.search")}...`} data-autofocus onKeyDown={(event) => { if (event.key === "Enter" && filteredApps.length === 1 && filteredApps[0]) { handleSelect(filteredApps[0].id); } }} />
{t("app.action.create.title")} {t("app.action.create.description")}
{filteredApps.map((app) => (
{app.name}
{app.name} {app.description ?? ""}
))} {filteredApps.length === 0 && !isPending && (
{t("app.action.select.noResults")}
)}
); }).withOptions({ defaultTitle: (t) => t("app.action.select.title"), size: "xl", });