-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsanity-category.tsx
More file actions
91 lines (86 loc) · 2.68 KB
/
sanity-category.tsx
File metadata and controls
91 lines (86 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { defineWidgetConfig } from "@medusajs/admin-sdk"
import { AdminProductCategory, DetailWidgetProps } from "@medusajs/types"
import { ArrowUpRightOnBox } from "@medusajs/icons"
import { Button, CodeBlock, Container, StatusBadge, toast } from "@medusajs/ui"
import { useState } from "react"
import {
useSanityDocument,
useTriggerSanityEntitySync,
} from "../hooks/sanity"
const CategoryWidget = ({ data }: DetailWidgetProps<AdminProductCategory>) => {
const { mutateAsync, isPending } = useTriggerSanityEntitySync(data.id, "category")
const { sanity_document, studio_url, isLoading } = useSanityDocument(data.id)
const [showCodeBlock, setShowCodeBlock] = useState(false)
const handleSync = async () => {
try {
await mutateAsync(undefined)
toast.success(`Sync triggered.`)
} catch (err) {
toast.error(`Couldn't trigger sync: ${(err as Record<string, unknown>).message
}`)
}
}
return (
<Container>
<div className="flex justify-between w-full items-center">
<div className="flex gap-2 items-center">
<h2>Sanity Status</h2>
<div>
{isLoading ? (
"Loading..."
) : sanity_document?.title === data.name ? (
<StatusBadge color="green">Synced</StatusBadge>
) : (
<StatusBadge color="red">Not Synced</StatusBadge>
)}
</div>
</div>
<Button
size="small"
variant="secondary"
onClick={handleSync}
disabled={isPending}
>
Sync
</Button>
</div>
<div className="mt-6">
<div className="mb-4 flex gap-4">
<Button
size="small"
variant="secondary"
onClick={() => setShowCodeBlock(!showCodeBlock)}
>
{showCodeBlock ? "Hide" : "Show"} Sanity Document
</Button>
{studio_url && (
<a href={studio_url} target="_blank" rel="noreferrer">
<Button variant="transparent">
<ArrowUpRightOnBox /> Sanity Studio
</Button>
</a>
)}
</div>
{!isLoading && showCodeBlock && (
<CodeBlock
className="dark"
snippets={[
{
language: "json",
label: "Sanity Document",
code: JSON.stringify(sanity_document, null, 2),
},
]}
>
<CodeBlock.Body />
</CodeBlock>
)}
</div>
</Container>
)
}
// The widget's configurations
export const config = defineWidgetConfig({
zone: "product_category.details.after",
})
export default CategoryWidget