Skip to content

Commit

Permalink
feat(devtools): prevent to remove all rules
Browse files Browse the repository at this point in the history
  • Loading branch information
Asuka109 committed Nov 21, 2023
1 parent 5152aa0 commit 4ee0094
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { ChangeEvent } from 'react';
import React, { ChangeEvent, useEffect } from 'react';
import { nanoid } from 'nanoid';
import { Box, IconButton, TextField } from '@radix-ui/themes';
import { HiPlusSmall, HiMinusSmall } from 'react-icons/hi2';
Expand Down Expand Up @@ -41,10 +41,28 @@ export const HeaderRuleEditor: React.FC<HeaderRuleEditorProps> = ({

const createRemoveHandler = (index: number) => {
return () => {
onDeleteRule?.(index);
if (value.length > 1) {
onDeleteRule?.(index);
} else {
onChangeRule?.(0, {
id: nanoid(),
key: '',
value: '',
});
}
};
};

useEffect(() => {
if (value.length === 0) {
onCreateRule?.(0, {
id: nanoid(),
key: '',
value: '',
});
}
}, []);

return (
<Box className={styles.container} {...props}>
{value?.map((rule, i) => (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import _ from 'lodash';
import React, { useState } from 'react';
import { nanoid } from 'nanoid';
import { Box, Button, Flex, Heading, Link, Text } from '@radix-ui/themes';
import { useList } from 'react-use';
import { useSnapshot } from 'valtio';
Expand All @@ -12,11 +11,7 @@ import { HeaderRuleEditor } from '@/client/components/HeaderRule/Editor';
const Page: React.FC = () => {
const state = useSnapshot($state);
const isActive = Boolean(state.service.href);
const [rules, $rules] = useList(() =>
state.service.rules?.length
? _.slice(state.service.rules)
: [{ id: nanoid(), key: '', value: '' }],
);
const [rules, $rules] = useList(() => _.slice(state.service.rules));
const [isOutdated, setIsOutdated] = useState(false);
let statusText = isActive ? 'Active' : 'Offline';
if (isOutdated && isActive) {
Expand Down

0 comments on commit 4ee0094

Please sign in to comment.