Skip to content

A plugin marketplace for GitHub Copilot and other AI agents that provides Power Platform development plugins, including reusable skills, agents, and commands for building and deploying solutions.

7.3 KB
Raw Blame
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <script src="https://unpkg.com/@fluentui/web-components@beta/dist/web-components.min.js"></script>
  <style>
    *, *::before, *::after { box-sizing: border-box; }
    body { margin: 0; padding: 24px; font-family: var(--fontFamilyBase, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif); font-size: 14px; line-height: 1.5; background: var(--colorNeutralBackground1, #fff); color: var(--colorNeutralForeground1, #242424); overflow-x: hidden; }
    .header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px; }
    .city { font-size: 20px; font-weight: 600; }
    .conditions { font-size: 13px; color: var(--colorNeutralForeground3, #707070); margin-top: 2px; }
    .temp { font-size: 32px; font-weight: 700; }
    .humidity { font-size: 13px; color: var(--colorNeutralForeground3, #707070); margin-top: 4px; }
    .forecast-strip { display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px; margin-top: 16px; padding-top: 16px; border-top: 1px solid var(--colorNeutralStroke2, #e0e0e0); }
    .forecast-day { text-align: center; padding: 8px 4px; border-radius: 8px; background: var(--colorNeutralBackground2, #f5f5f5); }
    .day-name { font-size: 12px; font-weight: 600; }
    .day-icon { font-size: 20px; margin: 4px 0; }
    .day-temps { font-size: 12px; color: var(--colorNeutralForeground3, #707070); }
    .refresh-row { display: flex; justify-content: space-between; align-items: center; margin-top: 12px; }
    .updated { font-size: 11px; color: var(--colorNeutralForeground3, #707070); }
    .error { color: var(--colorPaletteRedForeground1, #bc2f32); }
  </style>
</head>
<body>
  <div id="widget-root">
    <fluent-spinner></fluent-spinner>
    <span style="margin-left:8px;">Loading weather...</span>
  </div>
  <script type="module">
    import { App } from 'https://cdn.jsdelivr.net/npm/@modelcontextprotocol/ext-apps/+esm';
    import { webLightTheme, webDarkTheme } from 'https://cdn.jsdelivr.net/npm/@fluentui/tokens/+esm';

    function applyTheme(theme) {
      const tokens = theme === 'dark' ? webDarkTheme : webLightTheme;
      for (const [token, value] of Object.entries(tokens)) {
        document.documentElement.style.setProperty('--' + token, value);
      }
    }

    function escapeHtml(text) {
      const div = document.createElement('div');
      div.textContent = String(text ?? '');
      return div.innerHTML;
    }

    function getWeatherEmoji(conditions) {
      const c = String(conditions ?? '').toLowerCase();
      if (c.includes('sunny') || c.includes('clear')) { return '\u2600\uFE0F'; }
      if (c.includes('partly')) { return '\u26C5'; }
      if (c.includes('cloud')) { return '\u2601\uFE0F'; }
      if (c.includes('rain')) { return '\uD83C\uDF27\uFE0F'; }
      if (c.includes('storm') || c.includes('thunder')) { return '\u26C8\uFE0F'; }
      if (c.includes('snow')) { return '\u2744\uFE0F'; }
      return '\uD83C\uDF24\uFE0F';
    }

    const root = document.getElementById('widget-root');
    let appRef = null;
    let lastUpdated = null;

    function renderLoading() {
      root.innerHTML = '<fluent-spinner></fluent-spinner><span style="margin-left:8px;">Loading weather...</span>';
    }

    function renderError(msg) {
      root.innerHTML = '<div class="error">' + escapeHtml(msg) + '</div>';
    }

    function contentText(content) {
      if (typeof content === 'string') { return content; }
      if (!Array.isArray(content)) { return ''; }
      return content
        .filter((block) => block?.type === 'text' && typeof block.text === 'string')
        .map((block) => block.text)
        .join('\n');
    }

    function renderMessage(message) {
      root.innerHTML = '<div>' + escapeHtml(message) + '</div>';
    }

    function renderData(toolResult) {
      const data = toolResult.structuredContent;
      const temp = parseFloat(data.temperature);
      const humidity = parseFloat(data.humidity);
      const forecast = Array.isArray(data.forecast) ? data.forecast : [];
      lastUpdated = new Date().toLocaleTimeString('en-US', { hour: '2-digit', minute: '2-digit' });

      let forecastHtml = '';
      for (const day of forecast) {
        const hi = parseFloat(day.high);
        const lo = parseFloat(day.low);
        forecastHtml +=
          '<div class="forecast-day">' +
            '<div class="day-name">' + escapeHtml(day.day) + '</div>' +
            '<div class="day-icon">' + getWeatherEmoji(day.conditions) + '</div>' +
            '<div class="day-temps">' + (isFinite(hi) ? Math.round(hi) : '--') + '/' + (isFinite(lo) ? Math.round(lo) : '--') + '</div>' +
          '</div>';
      }

      root.innerHTML =
        '<div class="header">' +
          '<div>' +
            '<div class="city">' + getWeatherEmoji(data.conditions) + ' ' + escapeHtml(data.city) + '</div>' +
            '<div class="conditions">' + escapeHtml(data.conditions) + '</div>' +
          '</div>' +
          '<div>' +
            '<div class="temp">' + (isFinite(temp) ? Math.round(temp) + '\u00B0F' : '--') + '</div>' +
            '<div class="humidity">Humidity: ' + (isFinite(humidity) ? Math.round(humidity) + '%' : '--') + '</div>' +
          '</div>' +
        '</div>' +
        '<div class="forecast-strip">' + forecastHtml + '</div>' +
        '<div class="refresh-row">' +
          '<span class="updated">Updated ' + escapeHtml(lastUpdated) + '</span>' +
          '<fluent-button id="refresh-btn" appearance="subtle" size="small">Refresh</fluent-button>' +
        '</div>';

      document.getElementById('refresh-btn').addEventListener('click', handleRefresh);
    }

    // callServerTool example: the Refresh button calls the tool again to get fresh data
    async function handleRefresh() {
      const btn = document.getElementById('refresh-btn');
      if (btn) {
        btn.setAttribute('disabled', '');
        btn.textContent = 'Refreshing...';
      }
      try {
        const result = await appRef.callServerTool({
          name: 'get_weather',
          arguments: { city: 'Seattle' }
        });
        if (result.isError) {
          renderError('Failed to refresh: ' + (result.structuredContent?.message || 'Unknown error'));
        } else {
          renderToolResult(result);
        }
      } catch (err) {
        renderError('Refresh failed: ' + err.message);
      }
    }

    renderLoading();
    const app = new App({ name: "weather-refresh", version: "1.0.0" });
    appRef = app;

    function renderToolResult(result) {
      const toolResult = {
        content: result.content ?? [],
        structuredContent: result.structuredContent ?? {},
        _meta: result._meta ?? {}
      };
      if (Object.keys(toolResult.structuredContent).length > 0) {
        renderData(toolResult);
      } else {
        const message = contentText(toolResult.content);
        if (message) {
          renderMessage(message);
        } else {
          renderError('No weather data received.');
        }
      }
    }

    app.ontoolresult = renderToolResult;
    app.onhostcontextchanged = (ctx) => { if (ctx.theme) { applyTheme(ctx.theme); } };
    app.onteardown = () => ({});
    await app.connect();
    const hostCtx = app.getHostContext();
    if (hostCtx?.theme) { applyTheme(hostCtx.theme); }
  </script>
</body>
</html>