• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

waldiez / react / 12966103330

25 Jan 2025 03:13PM UTC coverage: 90.002%. Remained the same
12966103330

push

github

lazToum
update package.json

2784 of 3068 branches covered (90.74%)

Branch coverage included in aggregate %.

15634 of 17396 relevant lines covered (89.87%)

249.18 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

55.32
/src/waldiez/containers/flow/modals/importFlowModal/steps/load/hooks/useLoadFlowStep.ts
1
/**
2
 * SPDX-License-Identifier: Apache-2.0
3
 * Copyright 2024 - 2025 Waldiez & contributors
4
 */
5
import { ImportFlowState } from "@waldiez/containers/flow/modals/importFlowModal/types";
6
import { flowMapper } from "@waldiez/models/mappers";
1✔
7
import { ImportedFlow, ThingsToImport } from "@waldiez/types";
8
import { showSnackbar } from "@waldiez/utils";
1✔
9

10
export const useLoadFlowStep = (props: {
1✔
11
    flowId: string;
12
    state: ImportFlowState;
13
    initialState: ImportFlowState;
14
    onStateChange: (newState: Partial<ImportFlowState>) => void;
15
}) => {
43✔
16
    const { flowId, state, initialState, onStateChange } = props;
43✔
17
    const setLoadedFlowData = (loadedFlowData: ImportedFlow | null) => {
43✔
18
        onStateChange({
10✔
19
            loadedFlowData,
10✔
20
        });
10✔
21
    };
10✔
22
    const setRemoteUrl = (remoteUrl: string) => {
43✔
23
        onStateChange({
×
24
            remoteUrl,
×
25
        });
×
26
    };
×
27
    const onUpload = (files: File[]) => {
43✔
28
        if (files.length) {
8✔
29
            const file = files[0];
8✔
30
            try {
8✔
31
                const reader = new FileReader();
8✔
32
                reader.onload = () => {
8✔
33
                    const data = reader.result;
8✔
34
                    if (typeof data === "string") {
8✔
35
                        onFlowData(data);
8✔
36
                    }
8✔
37
                };
8✔
38
                reader.readAsText(file);
8✔
39
            } catch (e) {
8!
40
                console.error(e);
×
41
                setLoadedFlowData(null);
×
42
            }
×
43
        }
8✔
44
    };
8✔
45
    const onFlowData = (data: any) => {
43✔
46
        const flow = flowMapper.toReactFlow(flowMapper.importFlow(data, flowId));
8✔
47
        const isEmpty = flow.nodes.length === 0 && flow.edges.length === 0;
8!
48
        if (isEmpty) {
8!
49
            setLoadedFlowData(null);
×
50
            showSnackbar(flowId, "Failed to load flow", "error");
×
51
            return;
×
52
        }
×
53
        setLoadedFlowData(flow);
8✔
54
    };
8✔
55
    const onRemoteUrlChange = (event: React.ChangeEvent<HTMLInputElement>) => {
43✔
56
        setRemoteUrl(event.target.value);
×
57
    };
×
58
    const onRemoteUrlSubmit = () => {
43✔
59
        const timeout = 30000;
×
60
        const controller = new AbortController();
×
61
        const signal = controller.signal;
×
62
        setLoadedFlowData(null);
×
63
        setTimeout(() => {
×
64
            controller.abort();
×
65
        }, timeout);
×
66
        const remoteUrl = state.remoteUrl;
×
67
        fetch(remoteUrl, { signal, mode: "cors", redirect: "follow" })
×
68
            .then(response => response.json())
×
69
            .then(data => {
×
70
                onFlowData(data);
×
71
            })
×
72
            .catch(error => {
×
73
                console.error(error);
×
74
                showSnackbar(flowId, "Failed to load flow", "error");
×
75
                onStateChange(initialState);
×
76
            });
×
77
    };
×
78
    const onSelectedPropsChange = (selectedProps: Partial<ThingsToImport>) => {
43✔
79
        onStateChange({
×
80
            selectedProps: {
×
81
                ...state.selectedProps,
×
82
                ...selectedProps,
×
83
            },
×
84
        });
×
85
    };
×
86
    const onClearLoadedFlowData = () => {
43✔
87
        setLoadedFlowData(null);
2✔
88
    };
2✔
89

90
    // not yet needed
91
    // const onSearchSubmit = () => {
92
    //   console.log(state.searchTerm);
93
    // };
94
    // const onSearchChange = (event: React.ChangeEvent<HTMLInputElement>) => {
95
    //   setSearchTerm(event.target.value);
96
    // };
97
    // const setSearchTerm = (searchTerm: string) => {
98
    //   onStateChange({
99
    //     searchTerm
100
    //   });
101
    // };
102
    return {
43✔
103
        onUpload,
43✔
104
        onRemoteUrlChange,
43✔
105
        onRemoteUrlSubmit,
43✔
106
        // onSearchChange,
107
        // onSearchSubmit,
108
        onSelectedPropsChange,
43✔
109
        onClearLoadedFlowData,
43✔
110
    };
43✔
111
};
43✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc