forked from housseindjirdeh/angular2-hn
-
Notifications
You must be signed in to change notification settings - Fork 15
Models, fetch-based API, settings context (migration 2/8) #690
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
charityquinn-cognition
wants to merge
1
commit into
devin/react-migration-1-scaffold
from
devin/react-migration-2-core-logic
+231
−0
Open
Changes from all commits
Commits
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,40 @@ | ||
| import type { PollResult, Story, User } from '../models'; | ||
|
|
||
| const baseUrl = 'https://node-hnapi.herokuapp.com'; | ||
|
|
||
| async function fetchJson<T>(url: string, options?: RequestInit): Promise<T> { | ||
| const response = await fetch(url, options); | ||
|
|
||
| if (!response.ok) { | ||
| throw new Error(`Request failed with status ${response.status}: ${response.statusText}`); | ||
| } | ||
|
|
||
| return response.json() as Promise<T>; | ||
| } | ||
|
|
||
| export function fetchFeed(feedType: string, page: number): Promise<Story[]> { | ||
| return fetchJson<Story[]>(`${baseUrl}/${feedType}?page=${page}`); | ||
| } | ||
|
|
||
| export async function fetchItemContent(id: number): Promise<Story> { | ||
| const story = await fetchJson<Story>(`${baseUrl}/item/${id}`); | ||
|
|
||
| if (story.type === 'poll') { | ||
| const pollResults = await Promise.all( | ||
| story.poll.map((_, index) => fetchPollContent(story.id + index + 1)), | ||
| ); | ||
|
|
||
| story.poll = pollResults; | ||
| story.poll_votes_count = pollResults.reduce((total, pollResult) => total + pollResult.points, 0); | ||
| } | ||
|
|
||
| return story; | ||
| } | ||
|
|
||
| export function fetchPollContent(id: number): Promise<PollResult> { | ||
| return fetchJson<PollResult>(`${baseUrl}/item/${id}`); | ||
| } | ||
|
|
||
| export function fetchUser(id: string): Promise<User> { | ||
| return fetchJson<User>(`${baseUrl}/user/${id}`); | ||
| } |
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,10 @@ | ||
| export interface Comment { | ||
| id: number; | ||
| level: number; | ||
| user: string; | ||
| time: number; | ||
| time_ago: string; | ||
| content: string; | ||
| deleted: boolean; | ||
| comments: Comment[]; | ||
| } |
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,3 @@ | ||
| export type FeedType = 'poll' | 'story' | 'job'; | ||
|
|
||
| export type FeedName = 'news' | 'newest' | 'show' | 'ask' | 'jobs'; |
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,6 @@ | ||
| export type { Comment } from './comment'; | ||
| export type { FeedName, FeedType } from './feedType'; | ||
| export type { PollResult } from './pollResult'; | ||
| export type { Settings } from './settings'; | ||
| export type { Story } from './story'; | ||
| export type { User } from './user'; |
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,4 @@ | ||
| export interface PollResult { | ||
| points: number; | ||
| content: string; | ||
| } |
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,7 @@ | ||
| export interface Settings { | ||
| showSettings: boolean; | ||
| openLinkInNewTab: boolean; | ||
| theme: string; | ||
| titleFontSize: string; | ||
| listSpacing: string; | ||
| } |
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,23 @@ | ||
| import type { Comment } from './comment'; | ||
| import type { FeedType } from './feedType'; | ||
| import type { PollResult } from './pollResult'; | ||
|
|
||
| export interface Story { | ||
| id: number; | ||
| title: string; | ||
| points: number; | ||
| user: string; | ||
| time: number; | ||
| time_ago: number; | ||
| type: FeedType; | ||
| url: string; | ||
| domain: string; | ||
| content?: string; | ||
| text?: string; | ||
| comments: Comment[]; | ||
| comments_count: number; | ||
| poll: PollResult[]; | ||
| poll_votes_count: number; | ||
| deleted: boolean; | ||
| dead: boolean; | ||
| } |
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,8 @@ | ||
| export interface User { | ||
| id: string; | ||
| crated_time: number; | ||
| created: string; | ||
| karma: number; | ||
| avg: number; | ||
| about: string; | ||
| } |
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,123 @@ | ||
| import { createContext, useCallback, useContext, useEffect, useMemo, useState } from 'react'; | ||
| import type { ReactNode } from 'react'; | ||
|
|
||
| import type { Settings } from '../models'; | ||
|
|
||
| interface SettingsContextValue { | ||
| settings: Settings; | ||
| toggleSettings: () => void; | ||
| toggleOpenLinksInNewTab: () => void; | ||
| setTheme: (theme: string) => void; | ||
| setFont: (fontSize: string) => void; | ||
| setSpacing: (listSpace: string) => void; | ||
| } | ||
|
|
||
| const SettingsContext = createContext<SettingsContextValue | undefined>(undefined); | ||
|
|
||
| function getInitialSettings(): Settings { | ||
| const openLinkInNewTab = localStorage.getItem('openLinkInNewTab'); | ||
| const titleFontSize = localStorage.getItem('titleFontSize'); | ||
| const listSpacing = localStorage.getItem('listSpacing'); | ||
|
|
||
| return { | ||
| showSettings: false, | ||
| openLinkInNewTab: openLinkInNewTab ? JSON.parse(openLinkInNewTab) : false, | ||
| theme: 'default', | ||
| titleFontSize: titleFontSize || '16', | ||
| listSpacing: listSpacing || '0', | ||
| }; | ||
| } | ||
|
|
||
| export function SettingsProvider({ children }: { children: ReactNode }) { | ||
| const [settings, setSettings] = useState<Settings>(getInitialSettings); | ||
|
|
||
| const toggleSettings = useCallback(() => { | ||
| setSettings((current) => ({ | ||
| ...current, | ||
| showSettings: !current.showSettings, | ||
| })); | ||
| }, []); | ||
|
|
||
| const toggleOpenLinksInNewTab = useCallback(() => { | ||
| setSettings((current) => { | ||
| const openLinkInNewTab = !current.openLinkInNewTab; | ||
| localStorage.setItem('openLinkInNewTab', JSON.stringify(openLinkInNewTab)); | ||
|
|
||
| return { | ||
| ...current, | ||
| openLinkInNewTab, | ||
| }; | ||
| }); | ||
| }, []); | ||
|
|
||
| const setTheme = useCallback((theme: string) => { | ||
| setSettings((current) => ({ | ||
| ...current, | ||
| theme, | ||
| })); | ||
| localStorage.setItem('theme', theme); | ||
| }, []); | ||
|
|
||
| const setFont = useCallback((titleFontSize: string) => { | ||
| setSettings((current) => ({ | ||
| ...current, | ||
| titleFontSize, | ||
| })); | ||
| localStorage.setItem('titleFontSize', titleFontSize); | ||
| }, []); | ||
|
|
||
| const setSpacing = useCallback((listSpacing: string) => { | ||
| setSettings((current) => ({ | ||
| ...current, | ||
| listSpacing, | ||
| })); | ||
| localStorage.setItem('listSpacing', listSpacing); | ||
| }, []); | ||
|
|
||
| useEffect(() => { | ||
| const darkColorSchemeMedia = window.matchMedia('(prefers-color-scheme: dark)'); | ||
| const handleSystemPreferredColorSchemeChange = (event: MediaQueryListEvent) => { | ||
| setTheme(event.matches ? 'night' : 'default'); | ||
| }; | ||
| const savedTheme = localStorage.getItem('theme'); | ||
|
|
||
| if (savedTheme) { | ||
| setSettings((current) => ({ | ||
| ...current, | ||
| theme: savedTheme, | ||
| })); | ||
| } else { | ||
| setTheme(darkColorSchemeMedia.matches ? 'night' : 'default'); | ||
| } | ||
|
|
||
| darkColorSchemeMedia.addEventListener('change', handleSystemPreferredColorSchemeChange); | ||
|
|
||
| return () => { | ||
| darkColorSchemeMedia.removeEventListener('change', handleSystemPreferredColorSchemeChange); | ||
| }; | ||
| }, [setTheme]); | ||
|
|
||
| const value = useMemo( | ||
| () => ({ | ||
| settings, | ||
| toggleSettings, | ||
| toggleOpenLinksInNewTab, | ||
| setTheme, | ||
| setFont, | ||
| setSpacing, | ||
| }), | ||
| [settings, toggleSettings, toggleOpenLinksInNewTab, setTheme, setFont, setSpacing], | ||
| ); | ||
|
|
||
| return <SettingsContext.Provider value={value}>{children}</SettingsContext.Provider>; | ||
| } | ||
|
|
||
| export function useSettings(): SettingsContextValue { | ||
| const context = useContext(SettingsContext); | ||
|
|
||
| if (!context) { | ||
| throw new Error('useSettings must be used within a SettingsProvider'); | ||
| } | ||
|
|
||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| export function formatComments(count: number): string { | ||
| if (count > 0) { | ||
| return `${count} ${count === 1 ? 'comment' : 'comments'}`; | ||
| } | ||
|
|
||
| return 'discuss'; | ||
| } |
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.
📝 Info: Saved theme applied only after first paint
getInitialSettingshardcodestheme: 'default'and reads the persisted theme only inside a lateruseEffect(SettingsContext.tsx). The Angular service applied it in the constructor before render, so users with a savednighttheme can see a brief default-theme flash.Was this helpful? React with 👍 or 👎 to provide feedback.
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.
Accurate.
getInitialSettingsdeliberately doesn't readthemesynchronously because the effect that applies it also subscribes toprefers-color-scheme, and the saved value has to win over the media query — doing it in one place avoided duplicating that precedence logic. The flash is real though; the fix is to seedthemefromlocalStoragein the initializer and let the effect only handle the media-query default when nothing is saved. Flagging to the requester rather than folding a behavior tweak into this PR unprompted.