Some checks failed
build-packages / resolve bundled mosh-client (push) Has been cancelled
build-packages / resolve bundled et-client (push) Has been cancelled
build-packages / build-macos (push) Has been cancelled
build-packages / build-windows (push) Has been cancelled
build-packages / build-linux-x64 (push) Has been cancelled
build-packages / build-linux-arm64 (push) Has been cancelled
build-packages / release (push) Has been cancelled
build-packages / update Nix release metadata (push) Has been cancelled
build-packages / bump homebrew tap (push) Has been cancelled
test / lint-and-test (push) Has been cancelled
AI automation / Route event (push) Has been cancelled
AI automation / Hand reopened issue to maintainers (push) Has been cancelled
AI automation / Clean source issue state (push) Has been cancelled
AI automation / Reconcile handoffs (push) Has been cancelled
AI automation / Classify issue (push) Has been cancelled
AI automation / Claude Code smoke (push) Has been cancelled
AI automation / Review issue follow-up (push) Has been cancelled
AI automation / Publish issue follow-up (push) Has been cancelled
AI automation / Implement with Claude Code (push) Has been cancelled
AI automation / Publish implement PR (push) Has been cancelled
AI automation / Continue queued issue comments (push) Has been cancelled
AI automation / Codex review loop (push) Has been cancelled
AI automation / Publish Codex fix (push) Has been cancelled
AI automation / Clear Codex dispatch marker (push) Has been cancelled
AI automation / Own PR re-request Codex (push) Has been cancelled
AI automation / External PR re-request Codex (push) Has been cancelled
AI automation / Poll Codex reaction / retry (push) Has been cancelled
build-et-binaries / build-linux-x64 (push) Has been cancelled
build-et-binaries / build-linux-arm64 (push) Has been cancelled
build-et-binaries / build-macos-universal (push) Has been cancelled
build-et-binaries / build-windows-x64 (push) Has been cancelled
build-et-binaries / release (push) Has been cancelled
94 lines
2.9 KiB
TypeScript
94 lines
2.9 KiB
TypeScript
import { useCallback, useMemo, useSyncExternalStore } from "react";
|
|
import type { SftpBookmark } from "../../../domain/models";
|
|
import { localStorageAdapter } from "../../../infrastructure/persistence/localStorageAdapter";
|
|
import { STORAGE_KEY_SFTP_LOCAL_BOOKMARKS } from "../../../infrastructure/config/storageKeys";
|
|
import { createSftpBookmark, moveSftpBookmark, renameSftpBookmark } from "./bookmarkHelpers";
|
|
|
|
type Listener = () => void;
|
|
|
|
const listeners = new Set<Listener>();
|
|
|
|
let snapshot: SftpBookmark[] =
|
|
localStorageAdapter.read<SftpBookmark[]>(STORAGE_KEY_SFTP_LOCAL_BOOKMARKS) ?? [];
|
|
|
|
export function subscribeLocalSftpBookmarks(listener: Listener) {
|
|
listeners.add(listener);
|
|
return () => {
|
|
listeners.delete(listener);
|
|
};
|
|
}
|
|
|
|
export function getLocalSftpBookmarksSnapshot() {
|
|
return snapshot;
|
|
}
|
|
|
|
export function rehydrateLocalSftpBookmarks() {
|
|
snapshot = localStorageAdapter.read<SftpBookmark[]>(STORAGE_KEY_SFTP_LOCAL_BOOKMARKS) ?? [];
|
|
for (const listener of listeners) listener();
|
|
}
|
|
|
|
export function setLocalSftpBookmarks(
|
|
next: SftpBookmark[] | ((prev: SftpBookmark[]) => SftpBookmark[]),
|
|
) {
|
|
snapshot = typeof next === "function" ? next(snapshot) : next;
|
|
localStorageAdapter.write(STORAGE_KEY_SFTP_LOCAL_BOOKMARKS, snapshot);
|
|
for (const listener of listeners) listener();
|
|
}
|
|
|
|
if (typeof window !== "undefined") {
|
|
window.addEventListener("storage", (event) => {
|
|
if (event.key === STORAGE_KEY_SFTP_LOCAL_BOOKMARKS) {
|
|
rehydrateLocalSftpBookmarks();
|
|
}
|
|
});
|
|
}
|
|
|
|
interface UseLocalSftpBookmarksParams {
|
|
currentPath: string | undefined;
|
|
}
|
|
|
|
export const useLocalSftpBookmarks = ({
|
|
currentPath,
|
|
}: UseLocalSftpBookmarksParams) => {
|
|
const bookmarks = useSyncExternalStore(
|
|
subscribeLocalSftpBookmarks,
|
|
getLocalSftpBookmarksSnapshot,
|
|
getLocalSftpBookmarksSnapshot,
|
|
);
|
|
|
|
const isCurrentPathBookmarked = useMemo(
|
|
() => !!currentPath && bookmarks.some((b) => b.path === currentPath),
|
|
[currentPath, bookmarks],
|
|
);
|
|
|
|
const toggleBookmark = useCallback(() => {
|
|
if (!currentPath) return;
|
|
if (isCurrentPathBookmarked) {
|
|
setLocalSftpBookmarks((prev) => prev.filter((b) => b.path !== currentPath));
|
|
} else {
|
|
setLocalSftpBookmarks((prev) => [...prev, createSftpBookmark(currentPath)]);
|
|
}
|
|
}, [currentPath, isCurrentPathBookmarked]);
|
|
|
|
const deleteBookmark = useCallback((id: string) => {
|
|
setLocalSftpBookmarks((prev) => prev.filter((b) => b.id !== id));
|
|
}, []);
|
|
|
|
const reorderBookmark = useCallback((fromId: string, toId: string) => {
|
|
setLocalSftpBookmarks((prev) => moveSftpBookmark(prev, fromId, toId));
|
|
}, []);
|
|
|
|
const renameBookmark = useCallback((id: string, label: string) => {
|
|
setLocalSftpBookmarks((prev) => renameSftpBookmark(prev, id, label));
|
|
}, []);
|
|
|
|
return {
|
|
bookmarks,
|
|
isCurrentPathBookmarked,
|
|
toggleBookmark,
|
|
deleteBookmark,
|
|
reorderBookmark,
|
|
renameBookmark,
|
|
};
|
|
};
|