-
Notifications
You must be signed in to change notification settings - Fork 164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open recent files from within a sheet #2054
base: qa
Are you sure you want to change the base?
Changes from all commits
06e4607
f88ff25
dc2e80e
e0d70eb
9e48688
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
//! This updates localStorage with a list of recently opened files on the user's | ||
//! machine. This is called when a file is opened (successfully or not). The | ||
//! file menu uses useLocalStorage to access this data (it cannot be done here | ||
//! or in an Atom b/c of the timing of when the file is opened). | ||
|
||
export interface RecentFile { | ||
uuid: string; | ||
name: string; | ||
} | ||
|
||
const MAX_RECENT_FILES = 10; | ||
export const RECENT_FILES_KEY = 'recent_files'; | ||
|
||
// Updates the recent files list in localStorage. If loaded is false, then the | ||
// file is deleted. If onlyIfExists = true, then the file is only added if it | ||
// already exists in the list. | ||
export const updateRecentFiles = (uuid: string, name: string, loaded: boolean, onlyIfExists = false) => { | ||
try { | ||
if (loaded) { | ||
const existing = localStorage.getItem(RECENT_FILES_KEY); | ||
const recentFiles = existing ? JSON.parse(existing) : []; | ||
if (onlyIfExists && !recentFiles.find((file: RecentFile) => file.uuid === uuid)) { | ||
return; | ||
} | ||
const newRecentFiles = [{ uuid, name }, ...recentFiles.filter((file: RecentFile) => file.uuid !== uuid)]; | ||
while (newRecentFiles.length > MAX_RECENT_FILES) { | ||
newRecentFiles.pop(); | ||
} | ||
localStorage.setItem(RECENT_FILES_KEY, JSON.stringify(newRecentFiles)); | ||
} else { | ||
const existing = localStorage.getItem(RECENT_FILES_KEY); | ||
const recentFiles = existing ? JSON.parse(existing) : []; | ||
localStorage.setItem( | ||
RECENT_FILES_KEY, | ||
JSON.stringify(recentFiles.filter((file: RecentFile) => file.uuid !== uuid)) | ||
); | ||
window.dispatchEvent(new Event('local-storage')); | ||
} | ||
} catch (e) { | ||
console.warn('Unable to update recent files', e); | ||
} | ||
}; | ||
|
||
// Clears the recent files list in localStorage | ||
export const clearRecentFiles = () => { | ||
localStorage.removeItem(RECENT_FILES_KEY); | ||
window.dispatchEvent(new Event('local-storage')); | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like this file should be put in So this should live outside of |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import { updateRecentFiles } from '@/app/ui/menus/TopBar/TopBarMenus/updateRecentFiles'; | ||
import { useDashboardRouteLoaderData } from '@/routes/_dashboard'; | ||
import { useRootRouteLoaderData } from '@/routes/_root'; | ||
import { | ||
|
@@ -126,12 +127,14 @@ export function FilesListItemUserFile({ | |
// Update on the server and optimistically in the UI | ||
const data: FileAction['request.rename'] = { action: 'rename', name: value }; | ||
fetcherRename.submit(data, fetcherSubmitOpts); | ||
updateRecentFiles(uuid, value, true, true); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar feedback here, if you move these into the routes file, you won't have to find every place where you rename or delete a file across both the dashboard and the app. |
||
}; | ||
|
||
const handleDelete = () => { | ||
if (window.confirm(`Confirm you want to delete the file: “${name}”`)) { | ||
const data = getActionFileDelete({ userEmail: loggedInUser?.email ?? '', redirect: false }); | ||
fetcherDelete.submit(data, fetcherSubmitOpts); | ||
updateRecentFiles(uuid, '', false); | ||
} | ||
}; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should happen in
quadratic-client/src/routes/api.files.$uuid.ts
in the delete actionIf you put it in this file, then if a user deletes a file from the dashboard you won't see that reflected in your files list.