Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support changing attachment display name #6504

Merged
merged 3 commits into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 4 additions & 20 deletions ui/console-src/modules/contents/attachments/AttachmentList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ const handleCheckAllChange = (e: Event) => {
const onDetailModalClose = () => {
selectedAttachment.value = undefined;
nameQuery.value = undefined;
nameQueryAttachment.value = undefined;
detailVisible.value = false;
handleFetchAttachments();
};
Expand Down Expand Up @@ -209,16 +208,15 @@ const viewType = useLocalStorage("attachment-view-type", "list");
const routeQueryAction = useRouteQuery<string | undefined>("action");

onMounted(() => {
if (!routeQueryAction.value) {
return;
}
if (routeQueryAction.value === "upload") {
uploadVisible.value = true;
}
if (nameQuery.value) {
detailVisible.value = true;
}
});

const nameQuery = useRouteQuery<string | undefined>("name");
const nameQueryAttachment = ref<Attachment>();

watch(
() => selectedAttachment.value,
Expand All @@ -228,25 +226,11 @@ watch(
}
}
);

onMounted(() => {
if (!nameQuery.value) {
return;
}
coreApiClient.storage.attachment
.getAttachment({
name: nameQuery.value,
})
.then((response) => {
nameQueryAttachment.value = response.data;
detailVisible.value = true;
});
});
</script>
<template>
<AttachmentDetailModal
v-if="detailVisible"
:attachment="selectedAttachment || nameQueryAttachment"
:name="selectedAttachment?.metadata.name || nameQuery"
@close="onDetailModalClose"
>
<template #actions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,29 @@
import LazyImage from "@/components/image/LazyImage.vue";
import { formatDatetime } from "@/utils/date";
import { isImage } from "@/utils/image";
import type { Attachment } from "@halo-dev/api-client";
import { coreApiClient } from "@halo-dev/api-client";
import {
IconRiPencilFill,
VButton,
VDescription,
VDescriptionItem,
VLoading,
VModal,
VSpace,
} from "@halo-dev/components";
import { useQuery } from "@tanstack/vue-query";
import prettyBytes from "pretty-bytes";
import { computed, ref } from "vue";
import { useFetchAttachmentGroup } from "../composables/use-attachment-group";
import { computed, ref, toRefs } from "vue";
import AttachmentPermalinkList from "./AttachmentPermalinkList.vue";
import DisplayNameEditForm from "./DisplayNameEditForm.vue";

const props = withDefaults(
defineProps<{
attachment: Attachment | undefined;
name?: string;
mountToBody?: boolean;
}>(),
{
attachment: undefined,
name: undefined,
mountToBody: false,
}
);
Expand All @@ -32,16 +33,31 @@ const emit = defineEmits<{
(event: "close"): void;
}>();

const { groups } = useFetchAttachmentGroup();
const { name } = toRefs(props);

const onlyPreview = ref(false);

const { data: attachment, isLoading } = useQuery({
queryKey: ["core:attachment-by-name", name],
queryFn: async () => {
const { data } = await coreApiClient.storage.attachment.getAttachment({
name: name.value as string,
});
return data;
},
enabled: computed(() => !!name.value),
});

const policyName = computed(() => {
return props.attachment?.spec.policyName;
return attachment.value?.spec.policyName;
});

const groupName = computed(() => {
return attachment.value?.spec.groupName;
});

const { data: policy } = useQuery({
queryKey: ["attachment-policy", policyName],
queryKey: ["core:attachment-policy-by-name", policyName],
queryFn: async () => {
if (!policyName.value) {
return;
Expand All @@ -56,10 +72,23 @@ const { data: policy } = useQuery({
enabled: computed(() => !!policyName.value),
});

const getGroupName = (name: string | undefined) => {
const group = groups.value?.find((group) => group.metadata.name === name);
return group?.spec.displayName || name;
};
const { data: group } = useQuery({
queryKey: ["core:attachment-group-by-name", groupName],
queryFn: async () => {
if (!groupName.value) {
return;
}

const { data } = await coreApiClient.storage.group.getGroup({
name: groupName.value,
});

return data;
},
enabled: computed(() => !!groupName.value),
});

const showDisplayNameForm = ref(false);
</script>
<template>
<VModal
Expand All @@ -78,108 +107,129 @@ const getGroupName = (name: string | undefined) => {
<template #actions>
<slot name="actions"></slot>
</template>
<div class="overflow-hidden bg-white">
<div
v-if="onlyPreview && isImage(attachment?.spec.mediaType)"
class="flex justify-center p-4"
>
<img
v-tooltip.bottom="
$t('core.attachment.detail_modal.preview.click_to_exit')
"
:alt="attachment?.spec.displayName"
:src="attachment?.status?.permalink"
class="w-auto transform-gpu cursor-pointer rounded"
@click="onlyPreview = !onlyPreview"
/>
</div>
<div v-else>
<VDescription>
<VDescriptionItem
:label="$t('core.attachment.detail_modal.fields.preview')"
>
<div
v-if="isImage(attachment?.spec.mediaType)"
@click="onlyPreview = !onlyPreview"
>
<LazyImage
:alt="attachment?.spec.displayName"
:src="attachment?.status?.permalink"
classes="max-w-full cursor-pointer rounded sm:max-w-[50%]"
>
<template #loading>
<span class="text-gray-400">
{{ $t("core.common.status.loading") }}...
</span>
</template>
<template #error>
<span class="text-red-400">
{{ $t("core.common.status.loading_error") }}
</span>
</template>
</LazyImage>
</div>
<div v-else-if="attachment?.spec.mediaType?.startsWith('video/')">
<video
:src="attachment.status?.permalink"
controls
class="max-w-full rounded sm:max-w-[50%]"
>
{{
$t("core.attachment.detail_modal.preview.video_not_support")
}}
</video>
</div>
<div v-else-if="attachment?.spec.mediaType?.startsWith('audio/')">
<audio :src="attachment.status?.permalink" controls>
{{
$t("core.attachment.detail_modal.preview.audio_not_support")
}}
</audio>
</div>
<span v-else>
{{ $t("core.attachment.detail_modal.preview.not_support") }}
</span>
</VDescriptionItem>
<VDescriptionItem
:label="$t('core.attachment.detail_modal.fields.storage_policy')"
:content="policy?.spec.displayName"
></VDescriptionItem>
<VDescriptionItem
:label="$t('core.attachment.detail_modal.fields.group')"
:content="
getGroupName(attachment?.spec.groupName) ||
$t('core.attachment.common.text.ungrouped')
<div>
<VLoading v-if="isLoading" />
<div v-else class="overflow-hidden bg-white">
<div
v-if="onlyPreview && isImage(attachment?.spec.mediaType)"
class="flex justify-center p-4"
>
<img
v-tooltip.bottom="
$t('core.attachment.detail_modal.preview.click_to_exit')
"
:alt="attachment?.spec.displayName"
:src="attachment?.status?.permalink"
class="w-auto transform-gpu cursor-pointer rounded"
@click="onlyPreview = !onlyPreview"
/>
<VDescriptionItem
:label="$t('core.attachment.detail_modal.fields.display_name')"
:content="attachment?.spec.displayName"
/>
<VDescriptionItem
:label="$t('core.attachment.detail_modal.fields.media_type')"
:content="attachment?.spec.mediaType"
/>
<VDescriptionItem
:label="$t('core.attachment.detail_modal.fields.size')"
:content="prettyBytes(attachment?.spec.size || 0)"
/>
<VDescriptionItem
:label="$t('core.attachment.detail_modal.fields.owner')"
:content="attachment?.spec.ownerName"
/>
<VDescriptionItem
:label="$t('core.attachment.detail_modal.fields.creation_time')"
:content="formatDatetime(attachment?.metadata.creationTimestamp)"
/>
<VDescriptionItem
:label="$t('core.attachment.detail_modal.fields.permalink')"
>
<AttachmentPermalinkList :attachment="attachment" />
</VDescriptionItem>
</VDescription>
</div>
<div v-else>
<VDescription>
<VDescriptionItem
:label="$t('core.attachment.detail_modal.fields.preview')"
>
<div
v-if="isImage(attachment?.spec.mediaType)"
@click="onlyPreview = !onlyPreview"
>
<LazyImage
:alt="attachment?.spec.displayName"
:src="attachment?.status?.permalink"
classes="max-w-full cursor-pointer rounded sm:max-w-[50%]"
>
<template #loading>
<span class="text-gray-400">
{{ $t("core.common.status.loading") }}...
</span>
</template>
<template #error>
<span class="text-red-400">
{{ $t("core.common.status.loading_error") }}
</span>
</template>
</LazyImage>
</div>
<div v-else-if="attachment?.spec.mediaType?.startsWith('video/')">
<video
:src="attachment.status?.permalink"
controls
class="max-w-full rounded sm:max-w-[50%]"
>
{{
$t("core.attachment.detail_modal.preview.video_not_support")
}}
</video>
</div>
<div v-else-if="attachment?.spec.mediaType?.startsWith('audio/')">
<audio :src="attachment.status?.permalink" controls>
{{
$t("core.attachment.detail_modal.preview.audio_not_support")
}}
</audio>
</div>
<span v-else>
{{ $t("core.attachment.detail_modal.preview.not_support") }}
</span>
</VDescriptionItem>
<VDescriptionItem
:label="$t('core.attachment.detail_modal.fields.storage_policy')"
:content="policy?.spec.displayName"
>
</VDescriptionItem>
<VDescriptionItem
:label="$t('core.attachment.detail_modal.fields.group')"
:content="
group?.spec.displayName ||
$t('core.attachment.common.text.ungrouped')
"
/>
<VDescriptionItem
:label="$t('core.attachment.detail_modal.fields.display_name')"
>
<DisplayNameEditForm
v-if="showDisplayNameForm && attachment"
:attachment="attachment"
@close="showDisplayNameForm = false"
/>
<div v-else class="flex items-center gap-3">
<span>
{{ attachment?.spec.displayName }}
</span>
<HasPermission :permissions="['system:attachments:manage']">
<IconRiPencilFill
class="cursor-pointer text-sm text-gray-600 hover:text-gray-900"
@click="showDisplayNameForm = true"
/>
</HasPermission>
</div>
</VDescriptionItem>
<VDescriptionItem
:label="$t('core.attachment.detail_modal.fields.media_type')"
:content="attachment?.spec.mediaType"
/>
<VDescriptionItem
:label="$t('core.attachment.detail_modal.fields.size')"
:content="prettyBytes(attachment?.spec.size || 0)"
/>
<VDescriptionItem
:label="$t('core.attachment.detail_modal.fields.owner')"
:content="attachment?.spec.ownerName"
/>
<VDescriptionItem
:label="$t('core.attachment.detail_modal.fields.creation_time')"
:content="formatDatetime(attachment?.metadata.creationTimestamp)"
/>
<VDescriptionItem
:label="$t('core.attachment.detail_modal.fields.permalink')"
>
<AttachmentPermalinkList :attachment="attachment" />
</VDescriptionItem>
</VDescription>
</div>
</div>
</div>

<template #footer>
<VSpace>
<VButton type="default" @click="emit('close')">
Expand Down
Loading
Loading