From e323cdd9b8bf9cdb2ce53174eb434b83f12c2d91 Mon Sep 17 00:00:00 2001 From: Ravinou Date: Sun, 29 Dec 2024 18:35:07 +0100 Subject: [PATCH] =?UTF-8?q?refactor:=20=E2=9A=A1=20component=20Repo=20in?= =?UTF-8?q?=20TS?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Components/Repo/Repo.tsx | 45 +++++++++++++++---------- Containers/RepoList/RepoList.tsx | 2 +- domain/repository.ts | 16 +++++++++ helpers/functions/index.ts | 7 ++++ helpers/functions/timestampConverter.ts | 2 +- tsconfig.json | 4 +++ 6 files changed, 57 insertions(+), 19 deletions(-) create mode 100644 domain/repository.ts create mode 100644 helpers/functions/index.ts diff --git a/Components/Repo/Repo.tsx b/Components/Repo/Repo.tsx index 1e2312a..0d3c444 100644 --- a/Components/Repo/Repo.tsx +++ b/Components/Repo/Repo.tsx @@ -9,29 +9,40 @@ import { IconBellOff, IconLockPlus, } from '@tabler/icons-react'; -import timestampConverter from '../../helpers/functions/timestampConverter'; +import { timestampConverter } from '~/helpers/functions'; import StorageBar from '../UI/StorageBar/StorageBar'; import QuickCommands from './QuickCommands/QuickCommands'; +import { Repository } from '~/domain/repository'; -export default function Repo(props) { +type RepoProps = Repository & { + repoManageEditHandler: () => void; + wizardEnv: string; +}; + +export default function Repo(props: RepoProps) { //Load displayDetails from LocalStorage - const displayDetailsFromLS = () => { + const displayDetailsFromLS = (): boolean => { + const key = `displayDetailsRepo${props.id}`; + try { - if (localStorage.getItem('displayDetailsRepo' + props.id) === null) { - localStorage.setItem('displayDetailsRepo' + props.id, JSON.stringify(true)); - return true; - } else { - return JSON.parse(localStorage.getItem('displayDetailsRepo' + props.id)); + const storedValue = localStorage.getItem(key); + + if (storedValue === null) { + const defaultValue = true; + localStorage.setItem(key, JSON.stringify(defaultValue)); + return defaultValue; } + + const parsedValue = JSON.parse(storedValue); + if (typeof parsedValue === 'boolean') { + return parsedValue; + } + + localStorage.removeItem(key); + return true; } catch (error) { - console.log( - 'LocalStorage error, key', - 'displayDetailsRepo' + props.id, - 'will be removed. Try again.', - 'Error message on this key : ', - error - ); - localStorage.removeItem('displayDetailsRepo' + props.id); + localStorage.removeItem(key); + return true; } }; @@ -39,7 +50,7 @@ export default function Repo(props) { const [displayDetails, setDisplayDetails] = useState(displayDetailsFromLS); //BUTTON : Display or not repo details for ONE repo - const displayDetailsForOneHandler = (boolean) => { + const displayDetailsForOneHandler = (boolean: boolean) => { //Update localStorage localStorage.setItem('displayDetailsRepo' + props.id, JSON.stringify(boolean)); setDisplayDetails(boolean); diff --git a/Containers/RepoList/RepoList.tsx b/Containers/RepoList/RepoList.tsx index 276eda6..4c493e3 100644 --- a/Containers/RepoList/RepoList.tsx +++ b/Containers/RepoList/RepoList.tsx @@ -88,7 +88,7 @@ export default function RepoList() { }; //BUTTON : Display RepoManage component box for EDIT - const repoManageEditHandler = (id) => { + const repoManageEditHandler = (id: number) => { router.replace('/manage-repo/edit/' + id); }; diff --git a/domain/repository.ts b/domain/repository.ts new file mode 100644 index 0000000..eba42c0 --- /dev/null +++ b/domain/repository.ts @@ -0,0 +1,16 @@ +export type Repository = { + id: number; + alias: string; + repositoryName: string; + status: boolean; + lastSave: number; + alert: number; + storageSize: number; + storageUsed: number; + sshPublicKey: string; + comment: string; + displayDetails: boolean; + unixUser: string; + lanCommand: boolean; + appendOnlyMode: boolean; +}; diff --git a/helpers/functions/index.ts b/helpers/functions/index.ts new file mode 100644 index 0000000..ac151bc --- /dev/null +++ b/helpers/functions/index.ts @@ -0,0 +1,7 @@ +export * from './auth'; +export * from './lanCommandOption'; +export * from './isSshPubKeyDuplicate'; +export * from './nodemailerSMTP'; +export * from './repoHistory'; +export * from './timestampConverter'; +export * from './tokenController'; diff --git a/helpers/functions/timestampConverter.ts b/helpers/functions/timestampConverter.ts index 540674b..6f05239 100644 --- a/helpers/functions/timestampConverter.ts +++ b/helpers/functions/timestampConverter.ts @@ -1,5 +1,5 @@ // This function is used to parse the date and time into a human readable format from the timestamp. -export default function timestampConverter(UNIX_timestamp) { +export function timestampConverter(UNIX_timestamp) { const a = new Date(UNIX_timestamp * 1000); const year = a.getFullYear(); const month = a.getMonth() + 1; diff --git a/tsconfig.json b/tsconfig.json index f98804e..ed1c64d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,5 +1,9 @@ { "compilerOptions": { + "baseUrl": ".", + "paths": { + "~/*": ["./*"] + }, "target": "ES2017", "lib": ["dom", "dom.iterable", "esnext"], "allowJs": false,