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

cartesi / rollups-explorer / 10488786803

21 Aug 2024 11:13AM UTC coverage: 92.228% (-1.5%) from 93.766%
10488786803

Pull #232

github

nevendyulgerov
feat(apps/web): Refactoring
Pull Request #232: #229 Add import and export for specifications

1155 of 1372 branches covered (84.18%)

Branch coverage included in aggregate %.

227 of 478 new or added lines in 8 files covered. (47.49%)

50 existing lines in 10 files now uncovered.

12599 of 13541 relevant lines covered (93.04%)

45.5 hits per line

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

47.55
/apps/web/src/components/specification/hooks/useSpecificationsTransfer.tsx
1
"use client";
1✔
2

1✔
3
import { ReactNode, useCallback, useMemo } from "react";
1✔
4
import { notifications } from "@mantine/notifications";
1✔
5
import { useSpecification } from "./useSpecification";
1✔
6
import {
1✔
7
    Specification,
1✔
8
    SpecificationTransfer as SpecificationTransferModel,
1✔
9
    ValidationType,
1✔
10
    SPECIFICATION_TRANSFER_NAME,
1✔
11
} from "../types";
1✔
12
import { validateSpecification, VALIDATOR_VERSION } from "../transfer";
1✔
13

1✔
14
export const useSpecificationsTransfer = () => {
1✔
15
    const { listSpecifications, addSpecification } = useSpecification();
31✔
16
    const specifications = listSpecifications();
31✔
17
    const version = Number(VALIDATOR_VERSION);
31✔
18
    const specificationExport: SpecificationTransferModel = useMemo(
31✔
19
        () => ({
31✔
20
            name: SPECIFICATION_TRANSFER_NAME,
14✔
21
            version,
14✔
22
            timestamp: new Date().getTime(),
14✔
23
            specifications: specifications ?? [],
14✔
24
        }),
14✔
25
        [specifications, version],
31✔
26
    );
31✔
27

31✔
28
    const exportLink = useMemo(
31✔
29
        () =>
31✔
30
            `data:text/json;charset=utf-8,${encodeURIComponent(
14✔
31
                JSON.stringify(specificationExport, null, 4),
14✔
32
            )}`,
14✔
33
        [specificationExport],
31✔
34
    );
31✔
35

31✔
36
    const displayAlert = useCallback(
31✔
37
        (message: ReactNode, type: ValidationType = "error") => {
31✔
NEW
38
            const isSuccess = type === "success";
×
NEW
39
            notifications.show({
×
NEW
40
                message,
×
NEW
41
                color: isSuccess ? "green" : "red",
×
NEW
42
                withBorder: true,
×
NEW
43
                autoClose: isSuccess,
×
NEW
44
            });
×
NEW
45
        },
×
46
        [],
31✔
47
    );
31✔
48

31✔
49
    const onValidateSuccessfully = useCallback(
31✔
50
        (specificationImport: SpecificationTransferModel) => {
31✔
NEW
51
            Promise.all(
×
NEW
52
                specificationImport.specifications.map(
×
NEW
53
                    (specification: Specification) =>
×
NEW
54
                        new Promise((resolve, reject) => {
×
NEW
55
                            setTimeout(() =>
×
NEW
56
                                addSpecification(specification, {
×
NEW
57
                                    onSuccess: () => resolve(undefined),
×
NEW
58
                                    onFailure: (err) => reject(err),
×
NEW
59
                                }),
×
NEW
60
                            );
×
NEW
61
                        }),
×
NEW
62
                ),
×
NEW
63
            )
×
NEW
64
                .then(() =>
×
NEW
65
                    displayAlert(
×
NEW
66
                        "Specifications were imported successfully.",
×
NEW
67
                        "success",
×
NEW
68
                    ),
×
NEW
69
                )
×
NEW
70
                .catch((err) =>
×
NEW
71
                    displayAlert(
×
NEW
72
                        `Unable to import specifications. Error is: ${
×
NEW
73
                            err?.message ?? err
×
NEW
74
                        }`,
×
NEW
75
                    ),
×
NEW
76
                );
×
NEW
77
        },
×
78
        [addSpecification, displayAlert],
31✔
79
    );
31✔
80

31✔
81
    const readFileContent = useCallback(
31✔
82
        (file: File) => {
31✔
NEW
83
            const fileReader = new FileReader();
×
NEW
84

×
NEW
85
            fileReader.onload = () => {
×
NEW
86
                try {
×
NEW
87
                    const specificationImport = JSON.parse(
×
NEW
88
                        fileReader.result as string,
×
NEW
89
                    );
×
NEW
90

×
NEW
91
                    validateSpecification(specificationImport)
×
NEW
92
                        .then(() => onValidateSuccessfully(specificationImport))
×
NEW
93
                        .catch((errors: string[]) => {
×
NEW
94
                            displayAlert(
×
NEW
95
                                <>
×
NEW
96
                                    <div>
×
NEW
97
                                        The imported file is not valid. Please
×
NEW
98
                                        check the following errors:
×
NEW
99
                                    </div>
×
NEW
100
                                    <div>
×
NEW
101
                                        {errors.map((error) => (
×
NEW
102
                                            <div key={error}>{error}</div>
×
NEW
103
                                        ))}
×
NEW
104
                                    </div>
×
NEW
105
                                </>,
×
NEW
106
                            );
×
NEW
107
                        });
×
NEW
108
                } catch (err) {
×
NEW
109
                    displayAlert("Unable to parse specification import.");
×
NEW
110
                }
×
NEW
111
            };
×
NEW
112
            fileReader.onerror = () =>
×
NEW
113
                displayAlert(
×
NEW
114
                    "Unable to import file. File content may not be readable.",
×
NEW
115
                );
×
NEW
116

×
NEW
117
            fileReader.readAsText(file);
×
NEW
118
        },
×
119
        [displayAlert, onValidateSuccessfully],
31✔
120
    );
31✔
121

31✔
122
    const onChangeFile = useCallback(
31✔
123
        (file: File | null) => {
31✔
NEW
124
            if (file) {
×
NEW
125
                readFileContent(file);
×
NEW
126
            }
×
NEW
127
        },
×
128
        [readFileContent],
31✔
129
    );
31✔
130

31✔
131
    return useMemo(
31✔
132
        () => ({
31✔
133
            exportLink,
31✔
134
            onChangeFile,
31✔
135
        }),
31✔
136
        [exportLink, onChangeFile],
31✔
137
    );
31✔
138
};
31✔
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

© 2025 Coveralls, Inc