import Alert from '@app/components/Common/Alert'; import Modal from '@app/components/Common/Modal'; import useSettings from '@app/hooks/useSettings'; import globalMessages from '@app/i18n/globalMessages'; import axios from 'axios'; import { useState } from 'react'; import { defineMessages, useIntl } from 'react-intl'; import { useToasts } from 'react-toast-notifications'; import useSWR from 'swr'; interface PlexImportProps { onCancel?: () => void; onComplete?: () => void; } const messages = defineMessages({ importfromplex: 'Import Plex Users', importfromplexerror: 'Something went wrong while importing Plex users.', importedfromplex: '{userCount} Plex {userCount, plural, one {user} other {users}} imported successfully!', user: 'User', nouserstoimport: 'There are no Plex users to import.', newplexsigninenabled: 'The Enable New Plex Sign-In setting is currently enabled. Plex users with library access do not need to be imported in order to sign in.', }); const PlexImportModal = ({ onCancel, onComplete }: PlexImportProps) => { const intl = useIntl(); const settings = useSettings(); const { addToast } = useToasts(); const [isImporting, setImporting] = useState(false); const [selectedUsers, setSelectedUsers] = useState([]); const { data, error } = useSWR< { id: string; title: string; username: string; email: string; thumb: string; }[] >(`/api/v1/settings/plex/users`, { revalidateOnMount: true, }); const importUsers = async () => { setImporting(true); try { const { data: createdUsers } = await axios.post( '/api/v1/user/import-from-plex', { plexIds: selectedUsers } ); if (!createdUsers.length) { throw new Error('No users were imported from Plex.'); } addToast( intl.formatMessage(messages.importedfromplex, { userCount: createdUsers.length, strong: (msg: React.ReactNode) => {msg}, }), { autoDismiss: true, appearance: 'success', } ); if (onComplete) { onComplete(); } } catch (e) { addToast(intl.formatMessage(messages.importfromplexerror), { autoDismiss: true, appearance: 'error', }); } finally { setImporting(false); } }; const isSelectedUser = (plexId: string): boolean => selectedUsers.includes(plexId); const isAllUsers = (): boolean => selectedUsers.length === data?.length; const toggleUser = (plexId: string): void => { if (selectedUsers.includes(plexId)) { setSelectedUsers((users) => users.filter((user) => user !== plexId)); } else { setSelectedUsers((users) => [...users, plexId]); } }; const toggleAllUsers = (): void => { if (data && selectedUsers.length >= 0 && !isAllUsers()) { setSelectedUsers(data.map((user) => user.id)); } else { setSelectedUsers([]); } }; return ( { importUsers(); }} okDisabled={isImporting || !selectedUsers.length} okText={intl.formatMessage( isImporting ? globalMessages.importing : globalMessages.import )} onCancel={onCancel} > {data?.length ? ( <> {settings.currentSettings.newPlexLogin && ( ( {msg} ), })} type="info" /> )}
{data?.map((user) => ( ))}
toggleAllUsers()} onKeyDown={(e) => { if (e.key === 'Enter' || e.key === 'Space') { toggleAllUsers(); } }} className="relative inline-flex h-5 w-10 flex-shrink-0 cursor-pointer items-center justify-center pt-2 focus:outline-none" > {intl.formatMessage(messages.user)}
toggleUser(user.id)} onKeyDown={(e) => { if (e.key === 'Enter' || e.key === 'Space') { toggleUser(user.id); } }} className="relative inline-flex h-5 w-10 flex-shrink-0 cursor-pointer items-center justify-center pt-2 focus:outline-none" >
{user.username}
{user.username && user.username.toLowerCase() !== user.email && (
{user.email}
)}
) : ( )}
); }; export default PlexImportModal;