Skip to content

Commit bfcc46a

Browse files
authored
Merge pull request #20888 from davelopez/improve_typing_in_share_ui
Refactor sharing logic for unified type handling
2 parents 0b743f8 + 3878fac commit bfcc46a

File tree

4 files changed

+39
-39
lines changed

4 files changed

+39
-39
lines changed

client/src/api/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,3 +338,12 @@ export type CreateNewCollectionPayload = components["schemas"]["CreateNewCollect
338338
export type UnprivilegedToolResponse = components["schemas"]["UnprivilegedToolResponse"];
339339
export type UserToolSource = components["schemas"]["UserToolSource-Input"];
340340
export type DynamicUnprivilegedToolCreatePayload = components["schemas"]["DynamicUnprivilegedToolCreatePayload"];
341+
342+
export type ShareableItemWithStatus = components["schemas"]["ShareWithStatus"];
343+
export type ShareableHistoryWithStatus = components["schemas"]["ShareHistoryWithStatus"];
344+
export type AnyShareableItemWithStatus = ShareableItemWithStatus | ShareableHistoryWithStatus;
345+
export type ShareOption = components["schemas"]["SharingOptions"];
346+
347+
export function isShareableHistoryWithStatus(item: AnyShareableItemWithStatus): item is ShareableHistoryWithStatus {
348+
return item.extra != null && "accessible_count" in item.extra;
349+
}

client/src/components/Sharing/SharingPage.vue

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ import axios from "axios";
55
import { BFormCheckbox } from "bootstrap-vue";
66
import { computed, nextTick, reactive, ref, watch } from "vue";
77
8+
import type { AnyShareableItemWithStatus, ShareOption } from "@/api";
9+
import { isShareableHistoryWithStatus } from "@/api";
810
import { getGalaxyInstance } from "@/app";
911
import { getFullAppUrl } from "@/app/utils";
1012
import { useToast } from "@/composables/toast";
1113
import { getAppRoot } from "@/onload/loadConfig";
1214
import { errorMessageAsString } from "@/utils/simple-error";
1315
14-
import type { Item, ShareOption } from "./item";
15-
1616
import EditableUrl from "./EditableUrl.vue";
1717
import PageEmbed from "./Embeds/PageEmbed.vue";
1818
import WorkflowEmbed from "./Embeds/WorkflowEmbed.vue";
@@ -40,19 +40,15 @@ function onErrorDismissed(index: number) {
4040
errors.value.splice(index, 1);
4141
}
4242
43-
const defaultExtra = () =>
44-
({
45-
can_change: [],
46-
cannot_change: [],
47-
}) as Item["extra"];
48-
49-
const item = ref<Item>({
43+
const item = ref<AnyShareableItemWithStatus>({
44+
id: "_placeholder_",
5045
title: "title",
5146
username_and_slug: "__username__/__slug__",
5247
importable: false,
5348
published: false,
5449
users_shared_with: [],
55-
extra: defaultExtra(),
50+
extra: null,
51+
errors: [],
5652
});
5753
5854
const itemUrl = reactive({
@@ -112,8 +108,8 @@ async function getSharing() {
112108
113109
getSharing();
114110
115-
function permissionsChangeRequired(data: Item) {
116-
if (data.extra) {
111+
function permissionsChangeRequired(data: AnyShareableItemWithStatus) {
112+
if (isShareableHistoryWithStatus(data)) {
117113
return data.extra.can_change.length > 0 || data.extra.cannot_change.length > 0;
118114
} else {
119115
return false;
@@ -167,16 +163,12 @@ async function setSharing(
167163
168164
const userSharing = ref<InstanceType<typeof UserSharing>>();
169165
170-
async function assignItem(newItem: Item, overwriteCandidates: boolean) {
166+
async function assignItem(newItem: AnyShareableItemWithStatus, overwriteCandidates: boolean) {
171167
if (newItem.errors) {
172168
errors.value = newItem.errors;
173169
}
174170
item.value = newItem;
175171
176-
if ((!item.value.extra || newItem.errors?.length) ?? 0 > 0) {
177-
item.value.extra = defaultExtra();
178-
}
179-
180172
if (overwriteCandidates) {
181173
await nextTick();
182174
//@ts-ignore incorrect property not found error

client/src/components/Sharing/UserSharing.vue

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@ import { storeToRefs } from "pinia";
1515
import { computed, ref } from "vue";
1616
import Multiselect from "vue-multiselect";
1717
18+
import type { AnyShareableItemWithStatus, ShareOption } from "@/api";
19+
import { isShareableHistoryWithStatus } from "@/api";
1820
import { useConfig } from "@/composables/config";
1921
import { getAppRoot } from "@/onload";
2022
import { useUserStore } from "@/stores/userStore";
2123
import { assertArray } from "@/utils/assertions";
2224
23-
import type { Item, ShareOption } from "./item";
24-
2525
import Heading from "../Common/Heading.vue";
2626
2727
const props = defineProps<{
28-
item: Item;
28+
item: AnyShareableItemWithStatus;
2929
modelClass: string;
3030
}>();
3131
@@ -39,7 +39,7 @@ const { currentUser } = storeToRefs(useUserStore());
3939
const { config, isConfigLoaded } = useConfig(false);
4040
4141
const permissionsChangeRequired = computed(() => {
42-
if (props.item.extra) {
42+
if (isShareableHistoryWithStatus(props.item)) {
4343
return props.item.extra.can_change.length > 0 || props.item.extra.cannot_change.length > 0;
4444
} else {
4545
return false;
@@ -123,8 +123,21 @@ const noChanges = computed(() => {
123123
return !(newCandidates.length !== 0 || removedShared.length !== 0);
124124
});
125125
126-
const canChangeCount = computed(() => props.item.extra?.can_change.length ?? 0);
127-
const cannotChangeCount = computed(() => props.item.extra?.cannot_change.length ?? 0);
126+
const canChangeCount = computed(() => {
127+
return isShareableHistoryWithStatus(props.item) ? props.item.extra.can_change.length : 0;
128+
});
129+
130+
const cannotChangeCount = computed(() => {
131+
return isShareableHistoryWithStatus(props.item) ? props.item.extra.cannot_change.length : 0;
132+
});
133+
134+
const canChangeDatasets = computed(() => {
135+
return isShareableHistoryWithStatus(props.item) ? props.item.extra.can_change : [];
136+
});
137+
138+
const cannotChangeDatasets = computed(() => {
139+
return isShareableHistoryWithStatus(props.item) ? props.item.extra.cannot_change : [];
140+
});
128141
129142
const selectedSharingOption = ref<ShareOption>("make_public");
130143
@@ -251,7 +264,7 @@ defineExpose({
251264
v-if="canChangeCount > 0"
252265
header="The following datasets can be shared by updating their permissions">
253266
<BListGroup>
254-
<BListGroupItem v-for="dataset in props.item.extra?.can_change ?? []" :key="dataset.id">
267+
<BListGroupItem v-for="dataset in canChangeDatasets" :key="dataset.id">
255268
{{ dataset.name }}
256269
</BListGroupItem>
257270
</BListGroup>
@@ -261,7 +274,7 @@ defineExpose({
261274
v-if="cannotChangeCount > 0"
262275
header="The following datasets cannot be shared, you are not authorized to change their permissions">
263276
<BListGroup>
264-
<BListGroupItem v-for="dataset in props.item.extra?.cannot_change ?? []" :key="dataset.id">
277+
<BListGroupItem v-for="dataset in cannotChangeDatasets" :key="dataset.id">
265278
{{ dataset.name }}
266279
</BListGroupItem>
267280
</BListGroup>

client/src/components/Sharing/item.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)