forked from anydistro/bxt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(frontend): move hooks into their own files
Cleans up FileViewPage.tsx by moving general-purpose hooks into their own files.
- Loading branch information
1 parent
372bae2
commit d2da191
Showing
4 changed files
with
207 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* === This file is part of bxt === | ||
* | ||
* SPDX-FileCopyrightText: 2024 Artem Grinev <agrinev@manjaro.org> | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
* | ||
*/ | ||
|
||
import { useCallback } from "react"; | ||
|
||
export const usePackageDropHandler = ( | ||
path: string[], | ||
setCommit: (commits: ICommit) => void | ||
) => { | ||
return useCallback( | ||
(acceptedFiles: File[]) => { | ||
const section: ISection = { | ||
branch: path[1], | ||
repository: path[2], | ||
architecture: path[3] | ||
}; | ||
const packages: { | ||
[key: string]: Partial<IPackageUpload>; | ||
} = {}; | ||
for (const file of acceptedFiles) { | ||
if (file.name.endsWith(".sig")) { | ||
const name = file.name.replace(".sig", ""); | ||
if (!packages[name]) { | ||
packages[name] = {}; | ||
} | ||
packages[name].signatureFile = file; | ||
continue; | ||
} | ||
|
||
packages[file.name] = { | ||
name: file.name, | ||
section, | ||
file: file, | ||
...packages[file.name] | ||
}; | ||
} | ||
|
||
setCommit({ | ||
section, | ||
packages: Object.values(packages) | ||
.filter((partial) => partial as IPackageUpload) | ||
.map((partial) => partial as IPackageUpload) | ||
}); | ||
}, | ||
[path, setCommit] | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* === This file is part of bxt === | ||
* | ||
* SPDX-FileCopyrightText: 2024 Artem Grinev <agrinev@manjaro.org> | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
* | ||
*/ | ||
|
||
import { | ||
FileArray, | ||
ChonkyFileActionData, | ||
ChonkyActions, | ||
FileHelper, | ||
FileData | ||
} from "chonky"; | ||
import { OpenFilesPayload } from "chonky/dist/types/action-payloads.types"; | ||
import _ from "lodash"; | ||
import { useCallback } from "react"; | ||
import { SnapshotActionPayload } from "../components/SnapshotAction"; | ||
|
||
export const useFolderChainForPath = (path: string[]): FileArray => { | ||
const result: FileArray = []; | ||
|
||
result.push({ | ||
id: "root", | ||
name: "root", | ||
isDir: true | ||
}); | ||
|
||
for (let i = 1; i < path.length; i++) { | ||
result.push({ | ||
id: `${result[i - 1]!.id}/${path[i]}`, | ||
name: path[i], | ||
isDir: true | ||
}); | ||
} | ||
|
||
return result; | ||
}; | ||
|
||
export const packageFromFilePath = (filePath: string, packages: IPackage[]) => { | ||
const parts = filePath.split("/"); | ||
if (parts.length != 5) return; | ||
const section: ISection = { | ||
branch: parts[1], | ||
repository: parts[2], | ||
architecture: parts[3] | ||
}; | ||
const packageName = parts[4]; | ||
|
||
return packages.find((value) => { | ||
return _.isEqual(value.section, section) && value.name == packageName; | ||
}); | ||
}; | ||
|
||
export const useFileActionHandler = ( | ||
setPath: (path: string[]) => void, | ||
setSnapshotModalBranches: ( | ||
sourceBranch?: string, | ||
targetBranch?: string | ||
) => void, | ||
setPackage: (pkg?: IPackage) => void, | ||
packages: IPackage[] | ||
) => { | ||
return useCallback( | ||
(data: ChonkyFileActionData) => { | ||
switch (data.id as string) { | ||
case ChonkyActions.OpenFiles.id: | ||
const { targetFile, files } = | ||
data.payload as OpenFilesPayload; | ||
const fileToOpen = targetFile ?? files[0]; | ||
if (fileToOpen && FileHelper.isDirectory(fileToOpen)) { | ||
const pathToOpen = fileToOpen.id.split("/"); | ||
|
||
setPath(pathToOpen); | ||
} else if ( | ||
fileToOpen && | ||
!FileHelper.isDirectory(fileToOpen) | ||
) { | ||
setPackage( | ||
packageFromFilePath( | ||
(fileToOpen as FileData).id, | ||
packages | ||
) | ||
); | ||
} | ||
break; | ||
case "snap": | ||
const { sourceBranch, targetBranch } = | ||
data.payload as SnapshotActionPayload; | ||
|
||
setSnapshotModalBranches(sourceBranch, targetBranch); | ||
} | ||
}, | ||
[setPath, setSnapshotModalBranches, setPackage, packages] | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters