Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Playlist page: Prevent autofocus on add-track component #3168

Merged
merged 4 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions frontend/js/src/playlists/Playlist.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import {
getPlaylistId,
getRecordingMBIDFromJSPFTrack,
isPlaylistOwner,
JSPFTrackToListen,
LISTENBRAINZ_URI_PREFIX,
PLAYLIST_TRACK_URI_PREFIX,
PLAYLIST_URI_PREFIX,
Expand Down Expand Up @@ -98,14 +97,12 @@ export default function PlaylistPage() {

// Ref
const socketRef = React.useRef<Socket | null>(null);
const searchInputRef = React.useRef<HTMLInputElement>(null);

// States
const [playlist, setPlaylist] = React.useState<JSPFPlaylist>(
playlistProps?.playlist || {}
);
const [coverArtConfig, setCoverArtConfig] = React.useState<
CoverArtGridOptions
>(playlistProps?.cover_art || {});
const { track: tracks } = playlist;

// Functions
Expand Down Expand Up @@ -529,15 +526,17 @@ export default function PlaylistPage() {
</div>
{userHasRightToEdit && tracks && tracks.length > 10 && (
<div className="text-center">
<a
<button
className="btn btn-primary"
type="button"
href="#add-track"
style={{ marginBottom: "1em" }}
onClick={() => {
searchInputRef.current?.focus();
}}
>
<FontAwesomeIcon icon={faPlusCircle as IconProp} />
&nbsp;&nbsp;Add a track
</a>
</button>
</div>
)}
<div id="listens row">
Expand Down Expand Up @@ -573,6 +572,8 @@ export default function PlaylistPage() {
&nbsp;&nbsp;Add a track
</span>
<SearchTrackOrMBID
ref={searchInputRef}
autofocus={false}
onSelectRecording={addTrack}
expectedPayload="trackmetadata"
/>
Expand Down
18 changes: 13 additions & 5 deletions frontend/js/src/utils/SearchTrackOrMBID.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,18 @@ type ConditionalReturnValue =
};

type SearchTrackOrMBIDProps = {
autofocus?: boolean;
defaultValue?: string;
expectedPayload: PayloadType;
} & ConditionalReturnValue;

const SearchTrackOrMBID = forwardRef(function SearchTrackOrMBID(
{ onSelectRecording, expectedPayload, defaultValue }: SearchTrackOrMBIDProps,
{
onSelectRecording,
expectedPayload,
defaultValue,
autofocus = true,
}: SearchTrackOrMBIDProps,
inputRefForParent
) {
const { APIService } = useContext(GlobalAppContext);
Expand Down Expand Up @@ -69,10 +75,12 @@ const SearchTrackOrMBID = forwardRef(function SearchTrackOrMBID(

// Autofocus once on load
useEffect(() => {
setTimeout(() => {
inputRefLocal?.current?.focus();
}, 500);
}, []);
if (autofocus) {
setTimeout(() => {
inputRefLocal?.current?.focus();
}, 500);
}
}, [autofocus]);

const handleError = useCallback(
(error: string | Error, title?: string): void => {
Expand Down