Skip to content

Commit

Permalink
Remove unnecessary abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
shoonia committed Apr 29, 2024
1 parent 5e811d0 commit 101a3ef
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 36 deletions.
11 changes: 5 additions & 6 deletions src/components/CopyLinkButton/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useState } from 'preact/hooks';

import { useStore } from '../../store';
import { getState } from '../../store';
import { createValidatorLink } from '../../util/validatorValue';
import { IconCopyLink } from '../Icons/IconCopyLink';
import { BlankButton } from '../Button';
Expand All @@ -10,23 +10,22 @@ interface Props {
}

export const CopyLinkButton: FC<Props> = ({ className }) => {
const store = useStore();
const [isCopied, setState] = useState(false);
const [isCopied, setCopy] = useState(false);

const label = isCopied
? 'Copied!'
: 'Copy link to validation results';

const onClick: EventListener = async () => {
const { validatorValue } = store.get();
const { validatorValue } = getState();
const link = createValidatorLink(validatorValue);

if (link) {
history.pushState(null, '', link);

await navigator.clipboard.writeText(link);
setState(true);
setTimeout(setState, 2_000, false);
setCopy(true);
setTimeout(setCopy, 2_000, false);
}
};

Expand Down
3 changes: 1 addition & 2 deletions src/components/Jobs/CronTrue.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import s from './CronTrue.css';
import { parseCron } from '../../util/parseCron';
import { classNames } from '../../util/component';
import { useFormScope } from '../../hooks/formScope';
import { useDispatch } from '../../store';
import { dispatch } from '../../store';

export const CronTrue: FC = () => {
const dispatch = useDispatch();
const { id, cronExpression } = useFormScope();
const [isError, message] = parseCron(cronExpression);

Expand Down
4 changes: 1 addition & 3 deletions src/components/Jobs/Item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ import { ItemMenu } from './ItemMenu';
import { FunctionInfo } from './FunctionInfo';
import { ExecutionConfig } from './ExecutionConfig';
import { classNames, preventDefault } from '../../util/component';
import { useDispatch } from '../../store';
import { dispatch } from '../../store';

interface Props {
id: string;
isNew?: boolean;
}

export const Item: FC<Props> = ({ id, isNew }) => {
const dispatch = useDispatch();

useEffect(() => {
if (isNew) {
const t = setTimeout(() => {
Expand Down
4 changes: 1 addition & 3 deletions src/components/Parser/EditButton.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useDispatch } from '../../store';
import { dispatch } from '../../store';
import { Button } from '../Button';
import { IconEdit } from '../Icons/IconEdit';
import { type IConfig, createItems } from '../../util/items';
Expand All @@ -9,8 +9,6 @@ interface Props {
}

export const EditButton: FC<Props> = ({ config }) => {
const dispatch = useDispatch();

const onClick: EventListener = () => {
dispatch('items/replace', createItems(config));
location.hash = ROUTER.BUILDER;
Expand Down
39 changes: 19 additions & 20 deletions src/components/UploadModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { JSX } from 'preact';
import { useRef } from 'preact/hooks';

import s from './styles.css';
import { useDispatch } from '../../store';
import { dispatch } from '../../store';
import { preventDefault } from '../../util/component';
import { parseJSONC } from '../Parser/parseJSONC';
import { isValidConfig } from '../Parser/isValidConfig';
Expand All @@ -15,34 +15,33 @@ import { UploadFile } from '../UploadFile';
import { IconCancel } from '../Icons/IconCancel';
import { IconConfirm } from '../Icons/IconConfirm';

const close = (): void => {
const close = () => {
location.hash = ROUTER.BUILDER;
};

export const UploadModal: FC = () => {
const ref = useRef<string>('');
const dispatch = useDispatch();

const onLoad = (val: string): void => {
if (val.trim() === '') {
return close();
}
const onLoad = (val: string) => {
if (val.trim() === '') {
return close();
}

const [parsingError, config] = parseJSONC(val);
const [parsingError, config] = parseJSONC(val);

if (!parsingError) {
const [validationError] = isValidConfig(config);
if (!parsingError) {
const [validationError] = isValidConfig(config);

if (!validationError) {
dispatch('items/replace', createItems(config as IConfig));
if (!validationError) {
dispatch('items/replace', createItems(config as IConfig));

return close();
}
return close();
}
}

dispatch('validator/input', val);
location.hash = ROUTER.VALIDATOR;
};
dispatch('validator/input', val);
location.hash = ROUTER.VALIDATOR;
};

export const UploadModal: FC = () => {
const ref = useRef<string>('');

const onInput: JSX.InputEventHandler<HTMLTextAreaElement> = (event) => {
ref.current = event.currentTarget.value;
Expand Down
4 changes: 2 additions & 2 deletions src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ export const store = createStoreon<IState, IEvents>([

export const useStoreon = _useStoreon<IState, IEvents>;

export const useStore = () => store;
export const useDispatch = () => store.dispatch;
export const getState = store.get;
export const dispatch = store.dispatch;

0 comments on commit 101a3ef

Please sign in to comment.