mirror of
https://github.com/tiddly-gittly/TidGi-Desktop.git
synced 2025-12-06 02:30:47 -08:00
refactor: dup logic
This commit is contained in:
parent
ee31957186
commit
313536a300
5 changed files with 77 additions and 63 deletions
|
|
@ -1,10 +1,34 @@
|
|||
import { PageType } from '@/constants/pageTypes';
|
||||
import { usePreferenceObservable } from '@services/preferences/hooks';
|
||||
import type { IPreferences } from '@services/preferences/interface';
|
||||
import { WindowNames } from '@services/windows/WindowProperties';
|
||||
import { useWorkspacesListObservable } from '@services/workspaces/hooks';
|
||||
import type { IWorkspaceWithMetadata } from '@services/workspaces/interface';
|
||||
import { useEffect, useRef } from 'react';
|
||||
import { useLocation } from 'wouter';
|
||||
|
||||
/**
|
||||
* Helper function to determine the target workspace for tidgi mini window based on preferences
|
||||
*/
|
||||
function getTidgiMiniWindowTargetWorkspace(
|
||||
workspacesList: IWorkspaceWithMetadata[],
|
||||
preferences: IPreferences,
|
||||
): IWorkspaceWithMetadata | undefined {
|
||||
const { tidgiMiniWindowSyncWorkspaceWithMainWindow, tidgiMiniWindowFixedWorkspaceId } = preferences;
|
||||
// Default to sync (undefined means default to true, or explicitly true)
|
||||
const shouldSync = tidgiMiniWindowSyncWorkspaceWithMainWindow === undefined || tidgiMiniWindowSyncWorkspaceWithMainWindow;
|
||||
|
||||
if (shouldSync) {
|
||||
// Sync with main window - use active workspace
|
||||
return workspacesList.find(workspace => workspace.active);
|
||||
} else if (tidgiMiniWindowFixedWorkspaceId) {
|
||||
// Use fixed workspace
|
||||
return workspacesList.find(ws => ws.id === tidgiMiniWindowFixedWorkspaceId);
|
||||
}
|
||||
// No fixed workspace set - return undefined
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export function useInitialPage() {
|
||||
const [location, setLocation] = useLocation();
|
||||
const workspacesList = useWorkspacesListObservable();
|
||||
|
|
@ -21,17 +45,7 @@ export function useInitialPage() {
|
|||
|
||||
// For tidgi mini window, determine which workspace to show based on preferences
|
||||
if (windowName === WindowNames.tidgiMiniWindow && preferences) {
|
||||
const { tidgiMiniWindowSyncWorkspaceWithMainWindow, tidgiMiniWindowFixedWorkspaceId } = preferences;
|
||||
const shouldSync = tidgiMiniWindowSyncWorkspaceWithMainWindow === undefined || tidgiMiniWindowSyncWorkspaceWithMainWindow;
|
||||
|
||||
if (!shouldSync && tidgiMiniWindowFixedWorkspaceId) {
|
||||
// If not syncing with main window, use fixed workspace
|
||||
const fixedWorkspace = workspacesList.find(ws => ws.id === tidgiMiniWindowFixedWorkspaceId);
|
||||
if (fixedWorkspace) {
|
||||
targetWorkspace = fixedWorkspace;
|
||||
}
|
||||
}
|
||||
// Otherwise, use the active workspace (sync with main window)
|
||||
targetWorkspace = getTidgiMiniWindowTargetWorkspace(workspacesList, preferences) || targetWorkspace;
|
||||
}
|
||||
|
||||
if (!targetWorkspace) {
|
||||
|
|
@ -57,17 +71,8 @@ export function useInitialPage() {
|
|||
return;
|
||||
}
|
||||
|
||||
const { tidgiMiniWindowSyncWorkspaceWithMainWindow, tidgiMiniWindowFixedWorkspaceId } = preferences;
|
||||
const shouldSync = tidgiMiniWindowSyncWorkspaceWithMainWindow === undefined || tidgiMiniWindowSyncWorkspaceWithMainWindow;
|
||||
|
||||
// Determine target workspace
|
||||
let targetWorkspace = workspacesList.find(workspace => workspace.active);
|
||||
if (!shouldSync && tidgiMiniWindowFixedWorkspaceId) {
|
||||
const fixedWorkspace = workspacesList.find(ws => ws.id === tidgiMiniWindowFixedWorkspaceId);
|
||||
if (fixedWorkspace) {
|
||||
targetWorkspace = fixedWorkspace;
|
||||
}
|
||||
}
|
||||
// Determine target workspace using helper function
|
||||
const targetWorkspace = getTidgiMiniWindowTargetWorkspace(workspacesList, preferences);
|
||||
|
||||
if (!targetWorkspace) return;
|
||||
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@ import { logger } from '@services/libs/log';
|
|||
import type { INativeService } from '@services/native/interface';
|
||||
import { type IBrowserViewMetaData, WindowNames } from '@services/windows/WindowProperties';
|
||||
import { isWikiWorkspace, type IWorkspace } from '@services/workspaces/interface';
|
||||
import { getTidgiMiniWindowTargetWorkspace } from '@services/workspacesView/utilities';
|
||||
import debounce from 'lodash/debounce';
|
||||
import { setViewEventName } from './constants';
|
||||
import { ViewLoadUrlError } from './error';
|
||||
|
|
@ -412,22 +413,17 @@ export class View implements IViewService {
|
|||
public async setActiveViewForAllBrowserViews(workspaceID: string): Promise<void> {
|
||||
// Set main window workspace
|
||||
const mainWindowTask = this.setActiveView(workspaceID, WindowNames.main);
|
||||
const [tidgiMiniWindow, tidgiMiniWindowSyncWorkspaceWithMainWindow, tidgiMiniWindowFixedWorkspaceId] = await Promise.all([
|
||||
this.preferenceService.get('tidgiMiniWindow'),
|
||||
this.preferenceService.get('tidgiMiniWindowSyncWorkspaceWithMainWindow'),
|
||||
this.preferenceService.get('tidgiMiniWindowFixedWorkspaceId'),
|
||||
]);
|
||||
const tidgiMiniWindow = await this.preferenceService.get('tidgiMiniWindow');
|
||||
|
||||
// For tidgi mini window, decide which workspace to show based on preferences
|
||||
let tidgiMiniWindowTask = Promise.resolve();
|
||||
if (tidgiMiniWindow) {
|
||||
// Default to sync (undefined or true), otherwise use fixed workspace ID (fallback to main if not set)
|
||||
const shouldSync = tidgiMiniWindowSyncWorkspaceWithMainWindow === undefined || tidgiMiniWindowSyncWorkspaceWithMainWindow;
|
||||
const tidgiMiniWindowWorkspaceId = shouldSync ? workspaceID : (tidgiMiniWindowFixedWorkspaceId || workspaceID);
|
||||
const { targetWorkspaceId } = await getTidgiMiniWindowTargetWorkspace(workspaceID);
|
||||
const tidgiMiniWindowWorkspaceId = targetWorkspaceId || workspaceID;
|
||||
|
||||
logger.debug('setActiveViewForAllBrowserViews tidgi mini window decision', {
|
||||
function: 'setActiveViewForAllBrowserViews',
|
||||
shouldSync,
|
||||
tidgiMiniWindowWorkspaceId,
|
||||
willSetActiveView: true,
|
||||
});
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ import { container } from '@services/container';
|
|||
import getViewBounds from '@services/libs/getViewBounds';
|
||||
import { logger } from '@services/libs/log';
|
||||
import type { IThemeService } from '@services/theme/interface';
|
||||
import { getTidgiMiniWindowTargetWorkspace } from '@services/workspacesView/utilities';
|
||||
import { handleAttachToTidgiMiniWindow } from './handleAttachToTidgiMiniWindow';
|
||||
import { handleCreateBasicWindow } from './handleCreateBasicWindow';
|
||||
import type { IWindowOpenConfig, IWindowService } from './interface';
|
||||
|
|
@ -371,21 +372,12 @@ export class Window implements IWindowService {
|
|||
logger.debug('TidGi mini window is already enabled, bring it to front', { function: 'openTidgiMiniWindow' });
|
||||
if (showWindow) {
|
||||
// Before showing, get the target workspace
|
||||
const [tidgiMiniWindowSyncWorkspaceWithMainWindow, tidgiMiniWindowFixedWorkspaceId] = await Promise.all([
|
||||
this.preferenceService.get('tidgiMiniWindowSyncWorkspaceWithMainWindow'),
|
||||
this.preferenceService.get('tidgiMiniWindowFixedWorkspaceId'),
|
||||
]);
|
||||
const shouldSync = tidgiMiniWindowSyncWorkspaceWithMainWindow === undefined || tidgiMiniWindowSyncWorkspaceWithMainWindow;
|
||||
const targetWorkspaceId = shouldSync
|
||||
? (await container.get<IWorkspaceService>(serviceIdentifier.Workspace).getActiveWorkspace())?.id
|
||||
: tidgiMiniWindowFixedWorkspaceId;
|
||||
const { shouldSync, targetWorkspaceId } = await getTidgiMiniWindowTargetWorkspace();
|
||||
|
||||
logger.info('openTidgiMiniWindow: preparing to show window', {
|
||||
function: 'openTidgiMiniWindow',
|
||||
shouldSync,
|
||||
targetWorkspaceId,
|
||||
tidgiMiniWindowSyncWorkspaceWithMainWindow,
|
||||
tidgiMiniWindowFixedWorkspaceId,
|
||||
});
|
||||
|
||||
// Ensure view exists for the target workspace before realigning
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@ import { DELAY_MENU_REGISTER } from '@/constants/parameters';
|
|||
import type { ISyncService } from '@services/sync/interface';
|
||||
import type { IInitializeWorkspaceOptions, IWorkspaceViewService } from './interface';
|
||||
import { registerMenu } from './registerMenu';
|
||||
import { getTidgiMiniWindowTargetWorkspace } from './utilities';
|
||||
|
||||
@injectable()
|
||||
export class WorkspaceView implements IWorkspaceViewService {
|
||||
|
|
@ -207,22 +208,18 @@ export class WorkspaceView implements IWorkspaceViewService {
|
|||
|
||||
// For tidgi mini window, decide which workspace to show based on preferences
|
||||
const tidgiMiniWindowTask = (async () => {
|
||||
const [tidgiMiniWindow, tidgiMiniWindowSyncWorkspaceWithMainWindow, tidgiMiniWindowFixedWorkspaceId] = await Promise.all([
|
||||
this.preferenceService.get('tidgiMiniWindow'),
|
||||
this.preferenceService.get('tidgiMiniWindowSyncWorkspaceWithMainWindow'),
|
||||
this.preferenceService.get('tidgiMiniWindowFixedWorkspaceId'),
|
||||
]);
|
||||
const tidgiMiniWindow = await this.preferenceService.get('tidgiMiniWindow');
|
||||
if (!tidgiMiniWindow) {
|
||||
return;
|
||||
}
|
||||
// If syncing with main window (undefined means default to true, or explicitly true), use the current workspace
|
||||
const shouldSync = tidgiMiniWindowSyncWorkspaceWithMainWindow === undefined || tidgiMiniWindowSyncWorkspaceWithMainWindow;
|
||||
const { shouldSync, targetWorkspaceId } = await getTidgiMiniWindowTargetWorkspace(workspace.id);
|
||||
// If syncing with main window, use the current workspace
|
||||
if (shouldSync) {
|
||||
await container.get<IViewService>(serviceIdentifier.View).addView(workspace, WindowNames.tidgiMiniWindow);
|
||||
return;
|
||||
}
|
||||
// If not syncing and a fixed workspace is set, only add view if this IS the fixed workspace
|
||||
if (tidgiMiniWindowFixedWorkspaceId && workspace.id === tidgiMiniWindowFixedWorkspaceId) {
|
||||
if (targetWorkspaceId && workspace.id === targetWorkspaceId) {
|
||||
await container.get<IViewService>(serviceIdentifier.View).addView(workspace, WindowNames.tidgiMiniWindow);
|
||||
}
|
||||
// If not syncing and no fixed workspace is set, don't add any view (user needs to select one)
|
||||
|
|
@ -592,13 +589,8 @@ export class WorkspaceView implements IWorkspaceViewService {
|
|||
logger.info(`realignActiveWorkspaceView: no tidgiMiniWindowBrowserViewWebContent, skip tidgi mini window for ${workspaceToRealign.id}.`);
|
||||
} else {
|
||||
// For tidgi mini window, decide which workspace to show based on preferences
|
||||
const [tidgiMiniWindowSyncWorkspaceWithMainWindow, tidgiMiniWindowFixedWorkspaceId] = await Promise.all([
|
||||
this.preferenceService.get('tidgiMiniWindowSyncWorkspaceWithMainWindow'),
|
||||
this.preferenceService.get('tidgiMiniWindowFixedWorkspaceId'),
|
||||
]);
|
||||
// Default to sync (undefined or true), otherwise use fixed workspace ID (fallback to main if not set)
|
||||
const shouldSync = tidgiMiniWindowSyncWorkspaceWithMainWindow === undefined || tidgiMiniWindowSyncWorkspaceWithMainWindow;
|
||||
const tidgiMiniWindowWorkspaceId = shouldSync ? workspaceToRealign.id : (tidgiMiniWindowFixedWorkspaceId || workspaceToRealign.id);
|
||||
const { targetWorkspaceId } = await getTidgiMiniWindowTargetWorkspace(workspaceToRealign.id);
|
||||
const tidgiMiniWindowWorkspaceId = targetWorkspaceId || workspaceToRealign.id;
|
||||
tasks.push(container.get<IViewService>(serviceIdentifier.View).realignActiveView(tidgiMiniWindow, tidgiMiniWindowWorkspaceId, WindowNames.tidgiMiniWindow));
|
||||
}
|
||||
await Promise.all(tasks);
|
||||
|
|
@ -618,20 +610,15 @@ export class WorkspaceView implements IWorkspaceViewService {
|
|||
logger.debug(`hideWorkspaceView: no tidgiMiniWindowBrowserWindow, skip tidgi mini window browserView.`);
|
||||
} else {
|
||||
// For tidgi mini window, only hide if syncing with main window OR if this is the fixed workspace being deactivated
|
||||
const [tidgiMiniWindowSyncWorkspaceWithMainWindow, tidgiMiniWindowFixedWorkspaceId] = await Promise.all([
|
||||
this.preferenceService.get('tidgiMiniWindowSyncWorkspaceWithMainWindow'),
|
||||
this.preferenceService.get('tidgiMiniWindowFixedWorkspaceId'),
|
||||
]);
|
||||
// Default to sync (undefined or true)
|
||||
const shouldSync = tidgiMiniWindowSyncWorkspaceWithMainWindow === undefined || tidgiMiniWindowSyncWorkspaceWithMainWindow;
|
||||
const { shouldSync, targetWorkspaceId } = await getTidgiMiniWindowTargetWorkspace(idToDeactivate);
|
||||
// Only hide tidgi mini window view if:
|
||||
// 1. Syncing with main window (should hide when main window hides)
|
||||
// 2. OR the workspace being hidden is the fixed workspace (rare case, but should be handled)
|
||||
if (shouldSync || idToDeactivate === tidgiMiniWindowFixedWorkspaceId) {
|
||||
if (shouldSync || idToDeactivate === targetWorkspaceId) {
|
||||
logger.info(`hideWorkspaceView: hide tidgi mini window browserView.`);
|
||||
tasks.push(container.get<IViewService>(serviceIdentifier.View).hideView(tidgiMiniWindow, WindowNames.tidgiMiniWindow, idToDeactivate));
|
||||
} else {
|
||||
logger.debug(`hideWorkspaceView: skip hiding tidgi mini window browserView (fixed workspace: ${tidgiMiniWindowFixedWorkspaceId || 'none'}).`);
|
||||
logger.debug(`hideWorkspaceView: skip hiding tidgi mini window browserView (fixed workspace: ${targetWorkspaceId || 'none'}).`);
|
||||
}
|
||||
}
|
||||
await Promise.all(tasks);
|
||||
|
|
|
|||
34
src/services/workspacesView/utilities.ts
Normal file
34
src/services/workspacesView/utilities.ts
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
import { container } from '@services/container';
|
||||
import type { IPreferenceService } from '@services/preferences/interface';
|
||||
import serviceIdentifier from '@services/serviceIdentifier';
|
||||
import type { IWorkspaceService } from '@services/workspaces/interface';
|
||||
|
||||
/**
|
||||
* Helper function to determine the target workspace for tidgi mini window based on preferences
|
||||
* @param fallbackWorkspaceId - The workspace ID to use as fallback (usually the active/current workspace)
|
||||
* @returns Object containing shouldSync flag and targetWorkspaceId
|
||||
*/
|
||||
export async function getTidgiMiniWindowTargetWorkspace(fallbackWorkspaceId?: string): Promise<{
|
||||
shouldSync: boolean;
|
||||
targetWorkspaceId: string | undefined;
|
||||
}> {
|
||||
const preferenceService = container.get<IPreferenceService>(serviceIdentifier.Preference);
|
||||
const [tidgiMiniWindowSyncWorkspaceWithMainWindow, tidgiMiniWindowFixedWorkspaceId] = await Promise.all([
|
||||
preferenceService.get('tidgiMiniWindowSyncWorkspaceWithMainWindow'),
|
||||
preferenceService.get('tidgiMiniWindowFixedWorkspaceId'),
|
||||
]);
|
||||
|
||||
// Default to sync (undefined means default to true, or explicitly true)
|
||||
const shouldSync = tidgiMiniWindowSyncWorkspaceWithMainWindow === undefined || tidgiMiniWindowSyncWorkspaceWithMainWindow;
|
||||
|
||||
let targetWorkspaceId: string | undefined;
|
||||
if (shouldSync) {
|
||||
// Sync with main window - use fallback or active workspace
|
||||
targetWorkspaceId = fallbackWorkspaceId ?? (await container.get<IWorkspaceService>(serviceIdentifier.Workspace).getActiveWorkspace())?.id;
|
||||
} else {
|
||||
// Use fixed workspace
|
||||
targetWorkspaceId = tidgiMiniWindowFixedWorkspaceId;
|
||||
}
|
||||
|
||||
return { shouldSync, targetWorkspaceId };
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue