Team Ai
Apppublic

KBaba7/llama.cpp

sourceHugging Faceapache-2.0updated 2y agoView on Hugging Face
0likes
Header.tsx183 linesDownload Raw Back to components
1import { useEffect, useState } from 'react';2import StorageUtils from '../utils/storage';3import { useAppContext } from '../utils/app.context';4import { classNames } from '../utils/misc';5import daisyuiThemes from 'daisyui/src/theming/themes';6import { THEMES } from '../Config';7import { useNavigate } from 'react-router';8import SettingDialog from './SettingDialog';9 10export default function Header() {11  const navigate = useNavigate();12  const [selectedTheme, setSelectedTheme] = useState(StorageUtils.getTheme());13  const [showSettingDialog, setShowSettingDialog] = useState(false);14 15  const setTheme = (theme: string) => {16    StorageUtils.setTheme(theme);17    setSelectedTheme(theme);18  };19 20  useEffect(() => {21    document.body.setAttribute('data-theme', selectedTheme);22    document.body.setAttribute(23      'data-color-scheme',24      // @ts-expect-error daisyuiThemes complains about index type, but it should work25      daisyuiThemes[selectedTheme]?.['color-scheme'] ?? 'auto'26    );27  }, [selectedTheme]);28 29  const { isGenerating, viewingConversation } = useAppContext();30  const isCurrConvGenerating = isGenerating(viewingConversation?.id ?? '');31 32  const removeConversation = () => {33    if (isCurrConvGenerating || !viewingConversation) return;34    const convId = viewingConversation.id;35    if (window.confirm('Are you sure to delete this conversation?')) {36      StorageUtils.remove(convId);37      navigate('/');38    }39  };40 41  const downloadConversation = () => {42    if (isCurrConvGenerating || !viewingConversation) return;43    const convId = viewingConversation.id;44    const conversationJson = JSON.stringify(viewingConversation, null, 2);45    const blob = new Blob([conversationJson], { type: 'application/json' });46    const url = URL.createObjectURL(blob);47    const a = document.createElement('a');48    a.href = url;49    a.download = `conversation_${convId}.json`;50    document.body.appendChild(a);51    a.click();52    document.body.removeChild(a);53    URL.revokeObjectURL(url);54  };55 56  return (57    <div className="flex flex-row items-center mt-6 mb-6">58      {/* open sidebar button */}59      <label htmlFor="toggle-drawer" className="btn btn-ghost lg:hidden">60        <svg61          xmlns="http://www.w3.org/2000/svg"62          width="16"63          height="16"64          fill="currentColor"65          className="bi bi-list"66          viewBox="0 0 16 16"67        >68          <path69            fillRule="evenodd"70            d="M2.5 12a.5.5 0 0 1 .5-.5h10a.5.5 0 0 1 0 1H3a.5.5 0 0 1-.5-.5m0-4a.5.5 0 0 1 .5-.5h10a.5.5 0 0 1 0 1H3a.5.5 0 0 1-.5-.5m0-4a.5.5 0 0 1 .5-.5h10a.5.5 0 0 1 0 1H3a.5.5 0 0 1-.5-.5"71          />72        </svg>73      </label>74 75      <div className="grow text-2xl font-bold ml-2">llama.cpp</div>76 77      {/* action buttons (top right) */}78      <div className="flex items-center">79        <div v-if="messages.length > 0" className="dropdown dropdown-end">80          {/* "..." button */}81          <button82            tabIndex={0}83            role="button"84            className="btn m-1"85            disabled={isCurrConvGenerating}86          >87            <svg88              xmlns="http://www.w3.org/2000/svg"89              width="16"90              height="16"91              fill="currentColor"92              className="bi bi-three-dots-vertical"93              viewBox="0 0 16 16"94            >95              <path d="M9.5 13a1.5 1.5 0 1 1-3 0 1.5 1.5 0 0 1 3 0m0-5a1.5 1.5 0 1 1-3 0 1.5 1.5 0 0 1 3 0m0-5a1.5 1.5 0 1 1-3 0 1.5 1.5 0 0 1 3 0" />96            </svg>97          </button>98          {/* dropdown menu */}99          <ul100            tabIndex={0}101            className="dropdown-content menu bg-base-100 rounded-box z-[1] w-52 p-2 shadow"102          >103            <li onClick={downloadConversation}>104              <a>Download</a>105            </li>106            <li className="text-error" onClick={removeConversation}>107              <a>Delete</a>108            </li>109          </ul>110        </div>111        <div className="tooltip tooltip-bottom" data-tip="Settings">112          <button className="btn" onClick={() => setShowSettingDialog(true)}>113            {/* settings button */}114            <svg115              xmlns="http://www.w3.org/2000/svg"116              width="16"117              height="16"118              fill="currentColor"119              className="bi bi-gear"120              viewBox="0 0 16 16"121            >122              <path d="M8 4.754a3.246 3.246 0 1 0 0 6.492 3.246 3.246 0 0 0 0-6.492M5.754 8a2.246 2.246 0 1 1 4.492 0 2.246 2.246 0 0 1-4.492 0" />123              <path d="M9.796 1.343c-.527-1.79-3.065-1.79-3.592 0l-.094.319a.873.873 0 0 1-1.255.52l-.292-.16c-1.64-.892-3.433.902-2.54 2.541l.159.292a.873.873 0 0 1-.52 1.255l-.319.094c-1.79.527-1.79 3.065 0 3.592l.319.094a.873.873 0 0 1 .52 1.255l-.16.292c-.892 1.64.901 3.434 2.541 2.54l.292-.159a.873.873 0 0 1 1.255.52l.094.319c.527 1.79 3.065 1.79 3.592 0l.094-.319a.873.873 0 0 1 1.255-.52l.292.16c1.64.893 3.434-.902 2.54-2.541l-.159-.292a.873.873 0 0 1 .52-1.255l.319-.094c1.79-.527 1.79-3.065 0-3.592l-.319-.094a.873.873 0 0 1-.52-1.255l.16-.292c.893-1.64-.902-3.433-2.541-2.54l-.292.159a.873.873 0 0 1-1.255-.52zm-2.633.283c.246-.835 1.428-.835 1.674 0l.094.319a1.873 1.873 0 0 0 2.693 1.115l.291-.16c.764-.415 1.6.42 1.184 1.185l-.159.292a1.873 1.873 0 0 0 1.116 2.692l.318.094c.835.246.835 1.428 0 1.674l-.319.094a1.873 1.873 0 0 0-1.115 2.693l.16.291c.415.764-.42 1.6-1.185 1.184l-.291-.159a1.873 1.873 0 0 0-2.693 1.116l-.094.318c-.246.835-1.428.835-1.674 0l-.094-.319a1.873 1.873 0 0 0-2.692-1.115l-.292.16c-.764.415-1.6-.42-1.184-1.185l.159-.291A1.873 1.873 0 0 0 1.945 8.93l-.319-.094c-.835-.246-.835-1.428 0-1.674l.319-.094A1.873 1.873 0 0 0 3.06 4.377l-.16-.292c-.415-.764.42-1.6 1.185-1.184l.292.159a1.873 1.873 0 0 0 2.692-1.115z" />124            </svg>125          </button>126        </div>127 128        {/* theme controller is copied from https://daisyui.com/components/theme-controller/ */}129        <div className="tooltip tooltip-bottom" data-tip="Themes">130          <div className="dropdown dropdown-end dropdown-bottom">131            <div tabIndex={0} role="button" className="btn m-1">132              <svg133                xmlns="http://www.w3.org/2000/svg"134                width="16"135                height="16"136                fill="currentColor"137                className="bi bi-palette2"138                viewBox="0 0 16 16"139              >140                <path d="M0 .5A.5.5 0 0 1 .5 0h5a.5.5 0 0 1 .5.5v5.277l4.147-4.131a.5.5 0 0 1 .707 0l3.535 3.536a.5.5 0 0 1 0 .708L10.261 10H15.5a.5.5 0 0 1 .5.5v5a.5.5 0 0 1-.5.5H3a3 3 0 0 1-2.121-.879A3 3 0 0 1 0 13.044m6-.21 7.328-7.3-2.829-2.828L6 7.188zM4.5 13a1.5 1.5 0 1 0-3 0 1.5 1.5 0 0 0 3 0M15 15v-4H9.258l-4.015 4zM0 .5v12.495zm0 12.495V13z" />141              </svg>142            </div>143            <ul144              tabIndex={0}145              className="dropdown-content bg-base-300 rounded-box z-[1] w-52 p-2 shadow-2xl h-80 overflow-y-auto"146            >147              <li>148                <button149                  className={classNames({150                    'btn btn-sm btn-block btn-ghost justify-start': true,151                    'btn-active': selectedTheme === 'auto',152                  })}153                  onClick={() => setTheme('auto')}154                >155                  auto156                </button>157              </li>158              {THEMES.map((theme) => (159                <li key={theme}>160                  <input161                    type="radio"162                    name="theme-dropdown"163                    className="theme-controller btn btn-sm btn-block btn-ghost justify-start"164                    aria-label={theme}165                    value={theme}166                    checked={selectedTheme === theme}167                    onChange={(e) => e.target.checked && setTheme(theme)}168                  />169                </li>170              ))}171            </ul>172          </div>173        </div>174      </div>175 176      <SettingDialog177        show={showSettingDialog}178        onClose={() => setShowSettingDialog(false)}179      />180    </div>181  );182}183