Skip to content

Commit

Permalink
refactor: simplify page titles in setting pages (#2431)
Browse files Browse the repository at this point in the history
  • Loading branch information
ferferga authored Aug 18, 2024
1 parent 9ddffb1 commit c9c18d5
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
15 changes: 15 additions & 0 deletions frontend/src/components/Layout/SettingsPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,18 @@
</VRow>
</VContainer>
</template>

<script setup lang="ts">
import { useSlots, computed } from 'vue';
import { usePageTitle } from '@/composables/page-title';
import { isStr } from '@/utils/validation';
const slots = useSlots();
const pageTitle = computed(() => {
const slot = slots.title?.()[0].children;
return isStr(slot) ? slot : undefined;
});
usePageTitle(pageTitle);
</script>
15 changes: 8 additions & 7 deletions frontend/src/composables/page-title.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useTitle as _useTitle, watchImmediate } from '@vueuse/core';
import { onBeforeUnmount, onMounted, shallowRef, toRef, toValue, type MaybeRefOrGetter } from 'vue';
import { computed, onBeforeUnmount, onMounted, shallowRef, toRef, toValue } from 'vue';
import { useTitle as _useTitle, watchImmediate, type ReadonlyRefOrGetter } from '@vueuse/core';
import type { BaseItemDto } from '@jellyfin/sdk/lib/generated-client';
import { isNil } from '@/utils/validation';

Expand All @@ -8,8 +8,9 @@ import { isNil } from '@/utils/validation';
*/
const DEFAULT_PAGE_TITLE = 'Jellyfin Vue';
const _title = shallowRef<string>();
const _fullTitle = computed(() => _title.value ? `${_title.value.trim()} | ${DEFAULT_PAGE_TITLE}` : DEFAULT_PAGE_TITLE);

_useTitle(() => _title.value ? `${_title.value.trim()} | ${DEFAULT_PAGE_TITLE}` : DEFAULT_PAGE_TITLE);
_useTitle(_fullTitle);

/**
* Reactively sets the page title with the following template: **`title` | Jellyfin Vue**. Can be used in 2 ways:
Expand All @@ -20,10 +21,10 @@ _useTitle(() => _title.value ? `${_title.value.trim()} | ${DEFAULT_PAGE_TITLE}`
*
* Value will be set to default (undefined) when the component consuming this composable is unmounted.
*/
export function usePageTitle(title?: MaybeRefOrGetter<string>) {
export function usePageTitle(title?: ReadonlyRefOrGetter<Nullish<string>>) {
onMounted(() => {
if (!isNil(title)) {
watchImmediate(toRef(title), val => _title.value = val);
watchImmediate(toRef(title), val => _title.value = val ?? undefined);
}
});

Expand All @@ -35,6 +36,6 @@ export function usePageTitle(title?: MaybeRefOrGetter<string>) {
/**
* Same as useTitle, but is a shorthand for items only.
*/
export function useItemPageTitle(item: MaybeRefOrGetter<Nullish<BaseItemDto>>) {
usePageTitle(() => toValue(item)?.Name ?? '');
export function useItemPageTitle(item: ReadonlyRefOrGetter<Nullish<BaseItemDto>>) {
usePageTitle(() => toValue(item)?.Name);
};
3 changes: 0 additions & 3 deletions frontend/src/pages/settings/logs-and-activity.vue
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,10 @@ import { useTheme } from 'vuetify';
import { remote } from '@/plugins/remote';
import { useDateFns } from '@/composables/use-datefns';
import { useApi } from '@/composables/apis';
import { usePageTitle } from '@/composables/page-title';
const { t } = useI18n();
const theme = useTheme();
usePageTitle(() => t('logsAndActivity'));
/**
* Return a UI colour given log severity
*/
Expand Down
1 change: 0 additions & 1 deletion frontend/src/pages/settings/users/new.vue
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,4 @@ async function createUser(): Promise<void> {
loading.value = false;
}
}
</script>

0 comments on commit c9c18d5

Please sign in to comment.