Skip to content

Commit

Permalink
fix: index settings parse chaos after type.
Browse files Browse the repository at this point in the history
use react-json-view instead of mantine json input in index setting
editor.
replaced some mantine component with sira.
  • Loading branch information
riccox committed Mar 4, 2023
1 parent 76266dc commit c130744
Showing 1 changed file with 49 additions and 53 deletions.
102 changes: 49 additions & 53 deletions src/pages/index/setting/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ import './index.css';
import { useCallback, useMemo, useState } from 'react';
import { useMeiliClient } from '@/src/hooks/useMeiliClient';
import { useNavigate, useOutletContext, useSearchParams } from 'react-router-dom';
import { ActionIcon, Button, JsonInput, Modal, Paper, Text, TextInput, Tooltip } from '@mantine/core';
import { ActionIcon, Button, Modal, Text, TextInput, Tooltip } from '@mantine/core';
import { useAppStore } from '@/src/store';
import { useMutation, useQuery } from 'react-query';
import { hiddenRequestLoader, showRequestLoader } from '@/src/utils/loader';
import { getTimeText, showTaskSubmitNotification, stringifyJsonPretty } from '@/src/utils/text';
import { getTimeText, showTaskSubmitNotification } from '@/src/utils/text';
import { IndexObject, IndexOptions, Settings } from 'meilisearch';
import { IconPencilMinus } from '@tabler/icons-react';
import { matches, useForm } from '@mantine/form';
import { openConfirmModal } from '@mantine/modals';
import ReactJson, { InteractionProps } from 'react-json-view';

function SettingsPage() {
const outletContext = useOutletContext<{ refreshIndexes: () => void }>();
Expand Down Expand Up @@ -57,9 +58,11 @@ function SettingsPage() {
},
}
);
const [indexSettingDisplayData, setIndexSettingDisplayData] = useState<Settings>();
const [indexSettingInputData, setIndexSettingInputData] = useState<string>(
stringifyJsonPretty(indexSettingDisplayData)
const [indexSettingDisplayData, setIndexSettingDisplayData] = useState<Settings>({});
const [indexSettingInputData, setIndexSettingInputData] = useState<Settings>(indexSettingDisplayData);
const onSettingJsonEditorUpdate = useCallback(
(e: InteractionProps) => setIndexSettingInputData(e.updated_src as Settings),
[setIndexSettingInputData]
);

const queryRawInfo = useQuery(
Expand All @@ -85,8 +88,8 @@ function SettingsPage() {

const resetSettings = useCallback(() => {
setIsSettingsEditing(false);
setIndexSettingDisplayData(querySettings.data);
!isSettingsEditing && setIndexSettingInputData(stringifyJsonPretty(querySettings.data));
setIndexSettingDisplayData(querySettings.data || {});
!isSettingsEditing && setIndexSettingInputData(querySettings.data || {});
}, [isSettingsEditing, querySettings.data]);

const resetRawInfo = useCallback(() => {
Expand Down Expand Up @@ -130,7 +133,7 @@ function SettingsPage() {

const onSaveSettings = useCallback(() => {
setIsSettingsEditing(false);
indexSettingInputData && settingsMutation.mutate(JSON.parse(indexSettingInputData));
indexSettingInputData && settingsMutation.mutate(indexSettingInputData);
}, [indexSettingInputData, settingsMutation]);

const editRawInfoForm = useForm({
Expand Down Expand Up @@ -234,8 +237,8 @@ function SettingsPage() {
<div className={`font-extrabold text-3xl`}>🛠️ Settings</div>
</div>
<div className={`flex-1 flex flex-col gap-2 px-4 py-2 overflow-scroll`}>
<p className={`text-xl font-bold font-sans`}>Index Info</p>
<Paper radius="md" p="lg" withBorder>
<div className="has-border bg-bw-50 py-2 px-3 rounded-lg">
<p className={`text-xl font-bold font-sans`}>Index Info</p>
<div className={`index-properties grid grid-cols-6 gap-2`}>
<p className={`cell`}>Index UID</p>
<p className={`cell`}>{indexRawInfoDisplayData?.uid}</p>
Expand All @@ -251,64 +254,56 @@ function SettingsPage() {
<p className={`cell`}>Updated At</p>
<p className={`cell`}>{getTimeText(indexRawInfoDisplayData?.updatedAt)}</p>
</div>
</Paper>
<JsonInput
className={`h-fit`}
labelProps={{
style: {
width: '100%',
},
}}
label={
<div className={`flex justify-end items-center gap-x-2 w-full py-1`}>
<p className={`mr-auto text-xl font-bold font-sans`}>Index Settings</p>
<Button
size={'xs'}
hidden={isSettingsEditing}
variant="light"
color={'brand'}
</div>

<div className="has-border bg-bw-50 py-2 px-3 rounded-lg font-sans">
<div className={`flex justify-end items-center gap-x-2 w-full py-1`}>
<p className={`mr-auto text-xl font-bold font-sans`}>Index Settings</p>
{!isSettingsEditing && (
<button
className={'btn outline xs primary'}
onClick={() => {
onClickEditSettings();
}}
>
Edit
</Button>
<Button
size={'xs'}
hidden={!isSettingsEditing}
variant="light"
color={'brand'}
</button>
)}
{isSettingsEditing && (
<button
className={'btn outline xs primary'}
onClick={() => {
onSaveSettings();
}}
>
Save
</Button>
<Button
size={'xs'}
hidden={!isSettingsEditing}
variant="light"
color={'gray'}
</button>
)}
{isSettingsEditing && (
<button
className={'btn outline xs bw'}
onClick={() => {
resetSettings();
}}
>
Cancel
</Button>
</div>
}
radius="md"
size="md"
validationError="Invalid setting object"
formatOnBlur
autosize
value={isSettingsEditing ? indexSettingInputData : stringifyJsonPretty(indexSettingDisplayData)}
onChange={(e) => setIndexSettingInputData(e)}
disabled={!isSettingsEditing}
/>
<Paper radius="md" p="lg" withBorder className={`!bg-opacity-50 !bg-danger-100`}>
</button>
)}
</div>
<ReactJson
src={isSettingsEditing ? indexSettingInputData : indexSettingDisplayData}
onAdd={isSettingsEditing && onSettingJsonEditorUpdate}
onEdit={isSettingsEditing && onSettingJsonEditorUpdate}
onDelete={isSettingsEditing && onSettingJsonEditorUpdate}
name={false}
displayDataTypes={false}
displayObjectSize={false}
enableClipboard={false}
/>
</div>
<div className={`bg-danger-300 has-border py-2 px-3 rounded-lg`}>
<p className={`text-danger-900 text-xl font-bold font-sans py-1`}>Danger Zone</p>
<div className={`flex flex-col items-start gap-4`}>
<p className={`text-danger-700 text-xl font-bold font-sans`}>Danger Zone</p>
<div className={`flex items-center gap-x-2`}>
<Button color={'red'} onClick={onClickDeleteAllDocuments}>
Delete All Documents
Expand All @@ -318,7 +313,7 @@ function SettingsPage() {
</Button>
</div>
</div>
</Paper>
</div>
</div>
<Modal
opened={isRawInfoEditing}
Expand Down Expand Up @@ -388,6 +383,7 @@ function SettingsPage() {
onClickEditPrimaryKey,
onClickEditSettings,
onSaveSettings,
onSettingJsonEditorUpdate,
onSubmitEditRawInfoUpdate,
resetSettings,
]
Expand Down

1 comment on commit c130744

@vercel
Copy link

@vercel vercel bot commented on c130744 Mar 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.