feat: use global constant for demo mode

This commit is contained in:
Phan An 2024-02-28 11:37:27 +07:00
parent e9695495c9
commit 081aedb51f
13 changed files with 24 additions and 25 deletions

View file

@ -6,7 +6,7 @@ use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\File;
use Throwable; use Throwable;
class FetchDemoCreditController extends Controller class FetchDemoCreditsController extends Controller
{ {
public function __invoke() public function __invoke()
{ {

View file

@ -29,8 +29,7 @@
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import { computed, ref } from 'vue' import { ref } from 'vue'
import { isDemo } from '@/utils'
import { authService } from '@/services' import { authService } from '@/services'
import Btn from '@/components/ui/Btn.vue' import Btn from '@/components/ui/Btn.vue'
@ -42,10 +41,10 @@ const DEMO_ACCOUNT = {
password: 'demo' password: 'demo'
} }
const canResetPassword = window.MAILER_CONFIGURED && !isDemo() const canResetPassword = window.MAILER_CONFIGURED && !window.IS_DEMO
const email = ref(isDemo() ? DEMO_ACCOUNT.email : '') const email = ref(window.IS_DEMO ? DEMO_ACCOUNT.email : '')
const password = ref(isDemo() ? DEMO_ACCOUNT.password : '') const password = ref(window.IS_DEMO ? DEMO_ACCOUNT.password : '')
const failed = ref(false) const failed = ref(false)
const showingForgotPasswordForm = ref(false) const showingForgotPasswordForm = ref(false)

View file

@ -32,8 +32,7 @@ new class extends UnitTestCase {
it('shows demo notation', async () => { it('shows demo notation', async () => {
const getMock = this.mock(http, 'get').mockResolvedValue([]) const getMock = this.mock(http, 'get').mockResolvedValue([])
// @ts-ignore window.IS_DEMO = true
import.meta.env.VITE_KOEL_ENV = 'demo'
this.renderComponent() this.renderComponent()
@ -41,6 +40,8 @@ new class extends UnitTestCase {
screen.getByTestId('demo-credits') screen.getByTestId('demo-credits')
expect(getMock).toHaveBeenCalledWith('demo/credits') expect(getMock).toHaveBeenCalledWith('demo/credits')
}) })
window.IS_DEMO = false
}) })
} }
} }

View file

@ -67,7 +67,6 @@
<script lang="ts" setup> <script lang="ts" setup>
import { orderBy } from 'lodash' import { orderBy } from 'lodash'
import { onMounted, ref } from 'vue' import { onMounted, ref } from 'vue'
import { isDemo } from '@/utils'
import { useAuthorization, useKoelPlus, useNewVersionNotification } from '@/composables' import { useAuthorization, useKoelPlus, useNewVersionNotification } from '@/composables'
import { http } from '@/services' import { http } from '@/services'
@ -96,7 +95,7 @@ const emit = defineEmits<{ (e: 'close'): void }>()
const close = () => emit('close') const close = () => emit('close')
onMounted(async () => { onMounted(async () => {
credits.value = isDemo() ? orderBy(await http.get<DemoCredits[]>('demo/credits'), 'name') : null credits.value = window.IS_DEMO ? orderBy(await http.get<DemoCredits[]>('demo/credits'), 'name') : null
}) })
</script> </script>

View file

@ -46,7 +46,7 @@
<div class="form-row"> <div class="form-row">
<Btn class="btn-submit" type="submit">Save</Btn> <Btn class="btn-submit" type="submit">Save</Btn>
<span v-if="isDemo()" class="demo-notice"> <span v-if="isDemo" class="demo-notice">
Changes will not be saved in the demo version. Changes will not be saved in the demo version.
</span> </span>
</div> </div>
@ -57,7 +57,7 @@
import { onMounted, ref } from 'vue' import { onMounted, ref } from 'vue'
import { userStore } from '@/stores' import { userStore } from '@/stores'
import { authService, UpdateCurrentProfileData } from '@/services' import { authService, UpdateCurrentProfileData } from '@/services'
import { isDemo, logger, parseValidationError } from '@/utils' import { logger, parseValidationError } from '@/utils'
import { useDialogBox, useMessageToaster } from '@/composables' import { useDialogBox, useMessageToaster } from '@/composables'
import Btn from '@/components/ui/Btn.vue' import Btn from '@/components/ui/Btn.vue'
@ -67,6 +67,8 @@ const { toastSuccess } = useMessageToaster()
const { showErrorDialog } = useDialogBox() const { showErrorDialog } = useDialogBox()
const profile = ref<UpdateCurrentProfileData>({} as unknown as UpdateCurrentProfileData) const profile = ref<UpdateCurrentProfileData>({} as unknown as UpdateCurrentProfileData)
const isDemo = window.IS_DEMO
onMounted(() => { onMounted(() => {
profile.value = { profile.value = {
name: userStore.current.name, name: userStore.current.name,
@ -80,7 +82,7 @@ const update = async () => {
throw Error() throw Error()
} }
if (isDemo()) { if (isDemo) {
toastSuccess('Profile updated.') toastSuccess('Profile updated.')
return return
} }

View file

@ -72,7 +72,7 @@
<AlbumCard :album="a" layout="compact" /> <AlbumCard :album="a" layout="compact" />
</li> </li>
</ul> </ul>
<p v-else class="text-secondary">No other albums by {{ album.artist_name }} found in the library.</p> <p v-else class="none text-secondary">No other albums by {{ album.artist_name }} found in the library.</p>
</template> </template>
<ul v-else class="as-list"> <ul v-else class="as-list">
<li v-for="i in 12" :key="i"> <li v-for="i in 12" :key="i">

View file

@ -74,6 +74,10 @@
padding: 1.8rem; padding: 1.8rem;
overflow: auto; overflow: auto;
} }
.none {
padding: 1rem 1.8rem;
}
} }
:deep(.info-pane) { :deep(.info-pane) {

View file

@ -1,9 +1,5 @@
/// <reference types="vite/client" /> /// <reference types="vite/client" />
interface ImportMetaEnv {
readonly VITE_KOEL_ENV: 'demo' | undefined
}
interface ImportMeta { interface ImportMeta {
readonly env: ImportMetaEnv readonly env: ImportMetaEnv
} }

View file

@ -58,6 +58,7 @@ interface Constructable<T> {
interface Window { interface Window {
BASE_URL: string BASE_URL: string
MAILER_CONFIGURED: boolean MAILER_CONFIGURED: boolean
IS_DEMO: boolean
readonly PUSHER_APP_KEY: string readonly PUSHER_APP_KEY: string
readonly PUSHER_APP_CLUSTER: string readonly PUSHER_APP_CLUSTER: string

View file

@ -35,8 +35,3 @@ export const copyText = async (text: string) => {
document.execCommand('copy') document.execCommand('copy')
} }
} }
export const isDemo = () => {
// can't use one-liner as it would break production build with an "Unexpected token" error
return import.meta.env.VITE_KOEL_ENV === 'demo'
}

View file

@ -176,6 +176,7 @@ label {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
min-height: 100%; min-height: 100%;
position: relative;
[role=tablist] { [role=tablist] {
border-bottom: 2px solid rgba(255, 255, 255, .1); border-bottom: 2px solid rgba(255, 255, 255, .1);

View file

@ -34,6 +34,7 @@
<script> <script>
window.BASE_URL = @json(asset('')); window.BASE_URL = @json(asset(''));
window.MAILER_CONFIGURED = @json(mailer_configured()); window.MAILER_CONFIGURED = @json(mailer_configured());
window.IS_DEMO = @json(config('koel.misc.demo'));
window.PUSHER_APP_KEY = @json(config('broadcasting.connections.pusher.key')); window.PUSHER_APP_KEY = @json(config('broadcasting.connections.pusher.key'));
window.PUSHER_APP_CLUSTER = @json(config('broadcasting.connections.pusher.options.cluster')); window.PUSHER_APP_CLUSTER = @json(config('broadcasting.connections.pusher.options.cluster'));

View file

@ -13,7 +13,7 @@ use App\Http\Controllers\API\ExcerptSearchController;
use App\Http\Controllers\API\FetchAlbumInformationController; use App\Http\Controllers\API\FetchAlbumInformationController;
use App\Http\Controllers\API\FetchAlbumThumbnailController; use App\Http\Controllers\API\FetchAlbumThumbnailController;
use App\Http\Controllers\API\FetchArtistInformationController; use App\Http\Controllers\API\FetchArtistInformationController;
use App\Http\Controllers\API\FetchDemoCreditController; use App\Http\Controllers\API\FetchDemoCreditsController;
use App\Http\Controllers\API\FetchFavoriteSongsController; use App\Http\Controllers\API\FetchFavoriteSongsController;
use App\Http\Controllers\API\FetchInitialDataController; use App\Http\Controllers\API\FetchInitialDataController;
use App\Http\Controllers\API\FetchOverviewController; use App\Http\Controllers\API\FetchOverviewController;
@ -197,5 +197,5 @@ Route::prefix('api')->middleware('api')->group(static function (): void {
Route::delete('song', [S3SongController::class, 'remove'])->name('s3.song.remove'); // and here. Route::delete('song', [S3SongController::class, 'remove'])->name('s3.song.remove'); // and here.
}); });
Route::get('demo/credits', FetchDemoCreditController::class); Route::get('demo/credits', FetchDemoCreditsController::class);
}); });