Projects
Museum Shuffle
Description
This project is a React discovery app that fetches random artwork from the Harvard Art Museums API. Users can click Discover to see a new piece with its image, artist, culture, and classification, then ban any of those values so future results skip matching artworks. A scrollable history keeps track of everything seen in the current session.
Features
- Discover button that fetches a random artwork with an image
- Clickable artist, culture, and classification values that add to a ban list
- Banned values immediately excluded from later API results
- Clicking a ban list item removes it so that attribute can appear again
- Scrollable seen-artworks history for the current session
- Error message if no new artwork can be found without matching the ban list
Checks a newly fetched artwork against banned artist, culture, and classification values:
const getArtworkAttributes = (artworkItem) => {
return {
artist: artworkItem?.people?.[0]?.displayname,
culture: artworkItem?.culture,
classification: artworkItem?.classification,
}
}
const isArtworkBanned = (artworkItem) => {
const attributes = getArtworkAttributes(artworkItem)
return (
banList.includes(attributes.artist) ||
banList.includes(attributes.culture) ||
banList.includes(attributes.classification)
)
}
Fetches a random artwork and retries if the result matches something already on the ban list:
const fetchArtwork = async () => {
setErrorMessage("")
for (let attempt = 0; attempt < 20; attempt++) {
const url = "https://api.harvardartmuseums.org/object?apikey=YOUR_API_KEY&size=1&hasimage=1&sort=random"
const response = await fetch(url)
const data = await response.json()
const newArtwork = data.records[0]
if (!isArtworkBanned(newArtwork)) {
setArtwork(newArtwork)
setSeenList((previousSeenList) => [newArtwork, ...previousSeenList])
return
}
}
setErrorMessage("No new artwork found. Try removing something from the ban list.")
}
Outcome
A curated browsing experience that still feels random. The main challenge was combining random API results with a live ban list without showing artwork the user already filtered out.
Visit this project's GitHub repository here: GitHub Repository