1// Copyright (C) 2023 The Android Open Source Project 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15import m from 'mithril'; 16import {copyToClipboard} from '../base/clipboard'; 17import {Icons} from '../base/semantic_icons'; 18import {Anchor} from './anchor'; 19import {MenuItem, PopupMenu2} from './menu'; 20 21// This widget provides common styling and popup menu options for a SQL row, 22// given a table name and an ID. 23export interface SqlRefAttrs { 24 // The name of the table our row lives in. 25 table: string; 26 // The ID of our row. 27 // If not provided, `table[Unknown]` is shown with no popup menu. 28 id?: number; 29} 30 31export class SqlRef implements m.ClassComponent<SqlRefAttrs> { 32 view({attrs}: m.CVnode<SqlRefAttrs>) { 33 const {table, id} = attrs; 34 if (id !== undefined) { 35 return m( 36 PopupMenu2, 37 { 38 trigger: m(Anchor, {icon: Icons.ContextMenu}, `${table}[${id}]`), 39 }, 40 m(MenuItem, { 41 label: 'Copy ID', 42 icon: 'content_copy', 43 onclick: () => copyToClipboard(`${id}`), 44 }), 45 m(MenuItem, { 46 label: 'Copy SQL query', 47 icon: 'file_copy', 48 onclick: () => 49 copyToClipboard(`select * from ${table} where id=${id}`), 50 }), 51 ); 52 } else { 53 return `${table}[Unknown]`; 54 } 55 } 56} 57