Skip to content

Commit e5d3a08

Browse files
committed
fix: remove stupid code
Signed-off-by: Innei <[email protected]>
1 parent cd6044d commit e5d3a08

File tree

4 files changed

+145
-140
lines changed

4 files changed

+145
-140
lines changed

src/app/(dashboard)/dashboard/notes/edit/page.tsx

+45-45
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import {
2929
useEditorRef,
3030
Writing,
3131
} from '~/components/modules/dashboard/writing/Writing'
32-
import { LoadingButtonWrapper, StyledButton } from '~/components/ui/button'
32+
import { StyledButton } from '~/components/ui/button'
3333
import { PublishEvent, WriteEditEvent } from '~/events'
3434
import { useRefetchData } from '~/hooks/biz/use-refetch-data'
3535
import { useEventCallback } from '~/hooks/common/use-event-callback'
@@ -182,53 +182,53 @@ const ActionButtonGroup = ({ initialData }: { initialData?: NoteDto }) => {
182182
}}
183183
/>
184184
</div>
185-
<LoadingButtonWrapper isLoading={isPending}>
186-
<StyledButton
187-
onClick={() => {
188-
const currentData = {
189-
...getData(),
190-
}
191185

192-
const payload: NoteDto & {
193-
id?: string
194-
} = {
195-
...currentData,
196-
}
186+
<StyledButton
187+
isLoading={isPending}
188+
onClick={() => {
189+
const currentData = {
190+
...getData(),
191+
}
197192

198-
// if (
199-
// currentData.created === initialData?.created &&
200-
// currentData.created
201-
// ) {
202-
// payload.custom_created = new Date(currentData.created)
203-
// }
204-
205-
Reflect.deleteProperty(currentData, 'category')
206-
207-
const isCreate = !currentData.id
208-
const promise = isCreate
209-
? createNote(payload).then((res) => {
210-
router.replace(`/dashboard/notes/edit?id=${res.id}`)
211-
212-
return res
213-
})
214-
: updateNote(payload)
215-
promise
216-
.then((res) => {
217-
window.dispatchEvent(
218-
new PublishEvent({
219-
...payload,
220-
id: res.id,
221-
}),
222-
)
223-
})
224-
.catch((err) => {
225-
toast.error(err.message)
193+
const payload: NoteDto & {
194+
id?: string
195+
} = {
196+
...currentData,
197+
}
198+
199+
// if (
200+
// currentData.created === initialData?.created &&
201+
// currentData.created
202+
// ) {
203+
// payload.custom_created = new Date(currentData.created)
204+
// }
205+
206+
Reflect.deleteProperty(currentData, 'category')
207+
208+
const isCreate = !currentData.id
209+
const promise = isCreate
210+
? createNote(payload).then((res) => {
211+
router.replace(`/dashboard/notes/edit?id=${res.id}`)
212+
213+
return res
226214
})
227-
}}
228-
>
229-
{initialData ? '保存' : '发布'}
230-
</StyledButton>
231-
</LoadingButtonWrapper>
215+
: updateNote(payload)
216+
promise
217+
.then((res) => {
218+
window.dispatchEvent(
219+
new PublishEvent({
220+
...payload,
221+
id: res.id,
222+
}),
223+
)
224+
})
225+
.catch((err) => {
226+
toast.error(err.message)
227+
})
228+
}}
229+
>
230+
{initialData ? '保存' : '发布'}
231+
</StyledButton>
232232
</div>
233233
</>
234234
)

src/app/(dashboard)/dashboard/posts/edit/page.tsx

+45-45
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import {
2929
useEditorRef,
3030
Writing,
3131
} from '~/components/modules/dashboard/writing/Writing'
32-
import { LoadingButtonWrapper, StyledButton } from '~/components/ui/button'
32+
import { StyledButton } from '~/components/ui/button'
3333
import { PublishEvent, WriteEditEvent } from '~/events'
3434
import { useRefetchData } from '~/hooks/biz/use-refetch-data'
3535
import { useEventCallback } from '~/hooks/common/use-event-callback'
@@ -176,52 +176,52 @@ const ActionButtonGroup = ({ initialData }: { initialData?: PostDto }) => {
176176
}}
177177
/>
178178
</div>
179-
<LoadingButtonWrapper isLoading={isPending}>
180-
<StyledButton
181-
onClick={() => {
182-
const currentData = {
183-
...getData(),
184-
}
185179

186-
const payload: PostDto & {
187-
id?: string
188-
} = {
189-
...currentData,
190-
}
180+
<StyledButton
181+
isLoading={isPending}
182+
onClick={() => {
183+
const currentData = {
184+
...getData(),
185+
}
191186

192-
// if (
193-
// currentData.created === initialData?.created &&
194-
// currentData.created
195-
// ) {
196-
// payload.custom_created = new Date(currentData.created)
197-
// }
198-
199-
Reflect.deleteProperty(currentData, 'category')
200-
201-
const isCreate = !currentData.id
202-
const promise = isCreate
203-
? createPost(payload).then((res) => {
204-
router.replace(`/dashboard/posts/edit?id=${res.id}`)
205-
return res
206-
})
207-
: updatePost(payload)
208-
209-
promise.then((res) => {
210-
window.dispatchEvent(
211-
new PublishEvent({
212-
...payload,
213-
id: res.id,
214-
}),
215-
)
216-
})
217-
promise.catch((err) => {
218-
toast.error(err.message)
219-
})
220-
}}
221-
>
222-
{initialData ? '保存' : '发布'}
223-
</StyledButton>
224-
</LoadingButtonWrapper>
187+
const payload: PostDto & {
188+
id?: string
189+
} = {
190+
...currentData,
191+
}
192+
193+
// if (
194+
// currentData.created === initialData?.created &&
195+
// currentData.created
196+
// ) {
197+
// payload.custom_created = new Date(currentData.created)
198+
// }
199+
200+
Reflect.deleteProperty(currentData, 'category')
201+
202+
const isCreate = !currentData.id
203+
const promise = isCreate
204+
? createPost(payload).then((res) => {
205+
router.replace(`/dashboard/posts/edit?id=${res.id}`)
206+
return res
207+
})
208+
: updatePost(payload)
209+
210+
promise.then((res) => {
211+
window.dispatchEvent(
212+
new PublishEvent({
213+
...payload,
214+
id: res.id,
215+
}),
216+
)
217+
})
218+
promise.catch((err) => {
219+
toast.error(err.message)
220+
})
221+
}}
222+
>
223+
{initialData ? '保存' : '发布'}
224+
</StyledButton>
225225
</div>
226226
</>
227227
)
+30-25
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
2+
import { Fragment } from 'react'
23
import clsx from 'clsx'
34
import Link from 'next/link'
45
import { tv } from 'tailwind-variants'
@@ -31,47 +32,51 @@ type NativeLinkProps = React.AnchorHTMLAttributes<HTMLAnchorElement>
3132
type SharedProps = {
3233
variant?: 'primary' | 'secondary'
3334
className?: string
35+
isLoading?: boolean
3436
}
3537
type ButtonProps = SharedProps & (NativeButtonProps | NativeLinkProps)
3638

3739
export const StyledButton: FC<ButtonProps> = ({
3840
variant = 'primary',
3941
className,
42+
isLoading,
4043
href,
4144

4245
...props
4346
}) => {
44-
return href ? (
45-
<Link
46-
href={href}
47-
className={variantStyles({
48-
variant,
49-
className,
50-
})}
51-
{...(props as any)}
52-
/>
53-
) : (
54-
<MotionButtonBase
55-
className={variantStyles({
56-
variant,
57-
className,
58-
})}
59-
{...(props as any)}
60-
/>
47+
const Wrapper = isLoading ? LoadingButtonWrapper : Fragment
48+
return (
49+
<Wrapper>
50+
{href ? (
51+
<Link
52+
href={href}
53+
className={variantStyles({
54+
variant,
55+
className,
56+
})}
57+
{...(props as any)}
58+
/>
59+
) : (
60+
<MotionButtonBase
61+
className={variantStyles({
62+
variant,
63+
className,
64+
})}
65+
{...(props as any)}
66+
/>
67+
)}
68+
</Wrapper>
6169
)
6270
}
6371

64-
export const LoadingButtonWrapper: FC<
65-
{ isLoading: boolean } & PropsWithChildren
66-
> = ({ isLoading, children }) => {
72+
const LoadingButtonWrapper: FC<PropsWithChildren> = ({ children }) => {
6773
return (
6874
<div className="relative">
6975
{children}
70-
{isLoading && (
71-
<div className="absolute inset-0 z-[1] flex items-center justify-center">
72-
<div className="loading loading-spinner h-5 w-5" />
73-
</div>
74-
)}
76+
77+
<div className="absolute inset-0 z-[1] flex items-center justify-center">
78+
<div className="loading loading-spinner h-5 w-5" />
79+
</div>
7580
</div>
7681
)
7782
}

src/components/ui/editor/Milkdown/plugins/__internal/SharedModalAction.tsx

+25-25
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { NodeViewContext } from '@prosemirror-adapter/react'
33

44
import { schemaCtx } from '@milkdown/core'
55

6-
import { LoadingButtonWrapper, StyledButton } from '~/components/ui/button'
6+
import { StyledButton } from '~/components/ui/button'
77
import { useCurrentModal } from '~/components/ui/modal'
88

99
import { useEditorCtx } from '../../ctx'
@@ -33,35 +33,35 @@ export const SharedModalAction: Component<{
3333
<StyledButton variant="secondary" onClick={deleteNode}>
3434
删除
3535
</StyledButton>
36-
<LoadingButtonWrapper isLoading={waiting}>
37-
<StyledButton
38-
onClick={async () => {
39-
if (save) {
40-
setWaiting(true)
41-
await save(getValue()!)
42-
setWaiting(false)
4336

44-
dismiss()
45-
return
46-
}
47-
// set first firstChild text
48-
const pos = getPos()
49-
if (typeof pos === 'undefined') return
50-
const tr = view.state.tr
37+
<StyledButton
38+
isLoading={waiting}
39+
onClick={async () => {
40+
if (save) {
41+
setWaiting(true)
42+
await save(getValue()!)
43+
setWaiting(false)
5144

52-
const nextValue = getValue()!
45+
dismiss()
46+
return
47+
}
48+
// set first firstChild text
49+
const pos = getPos()
50+
if (typeof pos === 'undefined') return
51+
const tr = view.state.tr
5352

54-
const nextNode = ctx!.get(schemaCtx).text(nextValue)
53+
const nextValue = getValue()!
5554

56-
tr.replaceWith(pos + 1, pos + node.nodeSize, nextNode)
57-
view.dispatch(tr)
55+
const nextNode = ctx!.get(schemaCtx).text(nextValue)
5856

59-
dismiss()
60-
}}
61-
>
62-
保存
63-
</StyledButton>
64-
</LoadingButtonWrapper>
57+
tr.replaceWith(pos + 1, pos + node.nodeSize, nextNode)
58+
view.dispatch(tr)
59+
60+
dismiss()
61+
}}
62+
>
63+
保存
64+
</StyledButton>
6565
</div>
6666
)
6767
}

0 commit comments

Comments
 (0)