Files
navidrome/ui/src/user/LibrarySelectionField.jsx
T
Deluan 7fbf6c6382 feat: add default library assignment for new users
Introduced a new column `default_new_users` in the library table to
facilitate automatic assignment of default libraries to new regular users.
When a new user is created, they will now be assigned to libraries marked
as default, enhancing user experience by ensuring they have immediate access
to essential resources. Additionally, updated the user repository logic
to handle this new functionality and modified the user creation validation
to reflect that library selection is optional for non-admin users.

Signed-off-by: Deluan <deluan@navidrome.org>
2025-07-16 18:10:15 -04:00

56 lines
1.7 KiB
React

import { useInput, useTranslate, useRecordContext } from 'react-admin'
import { Box, FormControl, FormLabel, Typography } from '@material-ui/core'
import { SelectLibraryInput } from '../common/SelectLibraryInput.jsx'
import React, { useMemo } from 'react'
export const LibrarySelectionField = () => {
const translate = useTranslate()
const record = useRecordContext()
const {
input: { name, onChange, value },
meta: { error, touched },
} = useInput({ source: 'libraryIds' })
// Extract library IDs from either 'libraries' array or 'libraryIds' array
const libraryIds = useMemo(() => {
// First check if form has libraryIds (create mode or already transformed)
if (value && Array.isArray(value)) {
return value
}
// Then check if record has libraries array (edit mode from backend)
if (record?.libraries && Array.isArray(record.libraries)) {
return record.libraries.map((lib) => lib.id)
}
return []
}, [value, record])
// Determine if this is a new user (no ID means new record)
const isNewUser = !record?.id
return (
<FormControl error={!!(touched && error)} fullWidth margin="normal">
<FormLabel component="legend">
{translate('resources.user.fields.libraries')}
</FormLabel>
<Box mt={1} mb={1}>
<SelectLibraryInput
onChange={onChange}
value={libraryIds}
isNewUser={isNewUser}
/>
</Box>
{touched && error && (
<Typography color="error" variant="caption">
{error}
</Typography>
)}
<Typography variant="caption" color="textSecondary">
{translate('resources.user.helperTexts.libraries')}
</Typography>
</FormControl>
)
}