Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusab committed Aug 20, 2024
1 parent 8db6ba8 commit 361efdb
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@ export const metadata: Metadata = {
};

type Props = {
params: {
folders: string[];
};
params: { folders: string[] };
};

export default function Vault({ params }: Props) {
return <Table folders={params.folders ?? []} />;
const disableActions = [
"exports",
"inbox",
"imports",
"transactions",
].includes(params.folders?.[0] ?? "");

return (
<Table folders={params.folders ?? []} disableActions={disableActions} />
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ export function DataTableRow({ data, teamId }) {
path={folderPath}
/>

{data?.metadata?.size && (
{data?.metadata?.size > 0 && (
<span className="text-[#878787]">
{formatSize(data.metadata.size)}
</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type Props = {
};

export async function DataTableServer({ folders }: Props) {
const path = folders?.join("/");
const path = folders.at(-1);

const { data } = await getVault({
path: path && decodeURIComponent(path),
Expand Down
2 changes: 1 addition & 1 deletion apps/dashboard/src/components/vault-activity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export async function VaultActivity() {

const { data: storageData } = await getVaultActivityQuery(
supabase,
userData.id,
userData?.team_id,
);

const files = storageData
Expand Down
2 changes: 1 addition & 1 deletion apps/dashboard/src/hooks/use-enter-submit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function useEnterSubmit(): {
const formRef = useRef<HTMLFormElement>(null);

const handleKeyDown = (
event: React.KeyboardEvent<HTMLTextAreaElement>
event: React.KeyboardEvent<HTMLTextAreaElement>,
): void => {
if (
event.key === "Enter" &&
Expand Down
2 changes: 1 addition & 1 deletion packages/supabase/src/queries/cached-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ export const getMetrics = async (params: Omit<GetMetricsParams, "teamId">) => {
};

export const getVault = async (params: GetVaultParams) => {
const supabase = createClient();
const supabase = createClient({ db: { schema: "storage" } });

const user = await getUser();
const teamId = user?.data?.team_id;
Expand Down
54 changes: 38 additions & 16 deletions packages/supabase/src/queries/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,10 +520,19 @@ export async function getMetricsQuery(
export type GetVaultParams = {
teamId: string;
path?: string;
limit?: number;
};

export async function getVaultQuery(supabase: Client, params: GetVaultParams) {
const { teamId, path } = params;
const { teamId, path, limit = 10000 } = params;

const { data } = await supabase
.from("objects")
.select("*")
.eq("team_id", teamId)
.eq("bucket_id", "vault")
.limit(limit)
.order("name", { ascending: true });

const defaultFolders = path
? []
Expand All @@ -534,21 +543,34 @@ export async function getVaultQuery(supabase: Client, params: GetVaultParams) {
{ name: "transactions", isFolder: true },
];

let basePath = teamId;
// We get all the files in the current "folder" we know if it's a folder if it includes the .emptyFolderPlaceholder file name
// Otherwise we need to get all folders from the path_tokens array, and only add if they are not already in the array
// We need to sort the data so that folders are always at the top of the list

if (path) {
basePath = `${basePath}/${path}`;
}

// TODO: Change to real sql query and index
const { data } = await supabase.storage.from("vault").list(basePath, {
sortBy: { column: "name", order: "asc" },
});
const folders = Array.from(
new Set(
data?.map((item) => item.path_tokens?.at(-2)).filter(Boolean) || [],
),
);

const filteredData =
data
?.filter((file) => file.name !== EMPTY_FOLDER_PLACEHOLDER_FILE_NAME)
.map((item) => ({ ...item, isFolder: !item.id })) ?? [];
// const filteredData = (data ?? [])
// .map((item) => ({
// ...item,
// name:
// item.path_tokens?.at(-1) === EMPTY_FOLDER_PLACEHOLDER_FILE_NAME
// ? item.path_tokens?.at(-2)
// : item.path_tokens?.at(-1),
// isFolder: item.path_tokens?.at(-1) === EMPTY_FOLDER_PLACEHOLDER_FILE_NAME,
// }))
// .sort((a, b) => {
// if (a.isFolder && !b.isFolder) return -1;
// if (!a.isFolder && b.isFolder) return 1;
// return 0;
// });

// console.log("filteredData", filteredData);

const filteredData = [];

const mergedMap = new Map(
[...defaultFolders, ...filteredData].map((obj) => [obj.name, obj]),
Expand All @@ -561,11 +583,11 @@ export async function getVaultQuery(supabase: Client, params: GetVaultParams) {
};
}

export async function getVaultActivityQuery(supabase: Client, userId: string) {
export async function getVaultActivityQuery(supabase: Client, teamId: string) {
return supabase
.from("objects")
.select("*")
.eq("owner_id", userId)
.eq("team_id", teamId)
.limit(20)
.order("created_at", { ascending: false });
}
Expand Down

0 comments on commit 361efdb

Please sign in to comment.