#
vincentlu
昨天 f7543a1cc6c89ea03c0d6a0591526baf10d7934b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import React, { useState, useMemo } from "react";
import {
    List,
    DatagridConfigurable,
    SearchInput,
    TopToolbar,
    SelectColumnsButton,
    FilterButton,
    useTranslate,
    useRecordContext,
    TextField,
    NumberField,
    DateField,
    TextInput,
    DateInput,
    SelectInput,
} from 'react-admin';
import { Box, Chip } from '@mui/material';
import { styled } from '@mui/material/styles';
import IntegrationRecordCreate from "./IntegrationRecordCreate";
import IntegrationRecordDetail from "./IntegrationRecordDetail";
import EmptyDataLoader from "../components/EmptyDataLoader";
import MyCreateButton from "../components/MyCreateButton";
import MyExportButton from '../components/MyExportButton';
import PageDrawer from "../components/PageDrawer";
import { PAGE_DRAWER_WIDTH, DEFAULT_PAGE_SIZE } from '@/config/setting';
import { format } from 'date-fns';
import rowSx from './rowSx';
import { buildDirectionChoices, getDirectionLabel } from './direction';
 
const StyledDatagrid = styled(DatagridConfigurable)(({ theme }) => ({
    '& .css-1vooibu-MuiSvgIcon-root': {
        height: '.9em'
    },
    '& .RaDatagrid-row': {
        cursor: 'auto'
    },
    '& .column-url': {
        maxWidth: '16em',
        overflow: 'hidden',
        textOverflow: 'ellipsis',
        whiteSpace: 'nowrap',
    },
    '& .column-request': {
        maxWidth: '18em',
        overflow: 'hidden',
        textOverflow: 'ellipsis',
        whiteSpace: 'nowrap',
    },
    '& .column-response': {
        maxWidth: '18em',
        overflow: 'hidden',
        textOverflow: 'ellipsis',
        whiteSpace: 'nowrap',
    },
    '& .column-costMs': {
        maxWidth: '9em',
    },
    '& .opt': {
        width: 200
    },
    '& .RaDatagrid-thead': {
        borderLeftColor: 'transparent',
        borderLeftWidth: 5,
        borderLeftStyle: 'solid',
    },
}));
 
const IntegrationRecordList = () => {
    const translate = useTranslate();
    const [createDialog, setCreateDialog] = useState(false);
    const [drawerVal, setDrawerVal] = useState(false);
    const directionChoices = useMemo(() => buildDirectionChoices(translate), [translate]);
    const filters = useMemo(() => [
        <SearchInput source="condition" alwaysOn />,
        <DateInput label='common.time.after' source="timeStart" alwaysOn />,
        <DateInput label='common.time.before' source="timeEnd" alwaysOn />,
        <TextInput source="uuid" label="table.field.integrationRecord.uuid" />,
        <TextInput source="namespace" label="table.field.integrationRecord.namespace" />,
        <TextInput source="url" label="table.field.integrationRecord.url" />,
        <TextInput source="appkey" label="table.field.integrationRecord.appkey" />,
        <TextInput source="caller" label="table.field.integrationRecord.caller" />,
        <SelectInput source="direction"
            label="table.field.integrationRecord.direction"
            choices={directionChoices}
            alwaysOn
        />,
        // <TextInput source="timestamp" label="table.field.integrationRecord.timestamp" />,
        <TextInput source="clientIp" label="table.field.integrationRecord.clientIp" />,
        <SelectInput source="result" label="table.field.integrationRecord.result"
            choices={[
                { id: 1, name: 'common.enums.success' },
                { id: 0, name: 'common.enums.failure' },
            ]}
            alwaysOn
        />,
        <TextInput label="common.field.memo" source="memo" />,
    ], [directionChoices]);
 
    return (
        <Box display="flex">
            <List
                sx={{
                    flexGrow: 1,
                    transition: (theme) =>
                        theme.transitions.create(['all'], {
                            duration: theme.transitions.duration.enteringScreen,
                        }),
                    marginRight: !!drawerVal ? `${PAGE_DRAWER_WIDTH}px` : 0,
                }}
                title={"menu.integrationRecord"}
                empty={<EmptyDataLoader />}
                filters={filters}
                sort={{ field: "create_time", order: "desc" }}
                actions={(
                    <TopToolbar>
                        <FilterButton />
                        <SelectColumnsButton preferenceKey='integrationRecord' />
                        <MyExportButton />
                    </TopToolbar>
                )}
                perPage={DEFAULT_PAGE_SIZE}
            >
                <StyledDatagrid
                    preferenceKey='integrationRecord'
                    bulkActionButtons={false}
                    rowClick={(id, resource, record) => {
                        setDrawerVal(!!drawerVal && drawerVal === record ? null : record);
                        return false;
                    }}
                    omit={['id', 'uuid', 'appkey', 'direction', 'timestamp', 'updateTime', 'memo']}
                    rowSx={rowSx(drawerVal || null)}
                >
                    <NumberField source="id" />
                    <TextField source="uuid" label="table.field.integrationRecord.uuid" />
                    <TextField source="namespace" label="table.field.integrationRecord.namespace" />
                    <TextField source="url" label="table.field.integrationRecord.url" />
                    <TextField source="appkey" label="table.field.integrationRecord.appkey" />
                    <TextField source="caller" label="table.field.integrationRecord.caller" />
                    <DirectionField source="direction" label="table.field.integrationRecord.direction" sortable={false} />
                    <FormattedTimestampField source="timestamp" label="table.field.integrationRecord.timestamp" />
                    <TextField source="clientIp" label="table.field.integrationRecord.clientIp" />
                    <TextField source="request" label="table.field.integrationRecord.request" sortable={false} hidden={!!drawerVal} />
                    <TextField source="response" label="table.field.integrationRecord.response" sortable={false} hidden={!!drawerVal} />
                    <ResultField source="result" label="table.field.integrationRecord.result" sortable={false} />
                    <TextField source="err" label="table.field.integrationRecord.err" />
                    <NumberField source="costMs" label="table.field.integrationRecord.costMs" sx={{ fontWeight: 'bold' }} />
 
                    <DateField source="updateTime" label="common.field.updateTime" showTime />
                    <DateField source="createTime" label="common.field.createTime" showTime />
                    {/* <BooleanField source="statusBool" label="common.field.status" sortable={false} /> */}
                    <TextField source="memo" label="common.field.memo" sortable={false} />
                </StyledDatagrid>
            </List>
            <IntegrationRecordCreate
                open={createDialog}
                setOpen={setCreateDialog}
            />
            <PageDrawer
                title='IntegrationRecord Detail'
                drawerVal={drawerVal}
                setDrawerVal={setDrawerVal}
            >
                <IntegrationRecordDetail integration={drawerVal} />
            </PageDrawer>
        </Box>
    )
}
 
const FormattedTimestampField = ({ source }) => {
    const record = useRecordContext();
    if (!record) return null;
    const val = record[source];
    if (!val) return null;
    const formattedDate = format(new Date(Number(val)), 'yyyy-MM-dd HH:mm:ss');
    return <span>{formattedDate}</span>;
};
 
const ResultField = ({ source }) => {
    const translate = useTranslate();
    const record = useRecordContext();
    const val = record?.[source];
    return (
        <>
            {val === 1 ? (
                <Chip label={translate("common.enums.success")} color="success" variant="outlined" size="small" />
            ) : (
                <Chip label={translate("common.enums.failure")} color="error" variant="outlined" size="small" />
            )}
        </>
    );
};
 
const DirectionField = ({ source }) => {
    const record = useRecordContext();
    const translate = useTranslate();
    const val = record?.[source];
    if (val === undefined || val === null) {
        return null;
    }
    return (
        <Chip
            label={getDirectionLabel(translate, val)}
            variant="outlined"
            size="small"
            color="info"
        />
    );
};
 
export default IntegrationRecordList;