-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from azu/migrate-app-router
recactor(web): migrate to App Router
- Loading branch information
Showing
23 changed files
with
2,143 additions
and
1,015 deletions.
There are no files selected for viewing
This file contains 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 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,36 @@ | ||
"use client"; | ||
import { CSSProperties, useCallback, useState } from "react"; | ||
|
||
export function CompositionInput(props: { style?: CSSProperties; value: string; onInput: (value: string) => void }) { | ||
const [inputValue, setInputValue] = useState(props.value); | ||
const [isComposing, setIsComposing] = useState(false); | ||
const onInput = useCallback( | ||
(event) => { | ||
const value = event.currentTarget.value; | ||
setInputValue(value); | ||
if (!isComposing) { | ||
props.onInput(value); | ||
} | ||
}, | ||
[isComposing] | ||
); | ||
const onCompositionStart = useCallback((e) => { | ||
setIsComposing(true); | ||
}, []); | ||
const onCompositionEnd = useCallback((event) => { | ||
setIsComposing(false); | ||
const value = event.currentTarget.value; | ||
setInputValue(value); | ||
props.onInput(value); | ||
}, []); | ||
return ( | ||
<input | ||
type={"text"} | ||
value={inputValue} | ||
onInput={onInput} | ||
style={props.style} | ||
onCompositionStart={onCompositionStart} | ||
onCompositionEnd={onCompositionEnd} | ||
/> | ||
); | ||
} |
This file contains 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,58 @@ | ||
"use client"; | ||
import { useDeferredValue, useEffect, useMemo, useState, useTransition } from "react"; | ||
import { CompositionInput } from "./CompositionInput"; | ||
import { useTypeUrlSearchParams } from "../lib/useTypeUrlSearchParams"; | ||
import { HomPageSearchParam } from "../page"; | ||
import { useTransitionContext } from "./TransitionContext"; | ||
|
||
const debounce = (fn: (..._: any[]) => void, delay: number) => { | ||
let timeout: any; | ||
return (...args: any[]) => { | ||
clearTimeout(timeout); | ||
timeout = setTimeout(() => { | ||
fn(...args); | ||
}, delay); | ||
}; | ||
}; | ||
const useSearch = ({ query }: { query?: string }) => { | ||
const [isSearching, startTransition] = useTransition(); | ||
const [inputQuery, setInputQuery] = useState(query ?? ""); | ||
const deferredInputValue = useDeferredValue(inputQuery); | ||
const searchParams = useTypeUrlSearchParams<HomPageSearchParam>(); | ||
const { setIsLoadingTimeline } = useTransitionContext(); | ||
useEffect(() => { | ||
setIsLoadingTimeline(isSearching); | ||
}, [isSearching]); | ||
useEffect(() => { | ||
if (deferredInputValue === query) return; | ||
startTransition(() => { | ||
searchParams.pushParams({ | ||
q: deferredInputValue | ||
}); | ||
}); | ||
}, [deferredInputValue]); | ||
const handlers = useMemo( | ||
() => ({ | ||
search: debounce((query: string) => { | ||
setInputQuery(query); | ||
}, 300) | ||
}), | ||
[] | ||
); | ||
return { | ||
inputQuery, | ||
isSearching, | ||
handlers | ||
}; | ||
}; | ||
export const SearchBox = (props: { query?: string }) => { | ||
const { inputQuery, handlers } = useSearch({ query: props.query }); | ||
return ( | ||
<div> | ||
<label> | ||
Search: | ||
<CompositionInput value={inputQuery} onInput={handlers.search} style={{ width: "100%" }} /> | ||
</label> | ||
</div> | ||
); | ||
}; |
This file contains 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,50 @@ | ||
"use client"; | ||
import { LineTweetResponse } from "../server/search"; | ||
import { useMemo, useTransition } from "react"; | ||
import { useTypeUrlSearchParams } from "../lib/useTypeUrlSearchParams"; | ||
import { HomPageSearchParam } from "../page"; | ||
|
||
export const useSearchMore = (props: { searchResults: LineTweetResponse[] }) => { | ||
const searchParams = useTypeUrlSearchParams<HomPageSearchParam>(); | ||
const [isLoadingMore, startTransition] = useTransition(); | ||
const handlers = useMemo(() => { | ||
return { | ||
handleMoreTweets: () => { | ||
const lastItemTimeStamp = props.searchResults[props.searchResults.length - 1].timestamp; | ||
if (!lastItemTimeStamp) { | ||
return; | ||
} | ||
startTransition(() => { | ||
searchParams.pushParams({ | ||
timestamp: String(lastItemTimeStamp) | ||
}); | ||
}); | ||
} | ||
}; | ||
}, [props.searchResults]); | ||
return { | ||
handlers, | ||
isLoadingMore | ||
} as const; | ||
}; | ||
export const SearchMore = (props: { searchResults: LineTweetResponse[] }) => { | ||
const { handlers, isLoadingMore } = useSearchMore(props); | ||
return ( | ||
<div | ||
style={{ | ||
display: "flex", | ||
flexDirection: "column" | ||
}} | ||
> | ||
<button | ||
onClick={handlers.handleMoreTweets} | ||
disabled={isLoadingMore} | ||
style={{ | ||
opacity: isLoadingMore ? 0.5 : 1 | ||
}} | ||
> | ||
More Tweets | ||
</button> | ||
</div> | ||
); | ||
}; |
This file contains 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,19 @@ | ||
"use client"; | ||
import { useTransitionContext } from "./TransitionContext"; | ||
|
||
export const SearchResultContentWrapper = (props: { children: React.ReactNode }) => { | ||
const { isLoadingTimeline } = useTransitionContext(); | ||
return ( | ||
<div | ||
style={ | ||
isLoadingTimeline | ||
? { | ||
opacity: 0.5 | ||
} | ||
: {} | ||
} | ||
> | ||
{props.children} | ||
</div> | ||
); | ||
}; |
This file contains 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,26 @@ | ||
"use client"; | ||
import { createContext, ReactNode, useContext, useState } from "react"; | ||
|
||
export type TransitionContext = { | ||
isLoadingTimeline: boolean; | ||
setIsLoadingTimeline: (isLoading: boolean) => void; | ||
}; | ||
const TransitionContext = createContext<TransitionContext>({ | ||
isLoadingTimeline: false, | ||
setIsLoadingTimeline: () => {} | ||
}); | ||
export const TransitionContextProvider = (props: { children: ReactNode }) => { | ||
const [isLoadingTimeline, setIsLoadingTimeline] = useState(false); | ||
return ( | ||
<TransitionContext.Provider value={{ isLoadingTimeline, setIsLoadingTimeline }}> | ||
{props.children} | ||
</TransitionContext.Provider> | ||
); | ||
}; | ||
export const useTransitionContext = () => { | ||
const context = useContext(TransitionContext); | ||
if (!context) { | ||
throw new Error("useTransitionContext must be used within a TransitionContextProvider"); | ||
} | ||
return context; | ||
}; |
Oops, something went wrong.