Skip to content

Commit

Permalink
messaging/internal links -- improve support for handling links in con…
Browse files Browse the repository at this point in the history
…text outside of any project
  • Loading branch information
williamstein committed Dec 3, 2024
1 parent 3f51388 commit b673029
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 20 deletions.
11 changes: 11 additions & 0 deletions src/packages/frontend/components/smart-anchor-tag.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
} from "@cocalc/util/misc";
import { TITLE as SERVERS_TITLE } from "../project/servers";
import { alert_message } from "@cocalc/frontend/alerts";
import { load_target as globalLoadTarget } from "@cocalc/frontend/history";

interface Options {
project_id: string;
Expand Down Expand Up @@ -349,6 +350,16 @@ function InternalRelativeLink({ project_id, path, href, title, children }) {
onClick={(e) => {
e.preventDefault();
e.stopPropagation();

if (!project_id) {
// link is being opened outside of any specific project, e.g.,
// opening /settings outside of a project will open cocalc-wide
// settings for the user, not project settings. E.g.,
// this could happen in the messages panel.
globalLoadTarget(href);
return;
}

const dir = containingPath(path);
const url = new URL("http://dummy/" + join(dir, href));
const fragmentId = Fragment.decode(url.hash);
Expand Down
33 changes: 21 additions & 12 deletions src/packages/frontend/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,32 @@ export function load_target(
ignore_kiosk: boolean = false,
change_history: boolean = true,
) {
if (target?.[0] == "/") {
target = target.slice(1);
}
let hash;
const i = target.lastIndexOf("#");
if (i != -1) {
hash = target.slice(i + 1);
target = target.slice(0, i);
} else {
hash = "";
}
if (!target) {
return;
}
const logged_in = redux.getStore("account").get("is_logged_in");
if (!redux.getStore("account").get("is_logged_in")) {
// this will redirect to the sign in page after a brief pause
redux.getActions("page").set_active_tab("account", false);
return;
}

const segments = target.split("/");
switch (segments[0]) {
case "help":
redux.getActions("page").set_active_tab("about", change_history);
break;

case "projects":
if (segments.length > 1) {
redux
Expand All @@ -130,10 +147,8 @@ export function load_target(
redux.getActions("page").set_active_tab("projects", change_history);
}
break;

case "settings":
if (!logged_in) {
return;
}
redux.getActions("page").set_active_tab("account", false);

if (segments[1] === "billing") {
Expand All @@ -154,21 +169,15 @@ export function load_target(
break;

case "notifications":
if (!logged_in) return;
const { filter, id } = getNotificationFilterFromFragment();
if (filter) {
redux.getActions("mentions").set_filter(filter, id);
}
const { filter, id } = getNotificationFilterFromFragment(hash);
redux.getActions("mentions").set_filter(filter, id);
redux.getActions("page").set_active_tab("notifications", change_history);
break;

case "file-use":
// not implemented
break;
case "admin":
if (!logged_in) {
return;
}
redux.getActions("page").set_active_tab(segments[0], change_history);
break;
}
Expand Down
12 changes: 7 additions & 5 deletions src/packages/frontend/messages/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -421,11 +421,13 @@ function ShowOneThread({
// so change state to viewing threads instead... soon.
// (Always stay in a search view though!)
const message = messages.get(showThread);
const inFolder = folder=='search' || isInFolderThreaded({
threads,
message,
folder,
});
const inFolder =
folder == "search" ||
isInFolderThreaded({
threads,
message,
folder,
});
if (message != null && inFolder) {
// Maybe thread exists, but has a newer HEAD, so change to that.
const thread_id = getThreadId(message);
Expand Down
2 changes: 1 addition & 1 deletion src/packages/frontend/messages/redux.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ Body: ${message.get("body")}
async (query: string) => {
if (!query.trim()) {
// easy special case
this.setState({ search: new Set() });
this.setState({ search: new Set(), searchWords: new Set() });
return;
}
// used for highlighting
Expand Down
4 changes: 2 additions & 2 deletions src/packages/frontend/notifications/fragment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
import Fragment from "@cocalc/frontend/misc/fragment-id";
import { NotificationFilter, isNotificationFilter } from "./mentions/types";

export function getNotificationFilterFromFragment(): {
export function getNotificationFilterFromFragment(hash?): {
filter: NotificationFilter;
id?: number;
} {
const fragmentId = Fragment.get();
const fragmentId = hash ? Fragment.decode(hash) : Fragment.get();
if (fragmentId == null) {
return { filter: "messages-inbox" as NotificationFilter };
}
Expand Down

0 comments on commit b673029

Please sign in to comment.