Skip to content

Commit

Permalink
feat: native menu add hide prop
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <i@innei.in>
  • Loading branch information
Innei committed Oct 4, 2024
1 parent f96df54 commit 8c6753d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
33 changes: 15 additions & 18 deletions apps/renderer/src/hooks/biz/useFeedActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useTranslation } from "react-i18next"
import { whoami } from "~/atoms/user"
import { useModalStack } from "~/components/ui/modal"
import type { FeedViewType } from "~/lib/enum"
import type { NativeMenuItem } from "~/lib/native-menu"
import type { NativeMenuItem, NullableNativeMenuItem } from "~/lib/native-menu"
import { useFeedClaimModal } from "~/modules/claim"
import { FeedForm } from "~/modules/discover/feed-form"
import { InboxForm } from "~/modules/discover/inbox-form"
Expand Down Expand Up @@ -47,8 +47,8 @@ export const useFeedActions = ({
const navigateEntry = useNavigateEntry()
const isEntryList = type === "entryList"

const addMutation = useAddFeedToFeedList()
const removeMutation = useRemoveFeedFromFeedList()
const { mutateAsync: addFeedToListMutation } = useAddFeedToFeedList()
const { mutateAsync: removeFeedFromListMutation } = useRemoveFeedFromFeedList()

const listByView = useListByView(view!)

Expand Down Expand Up @@ -101,12 +101,12 @@ export const useFeedActions = ({
checked: isIncluded,
click() {
if (!isIncluded) {
addMutation.mutate({
addFeedToListMutation({
feedId,
listId: list.id,
})
} else {
removeMutation.mutate({
removeFeedFromListMutation({
feedId,
listId: list.id,
})
Expand Down Expand Up @@ -209,7 +209,8 @@ export const useFeedActions = ({
listByView,
feedId,
claimFeed,
addMutation,
addFeedToListMutation,
removeFeedFromListMutation,
present,
deleteSubscription,
subscription,
Expand All @@ -226,25 +227,21 @@ export const useListActions = ({ listId, view }: { listId: string; view: FeedVie
const subscription = useSubscriptionByFeedId(listId)

const { present } = useModalStack()
const deleteSubscription = useDeleteSubscription({})
const { mutateAsync: deleteSubscription } = useDeleteSubscription({})

const navigateEntry = useNavigateEntry()

const items = useMemo(() => {
if (!list) return []

const items: NativeMenuItem[] = [
...(list.ownerUserId === whoami()?.id
? [
{
type: "text" as const,
label: t("sidebar.feed_actions.list_owned_by_you"),
},
]
: []),
const items: NullableNativeMenuItem[] = [
list.ownerUserId === whoami()?.id && {
type: "text" as const,
label: t("sidebar.feed_actions.list_owned_by_you"),
},
{
type: "separator" as const,
disabled: false,
hide: list.ownerUserId !== whoami()?.id,
},

{
Expand All @@ -262,7 +259,7 @@ export const useListActions = ({ listId, view }: { listId: string; view: FeedVie
type: "text" as const,
label: t("sidebar.feed_actions.unfollow"),
shortcut: "Meta+Backspace",
click: () => deleteSubscription.mutate(subscription),
click: () => deleteSubscription(subscription),
},
{
type: "text" as const,
Expand Down
11 changes: 9 additions & 2 deletions apps/renderer/src/lib/native-menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { get } from "lodash-es"
import { tipcClient } from "./client"
import { getOS } from "./utils"

export type NativeMenuItem =
export type NativeMenuItem = (
| {
type: "text"
label: string
Expand All @@ -17,6 +17,9 @@ export type NativeMenuItem =
checked?: boolean
}
| { type: "separator"; disabled?: boolean }
) & { hide?: boolean }

export type NullableNativeMenuItem = NativeMenuItem | null | undefined | false | ""

function sortShortcutsString(shortcut: string) {
const order = ["Shift", "Ctrl", "Alt", "Meta"]
Expand All @@ -32,7 +35,7 @@ function sortShortcutsString(shortcut: string) {
return [...sortedModifiers, ...otherKeys].join("+")
}
export const showNativeMenu = async (
items: Array<Nullable<NativeMenuItem | false>>,
items: Array<NullableNativeMenuItem>,
e?: MouseEvent | React.MouseEvent,
) => {
const nextItems = (items.filter(Boolean) as NativeMenuItem[]).map((item) => {
Expand All @@ -42,6 +45,10 @@ export const showNativeMenu = async (
shortcut: item.shortcut ? sortShortcutsString(item.shortcut) : undefined,
}
}

if (item.hide) {
return []
}
return item
}) as NativeMenuItem[]

Expand Down

0 comments on commit 8c6753d

Please sign in to comment.