• 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';
16
17import {copyToClipboard} from '../base/clipboard';
18import {Icons} from '../base/semantic_icons';
19
20import {Anchor} from './anchor';
21import {MenuItem, PopupMenu2} from './menu';
22
23// This widget provides common styling and popup menu options for a SQL row,
24// given a table name and an ID.
25export interface SqlRefAttrs {
26  // The name of the table our row lives in.
27  table: string;
28  // The ID of our row.
29  // If not provided, `table[Unknown]` is shown with no popup menu.
30  id?: number;
31}
32
33export class SqlRef implements m.ClassComponent<SqlRefAttrs> {
34  view({attrs}: m.CVnode<SqlRefAttrs>) {
35    const {table, id} = attrs;
36    if (id !== undefined) {
37      return m(
38        PopupMenu2,
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      );
54    } else {
55      return `${table}[Unknown]`;
56    }
57  }
58}
59