@open-commune/web 0.2.13 → 0.2.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/app.config.ts +13 -0
- package/assets/css/main.css +119 -0
- package/components/data/lists/data-table.vue +203 -0
- package/components/data/lists/row-actions.vue +41 -0
- package/components/elements/badges/status-badge.vue +23 -0
- package/components/elements/media/media-image.vue +26 -0
- package/components/forms/fields/input-files-media.vue +87 -0
- package/components/forms/{inputs → fields}/input-filter-status.vue +2 -2
- package/components/forms/{inputs → fields}/input-search.vue +8 -2
- package/components/forms/fields/input-text-area.vue +86 -0
- package/components/forms/fields/input-text.vue +47 -0
- package/components/forms/fields/select-enum.vue +69 -0
- package/components/forms/handlers/handler-collections.vue +100 -170
- package/components/forms/handlers/handler-list.vue +187 -0
- package/components/forms/handlers/handler-members.vue +171 -0
- package/components/forms/handlers/handler-password-reset.vue +45 -0
- package/components/forms/handlers/handler-roles.vue +28 -166
- package/components/forms/handlers/handler-sessions.vue +77 -0
- package/components/forms/handlers/handler-tokens.vue +54 -69
- package/components/forms/layouts/form-default.vue +99 -0
- package/components/forms/layouts/form.vue +4 -0
- package/components/forms/layouts/forms-layout.vue +236 -0
- package/components/forms/layouts/forms-section.vue +186 -0
- package/components/forms/layouts/layout.vue +4 -0
- package/components/forms/layouts/modal.vue +4 -0
- package/components/forms/layouts/section.vue +4 -0
- package/components/layout/entrance-mark.vue +18 -0
- package/components/layout/navigations/primary/dropdown-publish.vue +6 -4
- package/components/layout/shell/shell-account-menu.vue +63 -0
- package/components/layout/shell/shell-header.vue +99 -0
- package/composables/core/domains/use-api-tokens.ts +80 -0
- package/composables/core/domains/use-auth.ts +162 -0
- package/composables/core/domains/use-definitions.ts +18 -5
- package/composables/core/domains/use-entries.ts +113 -3
- package/composables/core/domains/use-media.ts +24 -1
- package/composables/core/domains/use-members.ts +68 -0
- package/composables/core/domains/use-organization.ts +53 -0
- package/composables/use-models.js +1 -9
- package/constants/errors.ts +28 -0
- package/constants/forms/account.ts +62 -0
- package/constants/forms/collections.ts +39 -0
- package/constants/forms/members.ts +40 -0
- package/constants/forms/organization.ts +188 -0
- package/constants/forms/tokens.ts +15 -0
- package/constants/roles.ts +57 -0
- package/constants/settings.ts +26 -0
- package/constants/statuses.ts +8 -5
- package/layouts/default.vue +1 -2
- package/layouts/entrance.vue +1 -6
- package/layouts/record.vue +1 -2
- package/middleware/00.light-mode.global.ts +8 -0
- package/nuxt.config.ts +8 -0
- package/package.json +5 -3
- package/pages/[collection]/index.vue +92 -79
- package/pages/account.vue +40 -0
- package/pages/index/index.vue +54 -62
- package/pages/index/password/define/[token].vue +44 -68
- package/pages/index/password/recover/index.vue +43 -54
- package/pages/index/welcome/[token].vue +42 -79
- package/pages/index.vue +6 -37
- package/pages/settings.vue +26 -181
- package/plugins/00.appearance.client.ts +7 -0
- package/plugins/01.validation.client.ts +23 -0
- package/store/layout/workspace.js +4 -28
- package/utils/colors.ts +26 -0
- package/utils/errors.ts +13 -0
- package/utils/forms.ts +67 -0
- package/utils/list-columns.ts +90 -0
- package/utils/settings.ts +38 -0
- package/utils/statuses.ts +4 -0
- package/components/layout/navigations/primary.vue +0 -132
- package/models/entrance.js +0 -42
- package/models/role.js +0 -66
- package/models/token.js +0 -56
- package/models/user.js +0 -97
- package/pages/management/index.vue +0 -15
- package/pages/management/members.vue +0 -176
- package/pages/management/organizations.vue +0 -192
- package/pages/management/roles.vue +0 -183
- package/public/logo-black.svg +0 -1
- package/public/logo-white.svg +0 -1
- /package/components/forms/{inputs → fields}/input-filter.vue +0 -0
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<header class="border-b border-default bg-elevated plain:bg-default">
|
|
3
|
+
<UContainer class="max-w-7xl">
|
|
4
|
+
<div class="flex items-center justify-between gap-4 py-5">
|
|
5
|
+
<!-- Single tenant: the commune's identity, no selector. -->
|
|
6
|
+
<NuxtLink to="/overview" class="flex min-w-0 items-center gap-3">
|
|
7
|
+
<!-- The logo, or the initials of the commune when it has none. -->
|
|
8
|
+
<avatar-circular
|
|
9
|
+
:place-holder="workspaceStore.workspaceName || 'C'"
|
|
10
|
+
:media="workspaceStore.workspaceLogo"
|
|
11
|
+
size="sm"
|
|
12
|
+
/>
|
|
13
|
+
<span class="truncate font-serif text-[15px] font-semibold text-highlighted plain:font-sans plain:text-base plain:font-medium plain:text-default">
|
|
14
|
+
{{ workspaceStore.workspaceName }}
|
|
15
|
+
</span>
|
|
16
|
+
</NuxtLink>
|
|
17
|
+
|
|
18
|
+
<div class="flex shrink-0 items-center gap-2">
|
|
19
|
+
<dropdown-publish v-if="displayPublishButton" />
|
|
20
|
+
<!-- Help stays in sight, beside the account, whenever the instance
|
|
21
|
+
has an address for it. -->
|
|
22
|
+
<UButton
|
|
23
|
+
v-if="supportUrl"
|
|
24
|
+
:to="supportUrl"
|
|
25
|
+
target="_blank"
|
|
26
|
+
rel="noopener"
|
|
27
|
+
icon="i-lucide-message-circle-question"
|
|
28
|
+
color="neutral"
|
|
29
|
+
variant="outline"
|
|
30
|
+
square
|
|
31
|
+
class="size-9 justify-center"
|
|
32
|
+
aria-label="Demander de l'aide"
|
|
33
|
+
/>
|
|
34
|
+
<layout-shell-account-menu />
|
|
35
|
+
</div>
|
|
36
|
+
</div>
|
|
37
|
+
</UContainer>
|
|
38
|
+
|
|
39
|
+
<div v-if="navItems.length">
|
|
40
|
+
<UContainer class="max-w-7xl">
|
|
41
|
+
<UNavigationMenu
|
|
42
|
+
:items="navItems"
|
|
43
|
+
color="neutral"
|
|
44
|
+
highlight
|
|
45
|
+
highlight-color="neutral"
|
|
46
|
+
class="hidden sm:flex"
|
|
47
|
+
:ui="{
|
|
48
|
+
// Spaced as the frozen admin had them, in both looks. A commune
|
|
49
|
+
// with many collections scrolls its bar rather than truncating a
|
|
50
|
+
// label. The mark under the current tab is inset by the link's own
|
|
51
|
+
// padding, so it spans the text.
|
|
52
|
+
list: 'overflow-x-auto',
|
|
53
|
+
item: 'shrink-0 mr-6',
|
|
54
|
+
link: 'px-1 py-1.5 after:inset-x-1 text-sm font-medium hover:text-highlighted plain:font-light plain:tracking-wide plain:aria-[current=page]:font-normal',
|
|
55
|
+
}"
|
|
56
|
+
/>
|
|
57
|
+
</UContainer>
|
|
58
|
+
</div>
|
|
59
|
+
</header>
|
|
60
|
+
</template>
|
|
61
|
+
|
|
62
|
+
<script setup>
|
|
63
|
+
import { useWorkspaceStore } from '~/store/layout/workspace'
|
|
64
|
+
import avatarCircular from '~/components/elements/avatars/avatar-circular'
|
|
65
|
+
import dropdownPublish from '~/components/layout/navigations/primary/dropdown-publish'
|
|
66
|
+
|
|
67
|
+
const { Collection } = useModels()
|
|
68
|
+
const $ability = useAbility()
|
|
69
|
+
const $route = useRoute()
|
|
70
|
+
|
|
71
|
+
const workspaceStore = useWorkspaceStore()
|
|
72
|
+
const { public: { supportUrl } } = useRuntimeConfig()
|
|
73
|
+
|
|
74
|
+
// Single tenant: the Organisations and Roles screens have no object any more
|
|
75
|
+
// (one commune, fixed roles), so they are not in the bar.
|
|
76
|
+
const displayPublishButton = computed(() => !!workspaceStore.workspaceId
|
|
77
|
+
&& ($ability.can('manage:all')
|
|
78
|
+
|| $ability.can('admin:organization')
|
|
79
|
+
|| $ability.can('create:deployments'))
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
const navItems = computed(() => {
|
|
83
|
+
if (!workspaceStore.workspaceId) return []
|
|
84
|
+
const collections = Collection.repo.find(workspaceStore.workspaceCollections)
|
|
85
|
+
const items = [
|
|
86
|
+
{ label: 'Accueil', to: '/overview', active: $route.path === '/overview' },
|
|
87
|
+
...collections.map(({ name, slug }) => ({
|
|
88
|
+
label: name,
|
|
89
|
+
to: `/${slug}`,
|
|
90
|
+
active: slug === $route.params.collection,
|
|
91
|
+
})),
|
|
92
|
+
]
|
|
93
|
+
// Settings sit at the far end, for whoever may administer.
|
|
94
|
+
if ($ability.can('admin:organization')) {
|
|
95
|
+
items.push({ label: 'Paramètres', to: '/settings', active: $route.path.startsWith('/settings') })
|
|
96
|
+
}
|
|
97
|
+
return [items]
|
|
98
|
+
})
|
|
99
|
+
</script>
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { useMutation, useQuery, useQueryClient } from '@tanstack/vue-query'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* API tokens — what a theme's build uses to read the content.
|
|
5
|
+
*
|
|
6
|
+
* They are stored HASHED: the plaintext exists once, in the create response.
|
|
7
|
+
* A deliberate difference from the legacy, which kept them in clear and could
|
|
8
|
+
* therefore show them again.
|
|
9
|
+
*
|
|
10
|
+
* A write owns everything around it — its invalidation AND its toast, success
|
|
11
|
+
* or failure. It is a mutation factory: the screen calls it in its setup, then
|
|
12
|
+
* `mutate`, and has nothing left to write.
|
|
13
|
+
*/
|
|
14
|
+
export default function useApiTokens() {
|
|
15
|
+
const trpc = useTrpc()
|
|
16
|
+
const queryClient = useQueryClient()
|
|
17
|
+
|
|
18
|
+
const invalidate = () => queryClient.invalidateQueries({ queryKey: ['api-tokens'] })
|
|
19
|
+
|
|
20
|
+
/** The tokens in service — a revoked one has no business on screen. */
|
|
21
|
+
const list = () =>
|
|
22
|
+
useQuery({
|
|
23
|
+
queryKey: ['api-tokens'],
|
|
24
|
+
queryFn: async () => (await trpc.apiTokens.list.query()).filter(({ revokedAt }) => !revokedAt),
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Creates one, and SHOWS it.
|
|
29
|
+
*
|
|
30
|
+
* The plaintext exists ONLY in this answer: the server keeps a hash and can
|
|
31
|
+
* never give it back. So it is said at the moment it exists, said to be the
|
|
32
|
+
* only time, and the toast never expires — closing it is the agent's move,
|
|
33
|
+
* not a timer's.
|
|
34
|
+
*/
|
|
35
|
+
const create = () => {
|
|
36
|
+
const toast = useToast()
|
|
37
|
+
return useMutation({
|
|
38
|
+
mutationFn: (name: string) => trpc.apiTokens.create.mutate({ name }),
|
|
39
|
+
onSuccess: ({ token }) => {
|
|
40
|
+
invalidate()
|
|
41
|
+
toast.add({
|
|
42
|
+
color: 'success',
|
|
43
|
+
title: 'Token créé',
|
|
44
|
+
description: `${token} — copiez-le maintenant, il ne sera plus affiché.`,
|
|
45
|
+
duration: 0,
|
|
46
|
+
})
|
|
47
|
+
},
|
|
48
|
+
onError: (_error, name) => toast.add({
|
|
49
|
+
color: 'error',
|
|
50
|
+
title: `Le token « ${name} » n'a pas pu être créé`,
|
|
51
|
+
description: 'Le serveur a refusé la création. Réessayez dans un instant.',
|
|
52
|
+
}),
|
|
53
|
+
})
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Revokes one, and says so either way: a refused revocation leaves a live
|
|
58
|
+
* token behind a screen that would otherwise show nothing.
|
|
59
|
+
*
|
|
60
|
+
* The NAME travels beside the id for the sentence alone — « le token » tout
|
|
61
|
+
* court ne dit pas lequel est resté actif.
|
|
62
|
+
*/
|
|
63
|
+
const revoke = () => {
|
|
64
|
+
const toast = useToast()
|
|
65
|
+
return useMutation({
|
|
66
|
+
mutationFn: ({ id }: { id: string; name: string }) => trpc.apiTokens.revoke.mutate({ id }),
|
|
67
|
+
onSuccess: (_result, { name }) => {
|
|
68
|
+
invalidate()
|
|
69
|
+
toast.add({ title: `Token « ${name} » révoqué.` })
|
|
70
|
+
},
|
|
71
|
+
onError: (_error, { name }) => toast.add({
|
|
72
|
+
color: 'error',
|
|
73
|
+
title: `Le token « ${name} » n'a pas pu être révoqué`,
|
|
74
|
+
description: 'Il reste actif. Réessayez, et prévenez un administrateur si cela persiste.',
|
|
75
|
+
}),
|
|
76
|
+
})
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return { list, create, revoke }
|
|
80
|
+
}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { useMutation, useQuery, useQueryClient } from '@tanstack/vue-query'
|
|
2
|
+
import type { PublicUser } from '@open-commune/core'
|
|
3
|
+
|
|
4
|
+
/** Prefix invalidated by every write on the sessions of this member. */
|
|
5
|
+
const SESSIONS_KEY = ['auth', 'sessions']
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The entrance flows that need no session (accepting an invitation, asking for
|
|
9
|
+
* a password reset, defining the new password), and what a signed-in member
|
|
10
|
+
* changes on their own account: their name, their open sessions.
|
|
11
|
+
*
|
|
12
|
+
* A write owns what surrounds it, whole: its `catch`, its `is…` pending flag,
|
|
13
|
+
* the sentence it puts on screen and the redirection that follows — none of
|
|
14
|
+
* which a screen should have to repeat. It answers whether it went through
|
|
15
|
+
* instead of throwing, and the caller has nothing left to do but call it.
|
|
16
|
+
*/
|
|
17
|
+
export default function useAuth() {
|
|
18
|
+
const trpc = useTrpc()
|
|
19
|
+
|
|
20
|
+
// Created per `useAuth()`, so two screens never share a spinner.
|
|
21
|
+
const isAcceptingInvitation = ref(false)
|
|
22
|
+
const isDefiningPassword = ref(false)
|
|
23
|
+
const isRequestingPasswordReset = ref(false)
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* One public step. The account exists once this returns: the sign-in screen
|
|
27
|
+
* is where it is used, so that is where it lands.
|
|
28
|
+
*/
|
|
29
|
+
const acceptInvitation = async (input: { token: string; name: string; password: string }): Promise<boolean> => {
|
|
30
|
+
const toast = useToast()
|
|
31
|
+
isAcceptingInvitation.value = true
|
|
32
|
+
try {
|
|
33
|
+
await trpc.invitations.accept.mutate(input)
|
|
34
|
+
} catch {
|
|
35
|
+
// The password is validated before sending: only the token can be wrong.
|
|
36
|
+
toast.add({
|
|
37
|
+
icon: 'iconoir:password-error',
|
|
38
|
+
color: 'warning',
|
|
39
|
+
title: 'Cette invitation n\'est plus valide.',
|
|
40
|
+
description: 'Demandez-en une nouvelle à un administrateur.',
|
|
41
|
+
})
|
|
42
|
+
return false
|
|
43
|
+
} finally {
|
|
44
|
+
isAcceptingInvitation.value = false
|
|
45
|
+
}
|
|
46
|
+
toast.add({ icon: 'iconoir:password-cursor', color: 'success', title: 'Compte activé, connectez-vous.' })
|
|
47
|
+
await navigateTo('/')
|
|
48
|
+
return true
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* The answer is the same whether the address exists or not: otherwise the
|
|
53
|
+
* screen becomes a directory of accounts. No redirection — the screen that
|
|
54
|
+
* asked stays, and says the mail is gone.
|
|
55
|
+
*/
|
|
56
|
+
const requestPasswordReset = async (email: string): Promise<boolean> => {
|
|
57
|
+
const toast = useToast()
|
|
58
|
+
isRequestingPasswordReset.value = true
|
|
59
|
+
try {
|
|
60
|
+
await trpc.invitations.requestPasswordReset.mutate({ email })
|
|
61
|
+
toast.add({
|
|
62
|
+
icon: 'iconoir:mail-out',
|
|
63
|
+
color: 'success',
|
|
64
|
+
title: 'Email envoyé.',
|
|
65
|
+
description: `Le lien part vers ${email}.`,
|
|
66
|
+
})
|
|
67
|
+
return true
|
|
68
|
+
} catch {
|
|
69
|
+
toast.add({
|
|
70
|
+
icon: 'iconoir:warning-triangle',
|
|
71
|
+
color: 'warning',
|
|
72
|
+
title: 'La demande n\'a pas pu être envoyée.',
|
|
73
|
+
description: 'Réessayez dans quelques minutes.',
|
|
74
|
+
})
|
|
75
|
+
return false
|
|
76
|
+
} finally {
|
|
77
|
+
isRequestingPasswordReset.value = false
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* The reset mail's link lands on /password/define/<token>: the same
|
|
83
|
+
* acceptance, without the name — the server already holds it.
|
|
84
|
+
*/
|
|
85
|
+
const definePassword = async (input: { token: string; password: string }): Promise<boolean> => {
|
|
86
|
+
const toast = useToast()
|
|
87
|
+
isDefiningPassword.value = true
|
|
88
|
+
try {
|
|
89
|
+
await trpc.invitations.accept.mutate(input)
|
|
90
|
+
} catch {
|
|
91
|
+
// The length is validated before sending: only the link can be wrong.
|
|
92
|
+
toast.add({
|
|
93
|
+
icon: 'iconoir:password-error',
|
|
94
|
+
color: 'warning',
|
|
95
|
+
title: 'Ce lien a expiré ou a déjà été utilisé.',
|
|
96
|
+
description: 'Demandez-en un nouveau depuis « mot de passe oublié ».',
|
|
97
|
+
})
|
|
98
|
+
return false
|
|
99
|
+
} finally {
|
|
100
|
+
isDefiningPassword.value = false
|
|
101
|
+
}
|
|
102
|
+
toast.add({ icon: 'iconoir:password-cursor', color: 'success', title: 'Mot de passe mis à jour, connectez-vous.' })
|
|
103
|
+
await navigateTo('/')
|
|
104
|
+
return true
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* A member renames themself. Not their email — it identifies the account —
|
|
109
|
+
* nor their role, which the server's DTO REFUSES rather than ignores.
|
|
110
|
+
*
|
|
111
|
+
* The session carries the name shown everywhere else, so it follows here:
|
|
112
|
+
* every caller would otherwise have to remember to keep it in step.
|
|
113
|
+
*/
|
|
114
|
+
const updateOwnProfile = () =>
|
|
115
|
+
useMutation({
|
|
116
|
+
mutationFn: async (name: string): Promise<PublicUser> =>
|
|
117
|
+
(await trpc.users.updateOwnProfile.mutate({ name })).user,
|
|
118
|
+
onSuccess: (user) => { useSession().user.value = user },
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* This member's open sessions — the current one is flagged. A query and not
|
|
123
|
+
* a bare call: the list carries its own `isLoading`, and `revoke` invalidates
|
|
124
|
+
* it, so no screen reloads it by hand. Call it from a setup.
|
|
125
|
+
*/
|
|
126
|
+
const sessions = () => {
|
|
127
|
+
const queryClient = useQueryClient()
|
|
128
|
+
|
|
129
|
+
const query = useQuery({
|
|
130
|
+
queryKey: SESSIONS_KEY,
|
|
131
|
+
queryFn: () => trpc.auth.sessions.list.query(),
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
/** Closes one session remotely. The server refuses other people's. */
|
|
135
|
+
const toast = useToast()
|
|
136
|
+
const { mutate: revoke, isPending: isRevoking } = useMutation({
|
|
137
|
+
mutationFn: (id: string) => trpc.auth.sessions.revoke.mutate({ id }),
|
|
138
|
+
onSuccess: async () => {
|
|
139
|
+
await queryClient.invalidateQueries({ queryKey: SESSIONS_KEY })
|
|
140
|
+
toast.add({ title: 'Session fermée.' })
|
|
141
|
+
},
|
|
142
|
+
onError: () => toast.add({
|
|
143
|
+
color: 'error',
|
|
144
|
+
title: 'La session n\'a pas pu être fermée.',
|
|
145
|
+
description: 'Réessayez dans un instant.',
|
|
146
|
+
}),
|
|
147
|
+
})
|
|
148
|
+
|
|
149
|
+
return { ...query, revoke, isRevoking }
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return {
|
|
153
|
+
updateOwnProfile,
|
|
154
|
+
sessions,
|
|
155
|
+
acceptInvitation,
|
|
156
|
+
isAcceptingInvitation: readonly(isAcceptingInvitation),
|
|
157
|
+
requestPasswordReset,
|
|
158
|
+
isRequestingPasswordReset: readonly(isRequestingPasswordReset),
|
|
159
|
+
definePassword,
|
|
160
|
+
isDefiningPassword: readonly(isDefiningPassword),
|
|
161
|
+
}
|
|
162
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { useMutation, useQuery, useQueryClient } from '@tanstack/vue-query'
|
|
2
2
|
import { buildDataSchema, type FieldDefinition } from '@open-commune/core/collections/utils'
|
|
3
|
-
import { computed, unref, type MaybeRef } from 'vue'
|
|
3
|
+
import { computed, toValue, unref, type MaybeRef, type MaybeRefOrGetter } from 'vue'
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Collection-definition domain composable — the only data surface pages use.
|
|
@@ -18,15 +18,28 @@ export default function useDefinitions() {
|
|
|
18
18
|
queryFn: () => trpc.collections.list.query(),
|
|
19
19
|
})
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
/**
|
|
22
|
+
* One definition, by id or by slug: the server takes either. A getter is
|
|
23
|
+
* welcome, because a page is reused from one collection to the next. While
|
|
24
|
+
* it travels, the list already in cache answers, so a screen that opens on
|
|
25
|
+
* a collection the navigation has listed never waits.
|
|
26
|
+
*/
|
|
27
|
+
const get = (idOrSlug: MaybeRefOrGetter<string | undefined>) =>
|
|
22
28
|
useQuery({
|
|
23
|
-
queryKey: computed(() => ['definitions',
|
|
24
|
-
queryFn: () => trpc.collections.get.query({ idOrSlug:
|
|
29
|
+
queryKey: computed(() => ['definitions', toValue(idOrSlug)]),
|
|
30
|
+
queryFn: () => trpc.collections.get.query({ idOrSlug: toValue(idOrSlug)! }),
|
|
31
|
+
enabled: computed(() => Boolean(toValue(idOrSlug))),
|
|
32
|
+
placeholderData: () =>
|
|
33
|
+
queryClient
|
|
34
|
+
.getQueryData<Awaited<ReturnType<typeof trpc.collections.list.query>>>(['definitions'])
|
|
35
|
+
?.find(({ id, slug }) => [id, slug].includes(toValue(idOrSlug)!)),
|
|
25
36
|
})
|
|
26
37
|
|
|
38
|
+
// A write invalidates the list: the settings' collection table refreshes
|
|
39
|
+
// through its key. Call the factory in a setup, then `mutateAsync`.
|
|
27
40
|
const create = () =>
|
|
28
41
|
useMutation({
|
|
29
|
-
mutationFn: (input:
|
|
42
|
+
mutationFn: (input: Parameters<typeof trpc.collections.create.mutate>[0]) => trpc.collections.create.mutate(input),
|
|
30
43
|
onSuccess: invalidate,
|
|
31
44
|
})
|
|
32
45
|
|
|
@@ -1,5 +1,34 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import type { PublicationStatus } from '@open-commune/core/collections/constants'
|
|
2
|
+
import { keepPreviousData, useInfiniteQuery, useMutation, useQueries, useQuery, useQueryClient } from '@tanstack/vue-query'
|
|
3
|
+
import { computed, toValue, unref, type MaybeRef, type MaybeRefOrGetter } from 'vue'
|
|
4
|
+
|
|
5
|
+
type ListOptions = {
|
|
6
|
+
status?: PublicationStatus[]
|
|
7
|
+
search?: string
|
|
8
|
+
skip?: number
|
|
9
|
+
limit?: number
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** What NARROWS a listing, as opposed to what windows it. */
|
|
13
|
+
type Filters = Omit<ListOptions, 'skip' | 'limit'>
|
|
14
|
+
|
|
15
|
+
type Entry = Record<string, unknown>
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The filters alone, the window dropped. The count is taken under them and
|
|
19
|
+
* only them, or the total would describe another set than the rows beside it.
|
|
20
|
+
*/
|
|
21
|
+
const filtersOf = ({ skip: _skip, limit: _limit, ...filters }: ListOptions): Filters => filters
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* An entry has columns (title, slug, status…) and a bag of `data` holding the
|
|
25
|
+
* fields its collection declares. A listing sees one flat object.
|
|
26
|
+
*/
|
|
27
|
+
const flatten = (entry: Entry | null | undefined) => {
|
|
28
|
+
if (!entry) return entry
|
|
29
|
+
const { data, ...columns } = entry as { data?: Entry }
|
|
30
|
+
return { ...columns, ...data }
|
|
31
|
+
}
|
|
3
32
|
|
|
4
33
|
/**
|
|
5
34
|
* Entries domain composable. Keys: ['entries', collectionId] for lists,
|
|
@@ -30,6 +59,80 @@ export default function useEntries() {
|
|
|
30
59
|
enabled: computed(() => Boolean(unref(collectionId))),
|
|
31
60
|
})
|
|
32
61
|
|
|
62
|
+
/**
|
|
63
|
+
* A listing that grows: TanStack's infinite query holds the slices, fetches
|
|
64
|
+
* the next one, and refetches them all when a write invalidates the
|
|
65
|
+
* collection. Collection and filters are in the key, so moving either
|
|
66
|
+
* starts the window over; the rows on screen are kept while the next ones
|
|
67
|
+
* travel, which lets `isPending` mean the first load and nothing else.
|
|
68
|
+
*
|
|
69
|
+
* The total is the server's own count under the same filters, and only
|
|
70
|
+
* them, or it would describe another set than the rows beside it.
|
|
71
|
+
*/
|
|
72
|
+
const infinite = (
|
|
73
|
+
collectionId: MaybeRefOrGetter<string | undefined>,
|
|
74
|
+
options: MaybeRefOrGetter<Filters & { limit?: number }> = {},
|
|
75
|
+
) => {
|
|
76
|
+
const id = computed(() => toValue(collectionId))
|
|
77
|
+
const enabled = computed(() => Boolean(id.value))
|
|
78
|
+
const limit = computed(() => toValue(options).limit ?? 20)
|
|
79
|
+
const filters = computed(() => filtersOf(toValue(options)))
|
|
80
|
+
|
|
81
|
+
const pages = useInfiniteQuery({
|
|
82
|
+
queryKey: computed(() => ['entries', id.value, 'pages', filters.value, limit.value]),
|
|
83
|
+
queryFn: async ({ pageParam }) =>
|
|
84
|
+
(await trpc.collections.entries.list.query({
|
|
85
|
+
collectionId: id.value as string,
|
|
86
|
+
...filters.value,
|
|
87
|
+
skip: pageParam,
|
|
88
|
+
limit: limit.value,
|
|
89
|
+
})).map(flatten) as Entry[],
|
|
90
|
+
initialPageParam: 0,
|
|
91
|
+
// A slice shorter than asked is the last one.
|
|
92
|
+
getNextPageParam: (last, all) => (last.length < limit.value ? undefined : all.flat().length),
|
|
93
|
+
enabled,
|
|
94
|
+
placeholderData: keepPreviousData,
|
|
95
|
+
})
|
|
96
|
+
const counted = useQuery({
|
|
97
|
+
queryKey: computed(() => ['entries', id.value, 'count', filters.value]),
|
|
98
|
+
queryFn: () => trpc.collections.entries.count.query({ collectionId: id.value as string, ...filters.value }),
|
|
99
|
+
enabled,
|
|
100
|
+
placeholderData: keepPreviousData,
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
const records = computed(() => pages.data.value?.pages.flat() ?? [])
|
|
104
|
+
const total = computed(() => counted.data.value ?? null)
|
|
105
|
+
return {
|
|
106
|
+
records,
|
|
107
|
+
total,
|
|
108
|
+
isPending: computed(() => pages.isPending.value || counted.isPending.value),
|
|
109
|
+
isFetching: pages.isFetching,
|
|
110
|
+
isFetchingNextPage: pages.isFetchingNextPage,
|
|
111
|
+
// The count settles the one case the slices cannot: a total that is an
|
|
112
|
+
// exact multiple of the slice.
|
|
113
|
+
hasNextPage: computed(() => Boolean(pages.hasNextPage.value) && (total.value === null || records.value.length < total.value)),
|
|
114
|
+
fetchNextPage: pages.fetchNextPage,
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Several entries by id: a relation column shows titles, the server sends
|
|
120
|
+
* ids. Same keys as `get`, so an entry is not asked for twice.
|
|
121
|
+
*/
|
|
122
|
+
const getMany = (ids: MaybeRefOrGetter<string[] | string | undefined>) => {
|
|
123
|
+
const wanted = computed(() => {
|
|
124
|
+
const value = toValue(ids)
|
|
125
|
+
return (Array.isArray(value) ? value : [value]).filter(Boolean) as string[]
|
|
126
|
+
})
|
|
127
|
+
return useQueries({
|
|
128
|
+
queries: computed(() => wanted.value.map(id => ({
|
|
129
|
+
queryKey: ['entries', 'byId', id],
|
|
130
|
+
queryFn: () => trpc.collections.entries.get.query({ id }),
|
|
131
|
+
}))),
|
|
132
|
+
combine: results => results.map(({ data }) => data).filter(Boolean) as Entry[],
|
|
133
|
+
})
|
|
134
|
+
}
|
|
135
|
+
|
|
33
136
|
const get = (id: MaybeRef<string>) =>
|
|
34
137
|
useQuery({
|
|
35
138
|
queryKey: computed(() => ['entries', 'byId', unref(id)]),
|
|
@@ -58,5 +161,12 @@ export default function useEntries() {
|
|
|
58
161
|
onSuccess: (_result, { collectionId }) => invalidateCollection(collectionId),
|
|
59
162
|
})
|
|
60
163
|
|
|
61
|
-
|
|
164
|
+
/** How many entries a collection holds, counted on the server. */
|
|
165
|
+
const count = (collectionId: string) =>
|
|
166
|
+
queryClient.fetchQuery({
|
|
167
|
+
queryKey: ['entries', 'count', collectionId],
|
|
168
|
+
queryFn: () => trpc.collections.entries.count.query({ collectionId }),
|
|
169
|
+
})
|
|
170
|
+
|
|
171
|
+
return { list, infinite, get, getMany, count, create, update, remove, invalidateCollection }
|
|
62
172
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { OpenCommuneEditorMedia } from '@open-commune/editor'
|
|
2
|
+
import { readonly, ref, toValue, watch, type MaybeRefOrGetter } from 'vue'
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* Media domain composable: tRPC calls plus the direct-to-S3 upload step.
|
|
@@ -78,5 +79,27 @@ export default function useMedia() {
|
|
|
78
79
|
/** The upload/resolve pair in the shape the editor extensions expect. */
|
|
79
80
|
const forEditor = (): OpenCommuneEditorMedia => ({ upload, fetch: resolve })
|
|
80
81
|
|
|
81
|
-
|
|
82
|
+
/**
|
|
83
|
+
* A media followed reactively: turning an id into an image took the same
|
|
84
|
+
* ref, watch and catch in every component that shows one. The source is a
|
|
85
|
+
* ref or a getter, so a thumbnail changes media without being remounted.
|
|
86
|
+
*/
|
|
87
|
+
const asset = (source: MaybeRefOrGetter<string | undefined>) => {
|
|
88
|
+
const src = ref('')
|
|
89
|
+
const title = ref('')
|
|
90
|
+
|
|
91
|
+
watch(() => toValue(source), async (id) => {
|
|
92
|
+
if (!id) {
|
|
93
|
+
src.value = title.value = ''
|
|
94
|
+
return
|
|
95
|
+
}
|
|
96
|
+
const media = await resolve({ id }).catch(() => undefined)
|
|
97
|
+
src.value = media?.src ?? ''
|
|
98
|
+
title.value = media?.title ?? ''
|
|
99
|
+
}, { immediate: true })
|
|
100
|
+
|
|
101
|
+
return { src: readonly(src), title: readonly(title) }
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return { upload, resolve, asset, forEditor }
|
|
82
105
|
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { useMutation, useQuery, useQueryClient } from '@tanstack/vue-query'
|
|
2
|
+
import { computed, toValue, type MaybeRefOrGetter } from 'vue'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Members: who reaches this administration space, and with which role.
|
|
6
|
+
*
|
|
7
|
+
* There is no direct creation — one INVITES. The server sends the mail and
|
|
8
|
+
* returns the link, which stays the fallback when the mail does not arrive.
|
|
9
|
+
*/
|
|
10
|
+
export default function useMembers() {
|
|
11
|
+
const trpc = useTrpc()
|
|
12
|
+
const queryClient = useQueryClient()
|
|
13
|
+
|
|
14
|
+
const invalidate = () => queryClient.invalidateQueries({ queryKey: ['members'] })
|
|
15
|
+
|
|
16
|
+
const list = () =>
|
|
17
|
+
useQuery({ queryKey: ['members'], queryFn: () => trpc.users.list.query() })
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Invitations sent and not yet accepted. Nothing else shows them: an invited
|
|
21
|
+
* person becomes a member only once they have opened their link.
|
|
22
|
+
*/
|
|
23
|
+
const pending = () =>
|
|
24
|
+
useQuery({
|
|
25
|
+
queryKey: ['members', 'pending'],
|
|
26
|
+
queryFn: () => trpc.invitations.list.query(),
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Who is who, readable by any signed-in member: a listing shows the name of
|
|
31
|
+
* whoever last touched an entry, and the server sends an id.
|
|
32
|
+
*/
|
|
33
|
+
const directory = () =>
|
|
34
|
+
useQuery({
|
|
35
|
+
queryKey: ['members', 'directory'],
|
|
36
|
+
queryFn: () => trpc.users.directory.query(),
|
|
37
|
+
staleTime: 5 * 60 * 1000,
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
// Writes are mutation factories: call one in a setup, then `mutateAsync`.
|
|
41
|
+
const invite = () =>
|
|
42
|
+
useMutation({
|
|
43
|
+
mutationFn: ({ email, role = 'redacteur' }: { email: string; role?: 'admin' | 'redacteur' }) =>
|
|
44
|
+
trpc.invitations.create.mutate({ email, role }),
|
|
45
|
+
onSuccess: invalidate,
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
const revokeInvitation = () =>
|
|
49
|
+
useMutation({
|
|
50
|
+
mutationFn: (id: string) => trpc.invitations.revoke.mutate({ id }),
|
|
51
|
+
onSuccess: invalidate,
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
const update = () =>
|
|
55
|
+
useMutation({
|
|
56
|
+
mutationFn: ({ id, data }: { id: string; data: { name?: string; role?: 'admin' | 'redacteur' } }) =>
|
|
57
|
+
trpc.users.update.mutate({ id, data }),
|
|
58
|
+
onSuccess: invalidate,
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
const remove = () =>
|
|
62
|
+
useMutation({
|
|
63
|
+
mutationFn: (id: string) => trpc.users.remove.mutate({ id }),
|
|
64
|
+
onSuccess: invalidate,
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
return { list, pending, directory, invite, update, remove, revokeInvitation }
|
|
68
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { useMutation, useQuery, useQueryClient } from '@tanstack/vue-query'
|
|
2
|
+
import { useWorkspaceStore } from '~/store/layout/workspace'
|
|
3
|
+
|
|
4
|
+
/** What the settings screen may change; the rest is derived. */
|
|
5
|
+
const UPDATABLE = ['name', 'type', 'slug', 'description', 'address', 'deployment', 'settings'] as const
|
|
6
|
+
|
|
7
|
+
type Organization = Awaited<ReturnType<ReturnType<typeof useTrpc>['organization']['get']['query']>>
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The organization — there is exactly one per instance (single-tenant).
|
|
11
|
+
*
|
|
12
|
+
* The logo is stored in `settings.logo`; the settings form edits it as a field
|
|
13
|
+
* of its own, `logo`, so it is lifted out on read and put back on write.
|
|
14
|
+
*/
|
|
15
|
+
export default function useOrganization() {
|
|
16
|
+
const trpc = useTrpc()
|
|
17
|
+
const queryClient = useQueryClient()
|
|
18
|
+
|
|
19
|
+
const query = () =>
|
|
20
|
+
useQuery({
|
|
21
|
+
queryKey: ['organization'],
|
|
22
|
+
queryFn: () => trpc.organization.get.query(),
|
|
23
|
+
select: (organization: Organization) => organization && {
|
|
24
|
+
...organization,
|
|
25
|
+
logo: (organization.settings as { logo?: unknown } | null)?.logo ?? null,
|
|
26
|
+
},
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
/** A mutation factory: call it in a setup, then `mutateAsync(changes)`. */
|
|
30
|
+
const update = () =>
|
|
31
|
+
useMutation({
|
|
32
|
+
mutationFn: (changes: Record<string, unknown>) => {
|
|
33
|
+
const payload: Record<string, unknown> = {}
|
|
34
|
+
for (const key of UPDATABLE) {
|
|
35
|
+
if (changes[key] !== undefined) payload[key] = changes[key]
|
|
36
|
+
}
|
|
37
|
+
if (changes.logo !== undefined) {
|
|
38
|
+
const current = queryClient.getQueryData<Organization>(['organization'])
|
|
39
|
+
payload.settings = { ...(current?.settings ?? {}), ...((payload.settings as object) ?? {}), logo: changes.logo }
|
|
40
|
+
}
|
|
41
|
+
return trpc.organization.update.mutate(payload)
|
|
42
|
+
},
|
|
43
|
+
onSuccess: async () => {
|
|
44
|
+
await queryClient.invalidateQueries({ queryKey: ['organization'] })
|
|
45
|
+
// The shell still reads the organization from the Pinia store, until the
|
|
46
|
+
// store goes (A8): it is refreshed here so a new name or logo shows at once.
|
|
47
|
+
const [legacy] = await useModels().Organization.list()
|
|
48
|
+
if (legacy) useWorkspaceStore().organization = legacy
|
|
49
|
+
},
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
return { query, update }
|
|
53
|
+
}
|