Skip to content

Commit

Permalink
feat: add button style and rename useAuthQuery (#105)
Browse files Browse the repository at this point in the history
* feat: add button style and rename `useAuthQuery`

Signed-off-by: Innei <[email protected]>

* fix: types

Signed-off-by: Innei <[email protected]>

---------

Signed-off-by: Innei <[email protected]>
  • Loading branch information
Innei authored Jul 2, 2024
1 parent 03a4246 commit 43a93ae
Show file tree
Hide file tree
Showing 23 changed files with 74 additions and 50 deletions.
2 changes: 1 addition & 1 deletion src/renderer/src/atoms/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ export const [, , useUser, useSetUser, getUser, setUser] = createAtomHooks(
atom<Nullable<User>>(null),
)

export const [, , useAuthFail, useSetAuthFail, getAuthFail, setAuthFail] =
export const [, , useLoginModalShow, useSetLoginModalShow, getLoginModalShow, setLoginModalShow] =
createAtomHooks(atom<boolean>(false))
13 changes: 11 additions & 2 deletions src/renderer/src/components/ui/button/variants.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,15 @@ export const styledButtonVariant = cva(
className: "text-zinc-50 bg-theme-disabled",
},
{
variant: "plain",
variant: "outline",
status: "disabled",
className: "text-theme-disabled border-theme-inactive dark:border-zinc-800 hover:!bg-theme-background",
},
{
variant: "text",
status: "disabled",
className: "opacity-60",
},
],
variants: {
status: {
Expand All @@ -58,10 +63,14 @@ export const styledButtonVariant = cva(
"text-zinc-100 dark:text-zinc-200/90",
),

plain: cn(
outline: cn(
"bg-theme-background font-semibold transition-colors duration-200",
"border border-border hover:bg-zinc-50/20 dark:bg-neutral-900/30",
),
text: cn(
"font-semibold text-theme-accent",
"hover:contrast-[1.10] active:contrast-125",
),
},
},

Expand Down
4 changes: 4 additions & 0 deletions src/renderer/src/hooks/biz/useAuth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { signOut } from "@hono/auth-js/react"
import { setLoginModalShow, setUser } from "@renderer/atoms"
import { QUERY_PERSIST_KEY } from "@renderer/lib/constants"
import { clearLocalPersistStoreData } from "@renderer/store/utils/clear"
import { useCallback } from "react"
Expand All @@ -13,6 +14,9 @@ export const useSignOut = () =>
// Clear query cache
localStorage.removeItem(QUERY_PERSIST_KEY)

setLoginModalShow(true)
setUser(null)

// Clear local storage
// TODO
}, [])
20 changes: 15 additions & 5 deletions src/renderer/src/hooks/common/useBizQuery.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { useLoginModalShow } from "@renderer/atoms"
import type { DefinedQuery } from "@renderer/lib/defineQuery"
import type {
InfiniteData,
Expand All @@ -17,7 +18,7 @@ export type SafeReturnType<T> = T extends (...args: any[]) => infer R
: never

export type CombinedObject<T, U> = T & U
export function useBizQuery<
export function useAuthQuery<
TQuery extends DefinedQuery<QueryKey, any>,
TError = FetchError,
TQueryFnData = Awaited<ReturnType<TQuery["fn"]>>,
Expand All @@ -31,13 +32,15 @@ export function useBizQuery<
): CombinedObject<
UseQueryResult<TData, TError>,
{ key: TQuery["key"], fn: TQuery["fn"] }
> {
> {
const authFail = useLoginModalShow()
// @ts-expect-error
return Object.assign(
{},
useQuery({
queryKey: query.key,
queryFn: query.fn,
enabled: !authFail && options.enabled !== false,
...options,
}),
{
Expand All @@ -47,7 +50,7 @@ export function useBizQuery<
)
}

export function useBizInfiniteQuery<
export function useAuthInfiniteQuery<
T extends DefinedQuery<any, any>,
E = FetchError,
FNR = Awaited<ReturnType<T["fn"]>>,
Expand All @@ -58,14 +61,16 @@ export function useBizInfiniteQuery<
): CombinedObject<
UseInfiniteQueryResult<InfiniteData<R>, FetchError>,
{ key: T["key"], fn: T["fn"] }
> {
> {
const authFail = useLoginModalShow()
// @ts-expect-error
return Object.assign(
{},
// @ts-expect-error
useInfiniteQuery<T, E>({
// @ts-expect-error
queryFn: query.fn,
queryKey: query.key,
enabled: !authFail && options.enabled !== false,
...options,
}),
{
Expand All @@ -74,3 +79,8 @@ export function useBizInfiniteQuery<
},
)
}

/**
* @deprecated use `useAuthQuery` instead
*/
export const useBizQuery = useAuthQuery
4 changes: 2 additions & 2 deletions src/renderer/src/lib/api-fetch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getCsrfToken } from "@hono/auth-js/react"
import { setAuthFail } from "@renderer/atoms"
import { setLoginModalShow } from "@renderer/atoms"
import type { AppType } from "@renderer/hono"
import { hc } from "hono/client"
import { FetchError, ofetch } from "ofetch"
Expand Down Expand Up @@ -31,7 +31,7 @@ export const apiFetch = ofetch.create({
// Or we can present LoginModal here.
// router.navigate("/login")
// If any response status is 401, we can set auth fail. Maybe some bug, but if navigate to login page, had same issues
setAuthFail(true)
setLoginModalShow(true)
}
try {
const json = JSON.parse(context.response._data)
Expand Down
6 changes: 3 additions & 3 deletions src/renderer/src/modules/claim/feed-claim-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
TabsList,
TabsTrigger,
} from "@renderer/components/ui/tabs"
import { useBizQuery } from "@renderer/hooks"
import { useAuthQuery } from "@renderer/hooks"
import { Queries } from "@renderer/queries"
import { useClaimFeedMutation } from "@renderer/queries/feed"
import { useFeedById } from "@renderer/store"
Expand All @@ -26,7 +26,7 @@ export const FeedClaimModalContent: FC<{
data: claimMessage,
isLoading,
error,
} = useBizQuery(Queries.feed.claimMessage({ feedId }), {
} = useAuthQuery(Queries.feed.claimMessage({ feedId }), {
enabled: !!feed,
})

Expand Down Expand Up @@ -125,7 +125,7 @@ export const FeedClaimModalContent: FC<{
disabled={isSuccess}
isLoading={isPending}
onClick={() => claim()}
variant={isSuccess ? "plain" : "primary"}
variant={isSuccess ? "outline" : "primary"}
>
{isSuccess && (
<i className="i-mgc-check-circle-filled mr-2 bg-green-500" />
Expand Down
7 changes: 4 additions & 3 deletions src/renderer/src/modules/discover/feed-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
} from "@renderer/components/ui/select"
import { Switch } from "@renderer/components/ui/switch"
import { ViewSelectContent } from "@renderer/components/view-select-content"
import { useBizQuery } from "@renderer/hooks"
import { useAuthQuery } from "@renderer/hooks"
import { useDeleteSubscription } from "@renderer/hooks/biz/useSubscriptionActions"
import { apiClient } from "@renderer/lib/api-fetch"
import { tipcClient } from "@renderer/lib/client"
Expand Down Expand Up @@ -145,7 +145,7 @@ export const FeedForm: Component<{
followMutation.mutate(values)
}

const categories = useBizQuery(
const categories = useAuthQuery(
Queries.subscription.categories(Number.parseInt(form.watch("view"))),
)

Expand Down Expand Up @@ -270,8 +270,9 @@ export const FeedForm: Component<{
{isSubscribed && (
<StyledButton
ref={buttonRef}
variant="text"
isLoading={deleteSubscription.isPending}
className="bg-red-500"
className="text-red-500"
onClick={(e) => {
e.preventDefault()
if (feed.data?.subscription) {
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/src/modules/discover/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export function DiscoverForm({ type }: { type: string }) {
</CardContent>
<CardFooter>
{item.isSubscribed ? (
<StyledButton variant="plain" disabled>
<StyledButton variant="outline" disabled>
Followed
</StyledButton>
) : (
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/src/modules/discover/recommendations.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { LoadingCircle } from "@renderer/components/ui/loading"
import { useBizQuery } from "@renderer/hooks"
import { useAuthQuery } from "@renderer/hooks"
import { isASCII } from "@renderer/lib/utils"
import { Queries } from "@renderer/queries"
import { useMemo } from "react"

import { RecommendationCard } from "./recommendations-card"

export function Recommendations() {
const rsshubPopular = useBizQuery(
const rsshubPopular = useAuthQuery(
Queries.discover.rsshubCategory({
category: "popular",
}),
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/src/modules/entry-column/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ const ListHeader: FC<{
<div>Mark all as read?</div>
<div className="space-x-4">
<PopoverClose>
<StyledButton variant="plain">Cancel</StyledButton>
<StyledButton variant="outline">Cancel</StyledButton>
</PopoverClose>
{/* TODO */}
<StyledButton onClick={handleMarkAllAsRead}>
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/src/modules/entry-column/item.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useAsRead, useBizQuery, useEntryActions } from "@renderer/hooks"
import { useAsRead, useAuthQuery, useEntryActions } from "@renderer/hooks"
import { useNavigateEntry } from "@renderer/hooks/biz/useNavigateEntry"
import { useRouteParamsSelector } from "@renderer/hooks/biz/useRouteParams"
import { views } from "@renderer/lib/constants"
Expand Down Expand Up @@ -30,7 +30,7 @@ function EntryItemImpl({ entry, view }: { entry: CombinedEntryModel, view?: numb
entry,
})

const translation = useBizQuery(
const translation = useAuthQuery(
Queries.ai.translation({
entry,
view,
Expand Down
8 changes: 4 additions & 4 deletions src/renderer/src/modules/entry-content/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useUISettingKey } from "@renderer/atoms"
import { m } from "@renderer/components/common/Motion"
import { Logo } from "@renderer/components/icons/logo"
import { AutoResizeHeight } from "@renderer/components/ui/auto-resize-height"
import { useBizQuery } from "@renderer/hooks"
import { useAuthQuery } from "@renderer/hooks"
import { stopPropagation } from "@renderer/lib/dom"
import { parseHtml } from "@renderer/lib/parse-html"
import type { ActiveEntryId } from "@renderer/models"
Expand Down Expand Up @@ -38,7 +38,7 @@ export const EntryContent = ({ entryId }: { entryId: ActiveEntryId }) => {
}

function EntryContentRender({ entryId }: { entryId: string }) {
const { error, data } = useBizQuery(Queries.entries.byId(entryId), {
const { error, data } = useAuthQuery(Queries.entries.byId(entryId), {
staleTime: 300_000,
})

Expand All @@ -59,7 +59,7 @@ function EntryContentRender({ entryId }: { entryId: string }) {
}
}, [data?.entries.content, entry?.entries.content, readerRenderInlineStyle])

const translation = useBizQuery(
const translation = useAuthQuery(
Queries.ai.translation({
entry: entry!,
language: entry?.settings?.translation,
Expand All @@ -72,7 +72,7 @@ function EntryContentRender({ entryId }: { entryId: string }) {
},
)

const summary = useBizQuery(
const summary = useAuthQuery(
Queries.ai.summary({
entryId,
language: entry?.settings?.translation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function CategoryRemoveDialogContent({
</p>

<div className="flex items-center justify-end gap-3">
<StyledButton variant="plain" onClick={dismiss}>
<StyledButton variant="outline" onClick={dismiss}>
Cancel
</StyledButton>
<StyledButton
Expand Down
6 changes: 3 additions & 3 deletions src/renderer/src/modules/feed-column/list.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useUISettingKey } from "@renderer/atoms"
import { useBizQuery } from "@renderer/hooks"
import { useAuthQuery } from "@renderer/hooks"
import { useNavigateEntry } from "@renderer/hooks/biz/useNavigateEntry"
import { useRouteFeedId } from "@renderer/hooks/biz/useRouteParams"
import { FEED_COLLECTION_LIST, levels, views } from "@renderer/lib/constants"
Expand All @@ -21,7 +21,7 @@ import { parse } from "tldts"
import { FeedCategory } from "./category"

const useData = (view: FeedViewType) => {
const { data: remoteData } = useBizQuery(Queries.subscription.byView(view))
const { data: remoteData } = useAuthQuery(Queries.subscription.byView(view))

const data = useSubscriptionByView(view) || remoteData

Expand Down Expand Up @@ -101,7 +101,7 @@ export function FeedList({
const [expansion, setExpansion] = useState(false)
const data = useData(view)

useBizQuery(Queries.subscription.unreadAll())
useAuthQuery(Queries.subscription.unreadAll())

const totalUnread = useUnreadStore((state) => {
let unread = 0
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/src/modules/wallet/tip-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export const TipModalContent: FC<{
})
setShowConfirm(false)
}}
variant={tipMutation.isSuccess ? "plain" : "primary"}
variant={tipMutation.isSuccess ? "outline" : "primary"}
>
{tipMutation.isSuccess && (
<i className="i-mgc-check-circle-filled mr-2 bg-green-500" />
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/src/pages/(external)/redirect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ export function Component() {
</h2>
<div className="center flex gap-4">
<StyledButton
variant="plain"
className="h-14 border-transparent px-10 text-base"
variant="text"
className="h-14 px-10 text-base"
onClick={() => navigate("/")}
>
Continue in Browser
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function Component() {

<div>
<StyledButton
variant="plain"
variant="outline"
onClick={() => {
signOut()
}}
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/src/pages/(main)/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
setMainContainerElement,
useAuthFail,
useLoginModalShow,
useUser,
} from "@renderer/atoms"
import { DeclarativeModal } from "@renderer/components/ui/modal/stacked/declarative-modal"
Expand All @@ -12,7 +12,7 @@ import { FeedColumn } from "@renderer/modules/feed-column"
import { Outlet } from "react-router-dom"

export function Component() {
const isAuthFail = useAuthFail()
const isAuthFail = useLoginModalShow()
const user = useUser()

return (
Expand Down
6 changes: 3 additions & 3 deletions src/renderer/src/pages/settings/(settings)/actions.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { StyledButton } from "@renderer/components/ui/button"
import { useBizQuery } from "@renderer/hooks"
import { useAuthQuery } from "@renderer/hooks"
import { apiClient } from "@renderer/lib/api-fetch"
import type { ActionsResponse } from "@renderer/models"
import { ActionCard } from "@renderer/modules/settings/action-card"
Expand Down Expand Up @@ -54,7 +54,7 @@ type ActionsInput = {
}[]

export function Component() {
const actions = useBizQuery(Queries.action.getAll())
const actions = useAuthQuery(Queries.action.getAll())
const [actionsData, setActionsData] = useState<ActionsInput>([])

useEffect(() => {
Expand Down Expand Up @@ -112,7 +112,7 @@ export function Component() {
/>
))}
<StyledButton
variant="plain"
variant="outline"
className="center w-full gap-1"
onClick={() => {
setActionsData([
Expand Down
Loading

0 comments on commit 43a93ae

Please sign in to comment.