Skip to content

Commit

Permalink
refactor: simplify useTipModal parameter handling
Browse files Browse the repository at this point in the history
  • Loading branch information
lawvs committed Nov 9, 2024
1 parent 072dec0 commit b706557
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 33 deletions.
14 changes: 8 additions & 6 deletions apps/renderer/src/hooks/biz/useEntryActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,7 @@ export const useEntryActions = ({
} as CombinedEntryModel
}, [entry, feed, inbox])

const openTipModal = useTipModal({
userId: populatedEntry?.feeds?.ownerUserId ?? undefined,
feedId: populatedEntry?.feeds?.id ?? undefined,
entryId: populatedEntry?.entries.id ?? undefined,
})
const openTipModal = useTipModal()

const showSourceContent = useShowSourceContent()
const showSourceContentModal = useSourceContentModal()
Expand Down Expand Up @@ -210,7 +206,13 @@ export const useEntryActions = ({
className: "i-mgc-power-outline",
hide: isInbox || feed?.ownerUserId === whoami()?.id,
onClick: () => {
nextFrame(openTipModal)
nextFrame(() =>
openTipModal({
userId: populatedEntry?.feeds?.ownerUserId ?? undefined,
feedId: populatedEntry?.feeds?.id ?? undefined,
entryId: populatedEntry?.entries.id ?? undefined,
}),
)
},
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ export const SupportCreator = ({ entryId }: { entryId: string }) => {
const feed = useFeedById(entry?.feedId)
const { data: feedBoosters } = useFeedBoostersQuery(entry?.feedId)

const openTipModal = useTipModal({
userId: feed?.ownerUserId ?? undefined,
feedId: entry?.feedId,
entryId,
})
const openTipModal = useTipModal()
const openBoostModal = useBoostModal()

const isMyOwnedFeed = feed?.ownerUserId === useWhoami()?.id
Expand Down Expand Up @@ -60,7 +56,15 @@ export const SupportCreator = ({ entryId }: { entryId: string }) => {

<div className="flex items-center gap-4">
{!isMyOwnedFeed && (
<Button onClick={() => openTipModal()}>
<Button
onClick={() =>
openTipModal({
userId: feed.ownerUserId ?? undefined,
feedId: entry?.feedId,
entryId,
})
}
>
<i className="i-mgc-power-outline mr-1.5 text-lg" />
{t("entry_content.support_creator")}
</Button>
Expand Down
37 changes: 16 additions & 21 deletions apps/renderer/src/modules/wallet/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,22 @@ import { useModalStack } from "~/components/ui/modal/stacked/hooks"

import { TipModalContent } from "./tip-modal"

export const useTipModal = ({
userId,
feedId,
entryId,
}: {
userId?: string
feedId?: string
entryId?: string
}) => {
export const useTipModal = () => {
const { present } = useModalStack()
const { t } = useTranslation()
return useCallback(() => {
if (!feedId || !entryId) {
// this should not happen unless there is a bug in the code
toast.error("Invalid feed id or entry id")
return
}
window.analytics?.capture("tip_modal_opened", { entryId })
present({
title: t("tip_modal.tip_title"),
content: () => createElement(TipModalContent, { userId, feedId, entryId }),
})
}, [present, userId, feedId, entryId])
return useCallback(
({ userId, feedId, entryId }: { userId?: string; feedId?: string; entryId?: string }) => {
if (!feedId || !entryId) {
// this should not happen unless there is a bug in the code
toast.error("Invalid feed id or entry id")
return
}
window.analytics?.capture("tip_modal_opened", { entryId })
present({
title: t("tip_modal.tip_title"),
content: () => createElement(TipModalContent, { userId, feedId, entryId }),
})
},
[present, t],
)
}

0 comments on commit b706557

Please sign in to comment.