• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, PopupMenu} 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 | bigint;
29  // Optional additional popup menu items.
30  additionalMenuItems?: m.Children;
31}
32
33export class SqlRef implements m.ClassComponent<SqlRefAttrs> {
34  view({attrs}: m.CVnode<SqlRefAttrs>) {
35    const {table, id, additionalMenuItems} = attrs;
36    if (id !== undefined) {
37      return m(
38        PopupMenu,
39        {
40          trigger: m(Anchor, {icon: Icons.ContextMenu}, `${table}[${id}]`),
41        },
42        m(MenuItem, {
43          label: 'Copy ID',
44          icon: 'content_copy',
45          onclick: () => copyToClipboard(`${id}`),
46        }),
47        m(MenuItem, {
48          label: 'Copy SQL query',
49          icon: 'file_copy',
50          onclick: () =>
51            copyToClipboard(`select * from ${table} where id=${id}`),
52        }),
53        additionalMenuItems,
54      );
55    } else {
56      return `${table}[Unknown]`;
57    }
58  }
59}
60