From 9d800eec319cc1a14e0ba19d028ed81c12e6c6ec Mon Sep 17 00:00:00 2001 From: nikec <43032218+niikeec@users.noreply.github.com> Date: Thu, 6 Jul 2023 00:27:38 +0200 Subject: [PATCH] [ENG-885] Move online locations to lib store (#1074) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Enable trace logging for invalidate query * Fixing quick rescan on explorer And annotating wrong quick rescan usage on top bar reload button * Move online locations to lib store * Fix topbar reload path * fix mobile * Remove unused imports + format - Adjust `arraysEqual` arguments type --------- Co-authored-by: Ericson Soares Co-authored-by: Utku Bakir <74243531+utkubakir@users.noreply.github.com> Co-authored-by: VĂ­tor Vasconcellos --- .vscode/launch.json | 4 ++++ .vscode/tasks.json | 1 + .../screens/settings/library/LocationSettings.tsx | 6 ++++-- core/src/api/utils/invalidate.rs | 6 ++++++ core/src/lib.rs | 15 ++++++--------- .../app/$libraryId/Explorer/TopBarOptions.tsx | 5 ++++- .../$libraryId/Layout/Sidebar/LibrarySection.tsx | 9 ++------- interface/app/$libraryId/location/$id.tsx | 2 +- .../settings/library/locations/ListItem.tsx | 4 ++-- packages/client/src/hooks/index.ts | 1 + packages/client/src/hooks/useLibraryContext.tsx | 15 +++++++-------- packages/client/src/hooks/useLibraryStore.ts | 15 +++++++++++++++ packages/client/src/utils/index.ts | 2 +- 13 files changed, 54 insertions(+), 31 deletions(-) create mode 100644 packages/client/src/hooks/useLibraryStore.ts diff --git a/.vscode/launch.json b/.vscode/launch.json index abe6eed11..411d7f5a1 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -16,6 +16,10 @@ ], "problemMatcher": "$rustc" }, + "env": { + "RUST_BACKTRACE": "short" + // "RUST_LOG": "sd_core::invalidate-query=trace" + }, "sourceLanguages": ["rust"], "preLaunchTask": "ui:dev" }, diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 75bf45f1b..a895b9fa6 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -53,6 +53,7 @@ ], "env": { "RUST_BACKTRACE": "short" + // "RUST_LOG": "sd_core::invalidate-query=trace" }, "problemMatcher": ["$rustc"], "group": "build", diff --git a/apps/mobile/src/screens/settings/library/LocationSettings.tsx b/apps/mobile/src/screens/settings/library/LocationSettings.tsx index 68a00cb5a..2ffca45d8 100644 --- a/apps/mobile/src/screens/settings/library/LocationSettings.tsx +++ b/apps/mobile/src/screens/settings/library/LocationSettings.tsx @@ -73,7 +73,9 @@ function LocationItem({ location, index, navigation }: LocationItemProps) { {/* Full Re-scan IS too much here */} fullRescan.mutate({ location_id: location.id, reidentify_objects: true })} + onPress={() => + fullRescan.mutate({ location_id: location.id, reidentify_objects: true }) + } > @@ -97,7 +99,7 @@ function LocationItem({ location, index, navigation }: LocationItemProps) { arraysEqual(location.pub_id, l)) + onlineLocations.some((l) => arraysEqual(location.pub_id, l)) ? 'bg-green-500' : 'bg-red-500' )} diff --git a/core/src/api/utils/invalidate.rs b/core/src/api/utils/invalidate.rs index 7ac6df329..29308edd1 100644 --- a/core/src/api/utils/invalidate.rs +++ b/core/src/api/utils/invalidate.rs @@ -136,6 +136,8 @@ macro_rules! invalidate_query { } } + ::tracing::trace!(target: "sd_core::invalidate-query", "invalidate_query!(\"{}\") at {}", $key, concat!(file!(), ":", line!())); + // The error are ignored here because they aren't mission critical. If they fail the UI might be outdated for a bit. ctx.emit(crate::api::CoreEvent::InvalidateOperation( crate::api::utils::InvalidateOperationEvent::dangerously_create($key, serde_json::Value::Null, None) @@ -165,6 +167,8 @@ macro_rules! invalidate_query { } } + ::tracing::trace!(target: "sd_core::invalidate-query", "invalidate_query!(\"{}\") at {}", $key, concat!(file!(), ":", line!())); + // The error are ignored here because they aren't mission critical. If they fail the UI might be outdated for a bit. let _ = serde_json::to_value($arg) .map(|v| @@ -203,6 +207,8 @@ macro_rules! invalidate_query { } } + ::tracing::trace!(target: "sd_core::invalidate-query", "invalidate_query!(\"{}\") at {}", $key, concat!(file!(), ":", line!())); + // The error are ignored here because they aren't mission critical. If they fail the UI might be outdated for a bit. let _ = serde_json::to_value($arg) .and_then(|arg| diff --git a/core/src/lib.rs b/core/src/lib.rs index b4377dce2..4885040b2 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -119,11 +119,6 @@ impl Node { } pub fn init_logger(data_dir: impl AsRef) -> WorkerGuard { - let log_filter = match cfg!(debug_assertions) { - true => tracing::Level::DEBUG, - false => tracing::Level::INFO, - }; - let (logfile, guard) = NonBlocking::new( RollingFileAppender::builder() .filename_prefix("sd.log") @@ -137,8 +132,8 @@ impl Node { .with(fmt::Subscriber::new().with_ansi(false).with_writer(logfile)) .with( fmt::Subscriber::new() - .with_writer(std::io::stdout.with_max_level(log_filter)) - .with_filter( + .with_writer(std::io::stdout) + .with_filter(if cfg!(debug_assertions) { EnvFilter::from_default_env() .add_directive( "warn".parse().expect("Error invalid tracing directive!"), @@ -177,8 +172,10 @@ impl Node { "rspc=debug" .parse() .expect("Error invalid tracing directive!"), - ), - ), + ) + } else { + EnvFilter::from("info") + }), ); tracing::collect::set_global_default(collector) diff --git a/interface/app/$libraryId/Explorer/TopBarOptions.tsx b/interface/app/$libraryId/Explorer/TopBarOptions.tsx index eef3d40c5..ad6df4b01 100644 --- a/interface/app/$libraryId/Explorer/TopBarOptions.tsx +++ b/interface/app/$libraryId/Explorer/TopBarOptions.tsx @@ -16,6 +16,7 @@ import TopBarOptions, { TOP_BAR_ICON_STYLE, ToolOption } from '../TopBar/TopBarO import { useExplorerContext } from './Context'; import OptionsPanel from './OptionsPanel'; import { getExplorerStore, useExplorerStore } from './store'; +import { useExplorerSearchParams } from './util'; export const useExplorerTopBarOptions = () => { const explorerStore = useExplorerStore(); @@ -84,6 +85,8 @@ export const useExplorerTopBarOptions = () => { const { parent } = useExplorerContext(); + const [{ path }] = useExplorerSearchParams(); + const toolOptions = [ { toolTipLabel: 'Key Manager', @@ -114,7 +117,7 @@ export const useExplorerTopBarOptions = () => { 'locations.quickRescan', { location_id: parent.location.id, - sub_path: '' + sub_path: path ?? '' } ], { onData() {} } diff --git a/interface/app/$libraryId/Layout/Sidebar/LibrarySection.tsx b/interface/app/$libraryId/Layout/Sidebar/LibrarySection.tsx index e8fdbb8b6..ec5f851c0 100644 --- a/interface/app/$libraryId/Layout/Sidebar/LibrarySection.tsx +++ b/interface/app/$libraryId/Layout/Sidebar/LibrarySection.tsx @@ -3,10 +3,8 @@ import clsx from 'clsx'; import { useEffect, useState } from 'react'; import { Link, NavLink } from 'react-router-dom'; import { - LibraryContextProvider, arraysEqual, useBridgeQuery, - useClientContext, useDebugState, useFeatureFlag, useLibraryQuery, @@ -14,8 +12,7 @@ import { } from '@sd/client'; import { Button, Tooltip } from '@sd/ui'; import { AddLocationButton } from '~/app/$libraryId/settings/library/locations/AddLocationButton'; -import { Folder } from '~/components/Folder'; -import { SubtleButton } from '~/components/SubtleButton'; +import { Folder, SubtleButton } from '~/components'; import SidebarLink from './Link'; import LocationsContextMenu from './LocationsContextMenu'; import Section from './Section'; @@ -128,10 +125,8 @@ export const LibrarySection = () => { } > {locations?.map((location) => { - const online = - onlineLocations?.some((l) => arraysEqual(location.pub_id, l)) || false; + const online = onlineLocations.some((l) => arraysEqual(location.pub_id, l)); - // const online = onlineLocations?.some((l) => arraysEqual(location.pub_id, l)); return ( { [ 'locations.quickRescan', { - sub_path: location.data?.path ?? '', + sub_path: path ?? '', location_id: locationId } ], diff --git a/interface/app/$libraryId/settings/library/locations/ListItem.tsx b/interface/app/$libraryId/settings/library/locations/ListItem.tsx index 6f39bff89..043a2059b 100644 --- a/interface/app/$libraryId/settings/library/locations/ListItem.tsx +++ b/interface/app/$libraryId/settings/library/locations/ListItem.tsx @@ -4,7 +4,7 @@ import { useState } from 'react'; import { useNavigate } from 'react-router'; import { Location, Node, arraysEqual, useLibraryMutation, useOnlineLocations } from '@sd/client'; import { Button, Card, Tooltip, dialogManager } from '@sd/ui'; -import { Folder } from '~/components/Folder'; +import { Folder } from '~/components'; import { useIsDark } from '~/hooks'; import DeleteDialog from './DeleteDialog'; @@ -23,7 +23,7 @@ export default ({ location }: Props) => { if (hide) return <>; - const online = onlineLocations?.some((l) => arraysEqual(location.pub_id, l)) || false; + const online = onlineLocations.some((l) => arraysEqual(location.pub_id, l)); return ( (null!); @@ -17,18 +17,17 @@ interface LibraryContextProviderProps extends PropsWithChildren { export const LibraryContextProvider = ({ children, library }: LibraryContextProviderProps) => { const { libraries } = useClientContext(); - const [onlineLocations, setOnlineLocations] = useState(null); // We put this into context because each hook creates a new subscription which means we get duplicate events from the backend if we don't do this // TODO: This should probs be a library subscription - https://linear.app/spacedriveapp/issue/ENG-724/locationsonline-should-be-a-library-not-a-bridge-subscription useBridgeSubscription(['locations.online'], { - onData: (d) => setOnlineLocations(d) + onData: (d) => { + getLibraryStore().onlineLocations = d; + } }); return ( - - {children} - + {children} ); }; @@ -41,6 +40,6 @@ export const useLibraryContext = () => { }; export function useOnlineLocations() { - const ctx = useLibraryContext(); - return ctx?.onlineLocations || []; + const { onlineLocations } = useLibraryStore(); + return onlineLocations; } diff --git a/packages/client/src/hooks/useLibraryStore.ts b/packages/client/src/hooks/useLibraryStore.ts new file mode 100644 index 000000000..215f31b30 --- /dev/null +++ b/packages/client/src/hooks/useLibraryStore.ts @@ -0,0 +1,15 @@ +import { proxy, useSnapshot } from 'valtio'; + +const state = { + onlineLocations: [] as number[][] +}; + +const libraryStore = proxy(state); + +export function useLibraryStore() { + return useSnapshot(libraryStore); +} + +export function getLibraryStore() { + return libraryStore; +} diff --git a/packages/client/src/utils/index.ts b/packages/client/src/utils/index.ts index 5c105c031..011ed9f68 100644 --- a/packages/client/src/utils/index.ts +++ b/packages/client/src/utils/index.ts @@ -8,7 +8,7 @@ export function isPath(item: ExplorerItem): item is Extract(a: T[], b: T[]) { +export function arraysEqual(a: readonly T[], b: readonly T[]) { if (a === b) return true; if (a == null || b == null) return false; if (a.length !== b.length) return false;