Skip to content

Commit

Permalink
webapp: Fix vanished media in single view
Browse files Browse the repository at this point in the history
  • Loading branch information
xemle committed Apr 23, 2023
1 parent f0639f1 commit c33d70d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- webapp: Fix vanished media in single view
- database: Fix preview paths on Windows hosts
- server: Fix database watcher
- cli: Honor custom server port in `gallery.config.yml`
Expand Down
29 changes: 20 additions & 9 deletions packages/webapp/src/single/MediaView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,11 @@ export const MediaView = () => {
const dimensions = useBodyDimensions();

const entries = useEntryStore(state => state.entries);
const lastIndex = useSingleViewStore(state => state.lastIndex);
const showDetails = useSingleViewStore(state => state.showDetails);
const showNavigation = useSingleViewStore(state => state.showNavigation);
const setLastId = useSingleViewStore(state => state.setLastId);
const setLastIndex = useSingleViewStore(state => state.setLastIndex);
const search = useSearchStore(state => state.search);
const setShowDetails = useSingleViewStore(actions => actions.setShowDetails);
const setShowNavigation = useSingleViewStore(actions => actions.setShowNavigation);
Expand All @@ -96,21 +98,21 @@ export const MediaView = () => {
const scaleSize = scaleDimensions(current, dimensions);
console.log(scaleSize, dimensions, current);

useEffect(() => {
if (id) {
setLastId(id)
}
}, [id])
useEffect(() => { id && setLastId(id) }, [id])
useEffect(() => { index >= 0 && setLastIndex(index) }, [index])

const viewEntry = (index) => {
const viewEntry = (index: number) => {
const { shortId } = entries[index]
navigate(`/view/${shortId}`, {state: {index, listLocation}});
}

const dispatch = (action) => {
const dispatch = (action: any) => {
const { type } = action
let prevNextMatch = type.match(/(prev|next)(-(\d+))?/)
if (prevNextMatch && entries.length) {
if (type === 'index') {
const i = Math.min(entries.length - 1, Math.max(0, action.index))
viewEntry(i)
} else if (prevNextMatch && entries.length) {
const offset = prevNextMatch[3] ? +prevNextMatch[3] : 1
const negate = prevNextMatch[1] == 'prev' ? -1 : 1
const i = Math.min(entries.length - 1, Math.max(0, index + (negate * offset)))
Expand All @@ -126,7 +128,7 @@ export const MediaView = () => {
} else if (type == 'last' && entries.length) {
viewEntry(entries.length - 1)
} else if (type == 'list') {
navigate(`${listLocation.pathname}${listLocation.search ? encodeUrl(listLocation.search) : ''}`, {state: {id: current.id}});
navigate(`${listLocation.pathname}${listLocation.search ? encodeUrl(listLocation.search) : ''}`, {state: {id: current?.id}});
} else if (type == 'chronology') {
search({type: 'none'});
navigate('/');
Expand Down Expand Up @@ -164,6 +166,15 @@ export const MediaView = () => {
}
}, [index, showDetails, showNavigation])

const mediaVanishes = index < 0 && lastIndex >= 0 && entries.length > 0
if (mediaVanishes) {
dispatch({type: 'index', index: lastIndex})
}
const listBecomesEmpty = entries.length == 0 && lastIndex >= 0
if (listBecomesEmpty) {
dispatch({type: 'list'})
}

console.log('Media object', current, showDetails);

return (
Expand Down
18 changes: 14 additions & 4 deletions packages/webapp/src/store/single-view-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@ import { persist } from 'zustand/middleware'

export interface SingleViewStore {
lastId: string
lastIndex: number
showDetails: boolean
showNavigation: boolean

setLastId(lastId: string): void
setLastIndex(lastIndex: number): void
setShowDetails(show: boolean): void
setShowNavigation(show: boolean): void
}

const excludeStateProps = (excludeProps: string[] = []) => (state: any): any => Object.fromEntries(
Object.entries(state).filter(([key]) => !excludeProps.includes(key)))

export const useSingleViewStore = create<
SingleViewStore,
[
Expand All @@ -19,10 +24,15 @@ export const useSingleViewStore = create<
>(
persist((set) => ({
lastId: '',
lastIndex: -1,
showDetails: false,
showNavigation: true,

setLastId: (lastId: string) => set((state) => ({...state, lastId})),
setShowDetails: (show: boolean) => set((state) => ({...state, showDetails: show})),
setShowNavigation: (show: boolean) => set((state) => ({...state, showNavigation: show})),
}), { name: 'gallery-single-view' }))
setLastId: (lastId: string) => set((state: SingleViewStore) => ({...state, lastId})),
setLastIndex: (lastIndex: number) => set((state: SingleViewStore) => ({...state, lastIndex})),
setShowDetails: (show: boolean) => set((state: SingleViewStore) => ({...state, showDetails: show})),
setShowNavigation: (show: boolean) => set((state: SingleViewStore) => ({...state, showNavigation: show})),
}), {
name: 'gallery-single-view',
partialize: excludeStateProps(['lastId', 'lastIndex']),
}))

0 comments on commit c33d70d

Please sign in to comment.