/*
* Copyright (C) 2026 Fluxer Contributors
*
* This file is part of Fluxer.
*
* Fluxer is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Fluxer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Fluxer. If not, see .
*/
import * as ContextMenuActionCreators from '@app/actions/ContextMenuActionCreators';
import * as ModalActionCreators from '@app/actions/ModalActionCreators';
import {modal} from '@app/actions/ModalActionCreators';
import * as PremiumModalActionCreators from '@app/actions/PremiumModalActionCreators';
import * as ToastActionCreators from '@app/actions/ToastActionCreators';
import * as VoiceSettingsActionCreators from '@app/actions/VoiceSettingsActionCreators';
import styles from '@app/components/modals/BackgroundImageGalleryModal.module.css';
import {ConfirmModal} from '@app/components/modals/ConfirmModal';
import * as Modal from '@app/components/modals/Modal';
import {Button} from '@app/components/uikit/button/Button';
import {MenuItem} from '@app/components/uikit/context_menu/MenuItem';
import FocusRing from '@app/components/uikit/focus_ring/FocusRing';
import {Tooltip} from '@app/components/uikit/tooltip/Tooltip';
import {Logger} from '@app/lib/Logger';
import VoiceSettingsStore, {BLUR_BACKGROUND_ID, NONE_BACKGROUND_ID} from '@app/stores/VoiceSettingsStore';
import * as BackgroundImageDB from '@app/utils/BackgroundImageDB';
import {openFilePicker} from '@app/utils/FilePickerUtils';
import {LimitResolver} from '@app/utils/limits/LimitResolverAdapter';
import {shouldShowPremiumFeatures} from '@app/utils/PremiumUtils';
import {Trans, useLingui} from '@lingui/react/macro';
import type {IconProps} from '@phosphor-icons/react';
import {
ArrowsClockwiseIcon,
CheckIcon,
CrownIcon,
EyeSlashIcon,
PlusIcon,
SparkleIcon,
TrashIcon,
WarningCircleIcon,
} from '@phosphor-icons/react';
import {observer} from 'mobx-react-lite';
import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react';
const logger = new Logger('BackgroundImageGalleryModal');
interface BackgroundImage {
id: string;
createdAt: number;
}
interface BuiltInBackground {
id: string;
type: 'none' | 'blur' | 'upload';
name: string;
icon: React.ComponentType;
description: string;
}
type BackgroundItemType = BuiltInBackground | BackgroundImage;
const MAX_FILE_SIZE = 10 * 1024 * 1024;
const ALLOWED_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif', 'image/webp', 'video/mp4'];
interface BackgroundItemProps {
background: BackgroundItemType;
isSelected: boolean;
onSelect: (background: BackgroundItemType) => void;
onContextMenu?: (event: React.MouseEvent, background: BackgroundImage) => void;
onDelete?: (background: BackgroundImage) => void;
}
const BackgroundItem: React.FC = React.memo(
({background, isSelected, onSelect, onContextMenu, onDelete}) => {
const {t} = useLingui();
const isBuiltIn = 'type' in background;
const Icon = isBuiltIn ? background.icon : undefined;
const [imageUrl, setImageUrl] = useState(null);
const [isLoading, setIsLoading] = useState(!isBuiltIn);
const [hasError, setHasError] = useState(false);
useEffect(() => {
if (isBuiltIn) return;
let objectUrl: string | null = null;
setIsLoading(true);
setHasError(false);
BackgroundImageDB.getBackgroundImageURL(background.id)
.then((url) => {
objectUrl = url;
setImageUrl(url);
setIsLoading(false);
})
.catch((error) => {
logger.error('Failed to load background image:', error);
setHasError(true);
setIsLoading(false);
});
return () => {
if (objectUrl) {
URL.revokeObjectURL(objectUrl);
}
};
}, [isBuiltIn, background.id]);
const handleClick = useCallback(() => {
onSelect(background);
}, [background, onSelect]);
const handleKeyDown = useCallback(
(e: React.KeyboardEvent) => {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
onSelect(background);
}
},
[background, onSelect],
);
const handleContextMenu = useCallback(
(e: React.MouseEvent) => {
if (!isBuiltIn) {
onContextMenu?.(e, background as BackgroundImage);
}
},
[isBuiltIn, background, onContextMenu],
);
const handleDelete = useCallback(
(e: React.MouseEvent) => {
e.preventDefault();
e.stopPropagation();
if (!isBuiltIn) {
onDelete?.(background as BackgroundImage);
}
},
[isBuiltIn, background, onDelete],
);
const handleRetry = useCallback(() => {
setHasError(false);
setIsLoading(true);
BackgroundImageDB.getBackgroundImageURL(background.id)
.then((url) => {
setImageUrl(url);
setIsLoading(false);
})
.catch((error) => {
logger.error('Failed to load background image:', error);
setHasError(true);
setIsLoading(false);
});
}, [background.id]);
return (