forked from Cloud-Pipelines/pipeline-editor
-
Notifications
You must be signed in to change notification settings - Fork 5
feat favorites infrastructure #1980
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| import "fake-indexeddb/auto"; | ||
|
|
||
| import { renderHook, waitFor } from "@testing-library/react"; | ||
| import { afterEach, describe, expect, it } from "vitest"; | ||
|
|
||
| import { LibraryDB } from "@/providers/ComponentLibraryProvider/libraries/storage"; | ||
|
|
||
| import { type FavoriteItem, useFavorites } from "./useFavorites"; | ||
|
|
||
| const pipeline: FavoriteItem = { | ||
| type: "pipeline", | ||
| id: "p1", | ||
| name: "My Pipeline", | ||
| }; | ||
| const run: FavoriteItem = { type: "run", id: "r1", name: "My Run" }; | ||
|
|
||
| afterEach(async () => { | ||
| await LibraryDB.favorites.clear(); | ||
| }); | ||
|
|
||
| describe("useFavorites", () => { | ||
| it("starts with no favorites", async () => { | ||
| const { result } = renderHook(() => useFavorites()); | ||
| await waitFor(() => { | ||
| expect(result.current.favorites).toEqual([]); | ||
| }); | ||
| }); | ||
|
|
||
| it("adds a favorite", async () => { | ||
| const { result } = renderHook(() => useFavorites()); | ||
| await result.current.addFavorite(pipeline); | ||
| await waitFor(() => { | ||
| expect(result.current.favorites).toEqual([pipeline]); | ||
| }); | ||
| }); | ||
|
|
||
| it("does not add a duplicate favorite", async () => { | ||
| const { result } = renderHook(() => useFavorites()); | ||
| await result.current.addFavorite(pipeline); | ||
| await result.current.addFavorite(pipeline); | ||
| await waitFor(() => { | ||
| expect(result.current.favorites).toHaveLength(1); | ||
| }); | ||
| }); | ||
|
|
||
| it("removes a favorite", async () => { | ||
| const { result } = renderHook(() => useFavorites()); | ||
| await result.current.addFavorite(pipeline); | ||
| await result.current.addFavorite(run); | ||
| await result.current.removeFavorite("pipeline", "p1"); | ||
| await waitFor(() => { | ||
| expect(result.current.favorites).toEqual([run]); | ||
| }); | ||
| }); | ||
|
|
||
| it("toggles a favorite on and off", async () => { | ||
| const { result } = renderHook(() => useFavorites()); | ||
| await result.current.toggleFavorite(pipeline); | ||
| await waitFor(() => { | ||
| expect(result.current.favorites).toEqual([pipeline]); | ||
| }); | ||
| await result.current.toggleFavorite(pipeline); | ||
| await waitFor(() => { | ||
| expect(result.current.favorites).toEqual([]); | ||
| }); | ||
| }); | ||
|
|
||
| it("correctly reports isFavorite", async () => { | ||
| const { result } = renderHook(() => useFavorites()); | ||
| await result.current.addFavorite(pipeline); | ||
| await waitFor(() => { | ||
| expect(result.current.isFavorite("pipeline", "p1")).toBe(true); | ||
| expect(result.current.isFavorite("run", "r1")).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| it("does not confuse items of different types with the same id", async () => { | ||
| const pipelineItem: FavoriteItem = { | ||
| type: "pipeline", | ||
| id: "1", | ||
| name: "Pipeline", | ||
| }; | ||
| const runItem: FavoriteItem = { type: "run", id: "1", name: "Run" }; | ||
| const { result } = renderHook(() => useFavorites()); | ||
|
|
||
| await result.current.addFavorite(pipelineItem); | ||
| await waitFor(() => { | ||
| expect(result.current.isFavorite("pipeline", "1")).toBe(true); | ||
| expect(result.current.isFavorite("run", "1")).toBe(false); | ||
| }); | ||
|
|
||
| await result.current.addFavorite(runItem); | ||
| await waitFor(() => { | ||
| expect(result.current.favorites).toHaveLength(2); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import { useLiveQuery } from "dexie-react-hooks"; | ||
|
|
||
| import { | ||
| type FavoriteItem, | ||
| type FavoriteType, | ||
| LibraryDB, | ||
| } from "@/providers/ComponentLibraryProvider/libraries/storage"; | ||
|
|
||
| export type { FavoriteItem, FavoriteType }; | ||
|
|
||
| export function useFavorites() { | ||
| const favorites = useLiveQuery(() => LibraryDB.favorites.toArray(), []) ?? []; | ||
|
|
||
| const addFavorite = async (item: FavoriteItem) => { | ||
| // put is an upsert — compound PK [type+id] prevents duplicates | ||
| await LibraryDB.favorites.put(item); | ||
| }; | ||
|
|
||
| const removeFavorite = async (type: FavoriteType, id: string) => { | ||
| await LibraryDB.favorites.delete([type, id]); | ||
| }; | ||
|
|
||
| const isFavorite = (type: FavoriteType, id: string) => | ||
| favorites.some((f) => f.type === type && f.id === id); | ||
|
|
||
| const toggleFavorite = async (item: FavoriteItem) => { | ||
| if (isFavorite(item.type, item.id)) { | ||
| await removeFavorite(item.type, item.id); | ||
| } else { | ||
| await addFavorite(item); | ||
| } | ||
| }; | ||
|
|
||
| return { favorites, addFavorite, removeFavorite, toggleFavorite, isFavorite }; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some things are being caught by |
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.