useFileList.ts 860 Bytes
import { useState, useCallback, useEffect } from "react";
import ky from "ky";

interface FileListData extends FileItem {
  list: FileItem[];
}

interface FileListResponse {
  data: FileListData;
}

export interface FileItem {
  is_folder: boolean;
  name: string;
  file_type: "folder" | "file";
  path: string;
  parent: number;
  user_id: number;
  size: number;
  is_deleted: boolean;
  created_time: string | null;
  updated_time: string;
  status: boolean;
  id: number;
}

export function useFileList(id: string | number) {
  const [data, setData] = useState<FileListData | null>(null);

  const reload = useCallback(async () => {
    const response = await ky
      .get(`/items/${id}/children/`)
      .json<FileListResponse>();

    setData(response.data);
  }, [id]);

  useEffect(() => {
    reload();
  }, [reload]);

  return { data, reload };
}