|
| 1 | +/** |
| 2 | + * Copyright (c) 2026, RTE (https://www.rte-france.com) |
| 3 | + * |
| 4 | + * See AUTHORS.txt |
| 5 | + * |
| 6 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 7 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 9 | + * |
| 10 | + * SPDX-License-Identifier: MPL-2.0 |
| 11 | + * |
| 12 | + * This file is part of the Antares project. |
| 13 | + */ |
| 14 | + |
| 15 | +import LinearProgressWithLabel from "@/components/LinearProgressWithLabel"; |
| 16 | +import useDialog from "@/hooks/useDialog"; |
| 17 | +import { tableModeCreationSchema } from "@/services/api/tablemode/schemas"; |
| 18 | +import type { TableModeCreation } from "@/services/api/tablemode/types"; |
| 19 | +import storage from "@/services/utils/localStorage"; |
| 20 | +import { appendColon } from "@/utils/i18nUtils"; |
| 21 | +import InfoOutlinedIcon from "@mui/icons-material/InfoOutlined"; |
| 22 | +import { |
| 23 | + Backdrop, |
| 24 | + Box, |
| 25 | + Button, |
| 26 | + IconButton, |
| 27 | + List, |
| 28 | + ListItem, |
| 29 | + ListItemText, |
| 30 | + Paper, |
| 31 | + Stack, |
| 32 | + Tooltip, |
| 33 | + Typography, |
| 34 | +} from "@mui/material"; |
| 35 | +import axios, { AxiosError } from "axios"; |
| 36 | +import { useState } from "react"; |
| 37 | +import { useTranslation } from "react-i18next"; |
| 38 | +import { useMount } from "react-use"; |
| 39 | +import useCreateTableMode from "../../-hooks/useCreateTableMode"; |
| 40 | + |
| 41 | +interface MigrationState { |
| 42 | + status: "disabled" | "loading" | "success" | "error"; |
| 43 | + migrated: number; |
| 44 | + failed: Array<[TableModeCreation, AxiosError | undefined]>; |
| 45 | + total: number; |
| 46 | +} |
| 47 | + |
| 48 | +const DEFAULT_MIGRATION_STATE: MigrationState = { |
| 49 | + status: "disabled", |
| 50 | + migrated: 0, |
| 51 | + failed: [], |
| 52 | + total: 0, |
| 53 | +} as const; |
| 54 | + |
| 55 | +const LOCAL_STORAGE_KEY = "studies.model.tableMode.templates" as const; |
| 56 | + |
| 57 | +/** |
| 58 | + * Workaround to migrate table modes stored in localStorage to the new backend system (since v2.32.0). |
| 59 | + * It can be removed after a certain period of time when we are confident that most users have migrated. |
| 60 | + */ |
| 61 | + |
| 62 | +function MigrationTableModes() { |
| 63 | + const createTableMode = useCreateTableMode({ notifyError: false }); |
| 64 | + const { t } = useTranslation(); |
| 65 | + const [state, setState] = useState<MigrationState>(DEFAULT_MIGRATION_STATE); |
| 66 | + const { confirm } = useDialog(); |
| 67 | + |
| 68 | + useMount(migrateTableModes); |
| 69 | + |
| 70 | + //////////////////////////////////////////////////////////////// |
| 71 | + // Functions |
| 72 | + //////////////////////////////////////////////////////////////// |
| 73 | + |
| 74 | + async function migrateTableModes() { |
| 75 | + const oldTableModes = storage.getItem(LOCAL_STORAGE_KEY); |
| 76 | + |
| 77 | + // Invalid data or no data to migrate |
| 78 | + if (!Array.isArray(oldTableModes) || oldTableModes.length === 0) { |
| 79 | + storage.removeItem(LOCAL_STORAGE_KEY); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + setState({ |
| 84 | + ...DEFAULT_MIGRATION_STATE, |
| 85 | + status: "loading", |
| 86 | + total: oldTableModes.length, |
| 87 | + }); |
| 88 | + |
| 89 | + const failed: MigrationState["failed"] = []; |
| 90 | + |
| 91 | + for (const value of oldTableModes) { |
| 92 | + // Ignore invalid table modes |
| 93 | + if (!tableModeCreationSchema.safeParse(value).success) { |
| 94 | + continue; |
| 95 | + } |
| 96 | + |
| 97 | + try { |
| 98 | + await createTableMode.mutateAsync(value); |
| 99 | + setState((prev) => ({ ...prev, migrated: prev.migrated + 1 })); |
| 100 | + } catch (err) { |
| 101 | + failed.push([value, axios.isAxiosError(err) ? err : undefined]); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + // If all table modes have been migrated successfully, we can remove the localStorage key |
| 106 | + if (failed.length === 0) { |
| 107 | + storage.removeItem(LOCAL_STORAGE_KEY); |
| 108 | + } |
| 109 | + // If some table modes failed to migrate, we keep them in localStorage to retry later |
| 110 | + else { |
| 111 | + storage.setItem( |
| 112 | + LOCAL_STORAGE_KEY, |
| 113 | + failed.map(([tableModeCreation]) => tableModeCreation), |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + setState({ |
| 118 | + status: failed.length > 0 ? "error" : "success", |
| 119 | + migrated: oldTableModes.length - failed.length, |
| 120 | + failed, |
| 121 | + total: oldTableModes.length, |
| 122 | + }); |
| 123 | + } |
| 124 | + |
| 125 | + //////////////////////////////////////////////////////////////// |
| 126 | + // Event Handlers |
| 127 | + //////////////////////////////////////////////////////////////// |
| 128 | + |
| 129 | + const handleDelete = async () => { |
| 130 | + const isConfirmed = await confirm({ |
| 131 | + content: t("study.tableModes.migration.deleteConfirm"), |
| 132 | + alert: "error", |
| 133 | + }); |
| 134 | + |
| 135 | + if (isConfirmed) { |
| 136 | + storage.removeItem(LOCAL_STORAGE_KEY); |
| 137 | + setState(DEFAULT_MIGRATION_STATE); |
| 138 | + } |
| 139 | + }; |
| 140 | + |
| 141 | + const handleClose = async () => { |
| 142 | + if (state.status === "success") { |
| 143 | + setState(DEFAULT_MIGRATION_STATE); |
| 144 | + return; |
| 145 | + } |
| 146 | + |
| 147 | + const isConfirmed = await confirm({ |
| 148 | + content: t("study.tableModes.migration.closeConfirm"), |
| 149 | + alert: "warning", |
| 150 | + }); |
| 151 | + |
| 152 | + if (isConfirmed) { |
| 153 | + setState(DEFAULT_MIGRATION_STATE); |
| 154 | + } |
| 155 | + }; |
| 156 | + |
| 157 | + //////////////////////////////////////////////////////////////// |
| 158 | + // JSX |
| 159 | + //////////////////////////////////////////////////////////////// |
| 160 | + |
| 161 | + if (state.status === "disabled") { |
| 162 | + return null; |
| 163 | + } |
| 164 | + |
| 165 | + return ( |
| 166 | + <Backdrop open sx={{ position: "absolute" }}> |
| 167 | + <Paper |
| 168 | + sx={{ |
| 169 | + display: "flex", |
| 170 | + flexDirection: "column", |
| 171 | + gap: 1, |
| 172 | + }} |
| 173 | + > |
| 174 | + <Box sx={{ p: 2 }}> |
| 175 | + <Typography variant="h6">{t("study.tableModes.migration.title")}</Typography> |
| 176 | + {/* Loading */} |
| 177 | + {state.status === "loading" && ( |
| 178 | + <LinearProgressWithLabel value={(state.migrated / state.total) * 100} /> |
| 179 | + )} |
| 180 | + {/* Error */} |
| 181 | + {state.status === "error" && ( |
| 182 | + <> |
| 183 | + <Typography variant="body2" color="error" sx={{ mt: 1 }}> |
| 184 | + {t("study.tableModes.migration.error", { count: state.failed.length })} |
| 185 | + </Typography> |
| 186 | + <List dense sx={{ overflow: "auto" }}> |
| 187 | + {state.failed.map(([tableModeCreation, error]) => ( |
| 188 | + <ListItem |
| 189 | + key={tableModeCreation.name} |
| 190 | + secondaryAction={ |
| 191 | + <Tooltip |
| 192 | + title={`${appendColon(t("global.columns"))} ${tableModeCreation.columns.join(", ")}`} |
| 193 | + > |
| 194 | + <IconButton edge="end"> |
| 195 | + <InfoOutlinedIcon /> |
| 196 | + </IconButton> |
| 197 | + </Tooltip> |
| 198 | + } |
| 199 | + > |
| 200 | + <ListItemText primary={tableModeCreation.name} secondary={error?.message} /> |
| 201 | + </ListItem> |
| 202 | + ))} |
| 203 | + </List> |
| 204 | + </> |
| 205 | + )} |
| 206 | + {/* Success */} |
| 207 | + {state.status === "success" && ( |
| 208 | + <Typography variant="body2" color="success.main" sx={{ mt: 1 }}> |
| 209 | + {t("study.tableModes.migration.success", { count: state.migrated })} |
| 210 | + </Typography> |
| 211 | + )} |
| 212 | + </Box> |
| 213 | + <Stack justifyContent="flex-end" spacing={1} sx={{ p: 1 }}> |
| 214 | + {state.status === "error" && ( |
| 215 | + <> |
| 216 | + <Button variant="contained" color="secondary" onClick={migrateTableModes}> |
| 217 | + {t("global.retry")} |
| 218 | + </Button> |
| 219 | + <Button variant="contained" color="error" onClick={handleDelete}> |
| 220 | + {t("global.delete")} |
| 221 | + </Button> |
| 222 | + </> |
| 223 | + )} |
| 224 | + <Button disabled={state.status === "loading"} onClick={handleClose}> |
| 225 | + {t("global.close")} |
| 226 | + </Button> |
| 227 | + </Stack> |
| 228 | + </Paper> |
| 229 | + </Backdrop> |
| 230 | + ); |
| 231 | +} |
| 232 | + |
| 233 | +export default MigrationTableModes; |
0 commit comments