-
Notifications
You must be signed in to change notification settings - Fork 770
feat:- bookmarks #2143
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
Open
riteshshukla04
wants to merge
9
commits into
react-native-community:main
Choose a base branch
from
riteshshukla04:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat:- bookmarks #2143
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ad732d6
feat:- bookmarks
riteshshukla04 49c807e
fix: lint
riteshshukla04 1b00075
fix: lint
riteshshukla04 d8b2917
chore:- fix-simek-comments
riteshshukla04 3a99043
Merge remote-tracking branch 'origin/main'
riteshshukla04 dc3a704
chore:- fix-merge
riteshshukla04 e5796ad
chore:- fix-packages
riteshshukla04 c068969
chore:- fix-lint
riteshshukla04 e393333
chore:- fix-comments
riteshshukla04 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import { type StyleProp, type ViewStyle } from 'react-native'; | ||
|
|
||
| import { HoverEffect } from '~/common/styleguide'; | ||
| import { Bookmark, BookmarkFilled } from '~/components/Icons'; | ||
| import Tooltip from '~/components/Tooltip'; | ||
| import { useBookmarks } from '~/context/BookmarksContext'; | ||
|
|
||
| type BookmarkButtonProps = { | ||
| bookmarkId: string; | ||
| style?: StyleProp<ViewStyle>; | ||
| iconStyle?: StyleProp<ViewStyle>; | ||
| filledIconStyle?: StyleProp<ViewStyle>; | ||
| }; | ||
|
|
||
| export default function BookmarkButton({ | ||
| bookmarkId, | ||
| style, | ||
| iconStyle, | ||
| filledIconStyle, | ||
| }: BookmarkButtonProps) { | ||
| const { isBookmarked: checkIsBookmarked, toggleBookmark: toggleBookmarkGlobal } = useBookmarks(); | ||
| const isBookmarked = checkIsBookmarked(bookmarkId); | ||
|
|
||
| function handleToggleBookmark() { | ||
| toggleBookmarkGlobal(bookmarkId); | ||
| } | ||
|
|
||
| return ( | ||
| <Tooltip | ||
| trigger={ | ||
| <HoverEffect onPress={handleToggleBookmark} style={style}> | ||
| {isBookmarked ? ( | ||
| <BookmarkFilled style={filledIconStyle ?? iconStyle} /> | ||
| ) : ( | ||
| <Bookmark style={iconStyle} /> | ||
| )} | ||
| </HoverEffect> | ||
| }> | ||
| {isBookmarked ? 'Remove from bookmarks' : 'Add to bookmarks'} | ||
| </Tooltip> | ||
| ); | ||
| } |
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
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
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
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,95 @@ | ||
| import { createContext, type PropsWithChildren, useContext, useEffect, useState } from 'react'; | ||
|
|
||
| import { TimeRange } from '~/util/datetime'; | ||
|
|
||
| const BOOKMARK_COOKIE_NAME = 'rnd_bookmarks'; | ||
| const COOKIE_MAX_AGE = TimeRange.YEAR; | ||
|
|
||
| type BookmarksContextType = { | ||
| bookmarkedIds: Set<string>; | ||
| isBookmarked: (id: string) => boolean; | ||
| toggleBookmark: (id: string) => void; | ||
| isLoading: boolean; | ||
| }; | ||
|
|
||
| const BookmarksContext = createContext<BookmarksContextType | null>(null); | ||
|
|
||
| function getCookie(name: string): string | null { | ||
| if (typeof document === 'undefined') { | ||
| return null; | ||
| } | ||
| const match = document.cookie.match(new RegExp(`(^| )${name}=([^;]+)`)); | ||
| return match ? decodeURIComponent(match[2]) : null; | ||
| } | ||
|
|
||
| function setCookie(name: string, value: string, maxAge: number) { | ||
| if (typeof document === 'undefined') { | ||
| return; | ||
| } | ||
| document.cookie = `${name}=${encodeURIComponent(value)}; path=/; max-age=${maxAge}; SameSite=Lax`; | ||
| } | ||
|
|
||
| export function getBookmarksFromCookie(cookieString?: string): string[] { | ||
| if (typeof cookieString === 'string') { | ||
| const match = cookieString.match(new RegExp(`(^| )${BOOKMARK_COOKIE_NAME}=([^;]+)`)); | ||
| if (match) { | ||
| try { | ||
| return JSON.parse(decodeURIComponent(match[2])); | ||
| } catch { | ||
| return []; | ||
| } | ||
| } | ||
| return []; | ||
| } | ||
|
|
||
| const value = getCookie(BOOKMARK_COOKIE_NAME); | ||
| if (!value) { | ||
| return []; | ||
| } | ||
| try { | ||
| return JSON.parse(value); | ||
| } catch { | ||
| return []; | ||
| } | ||
| } | ||
|
|
||
| export function BookmarksProvider({ children }: PropsWithChildren) { | ||
| const [bookmarkedIds, setBookmarkedIds] = useState<Set<string>>(new Set()); | ||
| const [isLoading, setIsLoading] = useState(true); | ||
|
|
||
| useEffect(() => { | ||
| const bookmarks = getBookmarksFromCookie(); | ||
| setBookmarkedIds(new Set(bookmarks)); | ||
| setIsLoading(false); | ||
| }, []); | ||
|
|
||
| function isBookmarked(id: string) { | ||
| return bookmarkedIds.has(id); | ||
| } | ||
|
|
||
| function toggleBookmark(id: string) { | ||
| const newSet = new Set(bookmarkedIds); | ||
| if (newSet.has(id)) { | ||
| newSet.delete(id); | ||
| } else { | ||
| newSet.add(id); | ||
| } | ||
|
|
||
| setCookie(BOOKMARK_COOKIE_NAME, JSON.stringify([...newSet]), COOKIE_MAX_AGE); | ||
| setBookmarkedIds(newSet); | ||
| } | ||
|
|
||
| return ( | ||
| <BookmarksContext.Provider value={{ bookmarkedIds, isBookmarked, toggleBookmark, isLoading }}> | ||
| {children} | ||
| </BookmarksContext.Provider> | ||
| ); | ||
| } | ||
|
|
||
| export function useBookmarks() { | ||
| const context = useContext(BookmarksContext); | ||
| if (!context) { | ||
| throw new Error('useBookmarks must be used within a BookmarksProvider'); | ||
| } | ||
| return context; | ||
| } |
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
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.