Skip to content

feat/zustand store#398

Merged
valeriocomo merged 27 commits intodevelopfrom
feat/zustand-store
Apr 11, 2025
Merged

feat/zustand store#398
valeriocomo merged 27 commits intodevelopfrom
feat/zustand-store

Conversation

@AlexPalaz
Copy link
Copy Markdown
Collaborator

✅ What’s been done

This PR refactors how the app handles state by introducing Zustand, a lightweight, scalable state management library for React.

Previously, state was managed in a more scattered way. Now, we've created clear, dedicated stores that improve modularity and make the app easier to maintain.


✨ Features

We now use three separate Zustand stores, each focused on a specific part of the app:

  • useYamlStore

    • Stores the YAML content, the YAML format version, and whether a PublicCode YAML has been imported.
    • Includes setters for each field and a resetYaml() method.
    • Uses persist to store only selected values (isPublicCodeImported, publiccodeYmlVersion) across sessions.
  • useLanguagesStore

    • Manages the selected languages.
    • Provides setters and a resetLanguages() function.
  • useWarningStore

    • Stores validation warnings (Warning[]).
    • Uses persist to retain them on reload.
    • Methods to set and reset the warning list.

💥 Breaking changes

No breaking changes introduced.
This is a refactor only, no business logic or UI behavior has been altered.

@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 11, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
publiccode-editor ✅ Ready (Inspect) Visit Preview Apr 11, 2025 1:38pm
publiccode-editor-legacy ✅ Ready (Inspect) Visit Preview Apr 11, 2025 1:38pm

@AlexPalaz AlexPalaz requested a review from valeriocomo April 11, 2025 10:26
Comment thread package.json
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

react-redux must be removed as dep.

renderListItem={renderListItem as RenderItemProp<Language>} //wa for tsc
renderTagValue={renderTagValue}
value={publiccodeYmlLanguages as Language[]}
value={languages as unknown as Language[]}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this cast is not correct. languages is an array of string. despite of this, it's forced to be an array of Language.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

In fact, we don't need to cast here. Removing the cast

const publiccodeYmlLanguages = useSelector(getPubliccodeYmlLanguages)

const handleChange = (newSelection: Language[]) => {
if (newSelection.length != 0) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

newSelection.length != 0 is equivalent to newSelection.length. prefer a shorter form.

const { warnings, setWarnings } = props;
const { warnings } = useWarningStore();

const [collapseElementOpen, setCollapseElementOpen] = useState("");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Does it make sense to move this state into the store?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Everything related to a component's behavior is better handled using useState hooks. In this case, since the hook manages the collapsing behavior, it's preferable to use a state hook.

const YamlPreview = ({ yaml }: Props): JSX.Element => {
const YamlPreview = (): JSX.Element => {
const { t } = useTranslation();
const [showUploadPanel, setShowUploadPanel] = useState(false);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Does it make sense to move this status to the store?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Everything related to a component's behavior is better handled using useState hooks. In this case, since the hook manages the show/hide panel behavior, it's preferable to use a state hook.

Copy link
Copy Markdown
Member

@valeriocomo valeriocomo left a comment

Choose a reason for hiding this comment

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

LGTM

@valeriocomo valeriocomo merged commit 8254095 into develop Apr 11, 2025
7 checks passed
@valeriocomo valeriocomo deleted the feat/zustand-store branch April 11, 2025 13:43
mburri pushed a commit to puzzle/publiccode-editor that referenced this pull request Oct 3, 2025
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