Skip to content

Happy Thoughts - Assignment #57

Open
qabalany wants to merge 17 commits intoTechnigo:mainfrom
qabalany:main
Open

Happy Thoughts - Assignment #57
qabalany wants to merge 17 commits intoTechnigo:mainfrom
qabalany:main

Conversation

@qabalany
Copy link

Live Demo

The original Technigo API was down (503 Service Unavailable) during development, so I built my own backend using Supabase.

src/App.jsx Outdated
event.preventDefault()

if (!newMessage.trim()) return
if (newMessage.length < 5 || newMessage.length > 140) return

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure, but it looks to me that user won't get any feedback from the app here if he is trying to send an empty, too short or too long message? That might be a bit confusing to the user, so maybe would be good to return some kind of feedback or error here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good observation! The user does get feedback, the character counter turns red and shows error text, plus the submit button is disabled when invalid. But you're right, making it more obvious could improve UX! 👍

src/App.jsx Outdated
// Function to like a thought (UPDATE in Supabase)
const handleLikeThought = async (thoughtId) => {
// Find current hearts count
const thought = thoughts.find(t => t.id === thoughtId)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really understand this part, but it's probably because you use your own backend? Anyway, I would love to learn the reasoning behind it!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question! The original Technigo API had a dedicated /thoughts/:id/like endpoint that automatically incremented the hearts count on the server.

Since I built my own backend with Supabase, I'm using a simple database table with just UPDATE operations. So I need to first Find the current hearts count from local state then add 1 to it then send the new value to Supabase

An alternative would be to use Supabase's RPC (Remote Procedure Call) to increment on the server it wasn't easy so i used this easier simpler approach which works just fine for this project! 😊

return `${days} days ago`
}

export const ThoughtCard = ({ id, message, hearts, createdAt, onLike }) => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good usage of named attributes instead of just props here! Keep it up:)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

<HeartButton
onClick={() => onLike(id)}
$isLiked={isLiked}
aria-label={`Like this thought, currently ${hearts} likes`}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really great that you added aria-label here, way to go!

/>
<CharacterCount $count={charCount} aria-live="polite">
{charCount}/140
{charCount < 5 && charCount > 0 && ' (min 5 characters)'}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great user experience here with the feedback to users and not letting them press the button!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! You always have a good eye for details 😊👍

src/supabase.js Outdated
import { createClient } from '@supabase/supabase-js'

const supabaseUrl = 'https://qoxtdfojwnokjnmuwazc.supabase.co'
const supabaseKey = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InFveHRkZm9qd25va2pubXV3YXpjIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NjU2MDM3ODUsImV4cCI6MjA4MTE3OTc4NX0.BrH7Sj5ALhX8ka2ZwUKEA--ey3gKtYImZjpI3i2a4wc'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure it is a good idea to leave the key open here? But it's beyond the code point anyway:)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! But this anon key is safe to expose. It's designed for frontend use. Supabase protects the database with Row Level Security (RLS) policies, so this key can only read/write what I explicitly allow.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are updates to the keys that one get from Supabase, check out the transition over to publishable and secret keys from anon and service_role keys here: https://github.com/orgs/supabase/discussions/29260 :)

margin: 8px 0 0 0;
color: ${props => {
if (props.$count < 5) return '#e74c3c'
if (props.$count > 140) return '#e74c3c'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try keeping reused numbers like this in one place as constants. That way it's a lot easier to change!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks ! I updated the code and put the repeated numbers (5, 140, 120) as constants at the top of the file. Now it's much easier if I want to change the limits - I just update one place instead of searching everywhere.

@Npahlfer
Copy link

Good work! Nice solution with the disabled state an that you set up your own supabase to handle the api. But it's not working and I receive errors.
The cause of why the other api is not working is that one has to visit the url first to get it started first, before using it as an API.
It would have been nice to see the use of localStorage as well so that one can see which likes one has done before.
But check why your supabase requests are not working :)

@qabalany
Copy link
Author

Hi! Thanks for taking the time to review my code and for the helpful feedback! 😊

You're absolutely right. I checked my Supabase project and it was indeed "sleeping" due to inactivity. I visited the dashboard and the REST endpoint to wake it up, and now everything is working smoothly!

I also read through the GitHub discussion you shared about the upcoming API key changes. Just to clarify - that discussion is about a future migration from legacy JWT keys to the new sb_publishable_ / sb_secret_ format, which won't be required until late 2026. My current issue was simply that free-tier Supabase projects pause after a week of inactivity, not related to the API key migration.

Regarding localStorage I've added localStorage to track liked thoughts. Now when you like a thought, it saves to localStorage, persists after refresh, and prevents duplicate likes.

Also refactored the hardcoded numbers (5, 140, 120) into constants as suggested!

Everything's working now! Should I push these updates in a new commit?

@Npahlfer
Copy link

Thank you for waking it up, it works fine now!
Thanks for the clarification of the Github discussion. I'm aware. I just wanted to point out that they do recommend the usage of the new keys. No biggie, I just wanted you to check it out :)

https://supabase.com/docs/guides/api/api-keys

anon and service_role keys are based on the project's JWT secret. They are generated when your project is created and can only be changed when you rotate the JWT secret. This can cause significant issues in production applications. Use the publishable and secret keys instead.

Yes, please push the new changes :)

@qabalany
Copy link
Author

Thanks! I’m starting to understand it better now. that’s a really good point about the new keys. 😊
I’ve pushed the changes. Appreciate the feedback!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants