Files
Przemyslaw Kukulski bb7ba42b90 fix: small template marketplace fixes (#6916)
* fix: small template marketplace fixes

* Review feedback
2026-11-30 21:08:04 +01:00

132 lines
3.5 KiB
JavaScript

import { toRaw } from 'vue'
/**
* Whether the template is one of the user's own templates, as told by the server.
*/
export function isOwnTemplate(template) {
return template.is_own === true
}
export function canMakePublic(template) {
return Boolean(template?.can_make_public)
}
export function makePublicResubmits(template) {
return (
canMakePublic(template) &&
Boolean(template?.make_public_needs_review) &&
Boolean(template?.listed)
)
}
export function canMakePrivate(template) {
if (template?.state === 'rejected') {
return Boolean(template.listed)
}
return ['public', 'in_review'].includes(template?.state)
}
export function getPublicAgainKey(template, requireApproval) {
return requireApproval || ['rejected', 'in_review'].includes(template?.state)
? 'updateUserTemplateModal.publicAgainNeedsApproval'
: 'updateUserTemplateModal.publicAgainAnyTime'
}
export function canChangeVisibilityInDetails(template) {
return ['private', 'public'].includes(template?.state)
}
export function canInstall(template, isStaff) {
return !(template?.state === 'blocked' && isOwnTemplate(template) && !isStaff)
}
/**
* The private link of the user's template, or null without one. The link of a
* blocked template can't be used.
*
* @param {object} template The template as listed in the user's templates, whose
* `private_link_token` is only shown to them.
* @param {object} router The router of the app.
* @param {string} baseUrl The URL of the web frontend.
*/
export function getPrivateLinkUrl(template, router, baseUrl) {
if (!template.private_link_token || template.state === 'blocked') {
return null
}
const { href } = router.resolve({
name: 'template-private-link',
params: { slug: template.slug, token: template.private_link_token },
})
return new URL(href, baseUrl).toString()
}
/**
* Whether a template is listed for everybody: an official one, or a user template
* the server says is listed.
*/
export function isListedTemplate(template) {
return (
(template?.type || 'official') === 'official' || template.listed === true
)
}
/**
* 'sharing', 'private' or null. Nothing for a user template while user templates
* are turned off.
*/
export function getCopyLinkKind(
template,
{ userTemplatesAllowed = true } = {}
) {
if (template?.state === 'blocked') {
return null
}
if ((template?.type || 'official') !== 'official' && !userTemplatesAllowed) {
return null
}
if (isListedTemplate(template)) {
return 'sharing'
}
return template?.listed === false && template.private_link_token
? 'private'
: null
}
export function getTemplateLinkUrl(template, router, baseUrl) {
const { href } = router.resolve({
name: 'template',
params: { slug: template.slug },
})
return new URL(href, baseUrl).toString()
}
export function getCopyLinkItem(kind, template, router, baseUrl) {
if (kind === 'private') {
return {
label: 'templateInstall.copyPrivateLink',
url: getPrivateLinkUrl(template, router, baseUrl),
}
}
if (kind === 'sharing') {
return {
label: 'templateInstall.copyLink',
url: getTemplateLinkUrl(template, router, baseUrl),
}
}
return null
}
const notifiedJobs = new WeakMap()
/**
* Runs `notification` once per job, however many modals follow it, and returns
* what its first run returned.
*/
export function notifyOncePerJob(job, notification) {
const key = toRaw(job)
if (!notifiedJobs.has(key)) {
notifiedJobs.set(key, notification())
}
return notifiedJobs.get(key)
}