|
1
|
import { CORE_LAYOUT_IDS } from "@medusajs/admin-shared" |
|
2
|
import { Spinner } from "@medusajs/icons" |
|
3
|
import { HttpTypes } from "@medusajs/types" |
|
4
|
import { |
|
5
|
Badge, |
|
6
|
Button, |
|
7
|
Container, |
|
8
|
Heading, |
|
9
|
StatusBadge, |
|
10
|
Text, |
|
11
|
Tooltip, |
|
12
|
toast, |
|
13
|
usePrompt, |
|
14
|
} from "@medusajs/ui" |
|
15
|
import { useEffect, useState } from "react" |
|
16
|
import { useTranslation } from "react-i18next" |
|
17
|
import { useNavigate } from "react-router-dom" |
|
18
|
|
|
19
|
import { LayoutComposer } from "../../../components/layout-composer" |
|
20
|
import { |
|
21
|
useDeleteSearchIndex, |
|
22
|
useSearchIndexes, |
|
23
|
} from "../../../hooks/api/search-indexes" |
|
24
|
import { SearchIndexReindexModal } from "./components/search-index-reindex-modal" |
|
25
|
|
|
26
|
const statusColor = ( |
|
27
|
status: HttpTypes.AdminSearchIndexStatus |
|
28
|
): "green" | "orange" | "grey" | "red" => { |
|
29
|
switch (status) { |
|
30
|
case "ready": |
|
31
|
return "green" |
|
32
|
case "building": |
|
33
|
return "orange" |
|
34
|
case "error": |
|
35
|
return "red" |
|
36
|
default: |
|
37
|
return "grey" |
|
38
|
} |
|
39
|
} |
|
40
|
|
|
41
|
const fieldTooltip = (field: HttpTypes.AdminSearchIndexField) => { |
|
42
|
const capabilities = [ |
|
43
|
field.type, |
|
44
|
field.searchable ? "searchable" : null, |
|
45
|
field.filterable ? "filterable" : null, |
|
46
|
field.sortable ? "sortable" : null, |
|
47
|
field.facetable ? "facetable" : null, |
|
48
|
].filter(Boolean) |
|
49
|
|
|
50
|
return capabilities.join(" · ") |
|
51
|
} |
|
52
|
|
|
53
|
const SearchIndexCard = ({ index }: { index: HttpTypes.AdminSearchIndex }) => { |
|
54
|
const { t } = useTranslation() |
|
55
|
const prompt = usePrompt() |
|
56
|
const [isReindexModalOpen, setIsReindexModalOpen] = useState(false) |
|
57
|
const { mutateAsync: deleteIndex, isPending: isDeleting } = |
|
58
|
useDeleteSearchIndex() |
|
59
|
|
|
60
|
const handleDelete = async () => { |
|
61
|
const confirmed = await prompt({ |
|
62
|
title: t("general.areYouSure"), |
|
63
|
description: t("searchIndexes.deleteWarning", { name: index.name }), |
|
64
|
confirmText: t("actions.delete"), |
|
65
|
cancelText: t("actions.cancel"), |
|
66
|
}) |
|
67
|
|
|
68
|
if (!confirmed) { |
|
69
|
return |
|
70
|
} |
|
71
|
|
|
72
|
await deleteIndex(index.name, { |
|
73
|
onSuccess: () => { |
|
74
|
toast.success(t("searchIndexes.deleteSuccess", { name: index.name })) |
|
75
|
}, |
|
76
|
onError: (error) => { |
|
77
|
toast.error(error.message) |
|
78
|
}, |
|
79
|
}) |
|
80
|
} |
|
81
|
|
|
82
|
return ( |
|
83
|
<Container className="divide-y p-0"> |
|
84
|
<div className="flex items-center justify-between px-6 py-4"> |
|
85
|
<div> |
|
86
|
<Heading level="h2">{index.name}</Heading> |
|
87
|
<Text className="text-ui-fg-subtle" size="small"> |
|
88
|
{t("searchIndexes.providerLabel")}: {index.provider} |
|
89
|
</Text> |
|
90
|
</div> |
|
91
|
<div className="flex items-center gap-x-2"> |
|
92
|
<StatusBadge color={statusColor(index.status)}> |
|
93
|
{t(`searchIndexes.status.${index.status}`)} |
|
94
|
</StatusBadge> |
|
95
|
<Button |
|
96
|
size="small" |
|
97
|
variant="secondary" |
|
98
|
onClick={() => setIsReindexModalOpen(true)} |
|
99
|
disabled={index.status === "building"} |
|
100
|
> |
|
101
|
{t("searchIndexes.reindex")} |
|
102
|
</Button> |
|
103
|
<Button |
|
104
|
size="small" |
|
105
|
variant="danger" |
|
106
|
onClick={handleDelete} |
|
107
|
isLoading={isDeleting} |
|
108
|
disabled={index.status === "building"} |
|
109
|
> |
|
110
|
{t("searchIndexes.delete")} |
|
111
|
</Button> |
|
112
|
{isReindexModalOpen && ( |
|
113
|
<SearchIndexReindexModal |
|
114
|
index={index} |
|
115
|
onClose={() => setIsReindexModalOpen(false)} |
|
116
|
/> |
|
117
|
)} |
|
118
|
</div> |
|
119
|
</div> |
|
120
|
<div className="flex flex-col gap-y-2 px-6 py-4"> |
|
121
|
<Text size="small" leading="compact" weight="plus"> |
|
122
|
{t("searchIndexes.fieldsLabel")} |
|
123
|
</Text> |
|
124
|
{index.fields.length ? ( |
|
125
|
<div className="flex flex-wrap gap-2"> |
|
126
|
{index.fields.map((field) => ( |
|
127
|
<Tooltip key={field.name} content={fieldTooltip(field)}> |
|
128
|
<span> |
|
129
|
<Badge size="2xsmall">{field.name}</Badge> |
|
130
|
</span> |
|
131
|
</Tooltip> |
|
132
|
))} |
|
133
|
</div> |
|
134
|
) : ( |
|
135
|
<Text size="small" leading="compact" className="text-ui-fg-subtle"> |
|
136
|
{t("searchIndexes.noFields")} |
|
137
|
</Text> |
|
138
|
)} |
|
139
|
</div> |
|
140
|
</Container> |
|
141
|
) |
|
142
|
} |
|
143
|
|
|
144
|
export const SearchIndexList = () => { |
|
145
|
const { t } = useTranslation() |
|
146
|
const navigate = useNavigate() |
|
147
|
const { search_indexes, enabled, isLoading, isError, error } = |
|
148
|
useSearchIndexes() |
|
149
|
|
|
150
|
useEffect(() => { |
|
151
|
if (!isLoading && enabled === false) { |
|
152
|
navigate(-1) |
|
153
|
} |
|
154
|
}, [enabled, isLoading, navigate]) |
|
155
|
|
|
156
|
if (isError) { |
|
157
|
throw error |
|
158
|
} |
|
159
|
|
|
160
|
if (isLoading || enabled === false) { |
|
161
|
return ( |
|
162
|
<div className="flex items-center justify-center p-8"> |
|
163
|
<Spinner className="text-ui-fg-interactive animate-spin" /> |
|
164
|
</div> |
|
165
|
) |
|
166
|
} |
|
167
|
|
|
168
|
return ( |
|
169
|
<LayoutComposer |
|
170
|
widgetsZonePrefix="search.list" |
|
171
|
preferredLayoutId={CORE_LAYOUT_IDS.SINGLE_COLUMN} |
|
172
|
hasOutlet={false} |
|
173
|
sections={{ |
|
174
|
main: ( |
|
175
|
<LayoutComposer.Entry id="SearchIndexList"> |
|
176
|
<div className="flex flex-col gap-y-3"> |
|
177
|
<Container className="p-0"> |
|
178
|
<div className="px-6 py-4"> |
|
179
|
<Heading>{t("searchIndexes.domain")}</Heading> |
|
180
|
<Text className="text-ui-fg-subtle" size="small"> |
|
181
|
{t("searchIndexes.subtitle")} |
|
182
|
</Text> |
|
183
|
</div> |
|
184
|
</Container> |
|
185
|
{search_indexes?.length ? ( |
|
186
|
search_indexes.map((index) => ( |
|
187
|
<SearchIndexCard key={index.name} index={index} /> |
|
188
|
)) |
|
189
|
) : ( |
|
190
|
<Container className="p-0"> |
|
191
|
<div className="px-6 py-4"> |
|
192
|
<Text |
|
193
|
size="small" |
|
194
|
leading="compact" |
|
195
|
className="text-ui-fg-subtle" |
|
196
|
> |
|
197
|
{t("searchIndexes.list.noRecordsMessage")} |
|
198
|
</Text> |
|
199
|
</div> |
|
200
|
</Container> |
|
201
|
)} |
|
202
|
</div> |
|
203
|
</LayoutComposer.Entry> |
|
204
|
), |
|
205
|
}} |
|
206
|
/> |
|
207
|
) |
|
208
|
} |