Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
TheUltDev committed Feb 15, 2025
1 parent 5ddf389 commit 43c31e5
Show file tree
Hide file tree
Showing 16 changed files with 75 additions and 68 deletions.
29 changes: 14 additions & 15 deletions client/src/media/dir/hooks/use-dir-hfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ import {getThumbnail} from '../utils/hfs/meta';
import {saveAs} from '../utils/hfs/fs';

import type {GestureResponderEvent} from 'react-native';
import type {HfsDirectoryEntry} from 'react-exo/fs';
import type {HfsCtx} from 'media/dir/types/hfs';
import type {HfsCtx, HfsFileEntry} from 'media/dir/types/hfs';

export function useDirHfs(path: string, tmp?: boolean): Omit<HfsCtx, 'bar'> {
const [list, setList] = useState<HfsDirectoryEntry[]>([]);
const [list, setList] = useState<HfsFileEntry[]>([]);

const hfs = useHfs();
const sel = useGet(media.selectors.getSelected);
Expand All @@ -34,7 +33,7 @@ export function useDirHfs(path: string, tmp?: boolean): Omit<HfsCtx, 'bar'> {

const refresh = useCallback(async () => {
const showHidden = true;
const entries: HfsDirectoryEntry[] = [];
const entries: HfsFileEntry[] = [];
const dirPath = path || '.';
try {
// Check if path is valid
Expand Down Expand Up @@ -76,13 +75,13 @@ export function useDirHfs(path: string, tmp?: boolean): Omit<HfsCtx, 'bar'> {
}
}, [hfs, path]);

const open = useCallback(async (entry: HfsDirectoryEntry, clearSel?: boolean) => {
const open = useCallback(async (entry: HfsFileEntry, clearSel?: boolean) => {
if (!entry.isDirectory) return;
nav(path ? `${path}/${entry.name}` : entry.name);
if (clearSel) put(media.actions.selectBulk([]));
}, [path, nav, put]);

const move = useCallback(async (from: HfsDirectoryEntry, to?: HfsDirectoryEntry) => {
const move = useCallback(async (from: HfsFileEntry, to?: HfsFileEntry) => {
if (to) {
const base = path ? `${path}/` : '';
const src = `${base}${from.name}`;
Expand All @@ -94,29 +93,29 @@ export function useDirHfs(path: string, tmp?: boolean): Omit<HfsCtx, 'bar'> {
}
}, [hfs, path]);

const copy = useCallback(async (from: HfsDirectoryEntry, to?: HfsDirectoryEntry) => {
const copy = useCallback(async (from: HfsFileEntry, to?: HfsFileEntry) => {
if (to) {
await hfs?.copy?.(from.name, to.name);
} else {
console.log('>> fs [copy]', from);
}
}, [hfs]);

const purge = useCallback(async (entry: HfsDirectoryEntry) => {
const purge = useCallback(async (entry: HfsFileEntry) => {
const base = path ? `${path}/` : '';
const uri = `${base}${entry.name}`;
await hfs?.deleteAll?.(uri);
}, [hfs, path]);

const rename = useCallback(async (entry: HfsDirectoryEntry, name?: string) => {
const rename = useCallback(async (entry: HfsFileEntry, name?: string) => {
if (name) {
await hfs?.move?.(entry.name, name);
} else {
console.log('>> fs [rename]', entry);
}
}, [hfs]);

const select = useCallback((entry: HfsDirectoryEntry, event?: GestureResponderEvent) => {
const select = useCallback((entry: HfsFileEntry, event?: GestureResponderEvent) => {
if (isZeego(event)) return;
const [isShift, isCtrl] = [event?.shiftKey, event?.metaKey || event?.ctrlKey];
const fullPath = path ? `${path}/${entry.name}` : entry.name;
Expand All @@ -132,7 +131,7 @@ export function useDirHfs(path: string, tmp?: boolean): Omit<HfsCtx, 'bar'> {
}));
}, [path, tmp, sel, open, put]);

const upload = useCallback(async (entry: HfsDirectoryEntry, files: File[]) => {
const upload = useCallback(async (entry: HfsFileEntry, files: File[]) => {
if (!hfs) return;
for (const file of files) {
const data = await file.arrayBuffer();
Expand All @@ -143,22 +142,22 @@ export function useDirHfs(path: string, tmp?: boolean): Omit<HfsCtx, 'bar'> {
}
}, [hfs]);

const download = useCallback(async (entry: HfsDirectoryEntry) => {
const download = useCallback(async (entry: HfsFileEntry) => {
if (entry.isFile) {
const uri = path ? `${path}/${entry.name}` : entry.name;
saveAs(await getData(uri, 'dataUrl'), entry.name);
}
}, [path]);

const compress = useCallback(async (entry: HfsDirectoryEntry) => {
const compress = useCallback(async (entry: HfsFileEntry) => {
console.log('>> fs [compress]', entry);
}, []);

const thumbnail = useCallback(async (entry: HfsDirectoryEntry) => {
const thumbnail = useCallback(async (entry: HfsFileEntry) => {
return getThumbnail(path, entry);
}, [path]);

const share = useCallback(async (entry: HfsDirectoryEntry) => {
const share = useCallback(async (entry: HfsFileEntry) => {
console.log('>> fs [share]', entry);
}, []);

Expand Down
7 changes: 4 additions & 3 deletions client/src/media/dir/hooks/use-dir-torrent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import media from 'media/store';

import type {Torrent, TorrentCtx, TorrentInfo, TorrentFileData, TorrentFileEntry} from 'media/dir/types/torrent';
import type {GestureResponderEvent} from 'react-native';
import type {HfsDirectoryEntry} from 'react-exo/fs';
import type {HfsFileEntry} from 'media/dir/types/hfs';

export function useDirTorrent(path: string): TorrentCtx {
const buffer = useFile(path, 'arrayBuffer');
const {path: url} = usePath();
const buffer = useFile(path, 'arrayBuffer');
const put = usePut();

const torrent: Torrent | null = useMemo(() => {
Expand All @@ -38,14 +38,15 @@ export function useDirTorrent(path: string): TorrentCtx {
const download = useCallback(async (
file: TorrentFileEntry,
event?: GestureResponderEvent,
target?: HfsDirectoryEntry,
target?: HfsFileEntry,
) => {
if (!torrent) return;
const client = new Tor();
// @ts-expect-error Incorrect vendor types
client.add(torrent.file, {store}, async ({files}) => {
const item = files.find(e => e.path.split('/').slice(1).join('/') === file.path);
const root = url ? `${url}/` : '';
console.log('>> torrent [url]', url);
const head = target?.name ? `${target.name}/` : '';
const dest = `${root}${head}${file.path}`;
const handle = await web.getFileHandle(dest, {create: true});
Expand Down
4 changes: 2 additions & 2 deletions client/src/media/dir/hooks/use-dir-zip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import media from 'media/store';
import type {FS} from '@zip.js/zip.js';
import type {Zip, ZipCtx, ZipFileEntry} from 'media/dir/types/zip';
import type {GestureResponderEvent} from 'react-native';
import type {HfsDirectoryEntry} from 'react-exo/fs';
import type {HfsFileEntry} from 'media/dir/types/hfs';

export function useDirZip(path: string): ZipCtx {
const [zip, setZip] = useState<Zip | null>(null);
Expand All @@ -22,7 +22,7 @@ export function useDirZip(path: string): ZipCtx {
const extract = useCallback(async (
file: ZipFileEntry,
event?: GestureResponderEvent,
target?: HfsDirectoryEntry,
target?: HfsFileEntry,
) => {
if (!zip) return;
const source = zipfs.current?.getById(file.id);
Expand Down
5 changes: 2 additions & 3 deletions client/src/media/dir/hooks/use-entry-hfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ import media from 'media/store';
import {is as isZip} from './use-entry-zip';
import {is as isTorrent} from './use-entry-torrent';

import type {HfsDirectoryEntry} from 'react-exo/fs';
import type {View, GestureResponderEvent} from 'react-native';
import type {HfsCmd, HfsFileEntry} from 'media/dir/types/hfs';
import type {EntryHfsProps} from 'media/dir/stacks/entry-hfs';
import type {CleanupFn} from 'app/utils/dragdrop';
import type {HfsCmd} from 'media/dir/types/hfs';

export const {is, get, type} = $.tag<HfsDirectoryEntry, HfsCmd>('hfs');
export const {is, get, type} = $.tag<HfsFileEntry, HfsCmd>('hfs');

export function useEntryHfs({item, cmd, opt, tmp}: EntryHfsProps) {
const [dropping, setDropping] = useState(false);
Expand Down
5 changes: 2 additions & 3 deletions client/src/media/dir/stacks/entry-hfs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ import {ListRow} from 'media/stacks/list/row';
import {MenuHfs} from 'media/dir/stacks/menu-hfs';
import {useEntryHfs} from 'media/dir/hooks/use-entry-hfs';

import type {HfsDirectoryEntry} from 'react-exo/fs';
import type {HfsCmd, HfsOpt} from 'media/dir/types/hfs';
import type {HfsCmd, HfsOpt, HfsFileEntry} from 'media/dir/types/hfs';

export interface EntryHfsProps {
item: HfsDirectoryEntry,
item: HfsFileEntry,
cmd: HfsCmd,
opt: HfsOpt,
tmp?: boolean,
Expand Down
4 changes: 2 additions & 2 deletions client/src/media/dir/stacks/menu-hfs.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {useLingui} from '@lingui/react/macro';
import {MenuContext} from 'app/stacks/float';

import type {HfsDirectoryEntry} from 'react-exo/fs';
import type {useEntryHfs} from 'media/dir/hooks/use-entry-hfs';
import type {HfsFileEntry} from 'media/dir/types/hfs';

export interface MenuHfsProps extends React.PropsWithChildren {
item: HfsDirectoryEntry,
item: HfsFileEntry,
cmd: ReturnType<typeof useEntryHfs>['cmd'],
on?: (open: boolean) => void,
}
Expand Down
26 changes: 14 additions & 12 deletions client/src/media/dir/types/hfs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {HfsDirectoryEntry} from 'react-exo/fs';
import type {GestureResponderEvent} from 'react-native';
import type {HfsDirectoryEntry} from 'react-exo/fs';
import type {ListBarProps} from 'media/stacks/list/bar';

export interface Hfs {
Expand Down Expand Up @@ -29,15 +29,17 @@ export type HfsOpt = {

export type HfsCmd = {
goUp: () => boolean,
share: (entry: HfsDirectoryEntry) => Promise<void>,
open: (entry: HfsDirectoryEntry, clearSel?: boolean) => Promise<void>,
copy: (entry: HfsDirectoryEntry) => Promise<void>,
move: (from: HfsDirectoryEntry, to?: HfsDirectoryEntry) => Promise<void>,
purge: (entry: HfsDirectoryEntry) => Promise<void>,
rename: (entry: HfsDirectoryEntry) => Promise<void>,
select: (entry: HfsDirectoryEntry, event?: GestureResponderEvent) => void,
upload: (entry: HfsDirectoryEntry, files: File[]) => Promise<void>,
download: (entry: HfsDirectoryEntry) => Promise<void>,
compress: (entry: HfsDirectoryEntry) => Promise<void>,
thumbnail: (entry: HfsDirectoryEntry) => Promise<string | null>,
share: (entry: HfsFileEntry) => Promise<void>,
open: (entry: HfsFileEntry, clearSel?: boolean) => Promise<void>,
copy: (entry: HfsFileEntry) => Promise<void>,
move: (from: HfsFileEntry, to?: HfsFileEntry) => Promise<void>,
purge: (entry: HfsFileEntry) => Promise<void>,
rename: (entry: HfsFileEntry) => Promise<void>,
select: (entry: HfsFileEntry, event?: GestureResponderEvent) => void,
upload: (entry: HfsFileEntry, files: File[]) => Promise<void>,
download: (entry: HfsFileEntry) => Promise<void>,
compress: (entry: HfsFileEntry) => Promise<void>,
thumbnail: (entry: HfsFileEntry) => Promise<string | null>,
}

export type HfsFileEntry = HfsDirectoryEntry & {}
19 changes: 13 additions & 6 deletions client/src/media/dir/types/torrent.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {GestureResponderEvent} from 'react-native';
import type {HfsDirectoryEntry} from 'react-exo/fs';
import type {HfsFileEntry} from 'media/dir/types/hfs';

export interface Torrent {
file: File,
Expand All @@ -19,21 +19,30 @@ export type TorrentCmd = {
download: (
file: TorrentFileEntry,
event?: GestureResponderEvent,
target?: HfsDirectoryEntry,
target?: HfsFileEntry,
) => Promise<void>,
}

export interface TorrentInfo {
export type TorrentFileEntry = TorrentFileData['files'][number];

export type TorrentInfo = {
/** Name of the torrent */
name: string,
/** List of URLs for the torrent */
urlList: string[],
/** List of announce URLs for the torrent */
announce: string[],
/** Comment for the torrent */
comment?: string,
/** Whether the torrent is private */
private?: boolean,
/** Date the torrent was created */
created?: Date,
/** Name of the creator of the torrent */
createdBy?: string,
}

export interface TorrentFileData {
export type TorrentFileData = {
/** Length of the file in bytes */
length: number;
/** Array of files */
Expand All @@ -54,5 +63,3 @@ export interface TorrentFileData {
/** Array of piece hashes */
pieces: string[];
}

export type TorrentFileEntry = TorrentFileData['files'][number];
4 changes: 2 additions & 2 deletions client/src/media/dir/types/zip.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {GestureResponderEvent} from 'react-native';
import type {HfsDirectoryEntry} from 'react-exo/fs';
import type {HfsFileEntry} from 'media/dir/types/hfs';

export interface Zip {
date: {
Expand Down Expand Up @@ -29,7 +29,7 @@ export type ZipCmd = {
extract: (
entry: ZipFileEntry,
event?: GestureResponderEvent,
target?: HfsDirectoryEntry,
target?: HfsFileEntry,
) => Promise<void>,
};

Expand Down
4 changes: 2 additions & 2 deletions client/src/media/dir/utils/hfs/meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import {FileType} from 'media/file/types';
import {getRenderer} from 'media/file/utils/render';
import {toPath} from 'app/utils/formatting';

import type {HfsDirectoryEntry} from 'react-exo/fs';
import type {HfsFileEntry} from 'media/dir/types/hfs';

const THUMB_MAX_SIZE = 320;

export async function getThumbnail(path: string, item: HfsDirectoryEntry) {
export async function getThumbnail(path: string, item: HfsFileEntry) {
if (item.isDirectory) return null;
// Resolve path
const uri = path ? `${path}/${item.name}` : item.name;
Expand Down
6 changes: 3 additions & 3 deletions translations/ar.po
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ msgstr "إغلاق"
msgid "Compress"
msgstr "ضغط"

#: ../../client/src/media/hooks/use-media-controls.ts:72
#: ../../client/src/media/dir/stacks/menu-hfs.tsx:55
msgid "Copy"
msgstr "نسخ"

Expand Down Expand Up @@ -166,7 +166,7 @@ msgstr "المستندات"
msgid "Documents"
msgstr "المستندات"

#: ../../client/src/media/hooks/use-media-controls.ts:91
#: ../../client/src/media/dir/stacks/menu-hfs.tsx:44
msgid "Download"
msgstr "تحميل"

Expand Down Expand Up @@ -476,7 +476,7 @@ msgstr "الخدمات"
msgid "Settings"
msgstr "الإعدادات"

#: ../../client/src/media/hooks/use-media-controls.ts:268
#: ../../client/src/media/dir/stacks/menu-hfs.tsx:30
msgid "Share"
msgstr "مشاركة"

Expand Down
6 changes: 3 additions & 3 deletions translations/en.po
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ msgstr "Close"
msgid "Compress"
msgstr "Compress"

#: ../../client/src/media/hooks/use-media-controls.ts:72
#: ../../client/src/media/dir/stacks/menu-hfs.tsx:55
msgid "Copy"
msgstr "Copy"

Expand Down Expand Up @@ -166,7 +166,7 @@ msgstr "Docs"
msgid "Documents"
msgstr "Documents"

#: ../../client/src/media/hooks/use-media-controls.ts:91
#: ../../client/src/media/dir/stacks/menu-hfs.tsx:44
msgid "Download"
msgstr "Download"

Expand Down Expand Up @@ -476,7 +476,7 @@ msgstr "Services"
msgid "Settings"
msgstr "Settings"

#: ../../client/src/media/hooks/use-media-controls.ts:268
#: ../../client/src/media/dir/stacks/menu-hfs.tsx:30
msgid "Share"
msgstr "Share"

Expand Down
6 changes: 3 additions & 3 deletions translations/es.po
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ msgstr "Cerrar"
msgid "Compress"
msgstr "Comprimir"

#: ../../client/src/media/hooks/use-media-controls.ts:72
#: ../../client/src/media/dir/stacks/menu-hfs.tsx:55
msgid "Copy"
msgstr "Copiar"

Expand Down Expand Up @@ -166,7 +166,7 @@ msgstr "Documentos"
msgid "Documents"
msgstr "Documentos"

#: ../../client/src/media/hooks/use-media-controls.ts:91
#: ../../client/src/media/dir/stacks/menu-hfs.tsx:44
msgid "Download"
msgstr "Descargar"

Expand Down Expand Up @@ -476,7 +476,7 @@ msgstr "Servicios"
msgid "Settings"
msgstr "Ajustes"

#: ../../client/src/media/hooks/use-media-controls.ts:268
#: ../../client/src/media/dir/stacks/menu-hfs.tsx:30
msgid "Share"
msgstr "Compartir"

Expand Down
Loading

0 comments on commit 43c31e5

Please sign in to comment.