mirror of
https://github.com/linsa-io/linsa.git
synced 2026-01-12 12:20:23 +01:00
Move to TanStack Start from Next.js (#184)
This commit is contained in:
35
web/app/hooks/actions/use-link-actions.ts
Normal file
35
web/app/hooks/actions/use-link-actions.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import * as React from "react"
|
||||
import { toast } from "sonner"
|
||||
import { LaAccount, PersonalLink } from "@/lib/schema"
|
||||
|
||||
export const useLinkActions = () => {
|
||||
const deleteLink = React.useCallback((me: LaAccount, link: PersonalLink) => {
|
||||
if (!me.root?.personalLinks) return
|
||||
|
||||
try {
|
||||
const index = me.root.personalLinks.findIndex(
|
||||
(item) => item?.id === link.id,
|
||||
)
|
||||
if (index === -1) {
|
||||
throw new Error(`Link with id ${link.id} not found`)
|
||||
}
|
||||
|
||||
me.root.personalLinks.splice(index, 1)
|
||||
|
||||
toast.success("Link deleted.", {
|
||||
position: "bottom-right",
|
||||
description: `${link.title} has been deleted.`,
|
||||
})
|
||||
} catch (error) {
|
||||
console.error("Failed to delete link:", error)
|
||||
toast.error("Failed to delete link", {
|
||||
description:
|
||||
error instanceof Error ? error.message : "An unknown error occurred",
|
||||
})
|
||||
}
|
||||
}, [])
|
||||
|
||||
return {
|
||||
deleteLink,
|
||||
}
|
||||
}
|
||||
50
web/app/hooks/actions/use-page-actions.ts
Normal file
50
web/app/hooks/actions/use-page-actions.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import * as React from "react"
|
||||
import { toast } from "sonner"
|
||||
import { LaAccount, PersonalPage } from "@/lib/schema"
|
||||
import { ID } from "jazz-tools"
|
||||
|
||||
export const usePageActions = () => {
|
||||
const newPage = React.useCallback((me: LaAccount): PersonalPage => {
|
||||
const newPersonalPage = PersonalPage.create(
|
||||
{ public: false, createdAt: new Date(), updatedAt: new Date() },
|
||||
{ owner: me._owner },
|
||||
)
|
||||
me.root?.personalPages?.push(newPersonalPage)
|
||||
return newPersonalPage
|
||||
}, [])
|
||||
|
||||
const deletePage = React.useCallback(
|
||||
(me: LaAccount, pageId: ID<PersonalPage>): void => {
|
||||
if (!me.root?.personalPages) return
|
||||
|
||||
const index = me.root.personalPages.findIndex(
|
||||
(item) => item?.id === pageId,
|
||||
)
|
||||
if (index === -1) {
|
||||
toast.error("Page not found")
|
||||
return
|
||||
}
|
||||
|
||||
const page = me.root.personalPages[index]
|
||||
if (!page) {
|
||||
toast.error("Page data is invalid")
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
me.root.personalPages.splice(index, 1)
|
||||
|
||||
toast.success("Page deleted", {
|
||||
position: "bottom-right",
|
||||
description: `${page.title} has been deleted.`,
|
||||
})
|
||||
} catch (error) {
|
||||
console.error("Failed to delete page", error)
|
||||
toast.error("Failed to delete page")
|
||||
}
|
||||
},
|
||||
[],
|
||||
)
|
||||
|
||||
return { newPage, deletePage }
|
||||
}
|
||||
61
web/app/hooks/actions/use-task-actions.ts
Normal file
61
web/app/hooks/actions/use-task-actions.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import { useCallback } from "react"
|
||||
import { toast } from "sonner"
|
||||
import { LaAccount } from "@/lib/schema"
|
||||
import { ID } from "jazz-tools"
|
||||
import { ListOfTasks, Task } from "~/lib/schema/task"
|
||||
|
||||
export const useTaskActions = () => {
|
||||
const newTask = useCallback((me: LaAccount): Task | null => {
|
||||
if (!me.root) {
|
||||
console.error("User root is not initialized")
|
||||
return null
|
||||
}
|
||||
|
||||
if (!me.root.tasks) {
|
||||
me.root.tasks = ListOfTasks.create([], { owner: me })
|
||||
}
|
||||
|
||||
const newTask = Task.create(
|
||||
{
|
||||
title: "",
|
||||
description: "",
|
||||
status: "todo",
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
},
|
||||
{ owner: me._owner },
|
||||
)
|
||||
|
||||
me.root.tasks.push(newTask)
|
||||
return newTask
|
||||
}, [])
|
||||
|
||||
const deleteTask = useCallback((me: LaAccount, taskId: ID<Task>): void => {
|
||||
if (!me.root?.tasks) return
|
||||
|
||||
const index = me.root.tasks.findIndex((item) => item?.id === taskId)
|
||||
if (index === -1) {
|
||||
toast.error("Task not found")
|
||||
return
|
||||
}
|
||||
|
||||
const task = me.root.tasks[index]
|
||||
if (!task) {
|
||||
toast.error("Task data is invalid")
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
me.root.tasks.splice(index, 1)
|
||||
|
||||
toast.success("Task completed", {
|
||||
position: "bottom-right",
|
||||
})
|
||||
} catch (error) {
|
||||
console.error("Failed to delete task", error)
|
||||
toast.error("Failed to delete task")
|
||||
}
|
||||
}, [])
|
||||
|
||||
return { newTask, deleteTask }
|
||||
}
|
||||
Reference in New Issue
Block a user