-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat(boilerplate, cli): remove mobx-state-tree from the ignite boilerplate completely #2960
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
14 commits
Select commit
Hold shift + click to select a range
5f2b4d1
feat: remove mobx-state-tree from the ignite boilerplate completely
markrickert 9a63074
fix: only show podcast title if subtitle doesn't exist
markrickert 4784ddb
fix: typo
markrickert 129a5bb
fix: tests were failing becuase of incorrect snapshot
markrickert 8f90636
feat(context): AuthContext actually persists with useMMKVString
markrickert 292724b
chore: syntax update suggestion
markrickert d6605aa
chore: prefer 1-line guards
markrickert 66829cf
chore: update docs
markrickert 2a62ffa
chore: Update docs/boilerplate/app/context/Context.md
markrickert e9dc538
chore: Update docs/boilerplate/app/context/Context.md
markrickert 1c9ff60
chore(types): move the episode Enclosure into the Episode type
markrickert ef14df9
chore: update promise on demo podcast screen to use allSettled instea…
markrickert 99333d4
fix: remove slice() from episodesForList on the flatlist
markrickert 8f67bc2
chore: move episode helpers to their own hook
markrickert 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
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,53 @@ | ||
| import { createContext, FC, PropsWithChildren, useCallback, useContext, useMemo } from "react" | ||
| import { useMMKVString } from "react-native-mmkv" | ||
|
|
||
| export type AuthContextType = { | ||
| isAuthenticated: boolean | ||
| authToken?: string | ||
| authEmail?: string | ||
| setAuthToken: (token?: string) => void | ||
| setAuthEmail: (email: string) => void | ||
| logout: () => void | ||
| validationError: string | ||
| } | ||
|
|
||
| export const AuthContext = createContext<AuthContextType | null>(null) | ||
|
|
||
| export interface AuthProviderProps {} | ||
|
|
||
| export const AuthProvider: FC<PropsWithChildren<AuthProviderProps>> = ({ children }) => { | ||
| const [authToken, setAuthToken] = useMMKVString("AuthProvider.authToken") | ||
| const [authEmail, setAuthEmail] = useMMKVString("AuthProvider.authEmail") | ||
|
|
||
| const logout = useCallback(() => { | ||
| setAuthToken(undefined) | ||
| setAuthEmail("") | ||
| }, [setAuthEmail, setAuthToken]) | ||
|
|
||
| const validationError = useMemo(() => { | ||
| if (!authEmail || authEmail.length === 0) return "can't be blank" | ||
| if (authEmail.length < 6) return "must be at least 6 characters" | ||
| if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(authEmail)) return "must be a valid email address" | ||
| return "" | ||
| }, [authEmail]) | ||
|
|
||
| const value = { | ||
| isAuthenticated: !!authToken, | ||
markrickert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| authToken, | ||
| authEmail, | ||
| setAuthToken, | ||
| setAuthEmail, | ||
| logout, | ||
| validationError, | ||
| } | ||
|
|
||
| return <AuthContext.Provider value={value}>{children}</AuthContext.Provider> | ||
| } | ||
|
|
||
| export const useAuth = () => { | ||
| const context = useContext(AuthContext) | ||
| if (!context) throw new Error("useAuth must be used within an AuthProvider") | ||
| return context | ||
| } | ||
|
|
||
| // @demo remove-file | ||
markrickert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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,154 @@ | ||
| import { | ||
| createContext, | ||
| FC, | ||
| PropsWithChildren, | ||
| useCallback, | ||
| useContext, | ||
| useMemo, | ||
| useState, | ||
| } from "react" | ||
|
|
||
| import { api } from "@/services/api" | ||
| import { translate } from "@/i18n/translate" | ||
| import { formatDate } from "@/utils/formatDate" | ||
|
|
||
| export interface Episode { | ||
| guid: string | ||
| title: string | ||
| pubDate: string | ||
| link: string | ||
| author: string | ||
| thumbnail: string | ||
| description: string | ||
| content: string | ||
| enclosure: { | ||
| link: string | ||
| type: string | ||
| length: number | ||
| duration: number | ||
| rating: { scheme: string; value: string } | ||
| } | ||
| categories: string[] | ||
| } | ||
|
|
||
| export type EpisodeContextType = { | ||
| totalEpisodes: number | ||
| totalFavorites: number | ||
| episodesForList: Episode[] | ||
| fetchEpisodes: () => Promise<void> | ||
| favoritesOnly: boolean | ||
| toggleFavoritesOnly: () => void | ||
| hasFavorite: (episode: Episode) => boolean | ||
| toggleFavorite: (episode: Episode) => void | ||
| } | ||
|
|
||
| export const EpisodeContext = createContext<EpisodeContextType | null>(null) | ||
|
|
||
| export interface EpisodeProviderProps {} | ||
|
|
||
| export const EpisodeProvider: FC<PropsWithChildren<EpisodeProviderProps>> = ({ children }) => { | ||
| const [episodes, setEpisodes] = useState<Episode[]>([]) | ||
| const [favorites, setFavorites] = useState<string[]>([]) | ||
markrickert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const [favoritesOnly, setFavoritesOnly] = useState<boolean>(false) | ||
|
|
||
| const fetchEpisodes = useCallback(async () => { | ||
| const response = await api.getEpisodes() | ||
| if (response.kind === "ok") { | ||
| setEpisodes(response.episodes) | ||
| } else { | ||
| console.error(`Error fetching episodes: ${JSON.stringify(response)}`) | ||
| } | ||
| }, []) | ||
|
|
||
| const toggleFavoritesOnly = useCallback(() => { | ||
| setFavoritesOnly((prev) => !prev) | ||
| }, []) | ||
|
|
||
| const toggleFavorite = useCallback( | ||
| (episode: Episode) => { | ||
| if (favorites.some((fav) => fav === episode.guid)) { | ||
| setFavorites((prev) => prev.filter((fav) => fav !== episode.guid)) | ||
| } else { | ||
| setFavorites((prev) => [...prev, episode.guid]) | ||
| } | ||
| }, | ||
| [favorites], | ||
| ) | ||
|
|
||
| const hasFavorite = useCallback( | ||
| (episode: Episode) => favorites.some((fav) => fav === episode.guid), | ||
| [favorites], | ||
| ) | ||
|
|
||
| const episodesForList = useMemo(() => { | ||
| return favoritesOnly ? episodes.filter((episode) => favorites.includes(episode.guid)) : episodes | ||
| }, [episodes, favorites, favoritesOnly]) | ||
|
|
||
| const value = { | ||
| totalEpisodes: episodes.length, | ||
| totalFavorites: favorites.length, | ||
| episodesForList, | ||
| fetchEpisodes, | ||
| favoritesOnly, | ||
| toggleFavoritesOnly, | ||
| hasFavorite, | ||
| toggleFavorite, | ||
| } | ||
|
|
||
| return <EpisodeContext.Provider value={value}>{children}</EpisodeContext.Provider> | ||
| } | ||
|
|
||
| export const useEpisodes = () => { | ||
| const context = useContext(EpisodeContext) | ||
| if (!context) throw new Error("useEpisodes must be used within an EpisodeProvider") | ||
| return context | ||
| } | ||
|
|
||
| // A helper hook to extract and format episode details | ||
| export const useEpisode = (episode: Episode) => { | ||
| const { hasFavorite } = useEpisodes() | ||
|
|
||
| const isFavorite = hasFavorite(episode) | ||
|
|
||
| let datePublished | ||
| try { | ||
| const formatted = formatDate(episode.pubDate) | ||
| datePublished = { | ||
| textLabel: formatted, | ||
| accessibilityLabel: translate("demoPodcastListScreen:accessibility.publishLabel", { | ||
| date: formatted, | ||
| }), | ||
| } | ||
| } catch { | ||
| datePublished = { textLabel: "", accessibilityLabel: "" } | ||
| } | ||
|
|
||
| const seconds = Number(episode.enclosure?.duration ?? 0) | ||
| const h = Math.floor(seconds / 3600) | ||
| const m = Math.floor((seconds % 3600) / 60) | ||
| const s = Math.floor((seconds % 3600) % 60) | ||
| const duration = { | ||
| textLabel: `${h > 0 ? `${h}:` : ""}${m > 0 ? `${m}:` : ""}${s}`, | ||
| accessibilityLabel: translate("demoPodcastListScreen:accessibility.durationLabel", { | ||
| hours: h, | ||
| minutes: m, | ||
| seconds: s, | ||
| }), | ||
| } | ||
|
|
||
| const trimmedTitle = episode.title?.trim() | ||
| const titleMatches = trimmedTitle?.match(/^(RNR.*\d)(?: - )(.*$)/) | ||
| const parsedTitleAndSubtitle = | ||
| titleMatches && titleMatches.length === 3 | ||
| ? { title: titleMatches[1], subtitle: titleMatches[2] } | ||
| : { title: trimmedTitle, subtitle: "" } | ||
|
|
||
| return { | ||
| isFavorite, | ||
| datePublished, | ||
| duration, | ||
| parsedTitleAndSubtitle, | ||
| } | ||
| } | ||
|
|
||
| // @demo remove-file | ||
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 was deleted.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I love this as a simple way to get persistence