<script setup lang="ts">
import type { TokenInjectType } from "@/api";
import { defineEmits, defineProps, inject } from "vue";
import { check_token, FS, Responses } from "@/api";

const jwt = inject<TokenInjectType>("jwt") as TokenInjectType;
const props = defineProps<{
  node: Responses.FS.GetNodeResponse;
}>();

const emit = defineEmits<{
  (e: "reloadNode"): void;
}>();

async function del() {
  const token = await check_token(jwt);
  if (!token) return;
  await FS.delete_node(token, props.node.id);
  emit("reloadNode");
}

async function download() {
  const token = await check_token(jwt);
  if (!token) return;
  FS.download_file(token, props.node.id);
}
</script>

<template>
  <td>
    <router-link :to="'/fs/' + props.node.id">{{ node.name }}</router-link>
  </td>
  <td>
    <a href="#" @click="download()" v-if="props.node.isFile">Download</a>
  </td>
  <td>
    <a href="#" @click="del()" v-if="props.node.name !== '..'">delete</a>
  </td>
</template>

<style scoped></style>
