Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/frontend/compute/inline.tsx
Views: 687
/*1Simple inline display of the compute server with given id, for use2elsewhere in cocalc.34This may get more sophisticated, e.g., clickable link, hover for status, etc.5*/67import { useEffect, useState } from "react";8import getTitle from "./get-title";9import { Spin, Tooltip } from "antd";10import { avatar_fontcolor } from "@cocalc/frontend/account/avatar/font-color";11import { PROJECT_COLOR } from "./select-server";12import { trunc_middle } from "@cocalc/util/misc";1314interface Props {15id: number;16noColor?: boolean;17colorOnly?: boolean;18style?;19idOnly?: boolean;20titleOnly?: boolean;21prompt?: boolean;22computeServer?; // immutable js object from store, if known23colorLabel?; // put in middle of colorOnly24}2526export default function ComputeServer({27id,28noColor,29colorOnly,30style,31titleOnly,32idOnly,33prompt,34computeServer,35colorLabel,36}: Props) {37const [server, setServer] = useState<null | {38title: string;39color: string;40project_specific_id: number;41}>(42computeServer != null43? {44title: computeServer.get("title"),45color: computeServer.get("color"),46project_specific_id: computeServer.get("project_specific_id"),47}48: null,49);50useEffect(() => {51if (computeServer != null) {52setServer({53title: computeServer.get("title"),54color: computeServer.get("color"),55project_specific_id: computeServer.get("project_specific_id"),56});57return;58}59if (!id) {60setServer({61title: "Home Base",62color: PROJECT_COLOR,63project_specific_id: 0,64});65return;66}67(async () => {68try {69setServer(await getTitle(id));70} catch (err) {71console.warn(err);72}73})();74}, [id, computeServer]);7576if (colorOnly) {77return (78<div79style={{80backgroundColor: server?.color,81height: "3px",82textAlign: "center",83color: server?.color ? avatar_fontcolor(server?.color) : undefined,84...style,85}}86>87{colorLabel}88</div>89);90}9192if (prompt) {93const s = (94<span style={style}>95compute-server-{server?.project_specific_id ?? "?"}96</span>97);98if (server == null) {99return s;100}101return (102<Tooltip title={<>Compute Server '{trunc_middle(server.title, 40)}'</>}>103{s}104</Tooltip>105);106}107108if (server == null) {109return (110<span style={style}>111<Spin />112</span>113);114}115let label;116if (idOnly) {117label = `Id: ${server.project_specific_id}`;118} else {119label = titleOnly ? (120trunc_middle(server.title, 30)121) : (122<>123Compute Server '{trunc_middle(server.title, 30)}' (Id:{" "}124{server.project_specific_id})125</>126);127}128if (noColor) {129return <span style={style}>{label}</span>;130}131return (132<span133style={{134backgroundColor: server.color,135color: avatar_fontcolor(server.color),136overflow: "hidden",137padding: "0 5px",138borderRadius: "3px",139...style,140}}141>142{label}143</span>144);145}146147148