Skip to content

Commit

Permalink
feat: use actions api
Browse files Browse the repository at this point in the history
  • Loading branch information
DIYgod committed Jun 12, 2024
1 parent abff0a3 commit e0cc5c7
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 34 deletions.
76 changes: 42 additions & 34 deletions src/renderer/src/pages/settings/actions.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { ActionCard } from "@renderer/components/settings/action-card"
import { SettingsTitle } from "@renderer/components/settings/title"
import { Button } from "@renderer/components/ui/button"
import { useState } from "react"
import { useBizQuery } from "@renderer/hooks/useBizQuery"
import { apiClient } from "@renderer/lib/api-fetch"
import { Queries } from "@renderer/queries"
import { useMutation } from "@tanstack/react-query"
import { useEffect, useState } from "react"
import { toast } from "sonner"

type Operation = "contains" | "not_contains" | "eq" | "not_eq" | "gt" | "lt" | "regex"
type EntryField = "all" | "title" | "content" | "author" | "link" | "order"
Expand Down Expand Up @@ -30,46 +35,43 @@ type ActionsInput = {
}[]

export function Component() {
const [testActions, setTestActions] = useState<ActionsInput>([{
name: "Twitter.com to x.com",
condition: [{
field: "view" as const,
operator: "eq" as const,
value: 1,
}, {
field: "title" as const,
operator: "contains" as const,
value: "Twitter",
}],
result: {
translation: "zh-CN",
summary: true,
rewriteRules: [{
from: "twitter.com",
to: "x.com",
}],
blockRules: [{
field: "content" as const,
operator: "contains" as const,
value: "next.js",
}, {
field: "author" as const,
operator: "eq" as const,
value: "elonmusk",
}],
const actions = useBizQuery(Queries.action.getAll())
const [actionsData, setActionsData] = useState<ActionsInput>([])

useEffect(() => {
if (actions.data?.rules) {
setActionsData(actions.data.rules)
}
}, [actions.data?.rules])

const mutation = useMutation({
mutationFn: async () => {
await apiClient.actions.$put({
json: {
rules: actionsData,
},
})
},
onSuccess: () => {
Queries.action.getAll().invalidate()
toast("🎉 Actions saved.")
},
}])
})

return (
<>
<SettingsTitle path="actions" className="mb-4" />
<div className="space-y-4">
{testActions.map((action, actionIdx) => (
{actionsData.map((action, actionIdx) => (
<ActionCard
key={actionIdx}
key={action.name}
data={action}
onChange={(newAction) => {
setTestActions(testActions.map((a, idx) => idx === actionIdx ? newAction : a))
if (!newAction) {
setActionsData(actionsData.filter((_, idx) => idx !== actionIdx))
} else {
setActionsData(actionsData.map((a, idx) => idx === actionIdx ? newAction : a))
}
}}
/>
))}
Expand All @@ -78,8 +80,8 @@ export function Component() {
size="sm"
className="w-full gap-1"
onClick={() => {
setTestActions([...testActions, {
name: `Action ${testActions.length + 1}`,
setActionsData([...actionsData, {
name: `Action ${actionsData.length + 1}`,
condition: [],
result: {},
}])
Expand All @@ -89,6 +91,12 @@ export function Component() {
{" "}
New Rule
</Button>
<Button
isLoading={mutation.isPending}
onClick={() => mutation.mutate()}
>
Save
</Button>
</div>
</>
)
Expand Down
10 changes: 10 additions & 0 deletions src/renderer/src/queries/actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { apiClient } from "@renderer/lib/api-fetch"
import { defineQuery } from "@renderer/lib/defineQuery"

export const action = {
getAll: () =>
defineQuery(["actions"], async () => {
const res = await apiClient.actions.$get()
return res.data
}),
}
2 changes: 2 additions & 0 deletions src/renderer/src/queries/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { action } from "./actions"
import { entries } from "./entries"
import { feed } from "./feed"
import { subscription } from "./subscriptions"
Expand All @@ -6,4 +7,5 @@ export const Queries = {
subscription,
entries,
feed,
action,
}

0 comments on commit e0cc5c7

Please sign in to comment.