mirror of
https://github.com/BetterSEQTA/BetterSEQTA-Plus.git
synced 2026-12-06 19:56:45 +10:30
389 lines
17 KiB
Svelte
389 lines
17 KiB
Svelte
<script lang="ts">
|
|
import type { CustomTheme, ThemeList } from '@/types/CustomThemes'
|
|
import { onDestroy, onMount } from 'svelte'
|
|
import browser from 'webextension-polyfill'
|
|
import { themeUpdates } from '@/interface/hooks/ThemeUpdates'
|
|
import { ThemeManager } from '@/plugins/built-in/themes/theme-manager'
|
|
import { cloudAuth } from '@/seqta/utils/CloudAuth'
|
|
import SignInToFavoriteModal from '@/interface/components/SignInToFavoriteModal.svelte'
|
|
import ThemeBlobImage from '@/interface/components/themes/ThemeBlobImage.svelte'
|
|
import ThemePlaceholderCover from '@/interface/components/themes/ThemePlaceholderCover.svelte'
|
|
import { filterThemesByMode, isLocalCustomTheme, type ThemeListMode } from '@/interface/utils/themeListFilters'
|
|
import { closeExtensionPopup } from '@/seqta/utils/Closers/closeExtensionPopup'
|
|
import { formatCustomThemeApiError } from '@/seqta/utils/customThemes/apiErrors'
|
|
|
|
const themeManager = ThemeManager.getInstance();
|
|
|
|
let themes = $state<ThemeList | null>(null);
|
|
let { isEditMode, showNavigation = true, listMode = 'all' } = $props<{
|
|
isEditMode: boolean;
|
|
showNavigation?: boolean;
|
|
listMode?: ThemeListMode;
|
|
}>();
|
|
let favoriteStatus = $state<Record<string, boolean>>({});
|
|
let cloudLoggedIn = $state(cloudAuth.state.isLoggedIn);
|
|
let prevLoggedIn = $state(false);
|
|
let showSignInModal = $state(false);
|
|
let submittingThemeId = $state<string | null>(null);
|
|
let submitError = $state<string | null>(null);
|
|
|
|
$effect(() => {
|
|
const unsub = cloudAuth.subscribe((s) => {
|
|
const now = s.isLoggedIn;
|
|
if (now && !prevLoggedIn && themes) void fetchThemes();
|
|
prevLoggedIn = now;
|
|
cloudLoggedIn = now;
|
|
});
|
|
return unsub;
|
|
});
|
|
|
|
const handleThemeClick = async (theme: CustomTheme, e: MouseEvent) => {
|
|
if (isEditMode) return;
|
|
if (theme.id === themes?.selectedTheme) {
|
|
themeManager.setTransitionPoint(e.clientX, e.clientY);
|
|
await themeManager.disableTheme();
|
|
themes.selectedTheme = '';
|
|
} else {
|
|
themeManager.setTransitionPoint(e.clientX, e.clientY);
|
|
await themeManager.setTheme(theme.id);
|
|
if (!themes) return;
|
|
themes.selectedTheme = theme.id;
|
|
}
|
|
}
|
|
|
|
const handleThemeDelete = async (themeId: string) => {
|
|
try {
|
|
await themeManager.deleteTheme(themeId);
|
|
if (!themes) return;
|
|
|
|
themes.themes = themes.themes.filter(theme => theme.id !== themeId);
|
|
if (themeId === themes.selectedTheme) {
|
|
themes.selectedTheme = '';
|
|
await themeManager.disableTheme();
|
|
}
|
|
} catch (error) {
|
|
console.error('Error deleting theme:', error);
|
|
}
|
|
}
|
|
|
|
const handleShareTheme = async (theme: CustomTheme) => {
|
|
try {
|
|
await themeManager.shareTheme(theme.id);
|
|
} catch (error) {
|
|
console.error('Error sharing theme:', error);
|
|
}
|
|
}
|
|
|
|
const fetchThemes = async () => {
|
|
themes = {
|
|
themes: await themeManager.getAvailableThemes(),
|
|
selectedTheme: themeManager.getSelectedThemeId() || '',
|
|
}
|
|
if (themes && cloudLoggedIn) {
|
|
const status: Record<string, boolean> = {};
|
|
await Promise.all(
|
|
themes.themes.map(async (t) => {
|
|
try {
|
|
const res = (await browser.runtime.sendMessage({
|
|
type: 'fetchThemeDetails',
|
|
themeId: t.id,
|
|
})) as { success?: boolean; data?: { theme?: { is_favorited?: boolean } } };
|
|
if (res?.success && res?.data?.theme) {
|
|
status[t.id] = !!res.data.theme.is_favorited;
|
|
}
|
|
} catch {
|
|
// Theme may not exist on store (e.g. locally created)
|
|
}
|
|
})
|
|
);
|
|
favoriteStatus = status;
|
|
} else {
|
|
favoriteStatus = {};
|
|
}
|
|
}
|
|
|
|
const openStorePage = async () => {
|
|
const { OpenStorePage } = await import('@/seqta/ui/renderStore')
|
|
await OpenStorePage()
|
|
}
|
|
|
|
const openThemeCreator = async (themeId?: string) => {
|
|
const { launchPageThemeBuilder } = await import('@/seqta/utils/launchPageThemeBuilder')
|
|
await launchPageThemeBuilder(themeId)
|
|
}
|
|
|
|
const openCommunitySubmit = async () => {
|
|
const { openCommunityThemeSubmit } = await import('@/seqta/utils/openCommunityThemeSubmit')
|
|
await openCommunityThemeSubmit()
|
|
}
|
|
|
|
const submitCustomTheme = async (theme: CustomTheme, e: MouseEvent) => {
|
|
e.stopPropagation();
|
|
submitError = null;
|
|
if (!cloudLoggedIn) {
|
|
showSignInModal = true;
|
|
return;
|
|
}
|
|
submittingThemeId = theme.id;
|
|
try {
|
|
const { submitThemeById } = await import('@/seqta/utils/customThemes/submitLocalTheme');
|
|
await submitThemeById(theme.id, theme.description?.trim() || undefined);
|
|
} catch (err) {
|
|
submitError = formatCustomThemeApiError(err);
|
|
} finally {
|
|
submittingThemeId = null;
|
|
}
|
|
}
|
|
|
|
const customThemeActionClass =
|
|
'flex h-12 items-center justify-center gap-2 w-full rounded-xl transition bg-zinc-100 text-xl dark:bg-zinc-900 dark:text-white hover:bg-zinc-200 dark:hover:bg-zinc-800'
|
|
|
|
const handleToggleFavorite = async (theme: CustomTheme, e: MouseEvent) => {
|
|
e.stopPropagation();
|
|
if (!cloudLoggedIn) {
|
|
showSignInModal = true;
|
|
return;
|
|
}
|
|
const isFavorite = !favoriteStatus[theme.id];
|
|
const result = (await browser.runtime.sendMessage({
|
|
type: 'cloudFavorite',
|
|
themeId: theme.id,
|
|
action: isFavorite ? 'favorite' : 'unfavorite',
|
|
})) as { success?: boolean };
|
|
if (result?.success) {
|
|
favoriteStatus = { ...favoriteStatus, [theme.id]: isFavorite };
|
|
}
|
|
}
|
|
|
|
onMount(async () => {
|
|
await fetchThemes();
|
|
themeUpdates.addListener(fetchThemes);
|
|
})
|
|
|
|
onDestroy(() => {
|
|
themeUpdates.removeListener(fetchThemes);
|
|
})
|
|
|
|
const visibleThemes = $derived(
|
|
themes ? filterThemesByMode(themes.themes, listMode) : [],
|
|
);
|
|
|
|
const useTwoColumnLayout = $derived(listMode === 'downloaded' || listMode === 'custom');
|
|
</script>
|
|
|
|
<div
|
|
class="relative mb-1 w-full {useTwoColumnLayout ? '' : 'max-w-lg mx-auto'}"
|
|
role="list"
|
|
tabindex="-1"
|
|
>
|
|
<div class="{useTwoColumnLayout ? 'grid grid-cols-2 gap-3' : 'flex flex-col gap-2 px-2'}">
|
|
{#if themes}
|
|
{#each visibleThemes as theme (theme.id)}
|
|
<button
|
|
class="relative group w-full aspect-theme flex justify-center items-center rounded-xl transition ring dark:ring-white ring-zinc-300 {theme.id === themes.selectedTheme ? 'dark:ring-2 ring-4' : 'ring-0'}"
|
|
onclick={(e) => handleThemeClick(theme, e)}
|
|
>
|
|
{#if isEditMode}
|
|
<div
|
|
class="flex absolute top-2 right-2 z-20 place-items-center p-2 w-6 h-6 text-white bg-red-600 rounded-full opacity-100"
|
|
onclick={(event) => { event.stopPropagation(); handleThemeDelete(theme.id) }}
|
|
onkeydown={(event) => { if (event.key === 'Enter' || event.key === ' ') handleThemeDelete(theme.id) }}
|
|
role="button"
|
|
tabindex="-1"
|
|
>
|
|
<div class="w-4 h-0.5 bg-white"></div>
|
|
</div>
|
|
{/if}
|
|
|
|
{#if !isEditMode}
|
|
<div class="absolute inset-y-0 right-3 z-20 flex items-center gap-2 opacity-0 transition-opacity group-hover:opacity-100">
|
|
{#if !isLocalCustomTheme(theme)}
|
|
<div
|
|
class="flex h-8 w-8 place-items-center rounded-full bg-black/50 p-2 text-center {(favoriteStatus[theme.id] ?? false) ? 'text-red-400' : 'text-white/80'}"
|
|
onclick={(event) => handleToggleFavorite(theme, event)}
|
|
onkeydown={(event) => { if (event.key === 'Enter' || event.key === ' ') handleToggleFavorite(theme, event as any) }}
|
|
role="button"
|
|
tabindex="-1"
|
|
title={cloudLoggedIn ? ((favoriteStatus[theme.id] ?? false) ? 'Remove from favorites' : 'Add to favorites') : 'Sign in to favorite themes'}
|
|
>
|
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill={(favoriteStatus[theme.id] ?? false) ? 'currentColor' : 'none'} stroke="currentColor" stroke-width="2" class="h-5 w-5">
|
|
<path stroke-linecap="round" stroke-linejoin="round" d="M4.318 6.318a4.5 4.5 0 000 6.364L12 20.364l7.682-7.682a4.5 4.5 0 00-6.364-6.364L12 7.636l-1.318-1.318a4.5 4.5 0 00-6.364 0z" />
|
|
</svg>
|
|
</div>
|
|
{/if}
|
|
|
|
<div
|
|
class="flex h-8 w-8 place-items-center rounded-full bg-black/50 p-2 text-white/80"
|
|
onclick={(event) => { event.stopPropagation(); handleShareTheme(theme) }}
|
|
onkeydown={(event) => { if (event.key === 'Enter' || event.key === ' ') handleShareTheme(theme) }}
|
|
role="button"
|
|
tabindex="-1"
|
|
title="Export theme"
|
|
>
|
|
<span class="text-lg font-IconFamily"></span>
|
|
</div>
|
|
|
|
{#if isLocalCustomTheme(theme)}
|
|
<div
|
|
class="flex h-8 w-8 place-items-center rounded-full bg-black/50 p-2 text-white"
|
|
onclick={(event) => { event.stopPropagation(); void openThemeCreator(theme.id) }}
|
|
onkeydown={(event) => { if (event.key === 'Enter' || event.key === ' ') void openThemeCreator(theme.id) }}
|
|
role="button"
|
|
tabindex="-1"
|
|
title="Edit theme"
|
|
>
|
|
<span class="text-lg font-IconFamily"></span>
|
|
</div>
|
|
{#if listMode === 'custom'}
|
|
<div
|
|
class="flex h-8 w-8 place-items-center rounded-full bg-black/50 p-2 text-white {submittingThemeId === theme.id ? 'opacity-60' : ''}"
|
|
onclick={(event) => void submitCustomTheme(theme, event)}
|
|
onkeydown={(event) => { if (event.key === 'Enter' || event.key === ' ') void submitCustomTheme(theme, event as unknown as MouseEvent) }}
|
|
role="button"
|
|
tabindex="-1"
|
|
title="Submit to community"
|
|
>
|
|
<span class="text-lg font-IconFamily"></span>
|
|
</div>
|
|
{/if}
|
|
{/if}
|
|
</div>
|
|
{/if}
|
|
|
|
<div class="relative top-0 z-10 flex justify-center w-full h-full overflow-hidden transition dark:text-white rounded-xl group place-items-center bg-zinc-100 dark:bg-zinc-900 { isEditMode ? 'animate-shake brightness-90' : ''}">
|
|
{#if theme.coverImage}
|
|
<ThemeBlobImage
|
|
source={theme.coverImage}
|
|
alt={theme.name}
|
|
class="object-cover absolute inset-0 z-0 w-full h-full pointer-events-none"
|
|
/>
|
|
{:else}
|
|
<ThemePlaceholderCover
|
|
accentHint={theme.defaultColour}
|
|
class="pointer-events-none absolute inset-0 z-0 h-full w-full object-cover"
|
|
/>
|
|
{/if}
|
|
{#if !theme.hideThemeName}
|
|
<div class="z-10 {theme.coverImage ? 'text-white' : ''}">{theme.name}</div>
|
|
{/if}
|
|
</div>
|
|
</button>
|
|
{/each}
|
|
{/if}
|
|
|
|
{#if themes && visibleThemes.length === 0}
|
|
<div class="flex min-h-56 flex-col items-center justify-center px-4 py-10 text-center {useTwoColumnLayout ? 'col-span-2' : ''}">
|
|
{#if listMode === 'custom'}
|
|
<h3 class="text-xl font-semibold text-zinc-900 dark:text-white" style="text-wrap: balance">
|
|
No custom themes yet
|
|
</h3>
|
|
<p class="mt-2 max-w-md text-base text-zinc-500 dark:text-zinc-400" style="text-wrap: pretty">
|
|
Use the theme builder above to get started.
|
|
</p>
|
|
{:else if listMode === 'downloaded'}
|
|
<h3 class="text-xl font-semibold text-zinc-900 dark:text-white" style="text-wrap: balance">
|
|
No downloaded themes
|
|
</h3>
|
|
<p class="mt-2 max-w-md text-base text-zinc-500 dark:text-zinc-400" style="text-wrap: pretty">
|
|
Browse the theme store to find and install themes.
|
|
</p>
|
|
<div class="mt-6 grid w-full max-w-md grid-cols-1 gap-2">
|
|
<button
|
|
type="button"
|
|
onclick={() => void openStorePage()}
|
|
class="flex h-11 items-center justify-center gap-2 rounded-lg bg-zinc-200 px-4 font-medium text-zinc-900 transition-[background-color,color,transform] duration-150 hover:bg-zinc-300 active:scale-[0.96] focus:outline-none focus:ring-2 focus:ring-zinc-400 dark:bg-zinc-700 dark:text-white dark:hover:bg-zinc-600"
|
|
>
|
|
<span class="font-IconFamily text-lg" aria-hidden="true"></span>
|
|
Browse the store
|
|
</button>
|
|
</div>
|
|
{:else}
|
|
<h3 class="text-xl font-semibold text-zinc-900 dark:text-white" style="text-wrap: balance">
|
|
No themes installed
|
|
</h3>
|
|
<p class="mt-2 max-w-md text-base text-zinc-500 dark:text-zinc-400" style="text-wrap: pretty">
|
|
Install a theme from the store or create one of your own.
|
|
</p>
|
|
<div class="mt-6 grid w-full max-w-md grid-cols-1 gap-2 sm:grid-cols-2">
|
|
<button
|
|
type="button"
|
|
onclick={() => void openStorePage()}
|
|
class="flex h-11 items-center justify-center gap-2 rounded-lg bg-zinc-200 px-4 font-medium text-zinc-900 transition-[background-color,color,transform] duration-150 hover:bg-zinc-300 active:scale-[0.96] focus:outline-none focus:ring-2 focus:ring-zinc-400 dark:bg-zinc-700 dark:text-white dark:hover:bg-zinc-600"
|
|
>
|
|
<span class="font-IconFamily text-lg" aria-hidden="true"></span>
|
|
Install a theme
|
|
</button>
|
|
<button
|
|
type="button"
|
|
onclick={() => void openThemeCreator()}
|
|
class="flex h-11 items-center justify-center gap-2 rounded-lg bg-zinc-200 px-4 font-medium text-zinc-900 transition-[background-color,color,transform] duration-150 hover:bg-zinc-300 active:scale-[0.96] focus:outline-none focus:ring-2 focus:ring-zinc-400 dark:bg-zinc-700 dark:text-white dark:hover:bg-zinc-600"
|
|
>
|
|
<span class="font-IconFamily text-lg" aria-hidden="true"></span>
|
|
Create a theme
|
|
</button>
|
|
</div>
|
|
{/if}
|
|
</div>
|
|
{/if}
|
|
|
|
{#if themes && (listMode === 'custom' || (showNavigation && visibleThemes.length > 0) || (listMode === 'downloaded' && visibleThemes.length > 0))}
|
|
<div id="divider" class="w-full h-[1px] my-2 bg-zinc-100 dark:bg-zinc-600 {useTwoColumnLayout ? 'col-span-2' : ''}"></div>
|
|
|
|
{#if listMode === 'custom' && submitError}
|
|
<p class="col-span-2 whitespace-pre-line text-sm text-red-600 dark:text-red-400">{submitError}</p>
|
|
{/if}
|
|
|
|
{#if listMode === 'custom'}
|
|
<div class="grid grid-cols-2 gap-2 {useTwoColumnLayout ? 'col-span-2' : ''}">
|
|
<a
|
|
href="https://docs.betterseqta.org/theme-creation/"
|
|
target="_blank"
|
|
rel="noopener noreferrer"
|
|
class={customThemeActionClass}
|
|
>
|
|
<span class="text-2xl font-IconFamily" aria-hidden="true">{'\uecb3'}</span>
|
|
<span>View docs</span>
|
|
</a>
|
|
|
|
<button
|
|
type="button"
|
|
onclick={() => void openCommunitySubmit()}
|
|
class={customThemeActionClass}
|
|
>
|
|
<span class="text-2xl font-IconFamily" aria-hidden="true"></span>
|
|
<span>Create & submit</span>
|
|
</button>
|
|
</div>
|
|
{:else if listMode === 'downloaded'}
|
|
<button
|
|
onclick={() => void openStorePage()}
|
|
class="flex justify-center items-center w-full rounded-xl transition aspect-theme bg-zinc-100 dark:bg-zinc-900 dark:text-white {useTwoColumnLayout ? 'col-span-2' : ''}"
|
|
>
|
|
<span class="text-xl font-IconFamily"></span>
|
|
<span class="ml-2">Theme Store</span>
|
|
</button>
|
|
{:else}
|
|
<button
|
|
onclick={() => void openStorePage()}
|
|
class="flex justify-center items-center w-full rounded-xl transition aspect-theme bg-zinc-100 dark:bg-zinc-900 dark:text-white"
|
|
>
|
|
<span class="text-xl font-IconFamily"></span>
|
|
<span class="ml-2">Theme Store</span>
|
|
</button>
|
|
|
|
<button
|
|
onclick={() => void openThemeCreator()}
|
|
class="flex justify-center items-center w-full rounded-xl transition aspect-theme bg-zinc-100 dark:bg-zinc-900 dark:text-white"
|
|
>
|
|
<span class="text-xl font-IconFamily"></span>
|
|
<span class="ml-2">Create your own</span>
|
|
</button>
|
|
{/if}
|
|
{/if}
|
|
</div>
|
|
</div>
|
|
|
|
{#if showSignInModal}
|
|
<SignInToFavoriteModal onClose={() => (showSignInModal = false)} />
|
|
{/if}
|