Skip to content

Commit

Permalink
refactor: remove useToastFunctions interface - handle everything via …
Browse files Browse the repository at this point in the history
…the provider
  • Loading branch information
leon-schmid committed Oct 21, 2024
1 parent e960ab5 commit de90c35
Show file tree
Hide file tree
Showing 14 changed files with 43 additions and 45 deletions.
4 changes: 2 additions & 2 deletions web/src/components/api-keys/create-api-keys.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use client";

import { useCreateApiKey } from "@/hooks/use-create-api-key";
import { useToastFunctions } from "@/lib/toast";
import { useToast } from "@/providers/toast";
import { useApiKeysStore } from "@/stores/api-keys-store";
import { InfoCircledIcon, PlusIcon } from "@radix-ui/react-icons";
import {
Expand All @@ -25,7 +25,7 @@ export default function CreateApiKey() {
const [state, setState] = useState<State>(State.CLOSED);
const [name, setName] = useState<string | undefined>(undefined);
const [secretKey, setSecretKey] = useState<string>("");
const { errorToast } = useToastFunctions();
const { errorToast } = useToast();

const createApiKey = useCreateApiKey();
const { addApiKey } = useApiKeysStore();
Expand Down
4 changes: 2 additions & 2 deletions web/src/components/api-keys/existing-api-key.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { TApiKeyMetadata } from "@/types/api-key";
import { TrashIcon } from "@radix-ui/react-icons";
import { Flex, IconButton, Text, TextField } from "@radix-ui/themes";
import { useEffect } from "react";
import { useToastFunctions } from "@/lib/toast";
import { useToast } from "@/providers/toast";

export default function ExistingApiKey({
key,
Expand All @@ -18,7 +18,7 @@ export default function ExistingApiKey({
const { removeApiKey } = useApiKeysStore();
const deleteApiKey = useDeleteApiKey();
const handleDelete = () => deleteApiKey.mutate({ id: apiKey.id });
const { errorToast } = useToastFunctions();
const { errorToast } = useToast();

useEffect(() => {
if (deleteApiKey.isSuccess) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useToastFunctions } from "@/lib/toast";
import { useToast } from "@/providers/toast";
import { Flex, Switch, Text } from "@radix-ui/themes";
import { usePublishWorkflowApi } from "@/hooks/use-publish-workflow-api";
import { useEffect } from "react";
Expand All @@ -14,7 +14,7 @@ export default function PublishWorkflowToggleBase({
isLive,
updateIsLiveState,
}: PublishWorkflowToggleBaseProps) {
const { infoToast } = useToastFunctions();
const { infoToast } = useToast();
const publishWorkflow = usePublishWorkflowApi();

useEffect(() => {
Expand Down
4 changes: 2 additions & 2 deletions web/src/components/run-workflow/run-workflow-dialog.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useTriggerWorkflowApi } from "@/hooks/use-trigger-workflow-api";
import { useToastFunctions } from "@/lib/toast";
import { useToast } from "@/providers/toast";
import { useWorkflowStore } from "@/stores/workflow-store";
import { Cross1Icon } from "@radix-ui/react-icons";
import { Button, Dialog, Flex, Text } from "@radix-ui/themes";
Expand All @@ -14,7 +14,7 @@ export default function RunWorkflowDialog({
const { workflowName, isActive, payloadCache, setPayloadCache } =
useWorkflowStore();
const triggerWorkflow = useTriggerWorkflowApi();
const { errorToast, infoToast } = useToastFunctions();
const { errorToast, infoToast } = useToast();

useEffect(() => {
if (triggerWorkflow.isSuccess) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import { useEffect } from "react";
import { AxiosError } from "axios";
import { useEditorActionStore } from "@/stores/editor-action-store";
import { EditorWorkflowNodeType } from "@/types/react-flow";
import { useToastFunctions } from "@/lib/toast";
import { useToast } from "@/providers/toast";

const SPACE = " ";

export default function SaveWorkflowButton() {
const { getWorkflow, updateWebhookIdAndSecret } = useWorkflowStore();
const { actionsIndex } = useEditorActionStore();
const saveWorkflow = useSaveWorkflowApi();
const { errorToast, infoToast } = useToastFunctions();
const { errorToast, infoToast } = useToast();

useEffect(() => {
if (saveWorkflow.isSuccess) {
Expand Down
4 changes: 2 additions & 2 deletions web/src/components/secrets/encrypted-secret.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import { Flex, IconButton, Text, TextField } from "@radix-ui/themes";
import { useSecretsStore } from "@/stores/secrets-store";
import { useDeleteSecretApi } from "@/hooks/use-delete-secret-api";
import { useEffect } from "react";
import { useToastFunctions } from "@/lib/toast";
import { useToast } from "@/providers/toast";
import { TrashIcon } from "@radix-ui/react-icons";

export default function EncryptedSecret({ idx }: { idx: number }) {
const { errorToast } = useToastFunctions();
const { errorToast } = useToast();
const { secrets, removeSecret } = useSecretsStore();
const deleteSecret = useDeleteSecretApi();

Expand Down
4 changes: 2 additions & 2 deletions web/src/components/secrets/new-secret.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import { useSecretsStore } from "@/stores/secrets-store";
import { useSetSecretApi } from "@/hooks/use-set-secret-api";
import { ChangeEvent, useEffect } from "react";
import { produce } from "immer";
import { useToastFunctions } from "@/lib/toast";
import { useToast } from "@/providers/toast";

export default function NewSecret({ idx }: { idx: number }) {
const { errorToast } = useToastFunctions();
const { errorToast } = useToast();
const { secrets, updateSecret } = useSecretsStore();
const saveSecret = useSetSecretApi();

Expand Down
4 changes: 2 additions & 2 deletions web/src/components/utils/copy-text.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useToastFunctions } from "@/lib/toast";
import { useToast } from "@/providers/toast";
import { CopyIcon } from "@radix-ui/react-icons";
import { Button, Flex, TextField, Tooltip } from "@radix-ui/themes";

Expand All @@ -7,7 +7,7 @@ export interface CopyTextProps {
}

export default function CopyText({ text }: CopyTextProps) {
const { successToast } = useToastFunctions();
const { successToast } = useToast();
const copyToClipboard = () => {
navigator.clipboard.writeText(text);
successToast("Copied to clipboard.");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useListWorkflowRunsApi } from "@/hooks/use-list-workflow-runs-api";
import { useToastFunctions } from "@/lib/toast";
import { useToast } from "@/providers/toast";
import { Box, Flex, Spinner, ScrollArea, Text } from "@radix-ui/themes";
import { useEffect, useState } from "react";
import WorkflowRunTrace from "./workflow-run-trace";
Expand Down Expand Up @@ -50,7 +50,7 @@ export default function WorkflowRunHistory({
const { data, isPending, error } = useListWorkflowRunsApi(workflowId);
const [selectedRunIdx, setSelectedRunIdx] = useState<number | null>(null);
const [runs, setRuns] = useState<TWorkflowRunMetadata[]>([]);
const { errorToast } = useToastFunctions();
const { errorToast } = useToast();

useEffect(() => {
if (data && data.length > 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ErrorCallout from "@/components/utils/error-callout";
import { useGetWorkflowRunStepApi } from "@/hooks/use-get-workflow-run-step-api";
import { useToastFunctions } from "@/lib/toast";
import { useToast } from "@/providers/toast";
import { TJson } from "@/types/json";
import { Box, DataList, Flex, ScrollArea, Tabs, Text } from "@radix-ui/themes";
import { useEffect } from "react";
Expand All @@ -20,7 +20,7 @@ export default function WorkflowRunStep({
workflowRunId,
workflowRunStepId,
);
const { errorToast } = useToastFunctions();
const { errorToast } = useToast();

useEffect(() => {
if (error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Box, Flex, Text } from "@radix-ui/themes";
import WorkflowRunStep from "./workflow-run-step";
import { useGetWorkflowRunStepsApi } from "@/hooks/use-get-workflow-run-steps-api";
import { useEffect, useState } from "react";
import { useToastFunctions } from "@/lib/toast";
import { useToast } from "@/providers/toast";
import Row from "./row";
import ErrorCallout from "@/components/utils/error-callout";
import { TWorkflowRunStepMetadata } from "@/types/workflow-runs";
Expand All @@ -24,7 +24,7 @@ export default function WorkflowRunTrace({
);
const [selectedStepIdx, setSelectedStepIdx] = useState<number>(0);
const [steps, setSteps] = useState<TWorkflowRunStepMetadata[]>([]);
const { errorToast } = useToastFunctions();
const { errorToast } = useToast();

useEffect(() => {
if (data) {
Expand Down
4 changes: 2 additions & 2 deletions web/src/components/workflow-editor/workflow-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import WorkflowSettingsButton from "./workflow-settings-button";
import WorkflowEditorActionEditSidebar from "./workflow-editor-action-edit-sidebar";
import WorkflowRunHistory from "./run-history/workflow-run-history";
import RunWorkflowButton from "../run-workflow/run-workflow-button";
import { useToastFunctions } from "@/lib/toast";
import { useToast } from "@/providers/toast";
import { AxiosError } from "axios";
import { useRouter } from "next/navigation";

Expand All @@ -36,7 +36,7 @@ export default function WorkflowEditor({

const { isNew, setWorkflow, clearWorkflowStore } = useWorkflowStore();
const { setEditorActions } = useEditorActionStore();
const { errorToast } = useToastFunctions();
const { errorToast } = useToast();

// Loading Actions
const {
Expand Down
11 changes: 0 additions & 11 deletions web/src/lib/toast.ts

This file was deleted.

29 changes: 19 additions & 10 deletions web/src/providers/toast.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,13 @@ import { Flex } from "@radix-ui/themes";
type ToastType = "success" | "error" | "info";

interface ToastContextType {
showToast: (message: string, type: ToastType) => void;
successToast: (message: string) => void;
errorToast: (message: string) => void;
infoToast: (message: string) => void;
}

const ToastContext = createContext<ToastContextType | undefined>(undefined);

export function useToast() {
const context = useContext(ToastContext);
if (context === undefined) {
throw new Error("useToast must be used within a ToastProvider");
}
return context;
}

const ToastIcon = ({ type }: { type: ToastType }) => {
switch (type) {
case "info":
Expand All @@ -48,7 +42,14 @@ export function ToastProvider({ children }: { children: React.ReactNode }) {
};

return (
<ToastContext.Provider value={{ showToast }}>
<ToastContext.Provider
value={{
successToast: (message: string) =>
showToast(message, "success"),
errorToast: (message: string) => showToast(message, "error"),
infoToast: (message: string) => showToast(message, "info"),
}}
>
{children}
<Toast.Provider swipeDirection="right">
<Toast.Root
Expand All @@ -73,3 +74,11 @@ export function ToastProvider({ children }: { children: React.ReactNode }) {
</ToastContext.Provider>
);
}

export function useToast() {
const context = useContext(ToastContext);
if (context === undefined) {
throw new Error("useToast must be used within a ToastProvider");
}
return context;
}

0 comments on commit de90c35

Please sign in to comment.