Skip to content

A self-hosted media server and file management system

feat: When directly opening a video via id, it's page should be selected Fixes #127

aminnausin680ccb28ccd2
diff --git a/resources/js/components/cedar-ui/table/TableBase.vue b/resources/js/components/cedar-ui/table/TableBase.vue
index b6946c9b..e60401a2 100644
--- a/resources/js/components/cedar-ui/table/TableBase.vue
+++ b/resources/js/components/cedar-ui/table/TableBase.vue
@@ -3,7 +3,7 @@ import type { TableSortOption, TableProps, TableRow } from '@aminnausin/cedar-ui
import type { Component } from 'vue';
import { PhSortDescendingLight, PhSortAscendingLight } from '../icons';
-import { onMounted, ref, toRef } from 'vue';
+import { nextTick, onMounted, ref, toRef, watch } from 'vue';
import { InputSelect } from '../select';
import { ButtonIcon } from '../button';
import { TextInput } from '../input';
@@ -13,18 +13,36 @@ import TableLoadingSpinner from './TableLoadingSpinner.vue';
import TablePagination from './TablePagination.vue';
import useTable from '@/composables/useTable';
-const props = withDefaults(defineProps<TableProps<T> & { forceVerticalToolbar?: boolean; sticky?: boolean; stickyClass?: string; loadingPlaceholder?: Component }>(), {
- useToolbar: true,
- usePagination: true,
- itemsPerPage: 12,
- selectedID: null,
- startAscending: true,
- usePaginationIcons: false,
- maxVisiblePages: 5,
- noResultsMessage: 'No Results',
- loadingPlaceholder: TableLoadingSpinner,
+const props = withDefaults(
+ defineProps<
+ TableProps<T> & {
+ forceVerticalToolbar?: boolean;
+ sticky?: boolean;
+ stickyClass?: string;
+ loadingPlaceholder?: Component;
+ selectedID?: number | null;
+ matchPageToIndexOnLoad?: boolean;
+ currentIndex?: number;
+ }
+ >(),
+ {
+ useToolbar: true,
+ usePagination: true,
+ itemsPerPage: 12,
+ selectedID: null,
+ startAscending: true,
+ usePaginationIcons: false,
+ maxVisiblePages: 5,
+ noResultsMessage: 'No Results',
+ loadingPlaceholder: TableLoadingSpinner,
+ matchPageToIndexOnLoad: true,
+ currentIndex: 1,
+ },
+);
+const { currentPage, itemsPerPage, pageData, setPage } = useTable<T>({
+ itemsPerPage: () => props.itemsPerPage,
+ data: toRef(props, 'data'),
});
-const { currentPage, itemsPerPage, pageData, setPage } = useTable<T>({ itemsPerPage: () => props.itemsPerPage, data: toRef(props, 'data') });
const sortAscending = ref(props.startAscending);
const lastSortKey = ref('');
@@ -45,9 +63,24 @@ const handleSortChange = (sortKey?: TableSortOption) => {
}
if (!lastSortKey.value) return;
+
props.sortAction?.(lastSortKey.value as keyof T, sortAscending.value ? 1 : -1);
+
+ // waits for current index to update before setting page to current item after sort
+ nextTick(() => {
+ setPage(Math.floor(props.currentIndex / props.itemsPerPage) + 1);
+ });
};
+watch(
+ () => props.currentIndex,
+ (index) => {
+ if (index === undefined || index < 0 || !props.itemsPerPage) return;
+ setPage(Math.floor(index / props.itemsPerPage) + 1);
+ },
+ { immediate: true },
+);
+
onMounted(() => {
if (props.useToolbar && props.sortAction) props.sortAction(lastSortKey.value as keyof T, props.startAscending ? 1 : -1);
});
diff --git a/resources/js/stores/ContentStore.ts b/resources/js/stores/ContentStore.ts
index 65939f99..a0462bde 100644
--- a/resources/js/stores/ContentStore.ts
+++ b/resources/js/stores/ContentStore.ts
@@ -102,24 +102,21 @@ export const useContentStore = defineStore('Content', () => {
});
// Relative media tracking
- const nextVideoURL = computed(() => {
- if (!stateFilteredPlaylist.value || !stateDirectory.value.name || !stateFolder.value.name || !stateVideo.value) return '';
-
- const currentIndex = stateFilteredPlaylist.value.findIndex((video) => video.id === stateVideo.value?.id);
-
- if (currentIndex === -1 || currentIndex === stateFilteredPlaylist.value.length - 1) return '';
+ const currentMediaIndex = computed(() => {
+ if (!stateFilteredPlaylist.value || !stateVideo.value) return -1;
+ return stateFilteredPlaylist.value.findIndex((video) => video.id === stateVideo.value?.id);
+ });
- return encodeURI(`/${stateDirectory.value.name}/${stateFolder.value.name}?video=${stateFilteredPlaylist.value[currentIndex + 1].id}`);
+ const nextVideoURL = computed(() => {
+ if (!stateDirectory.value.name || !stateFolder.value.name) return '';
+ if (currentMediaIndex.value === -1 || currentMediaIndex.value === stateFilteredPlaylist.value.length - 1) return '';
+ return encodeURI(`/${stateDirectory.value.name}/${stateFolder.value.name}?video=${stateFilteredPlaylist.value[currentMediaIndex.value + 1].id}`);
});
const previousVideoURL = computed(() => {
- if (!stateFilteredPlaylist.value || !stateDirectory.value.name || !stateFolder.value.name || !stateVideo.value) return '';
-
- const currentIndex = stateFilteredPlaylist.value.findIndex((video) => video.id === stateVideo.value?.id);
-
- if (currentIndex <= 0) return '';
-
- return encodeURI(`/${stateDirectory.value.name}/${stateFolder.value.name}?video=${stateFilteredPlaylist.value[currentIndex - 1].id}`);
+ if (!stateDirectory.value.name || !stateFolder.value.name) return '';
+ if (currentMediaIndex.value <= 0) return '';
+ return encodeURI(`/${stateDirectory.value.name}/${stateFolder.value.name}?video=${stateFilteredPlaylist.value[currentMediaIndex.value - 1].id}`);
});
/**
@@ -389,6 +386,7 @@ export const useContentStore = defineStore('Content', () => {
videoSort,
nextVideoURL,
previousVideoURL,
+ currentMediaIndex,
getCategory,
getFolder,
getMetadataById,
diff --git a/resources/js/views/VideoView.vue b/resources/js/views/VideoView.vue
index 1e955a63..4c2dbd4a 100755
--- a/resources/js/views/VideoView.vue
+++ b/resources/js/views/VideoView.vue
@@ -26,7 +26,7 @@ import ShareModal from '@/components/modals/ShareModal.vue';
import VideoCard from '@/components/cards/data/VideoCard.vue';
const { getFolder, getCategory, playlistFind, playlistSort } = useContentStore();
-const { searchQuery, stateFilteredPlaylist, stateDirectory, stateVideo, stateFolder } = storeToRefs(useContentStore());
+const { searchQuery, stateFilteredPlaylist, stateDirectory, stateVideo, stateFolder, currentMediaIndex } = storeToRefs(useContentStore());
const { pageTitle, selectedSideBar } = storeToRefs(useAppStore());
const ambientPlayer = useTemplateRef('ambientPlayer');
@@ -135,13 +135,7 @@ watch(
{ immediate: false },
);
-watch(
- () => [route.params.category, route.params.folder],
- async () => {
- await reload();
- },
- { immediate: false },
-);
+watch(() => `${route.params.category}/${route.params.folder}`, reload, { immediate: false });
watch(() => stateFolder.value, setFolderAsPageTitle);
watch(() => stateVideo.value, setVideoAsDocumentTitle, { immediate: false });
@@ -157,6 +151,7 @@ watch(() => stateVideo.value, setVideoAsDocumentTitle, { immediate: false });
</div>
<TableBase
+ ref="mediaTable"
:class="'flex-1'"
:data="stateFilteredPlaylist"
:row="VideoCard"
@@ -167,6 +162,7 @@ watch(() => stateVideo.value, setVideoAsDocumentTitle, { immediate: false });
:sortingOptions="sortingOptions.filter((s) => !s.hidden)"
:selectedID="stateVideo?.id"
:startAscending="true"
+ :currentIndex="currentMediaIndex"
v-model="searchQuery"
/>
</section>