Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,16 @@ const ANNOUNCEMENT_TYPES = [
];

const PRIORITY_OPTIONS = [
{ value: 'low', label: 'Low' },
{ value: 'normal', label: 'Normal' },
{ value: 'high', label: 'High' },
{ value: 'urgent', label: 'Urgent' },
];

const BOARD_TEAMS: { value: BoardTeam; label: string }[] = [
{ value: 'tech', label: 'Tech' },
{ value: 'finance', label: 'Finance' },
{ value: 'marketing', label: 'Marketing' },
{ value: 'design', label: 'Design' },
{ value: 'project_managers', label: 'Project Managers' },
];

export function DashboardAnnouncementModal({
Expand Down
56 changes: 51 additions & 5 deletions src/components/portal/project/events/AnnouncementForm.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use client";

import React, { useState } from 'react';
import { CreateAnnouncementInput } from '@/types/events';
import { CreateAnnouncementInput, AnnouncementVisibility, BoardTeam } from '@/types/events';

interface AnnouncementFormProps {
projectId: string;
Expand All @@ -19,10 +19,23 @@ const ANNOUNCEMENT_TYPES = [
];

const PRIORITY_OPTIONS = [
{ value: 'low', label: 'Low' },
{ value: 'normal', label: 'Normal' },
{ value: 'high', label: 'High' },
{ value: 'urgent', label: 'Urgent' },
];

const VISIBILITY_OPTIONS = [
{ value: 'project', label: 'Project Only' },
{ value: 'team', label: 'Specific Team' },
{ value: 'board', label: 'Board Members' },
{ value: 'club_wide', label: 'Entire Club' },
];

const TEAM_OPTIONS = [
{ value: 'tech', label: 'Tech' },
{ value: 'finance', label: 'Finance' },
{ value: 'marketing', label: 'Marketing' },
{ value: 'design', label: 'Design' },
{ value: 'project_managers', label: 'Project Managers' },
];

export function AnnouncementForm({
Expand All @@ -40,6 +53,8 @@ export function AnnouncementForm({
const [linkUrl, setLinkUrl] = useState('');
const [linkText, setLinkText] = useState('');
const [expireDate, setExpireDate] = useState('');
const [visibility, setVisibility] = useState<AnnouncementVisibility>('project');
const [targetTeam, setTargetTeam] = useState<BoardTeam | ''>('');
const [errors, setErrors] = useState<Record<string, string>>({});

const validate = (): boolean => {
Expand Down Expand Up @@ -68,8 +83,8 @@ export function AnnouncementForm({
publish_date: new Date().toISOString(),
expire_date: expireDate ? new Date(expireDate).toISOString() : null,
is_active: true,
visibility: 'project',
target_team: null,
visibility,
target_team: visibility === 'team' ? (targetTeam as BoardTeam) : null,
};

await onSubmit(input);
Expand Down Expand Up @@ -131,6 +146,37 @@ export function AnnouncementForm({
</div>
</div>

<div className="grid grid-cols-2 gap-4">
<div>
<label className={labelClass}>Visibility</label>
<select
value={visibility}
onChange={(e) => setVisibility(e.target.value as AnnouncementVisibility)}
className={inputClass}
>
{VISIBILITY_OPTIONS.map(opt => (
<option key={opt.value} value={opt.value}>{opt.label}</option>
))}
</select>
</div>
{visibility === 'team' && (
<div>
<label className={labelClass}>Target Team</label>
<select
value={targetTeam}
onChange={(e) => setTargetTeam(e.target.value as BoardTeam)}
className={inputClass}
required
>
<option value="">Select a team</option>
{TEAM_OPTIONS.map(opt => (
<option key={opt.value} value={opt.value}>{opt.label}</option>
))}
</select>
</div>
)}
</div>

<div>
<label className={labelClass}>Expiration Date</label>
<input
Expand Down
2 changes: 1 addition & 1 deletion src/types/events.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export type AnnouncementVisibility = 'project' | 'team' | 'board' | 'club_wide';
export type BoardTeam = 'tech' | 'finance' | 'marketing' | 'design';
export type BoardTeam = 'tech' | 'finance' | 'marketing' | 'design' | 'project_managers';

export interface ProjectEvent {
id: string;
Expand Down
1 change: 1 addition & 0 deletions src/types/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ export interface Project {
prototypeUrl?: string;
demoDayUrl?: string;
instaPostUrl?: string;
slack_channel_id?: string | null;
}
1 change: 1 addition & 0 deletions supabase/.temp/cli-latest
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v2.75.0
136 changes: 136 additions & 0 deletions supabase/functions/slack-announcement/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import { serve } from "https://deno.land/std@0.168.0/http/server.ts"
import { createClient } from 'https://esm.sh/@supabase/supabase-js@2'

console.log("Slack Announcement Function initialized");

const SLACK_BOT_TOKEN = Deno.env.get('SLACK_BOT_TOKEN');
const SUPABASE_URL = Deno.env.get('SUPABASE_URL');
const SUPABASE_SERVICE_ROLE_KEY = Deno.env.get('SUPABASE_SERVICE_ROLE_KEY');

const supabase = createClient(SUPABASE_URL!, SUPABASE_SERVICE_ROLE_KEY!);

// Mapping for specific visibility channels
const VISIBILITY_MAPPING: Record<string, string> = {
'club_wide': 'C042EJJE33K',
'board': 'C042EJJE33K',
};

const TEAM_CHANNELS: Record<string, string> = {
'tech': 'C07GEAH55EX',
'finance': 'C07GT3VRNDP',
'marketing': 'C07GBGY0AH3',
'design': 'C07GT3VMEHF',
'project_managers': 'C07GBGXEXMK'
};

serve(async (req) => {
try {
const payload = await req.json();
const { record, type } = payload;

// Only process new announcements
if (type !== 'INSERT') {
return new Response(JSON.stringify({ message: 'Ignore non-insert event' }), { status: 200 });
}

const {
title,
description,
visibility,
project_id,
target_team,
link_url,
priority
} = record;

let channelId = '';

// Determine the correct channel based on visibility
if (visibility === 'project' && project_id) {
// Fetch project's slack channel from the projects table
const { data: project, error: projectError } = await supabase
.from('projects')
.select('slack_channel_id, projectName')
.eq('id', project_id)
.single();

if (projectError) {
console.error('Error fetching project slack channel:', projectError);
} else {
channelId = project?.slack_channel_id;
console.log(`Found project channel: ${channelId} for project: ${project?.projectName}`);
}
} else if (visibility === 'team' && target_team) {
channelId = TEAM_CHANNELS[target_team];
console.log(`Selected team channel: ${channelId} for team: ${target_team}`);
} else {
channelId = VISIBILITY_MAPPING[visibility];
console.log(`Selected visibility channel: ${channelId} for visibility: ${visibility}`);
}

if (!channelId) {
console.warn(`No Slack channel mapping found for: visibility=${visibility}, team=${target_team}, project=${project_id}`);
return new Response(JSON.stringify({ error: 'No channel mapping' }), { status: 200 });
}

// Ping @channel for high priority announcements
const shouldPing = priority === 'high';
const slackDescription = shouldPing ? `<!channel>\n${description}` : description;

// Prepare Slack Message
const blocks = [
{
type: "header",
text: {
type: "plain_text",
text: `📢 ${title}`,
emoji: true
}
},
{
type: "section",
text: {
type: "mrkdwn",
text: description
}
}
];

if (link_url) {
blocks.push({
type: "section",
text: {
type: "mrkdwn",
text: `<${link_url}|View more details>`
}
});
}

// Send to Slack via chat.postMessage
const slackRes = await fetch('https://slack.com/api/chat.postMessage', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${SLACK_BOT_TOKEN}`
},
body: JSON.stringify({
channel: channelId,
blocks: blocks,
text: `New Announcement: ${title}` // Fallback text
})
});

const slackResult = await slackRes.json();
if (!slackResult.ok) {
console.error('Slack API error:', slackResult.error);
return new Response(JSON.stringify({ error: slackResult.error }), { status: 500 });
}

console.log(`Successfully posted announcement to Slack channel: ${channelId}`);
return new Response(JSON.stringify({ ok: true }), { status: 200 });

} catch (err: any) {
console.error('Function error:', err);
return new Response(JSON.stringify({ error: err.message }), { status: 500 });
}
})
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
".next/dev/types/**/*.ts"
],
"exclude": [
"node_modules"
"node_modules",
"supabase"
]
}