From c13a2826c208ca4f012ccef03705add77c9bf4fd Mon Sep 17 00:00:00 2001 From: "a.ilin" Date: Tue, 6 Oct 2026 11:17:37 +0300 Subject: [PATCH] camera --- .gitignore | 1 + src/components/CameraUpload.tsx | 45 +++++++++++++++++++ .../attractions/AttractionDetailPage.tsx | 29 +++++++++--- src/pages/attractions/PhotoGallery.tsx | 32 ++++++++++++- 4 files changed, 100 insertions(+), 7 deletions(-) create mode 100644 src/components/CameraUpload.tsx diff --git a/.gitignore b/.gitignore index a547bf3..f20a136 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,4 @@ dist-ssr *.njsproj *.sln *.sw? +.zcode/plans \ No newline at end of file diff --git a/src/components/CameraUpload.tsx b/src/components/CameraUpload.tsx new file mode 100644 index 0000000..bd56028 --- /dev/null +++ b/src/components/CameraUpload.tsx @@ -0,0 +1,45 @@ +import { Upload } from 'antd' +import type { ReactNode } from 'react' +import type { UploadProps } from 'antd' +import { notify } from '@/api/notify' + +const MAX_FILE_SIZE = 20 * 1024 * 1024 +const CAMERA_ACCEPTED_TYPES = 'image/jpeg,image/png,image/webp' +const CAMERA_MIME_TYPES = new Set(['image/jpeg', 'image/png', 'image/webp']) + +/** + * Загрузка снимка с камеры телефона: capture="environment" открывает нативную + * камеру, на десктопе превращается в обычный выбор файла. + */ +export function CameraUpload({ + onFile, + children, +}: { + onFile: (file: File) => void + children: ReactNode +}) { + const uploadProps: UploadProps = { + accept: CAMERA_ACCEPTED_TYPES, + capture: 'environment', + multiple: false, + showUploadList: false, + beforeUpload: (file) => { + if (file.size > MAX_FILE_SIZE) { + notify('error', 'Файл больше 20 МБ') + return Upload.LIST_IGNORE + } + if (!CAMERA_MIME_TYPES.has(file.type)) { + // Android-камера в режиме «высокой эффективности» может отдать HEIC. + notify('error', 'Формат не поддерживается', 'Снимайте в JPEG — проверьте режим камеры') + return Upload.LIST_IGNORE + } + return true + }, + customRequest: ({ file, onSuccess }) => { + onFile(file as File) + onSuccess?.(null) + }, + } + + return {children} +} diff --git a/src/pages/attractions/AttractionDetailPage.tsx b/src/pages/attractions/AttractionDetailPage.tsx index f323e02..9686565 100644 --- a/src/pages/attractions/AttractionDetailPage.tsx +++ b/src/pages/attractions/AttractionDetailPage.tsx @@ -14,14 +14,21 @@ import { Typography, Upload, } from 'antd' -import { ArrowLeftOutlined, EditOutlined, UploadOutlined } from '@ant-design/icons' +import { + ArrowLeftOutlined, + CameraOutlined, + EditOutlined, + UploadOutlined, +} from '@ant-design/icons' import type { UploadProps } from 'antd' import { Link, useLocation, useParams, useSearchParams } from 'react-router-dom' import { fetchAttractionCard, fetchAttractions, updateAttraction, uploadIcon } from '@/api/attractions' import { notifyApiError, notifySuccess } from '@/api/errors' import { notify } from '@/api/notify' import type { Attraction } from '@/api/types' +import { useIsMobile } from '@/hooks/useIsMobile' import { cvProfileMeta } from '@/utils/format' +import { CameraUpload } from '@/components/CameraUpload' import { AttractionForm, toUpdateRequest, @@ -37,6 +44,7 @@ export function AttractionDetailPage() { const [searchParams] = useSearchParams() const areaId = searchParams.get('area') const queryClient = useQueryClient() + const isMobile = useIsMobile() const passedState = (location.state as { attraction?: Attraction } | null)?.attraction // Карточка = последний ответ мутации (или router state), иначе — восстановление из списка зоны. @@ -216,11 +224,20 @@ export function AttractionDetailPage() { )} - - - + + + + + {isMobile && ( + iconMutation.mutate(file)}> + + + )} + JPEG / PNG / WebP / SVG, до 20 МБ. Перезаписывает текущую иконку. diff --git a/src/pages/attractions/PhotoGallery.tsx b/src/pages/attractions/PhotoGallery.tsx index 25ae5eb..38aa0e6 100644 --- a/src/pages/attractions/PhotoGallery.tsx +++ b/src/pages/attractions/PhotoGallery.tsx @@ -16,6 +16,7 @@ import { Upload, } from 'antd' import { + CameraOutlined, DeleteOutlined, ExperimentOutlined, SafetyCertificateOutlined, @@ -34,7 +35,9 @@ import { import { notifyApiError, notifySuccess } from '@/api/errors' import { notify } from '@/api/notify' import type { Photo } from '@/api/types' +import { useIsMobile } from '@/hooks/useIsMobile' import { formatUtc } from '@/utils/format' +import { CameraUpload } from '@/components/CameraUpload' const MAX_FILE_SIZE = 20 * 1024 * 1024 const ACCEPTED_TYPES = 'image/jpeg,image/png,image/webp,image/svg+xml' @@ -47,6 +50,7 @@ export function PhotoGallery({ onCountDelta: (delta: number) => void }) { const queryClient = useQueryClient() + const isMobile = useIsMobile() const [uploadSortOrder, setUploadSortOrder] = useState(null) /** Черновики позиций: photoId → введённое, но не сохранённое значение. */ const [pendingOrders, setPendingOrders] = useState>({}) @@ -169,8 +173,19 @@ export function PhotoGallery({ Загрузить фото + {isMobile && ( + + uploadMutation.mutate({ file, sortOrder: uploadSortOrder ?? undefined }) + } + > + + + )} - Позиция: + Позиция: + {isMobile && ( + verifyMutation.mutate({ photoId: photo.id, file })} + > +